diff options
Diffstat (limited to 'Xamarin.Forms.Platform.Tizen/Renderers/ScrollViewRenderer.cs')
-rwxr-xr-x | Xamarin.Forms.Platform.Tizen/Renderers/ScrollViewRenderer.cs | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.Tizen/Renderers/ScrollViewRenderer.cs b/Xamarin.Forms.Platform.Tizen/Renderers/ScrollViewRenderer.cs new file mode 100755 index 00000000..29f15fbe --- /dev/null +++ b/Xamarin.Forms.Platform.Tizen/Renderers/ScrollViewRenderer.cs @@ -0,0 +1,130 @@ +using System; +using System.ComponentModel; +using ElmSharp; + +namespace Xamarin.Forms.Platform.Tizen +{ + /// <summary> + /// This class provides a Renderer for a ScrollView widget. + /// </summary> + public class ScrollViewRenderer : ViewRenderer<ScrollView, Scroller> + { + EvasObject _content; + + /// <summary> + /// Initializes a new instance of the <see cref="Xamarin.Forms.Platform.Tizen.ScrollViewRenderer"/> class. + /// </summary> + public ScrollViewRenderer() + { + RegisterPropertyHandler("Content", FillContent); + } + + /// <summary> + /// Handles the element change event. + /// </summary> + /// <param name="e">Event arguments.</param> + protected override void OnElementChanged(ElementChangedEventArgs<ScrollView> 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; + } + } + + /// <summary> + /// An event raised on element's property change. + /// </summary> + /// <param name="sender">Sender.</param> + /// <param name="e">Event arguments</param> + 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); + } + } +} |