summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/Renderers/ScrollViewRenderer.cs
blob: f35a275353c18707447b78db772dc638a70c156c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
´╗┐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 != Element)
			{
				(Element 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)
		{
			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);
		}
	}
}