summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/Renderers/CarouselPageRenderer.cs
blob: b00b631ae857406325dc7db3259667acd1f3b6ac (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System;
using ElmSharp;
using ESize = ElmSharp.Size;

namespace Xamarin.Forms.Platform.Tizen
{
	/// <summary>
	/// Renderer of a CarouselPage widget.
	/// </summary>
	public class CarouselPageRenderer : VisualElementRenderer<CarouselPage>
	{
		Box _outterLayout;
		Box _innerContainer;
		Scroller _scroller;

		int _pageIndex = 0;

		int _changedByScroll = 0;
		ESize _layoutBound;

		/// <summary>
		/// Invoked whenever the CarouselPage element has been changed in Xamarin.
		/// </summary>
		/// <param name="e">Event parameters.</param>
		protected override void OnElementChanged(ElementChangedEventArgs<CarouselPage> e)
		{
			if (NativeView == null)
			{
				//Create box that holds toolbar and selected content
				_outterLayout = new Box(Forms.Context.MainWindow)
				{
					AlignmentX = -1,
					AlignmentY = -1,
					WeightX = 1,
					WeightY = 1,
					IsHorizontal = false,
				};
				_outterLayout.Show();

				_scroller = new Scroller(Forms.Context.MainWindow);
				_scroller.PageScrolled += OnScrolled;

				// Disables the visibility of the scrollbar in both directions:
				_scroller.HorizontalScrollBarVisiblePolicy = ElmSharp.ScrollBarVisiblePolicy.Invisible;
				_scroller.VerticalScrollBarVisiblePolicy = ElmSharp.ScrollBarVisiblePolicy.Invisible;
				// Sets the limit of scroll to one page maximum:
				_scroller.HorizontalPageScrollLimit = 1;
				_scroller.SetPageSize(1.0, 1.0);
				_scroller.SetAlignment(-1, -1);
				_scroller.SetWeight(1.0, 1.0);
				_scroller.Show();

				_innerContainer = new Box(Forms.Context.MainWindow);
				_innerContainer.SetLayoutCallback(OnInnerLayoutUpdate);
				_innerContainer.SetAlignment(-1, -1);
				_innerContainer.SetWeight(1.0, 1.0);
				_innerContainer.Show();
				_scroller.SetContent(_innerContainer);

				_outterLayout.PackEnd(_scroller);
				SetNativeControl(_outterLayout);
			}

			if (e.OldElement != null)
			{
				e.OldElement.CurrentPageChanged -= OnCurrentPageChanged;
				e.OldElement.PagesChanged -= OnPagesChanged;
			}

			if (e.NewElement != null)
			{
				Element.CurrentPageChanged += OnCurrentPageChanged;
				Element.PagesChanged += OnPagesChanged;
				UpdateCarouselContent();
			}

			base.OnElementChanged(e);
		}

		/// <summary>
		/// Called just before the associated element is deleted.
		/// </summary>
		/// <param name="disposing">True if the memory release was requested on demand.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (Element != null)
				{
					Element.CurrentPageChanged -= OnCurrentPageChanged;
			        Element.PagesChanged -= OnPagesChanged;
				}
				_innerContainer = null;
				_scroller = null;
			}
			base.Dispose(disposing);
		}

		void OnInnerLayoutUpdate()
		{
			if (_layoutBound == _innerContainer.Geometry.Size)
				return;

			_layoutBound = _innerContainer.Geometry.Size;

			int baseX = _innerContainer.Geometry.X;
			Rect bound = _scroller.Geometry;
			int index = 0;
			foreach (var page in Element.Children)
			{
				var nativeView = Platform.GetRenderer(page).NativeView;
				bound.X = baseX + index * bound.Width;
				nativeView.Geometry = bound;
				index++;
			}
			_innerContainer.MinimumWidth = Element.Children.Count * bound.Width;

			if (_scroller.HorizontalPageIndex != _pageIndex)
			{
				_scroller.ScrollTo(_pageIndex, 0, false);
			}
		}


		/// <summary>
		/// Handles the process of switching between the displayed pages.
		/// </summary>
		/// <param name="sender">An object originating the request</param>
		/// <param name="ea">Additional arguments to the event handler</param>
		void OnCurrentPageChanged(object sender, EventArgs ea)
		{
			if (IsChangedByScroll())
				return;

			if (Element.CurrentPage != Element.Children[_pageIndex])
			{
				var previousPageIndex = _pageIndex;
				_pageIndex = Element.Children.IndexOf(Element.CurrentPage);
				if (previousPageIndex != _pageIndex)
				{
					// notify disappearing/appearing pages and scroll to the requested page
					(Element.Children[previousPageIndex] as IPageController)?.SendDisappearing();
					_scroller.ScrollTo(_pageIndex, 0, false);
					(Element.CurrentPage as IPageController)?.SendAppearing();
				}
			}
		}

		/// <summary>
		/// Handles the PageScrolled event of the _scroller.
		/// </summary>
		void OnScrolled(object sender, EventArgs e2)
		{
			_changedByScroll++;
			int previousIndex = _pageIndex;
			_pageIndex = _scroller.HorizontalPageIndex;

			if (previousIndex != _pageIndex)
			{
				(Element.CurrentPage as IPageController)?.SendDisappearing();
				Element.CurrentPage = Element.Children[_pageIndex];
				(Element.CurrentPage as IPageController)?.SendAppearing();
			}

			_changedByScroll--;
		}

		/// <summary>
		/// Updates the content of the carousel.
		/// </summary>
		void UpdateCarouselContent()
		{
			_innerContainer.UnPackAll();
			foreach (var page in Element.Children)
			{
				EvasObject nativeView = Platform.GetOrCreateRenderer(page).NativeView;
				_innerContainer.PackEnd(nativeView);
				// if possible, make the subpage focusable, this ensures that there is something
				// to focus on all pages and prevents the scroller from auto-scrolling to focused widget
				(nativeView as ElmSharp.Widget)?.AllowFocus(true);
			}
			_pageIndex = Element.Children.IndexOf(Element.CurrentPage);
			_scroller.ScrollTo(_pageIndex, 0, false);
		}

		/// <summary>
		/// Handles the notifications about content sub-page changes.
		/// </summary>
		void OnPagesChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
		{
			UpdateCarouselContent();
		}

		bool IsChangedByScroll()
		{
			return _changedByScroll > 0;
		}
	}
}