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)
{
var scrollView = new Scroller(Forms.Context.MainWindow);
SetNativeControl(scrollView);
}
if (e.OldElement != null)
{
Control.Scrolled -= ScrollViewScrolledHandler;
(e.OldElement as IScrollViewController).ScrollToRequested -= ScrollRequestHandler;
}
if (e.NewElement != null)
{
Control.Scrolled += ScrollViewScrolledHandler;
(e.NewElement as IScrollViewController).ScrollToRequested += ScrollRequestHandler;
}
UpdateAll();
base.OnElementChanged(e);
}
protected override void Dispose(bool disposing)
{
if (null != Control)
{
(Control as IScrollViewController).ScrollToRequested -= ScrollRequestHandler;
}
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);
}
}
void UpdateAll()
{
UpdateOrientation();
}
void UpdateOrientation()
{
switch (Element.Orientation)
{
case ScrollOrientation.Horizontal:
Control.ScrollBlock = ScrollBlock.Vertical;
Control.HorizontalScrollBarVisiblePolicy = ScrollBarVisiblePolicy.Visible;
Control.VerticalScrollBarVisiblePolicy = ScrollBarVisiblePolicy.Invisible;
break;
case ScrollOrientation.Vertical:
Control.ScrollBlock = ScrollBlock.Horizontal;
Control.HorizontalScrollBarVisiblePolicy = ScrollBarVisiblePolicy.Invisible;
Control.VerticalScrollBarVisiblePolicy = ScrollBarVisiblePolicy.Visible;
break;
default:
Control.ScrollBlock = ScrollBlock.None;
Control.HorizontalScrollBarVisiblePolicy = ScrollBarVisiblePolicy.Visible;
Control.VerticalScrollBarVisiblePolicy = ScrollBarVisiblePolicy.Visible;
break;
}
}
///
/// 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();
}
base.OnElementPropertyChanged(sender, e);
}
void ScrollViewScrolledHandler(object sender, EventArgs e)
{
var region = Control.CurrentRegion;
((IScrollViewController)Element).SetScrolledPosition(region.X, region.Y);
}
void ScrollRequestHandler(object sender, ScrollToRequestedEventArgs e)
{
Rect region = new Rect(ToNativeDimension(e.ScrollX), ToNativeDimension(e.ScrollY), ToNativeDimension(Element.Width), ToNativeDimension(Element.Height));
Control.ScrollTo(region, e.ShouldAnimate);
}
}
}