summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/Renderers/NavigationPageRenderer.cs
blob: ee7a30b5dbbc12060a7d39ee906aec5cfac54b32 (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using Xamarin.Forms.Internals;
using Xamarin.Forms.PlatformConfiguration.TizenSpecific;
using ElmSharp;
using EButton = ElmSharp.Button;
using EToolbar = ElmSharp.Toolbar;
using EToolbarItem = ElmSharp.ToolbarItem;

namespace Xamarin.Forms.Platform.Tizen
{
	public class NavigationPageRenderer : VisualElementRenderer<NavigationPage>, IDisposable, IVisualElementRenderer
	{
		enum ToolbarButtonPosition
		{
			Left,
			Right
		};

		const string PartTitle = "default";
		const string PartBackButton = "elm.swallow.prev_btn";
		const string PartLeftToolbar = "title_left_btn";
		const string PartRightToolbar = "title_right_btn";
		const string PartNavigationBar = "navigationbar";
		const string StyleLeftToolBarButton = "naviframe/title_left";
		const string StyleRightToolbarButton = "naviframe/title_right";
		const string StyleBackButton = "naviframe/back_btn/default";
		const string StyleDefaultToolbarIcon = "naviframe/drawers";
		const string StyleNavigationBar = "navigationbar";

		readonly List<Widget> _naviItemContentPartList = new List<Widget>();
		Naviframe _naviFrame = null;
		Page _previousPage = null;
		TaskCompletionSource<bool> _currentTaskSource = null;
		ToolbarTracker _toolbarTracker = null;
		IDictionary<Page, NaviItem> _naviItemMap;

		Page CurrentPage => Element.CurrentPage;
		Page PreviousPage => Element.Navigation.NavigationStack.Count > 1 ? Element.Navigation.NavigationStack[Element.Navigation.NavigationStack.Count - 2] : null;
		NaviItem CurrentNaviItem => _naviFrame.NavigationStack.Count > 0 ? _naviFrame.NavigationStack.Last() : null;
		NaviItem PreviousNaviItem => _naviFrame.NavigationStack.Count > 1 ? _naviFrame.NavigationStack[_naviFrame.NavigationStack.Count - 2] : null;

		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (_naviFrame != null)
				{
					_naviFrame.AnimationFinished -= OnAnimationFinished;
				}
				if (_toolbarTracker != null)
				{
					_toolbarTracker.CollectionChanged -= OnToolbarCollectionChanged;
				}
			}
			base.Dispose(disposing);
		}

		protected override void OnElementChanged(ElementChangedEventArgs<NavigationPage> e)
		{
			if (_naviFrame == null)
			{
				_naviFrame = new Naviframe(Forms.Context.MainWindow);
				_naviFrame.PreserveContentOnPop = true;
				_naviFrame.DefaultBackButtonEnabled = false;
				_naviFrame.AnimationFinished += OnAnimationFinished;

				SetNativeControl(_naviFrame);
				_naviItemMap = new Dictionary<Page, NaviItem>();
			}

			if (_toolbarTracker == null)
			{
				_toolbarTracker = new ToolbarTracker();
				_toolbarTracker.CollectionChanged += OnToolbarCollectionChanged;
			}

			if (e.OldElement != null)
			{
				var navigation = e.OldElement as INavigationPageController;
				navigation.PopRequested -= OnPopRequested;
				navigation.PopToRootRequested -= OnPopToRootRequested;
				navigation.PushRequested -= OnPushRequested;
				navigation.RemovePageRequested -= OnRemovePageRequested;
				navigation.InsertPageBeforeRequested -= OnInsertPageBeforeRequested;

				var pageController = e.OldElement as IPageController;
				pageController.InternalChildren.CollectionChanged -= OnPageCollectionChanged;
			}

			if (e.NewElement != null)
			{
				var navigation = e.NewElement as INavigationPageController;
				navigation.PopRequested += OnPopRequested;
				navigation.PopToRootRequested += OnPopToRootRequested;
				navigation.PushRequested += OnPushRequested;
				navigation.RemovePageRequested += OnRemovePageRequested;
				navigation.InsertPageBeforeRequested += OnInsertPageBeforeRequested;

				var pageController = e.NewElement as IPageController;
				pageController.InternalChildren.CollectionChanged += OnPageCollectionChanged;

				foreach (Page page in pageController.InternalChildren)
				{
					_naviItemMap[page] = _naviFrame.Push(Platform.GetOrCreateRenderer(page).NativeView, SpanTitle(page.Title));
					page.PropertyChanged += NavigationBarPropertyChangedHandler;

					UpdateHasNavigationBar(page);
				}

				_toolbarTracker.Target = e.NewElement;
				_previousPage = e.NewElement.CurrentPage;
			}
			base.OnElementChanged(e);
		}

		protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
		{
			base.OnElementPropertyChanged(sender, e);

			if (e.PropertyName == NavigationPage.CurrentPageProperty.PropertyName)
			{
				(_previousPage as IPageController)?.SendDisappearing();
				_previousPage = Element.CurrentPage;
				Device.StartTimer(TimeSpan.Zero, () =>
				{
					(_previousPage as IPageController)?.SendAppearing();
					return false;
				});
			}
			else if (e.PropertyName == NavigationPage.BarTextColorProperty.PropertyName)
				UpdateTitle(CurrentPage);
			// Tizen does not support 'Tint', but only 'BarBackgroundColor'
			else if (e.PropertyName == NavigationPage.BarBackgroundColorProperty.PropertyName)
				UpdateBarBackgroundColor(CurrentNaviItem);
			else if (TizenPlatformServices.AppDomain.IsTizenSpecificAvailable && e.PropertyName == "HasBreadCrumbsBar")
				UpdateBreadCrumbsBar(CurrentNaviItem);

		}

		void OnPageCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
		{
			if (e.OldItems != null)
				foreach (Page page in e.OldItems)
					page.PropertyChanged -= NavigationBarPropertyChangedHandler;
			if (e.NewItems != null)
				foreach (Page page in e.NewItems)
					page.PropertyChanged += NavigationBarPropertyChangedHandler;
		}

		void OnToolbarCollectionChanged(object sender, EventArgs eventArgs)
		{
			UpdateToolbarItem(Element.CurrentPage);
		}

		void NavigationBarPropertyChangedHandler(object sender, System.ComponentModel.PropertyChangedEventArgs e)
		{
			// this handler is invoked only for child pages (contained on a navigation stack)
			if (e.PropertyName == NavigationPage.HasNavigationBarProperty.PropertyName)
				UpdateHasNavigationBar(sender as Page);
			else if (e.PropertyName == NavigationPage.HasBackButtonProperty.PropertyName ||
				e.PropertyName == NavigationPage.BackButtonTitleProperty.PropertyName)
				UpdateHasBackButton(sender as Page);
			else if (e.PropertyName == Page.TitleProperty.PropertyName)
				UpdateTitle(sender as Page);
			else if (TizenPlatformServices.AppDomain.IsTizenSpecificAvailable && e.PropertyName == "BreadCrumb")
				UpdateBreadCrumbsBar(GetNaviItemForPage(sender as Page));
		}

		void UpdateHasNavigationBar(Page page)
		{
			NaviItem item = GetNaviItemForPage(page);
			//According to TV UX Guideline, item style should be set to "tabbar" in case of TabbedPage only for TV profile.
			if (Device.Idiom == TargetIdiom.TV)
			{
				item.Style = page is TabbedPage ? "tabbar" : "default";
			}
			item.TitleBarVisible = (bool)page.GetValue(NavigationPage.HasNavigationBarProperty);
			UpdateToolbarItem(page, item);
			UpdateBarBackgroundColor(item);
			if (TizenPlatformServices.AppDomain.IsTizenSpecificAvailable)
				UpdateBreadCrumbsBar(item);
		}

		void UpdateToolbarItem(Page page, NaviItem item = null)
		{
			if (item == null)
				item = GetNaviItemForPage(page);

			if (_naviFrame.NavigationStack.Count == 0 || item == null || item != _naviFrame.NavigationStack.Last())
				return;

			Native.Button rightButton = GetToolbarButton(ToolbarButtonPosition.Right);
			item.SetPartContent(PartRightToolbar, rightButton);

			Native.Button leftButton = GetToolbarButton(ToolbarButtonPosition.Left);
			item.SetPartContent(PartLeftToolbar, leftButton);
			UpdateHasBackButton(page, item);
		}

		void UpdateHasBackButton(Page page, NaviItem item = null)
		{
			if (item == null)
				item = GetNaviItemForPage(page);

			EButton button = null;

			if ((bool)page.GetValue(NavigationPage.HasBackButtonProperty) && _naviFrame.NavigationStack.Count > 1)
			{
				button = CreateNavigationButton((string)page.GetValue(NavigationPage.BackButtonTitleProperty));
			}
			item.SetPartContent(PartBackButton, button);
		}

		void UpdateTitle(Page page, NaviItem item = null)
		{
			if (item == null)
				item = GetNaviItemForPage(page);

			item.SetPartText(PartTitle, SpanTitle(page.Title));
		}

		string SpanTitle(string Title)
		{
			Native.Span span = new Native.Span
			{
				Text = Title,
				HorizontalTextAlignment = Native.TextAlignment.Center,
				ForegroundColor = Element.BarTextColor.ToNative()
			};
			return span.GetMarkupText();
		}

		void UpdateBarBackgroundColor(NaviItem item)
		{
			item.TitleBarBackgroundColor = Element.BarBackgroundColor.ToNative();
		}

		void UpdateNavigationBar(Page page, NaviItem item = null)
		{
			if (item == null)
				item = GetNaviItemForPage(page);

			UpdateTitle(page, item);
			UpdateBarBackgroundColor(item);
		}

		void UpdateBreadCrumbsBar(NaviItem item)
		{
			if (Element.OnThisPlatform().HasBreadCrumbsBar())
			{
				item.Style = StyleNavigationBar;
				item.SetPartContent(PartNavigationBar, GetBreadCrumbsBar());
			}
			else
			{
				item.SetPartContent(PartNavigationBar, null, false);
			}
		}

		EButton CreateNavigationButton(string text)
		{
			EButton button = new EButton(Forms.Context.MainWindow);
			button.Clicked += (sender, e) =>
			{
				if (!Element.SendBackButtonPressed())
					Forms.Context.Exit();
			};

			button.Style = StyleBackButton;
			button.Text = text;

			_naviItemContentPartList.Add(button);
			button.Deleted += NaviItemPartContentDeletedHandler;

			return button;
		}

		void NaviItemPartContentDeletedHandler(object sender, EventArgs e)
		{
			_naviItemContentPartList.Remove(sender as Widget);
		}

		NaviItem GetNaviItemForPage(Page page)
		{
			NaviItem item;
			if (_naviItemMap.TryGetValue(page, out item))
			{
				return item;
			}
			return null;
		}

		Native.Button GetToolbarButton(ToolbarButtonPosition position)
		{
			ToolbarItem item = _toolbarTracker.ToolbarItems.Where(
				i => (position == ToolbarButtonPosition.Right && i.Order <= ToolbarItemOrder.Primary)
				|| (position == ToolbarButtonPosition.Left && i.Order == ToolbarItemOrder.Secondary))
				.OrderBy(i => i.Priority).FirstOrDefault();

			if (item == default(ToolbarItem))
				return null;

			Native.Button button = new Native.Button(Forms.Context.MainWindow);
			button.Clicked += (s, e) =>
			{
				IMenuItemController control = item;
				control.Activate();
			};
			button.Text = item.Text;
			button.BackgroundColor = Color.Transparent.ToNative();

			if (string.IsNullOrEmpty(item.Icon))
			{
				if (string.IsNullOrEmpty(item.Text))
				{
					// We assumed the default toolbar icon is "naviframe/drawer" if there are no icon and text.
					button.Style = StyleDefaultToolbarIcon;
				}
				else
				{
					if (position == ToolbarButtonPosition.Right)
						button.Style = StyleRightToolbarButton;
					else
						button.Style = StyleLeftToolBarButton;
				}
			}
			else
			{
				Native.Image iconImage = new Native.Image(Forms.Context.MainWindow);
				var task = iconImage.LoadFromImageSourceAsync(item.Icon);
				button.Image = iconImage;
			}

			return button;
		}

		EToolbar GetBreadCrumbsBar()
		{
			EToolbar toolbar = new EToolbar(Forms.Context.MainWindow)
			{
				Style = StyleNavigationBar,
				ItemAlignment = 0,
				Homogeneous = false,
				ShrinkMode = ToolbarShrinkMode.Scroll
			};

			foreach (var p in Element.Navigation.NavigationStack)
			{
				string breadCrumb = p.OnThisPlatform().GetBreadCrumb();
				if (!string.IsNullOrEmpty(breadCrumb))
				{
					EToolbarItem toolbarItem = toolbar.Append(breadCrumb);
					toolbarItem.Selected += (s, e) =>
					{
						var copyOfStack = Element.Navigation.NavigationStack.Reverse().Skip(1);
						foreach (var lp in copyOfStack)
						{
							if (lp == p) break;
							Element.Navigation.RemovePage(lp);
						}
						if (Element.Navigation.NavigationStack.Last() != p)
							Element.Navigation.PopAsync();
					};
				}
			}

			return toolbar;
		}

		void OnPopRequested(object sender, NavigationRequestedEventArgs nre)
		{
			if ((Element as IPageController).InternalChildren.Count == _naviFrame.NavigationStack.Count)
			{
				UpdateNavigationBar(PreviousPage, PreviousNaviItem);

				if (nre.Animated)
				{
					_naviFrame.Pop();

					_currentTaskSource = new TaskCompletionSource<bool>();
					nre.Task = _currentTaskSource.Task;

					// There is no TransitionFinished (AnimationFinished) event after Pop the last page
					if (_naviFrame.NavigationStack.Count == 0)
						CompleteCurrentNavigationTask();
				}
				else
				{
					CurrentNaviItem?.Delete();
				}

				if (_naviItemMap.ContainsKey(nre.Page))
					_naviItemMap.Remove(nre.Page);
			}
		}

		void OnPopToRootRequested(object sender, NavigationRequestedEventArgs nre)
		{
			List<NaviItem> copyOfStack = new List<NaviItem>(_naviFrame.NavigationStack);
			NaviItem rootItem = copyOfStack.FirstOrDefault();
			NaviItem topItem = copyOfStack.LastOrDefault();

			foreach (NaviItem naviItem in copyOfStack)
				if (naviItem != rootItem && naviItem != topItem)
					naviItem.Delete();

			if (topItem != rootItem)
			{
				UpdateNavigationBar(Element.Navigation.NavigationStack.Last(), rootItem);
				if (nre.Animated)
				{
					_naviFrame.Pop();

					_currentTaskSource = new TaskCompletionSource<bool>();
					nre.Task = _currentTaskSource.Task;
				}
				else
					topItem?.Delete();
			}

			_naviItemMap.Clear();
			_naviItemMap[Element.Navigation.NavigationStack.Last()] = rootItem;
		}

		void OnPushRequested(object sender, NavigationRequestedEventArgs nre)
		{
			if (nre.Animated || _naviFrame.NavigationStack.Count == 0)
			{
				_naviItemMap[nre.Page] = _naviFrame.Push(Platform.GetOrCreateRenderer(nre.Page).NativeView, SpanTitle(nre.Page.Title));
				_currentTaskSource = new TaskCompletionSource<bool>();
				nre.Task = _currentTaskSource.Task;

				// There is no TransitionFinished (AnimationFinished) event after the first Push
				if (_naviFrame.NavigationStack.Count == 1)
					CompleteCurrentNavigationTask();
			}
			else
			{
				_naviItemMap[nre.Page] = _naviFrame.InsertAfter(_naviFrame.NavigationStack.Last(), Platform.GetOrCreateRenderer(nre.Page).NativeView, SpanTitle(nre.Page.Title));
			}
			UpdateHasNavigationBar(nre.Page);
		}

		void OnRemovePageRequested(object sender, NavigationRequestedEventArgs nre)
		{
			GetNaviItemForPage(nre.Page).Delete();
			if (_naviItemMap.ContainsKey(nre.Page))
				_naviItemMap.Remove(nre.Page);
		}

		void OnInsertPageBeforeRequested(object sender, NavigationRequestedEventArgs nre)
		{
			if (nre.BeforePage == null)
				throw new ArgumentNullException("before");
			if (nre.Page == null)
				throw new ArgumentNullException("page");

			EvasObject page = Platform.GetOrCreateRenderer(nre.Page).NativeView;
			_naviItemMap[nre.Page] = _naviFrame.InsertBefore(GetNaviItemForPage(nre.BeforePage), page, SpanTitle(nre.Page.Title));
			UpdateHasNavigationBar(nre.Page);
		}

		void OnAnimationFinished(object sender, EventArgs e)
		{
			CompleteCurrentNavigationTask();
		}

		void CompleteCurrentNavigationTask()
		{
			if (_currentTaskSource != null)
			{
				var tmp = _currentTaskSource;
				_currentTaskSource = null;
				tmp.SetResult(true);
			}
		}
	}
}