summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/Renderers/ScrollViewRenderer.cs
blob: 0168d2df3f84805e946c5938fe92520afcd61da2 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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)
			{
				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);
		}

		/// <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();
			}
			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);
		}
	}
}