using System;
using System.ComponentModel;
using ElmSharp;
namespace Xamarin.Forms.Platform.Tizen
{
///
/// This class provides a Renderer for a ScrollView widget.
///
public class ScrollViewRenderer : ViewRenderer
{
EvasObject _content;
///
/// Initializes a new instance of the class.
///
public ScrollViewRenderer()
{
RegisterPropertyHandler("Content", FillContent);
}
///
/// Handles the element change event.
///
/// Event arguments.
protected override void OnElementChanged(ElementChangedEventArgs e)
{
if (Control == null)
{
SetNativeControl(new Scroller(Forms.Context.MainWindow));
Control.Scrolled += OnScrolled;
}
if (e.OldElement != null)
{
(e.OldElement as IScrollViewController).ScrollToRequested -= OnScrollRequested;
}
if (e.NewElement != null)
{
(e.NewElement as IScrollViewController).ScrollToRequested += OnScrollRequested;
}
UpdateAll();
base.OnElementChanged(e);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (null != Element)
{
(Element as IScrollViewController).ScrollToRequested -= OnScrollRequested;
}
if (Control != null)
{
Control.Scrolled -= OnScrolled;
}
}
base.Dispose(disposing);
}
void FillContent()
{
if (_content != null)
{
Control.SetContent(null, true);
}
_content = Platform.GetOrCreateRenderer(Element.Content).NativeView;
if (_content != null)
{
Control.SetContent(_content, true);
UpdateContentSize();
}
}
void UpdateAll()
{
UpdateOrientation();
}
void UpdateOrientation()
{
switch (Element.Orientation)
{
case ScrollOrientation.Horizontal:
Control.ScrollBlock = ScrollBlock.Vertical;
Control.HorizontalScrollBarVisiblePolicy = ScrollBarVisiblePolicy.Auto;
Control.VerticalScrollBarVisiblePolicy = ScrollBarVisiblePolicy.Invisible;
break;
case ScrollOrientation.Vertical:
Control.ScrollBlock = ScrollBlock.Horizontal;
Control.HorizontalScrollBarVisiblePolicy = ScrollBarVisiblePolicy.Invisible;
Control.VerticalScrollBarVisiblePolicy = ScrollBarVisiblePolicy.Auto;
break;
default:
Control.ScrollBlock = ScrollBlock.None;
Control.HorizontalScrollBarVisiblePolicy = ScrollBarVisiblePolicy.Auto;
Control.VerticalScrollBarVisiblePolicy = ScrollBarVisiblePolicy.Auto;
break;
}
}
void UpdateContentSize()
{
if (_content == null)
return;
_content.MinimumWidth = Forms.ConvertToScaledPixel(Element.ContentSize.Width);
_content.MinimumHeight = Forms.ConvertToScaledPixel(Element.ContentSize.Height);
}
///
/// An event raised on element's property change.
///
/// Sender.
/// Event arguments
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (ScrollView.OrientationProperty.PropertyName == e.PropertyName)
{
UpdateOrientation();
}
else if (ScrollView.ContentSizeProperty.PropertyName == e.PropertyName)
{
UpdateContentSize();
}
base.OnElementPropertyChanged(sender, e);
}
void OnScrolled(object sender, EventArgs e)
{
var region = Control.CurrentRegion;
((IScrollViewController)Element).SetScrolledPosition(region.X, region.Y);
}
void OnScrollRequested(object sender, ScrollToRequestedEventArgs e)
{
var x = e.ScrollX;
var y = e.ScrollY;
if (e.Mode == ScrollToMode.Element)
{
Point itemPosition = (Element as IScrollViewController).GetScrollPositionForElement(e.Element as VisualElement, e.Position);
x = itemPosition.X;
y = itemPosition.Y;
}
Rect region = new Rectangle(x, y, Element.Width, Element.Height).ToPixel();
Control.ScrollTo(region, e.ShouldAnimate);
}
}
}