summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core/NavigationPage.cs
blob: fdafec061459bc4987d9671485560c615520dde9 (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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using Xamarin.Forms.Internals;
using Xamarin.Forms.Platform;

namespace Xamarin.Forms
{
	[RenderWith(typeof(_NavigationPageRenderer))]
	public class NavigationPage : Page, IPageContainer<Page>, INavigationPageController, IElementConfiguration<NavigationPage> 
	{
		public static readonly BindableProperty BackButtonTitleProperty = BindableProperty.CreateAttached("BackButtonTitle", typeof(string), typeof(Page), null);

		public static readonly BindableProperty HasNavigationBarProperty = BindableProperty.CreateAttached("HasNavigationBar", typeof(bool), typeof(Page), true);

		public static readonly BindableProperty HasBackButtonProperty = BindableProperty.CreateAttached("HasBackButton", typeof(bool), typeof(NavigationPage), true);

		[Obsolete("Use BarBackgroundColorProperty and BarTextColorProperty to change NavigationPage bar color properties")] public static readonly BindableProperty TintProperty =
			BindableProperty.Create("Tint", typeof(Color), typeof(NavigationPage), Color.Default);

		public static readonly BindableProperty BarBackgroundColorProperty = BindableProperty.Create("BarBackgroundColor", typeof(Color), typeof(NavigationPage), Color.Default);

		public static readonly BindableProperty BarTextColorProperty = BindableProperty.Create("BarTextColor", typeof(Color), typeof(NavigationPage), Color.Default);

		public static readonly BindableProperty TitleIconProperty = BindableProperty.CreateAttached("TitleIcon", typeof(FileImageSource), typeof(NavigationPage), default(FileImageSource));

		static readonly BindablePropertyKey CurrentPagePropertyKey = BindableProperty.CreateReadOnly("CurrentPage", typeof(Page), typeof(NavigationPage), null);
		public static readonly BindableProperty CurrentPageProperty = CurrentPagePropertyKey.BindableProperty;
		
		public NavigationPage()
		{
			_platformConfigurationRegistry = new Lazy<PlatformConfigurationRegistry<NavigationPage>>(() => new PlatformConfigurationRegistry<NavigationPage>(this));

			Navigation = new NavigationImpl(this);
		}

		public NavigationPage(Page root) : this()
		{
			PushPage(root);
		}

		public Color BarBackgroundColor
		{
			get { return (Color)GetValue(BarBackgroundColorProperty); }
			set { SetValue(BarBackgroundColorProperty, value); }
		}

		public Color BarTextColor
		{
			get { return (Color)GetValue(BarTextColorProperty); }
			set { SetValue(BarTextColorProperty, value); }
		}

		[Obsolete("Use BarBackgroundColor and BarTextColor to change NavigationPage bar color properties")]
		public Color Tint
		{
			get { return (Color)GetValue(TintProperty); }
			set { SetValue(TintProperty, value); }
		}

		internal Task CurrentNavigationTask { get; set; }

		Page INavigationPageController.Peek(int depth)
		{
			if (depth < 0)
			{
				return null;
			}

			if (PageController.InternalChildren.Count <= depth)
			{
				return null;
			}

			return (Page)PageController.InternalChildren[PageController.InternalChildren.Count - depth - 1];
		}

		IEnumerable<Page> INavigationPageController.Pages => PageController.InternalChildren.Cast<Page>();

		int INavigationPageController.StackDepth
		{
			get { return PageController.InternalChildren.Count; }
		}

		IPageController PageController => this as IPageController;

		public Page CurrentPage
		{
			get { return (Page)GetValue(CurrentPageProperty); }
			private set { SetValue(CurrentPagePropertyKey, value); }
		}

		public static string GetBackButtonTitle(BindableObject page)
		{
			return (string)page.GetValue(BackButtonTitleProperty);
		}

		public static bool GetHasBackButton(Page page)
		{
			if (page == null)
				throw new ArgumentNullException("page");
			return (bool)page.GetValue(HasBackButtonProperty);
		}

		public static bool GetHasNavigationBar(BindableObject page)
		{
			return (bool)page.GetValue(HasNavigationBarProperty);
		}

		public static FileImageSource GetTitleIcon(BindableObject bindable)
		{
			return (FileImageSource)bindable.GetValue(TitleIconProperty);
		}

		public Task<Page> PopAsync()
		{
			return PopAsync(true);
		}

		public async Task<Page> PopAsync(bool animated)
		{
			if (CurrentNavigationTask != null && !CurrentNavigationTask.IsCompleted)
			{
				var tcs = new TaskCompletionSource<bool>();
				Task oldTask = CurrentNavigationTask;
				CurrentNavigationTask = tcs.Task;
				await oldTask;

				Page page = await ((INavigationPageController)this).PopAsyncInner(animated);
				tcs.SetResult(true);
				return page;
			}

			Task<Page> result = ((INavigationPageController)this).PopAsyncInner(animated);
			CurrentNavigationTask = result;
			return await result;
		}

		public event EventHandler<NavigationEventArgs> Popped;

		public event EventHandler<NavigationEventArgs> PoppedToRoot;

		public Task PopToRootAsync()
		{
			return PopToRootAsync(true);
		}

		public async Task PopToRootAsync(bool animated)
		{
			if (CurrentNavigationTask != null && !CurrentNavigationTask.IsCompleted)
			{
				var tcs = new TaskCompletionSource<bool>();
				Task oldTask = CurrentNavigationTask;
				CurrentNavigationTask = tcs.Task;
				await oldTask;

				await PopToRootAsyncInner(animated);
				tcs.SetResult(true);
				return;
			}

			Task result = PopToRootAsyncInner(animated);
			CurrentNavigationTask = result;
			await result;
		}

		public Task PushAsync(Page page)
		{
			return PushAsync(page, true);
		}

		public async Task PushAsync(Page page, bool animated)
		{
			if (CurrentNavigationTask != null && !CurrentNavigationTask.IsCompleted)
			{
				var tcs = new TaskCompletionSource<bool>();
				Task oldTask = CurrentNavigationTask;
				CurrentNavigationTask = tcs.Task;
				await oldTask;

				await PushAsyncInner(page, animated);
				tcs.SetResult(true);
				return;
			}

			CurrentNavigationTask = PushAsyncInner(page, animated);
			await CurrentNavigationTask;
		}

		public event EventHandler<NavigationEventArgs> Pushed;

		public static void SetBackButtonTitle(BindableObject page, string value)
		{
			page.SetValue(BackButtonTitleProperty, value);
		}

		public static void SetHasBackButton(Page page, bool value)
		{
			if (page == null)
				throw new ArgumentNullException("page");
			page.SetValue(HasBackButtonProperty, value);
		}

		public static void SetHasNavigationBar(BindableObject page, bool value)
		{
			page.SetValue(HasNavigationBarProperty, value);
		}

		public static void SetTitleIcon(BindableObject bindable, FileImageSource value)
		{
			bindable.SetValue(TitleIconProperty, value);
		}

		protected override bool OnBackButtonPressed()
		{
			if (CurrentPage.SendBackButtonPressed())
				return true;

			if (((INavigationPageController)this).StackDepth > 1)
			{
				SafePop();
				return true;
			}

			return base.OnBackButtonPressed();
		}

		event EventHandler<NavigationRequestedEventArgs> InsertPageBeforeRequestedInternal;

		event EventHandler<NavigationRequestedEventArgs> INavigationPageController.InsertPageBeforeRequested
		{
			add { InsertPageBeforeRequestedInternal += value; }
			remove { InsertPageBeforeRequestedInternal -= value; }
		}

		async Task<Page> INavigationPageController.PopAsyncInner(bool animated, bool fast)
		{
			if (((INavigationPageController)this).StackDepth == 1)
			{
				return null;
			}

			var page = (Page)PageController.InternalChildren.Last();

			var args = new NavigationRequestedEventArgs(page, animated);

			var removed = true;

			EventHandler<NavigationRequestedEventArgs> requestPop = PopRequestedInternal;
			if (requestPop != null)
			{
				requestPop(this, args);

				if (args.Task != null && !fast)
					removed = await args.Task;
			}

			if (!removed && !fast)
				return CurrentPage;

			PageController.InternalChildren.Remove(page);

			CurrentPage = (Page)PageController.InternalChildren.Last();

			if (Popped != null)
				Popped(this, args);

			return page;
		}

		event EventHandler<NavigationRequestedEventArgs> PopRequestedInternal;

		event EventHandler<NavigationRequestedEventArgs> INavigationPageController.PopRequested
		{
			add { PopRequestedInternal += value; }
			remove { PopRequestedInternal -= value; }
		}

		event EventHandler<NavigationRequestedEventArgs> PopToRootRequestedInternal;

		event EventHandler<NavigationRequestedEventArgs> INavigationPageController.PopToRootRequested
		{
			add { PopToRootRequestedInternal += value; }
			remove { PopToRootRequestedInternal -= value; }
		}

		event EventHandler<NavigationRequestedEventArgs> PushRequestedInternal;

		event EventHandler<NavigationRequestedEventArgs> INavigationPageController.PushRequested
		{
			add { PushRequestedInternal += value; }
			remove { PushRequestedInternal -= value; }
		}

		event EventHandler<NavigationRequestedEventArgs> RemovePageRequestedInternal;

		event EventHandler<NavigationRequestedEventArgs> INavigationPageController.RemovePageRequested
		{
			add { RemovePageRequestedInternal += value; }
			remove { RemovePageRequestedInternal -= value; }
		}

		void InsertPageBefore(Page page, Page before)
		{
			if (!PageController.InternalChildren.Contains(before))
				throw new ArgumentException("before must be a child of the NavigationPage", "before");

			if (PageController.InternalChildren.Contains(page))
				throw new ArgumentException("Cannot insert page which is already in the navigation stack");

			EventHandler<NavigationRequestedEventArgs> handler = InsertPageBeforeRequestedInternal;
			handler?.Invoke(this, new NavigationRequestedEventArgs(page, before, false));

			int index = PageController.InternalChildren.IndexOf(before);
			PageController.InternalChildren.Insert(index, page);

			// Shouldn't be required?
			if (Width > 0 && Height > 0)
				ForceLayout();
		}

		async Task PopToRootAsyncInner(bool animated)
		{
			if (((INavigationPageController)this).StackDepth == 1)
				return;

			var root = (Page)PageController.InternalChildren.First();

			var childrenToRemove = PageController.InternalChildren.ToArray().Where(c => c != root);
			foreach (var child in childrenToRemove)
				PageController.InternalChildren.Remove(child);

			CurrentPage = root;

			var args = new NavigationRequestedEventArgs(root, animated);

			EventHandler<NavigationRequestedEventArgs> requestPopToRoot = PopToRootRequestedInternal;
			if (requestPopToRoot != null)
			{
				requestPopToRoot(this, args);

				if (args.Task != null)
					await args.Task;
			}

			if (PoppedToRoot != null)
				PoppedToRoot(this, new PoppedToRootEventArgs(root, childrenToRemove.OfType<Page>().ToList()));
		}

		async Task PushAsyncInner(Page page, bool animated)
		{
			if (PageController.InternalChildren.Contains(page))
				return;

			PushPage(page);

			var args = new NavigationRequestedEventArgs(page, animated);

			EventHandler<NavigationRequestedEventArgs> requestPush = PushRequestedInternal;
			if (requestPush != null)
			{
				requestPush(this, args);

				if (args.Task != null)
					await args.Task;
			}

			if (Pushed != null)
				Pushed(this, args);
		}

		void PushPage(Page page)
		{
			PageController.InternalChildren.Add(page);

			CurrentPage = page;
		}

		void RemovePage(Page page)
		{
			if (page == CurrentPage && ((INavigationPageController)this).StackDepth <= 1)
				throw new InvalidOperationException("Cannot remove root page when it is also the currently displayed page.");
			if (page == CurrentPage)
			{
				Log.Warning("NavigationPage", "RemovePage called for CurrentPage object. This can result in undesired behavior, consider called PopAsync instead.");
				PopAsync();
				return;
			}

			if (!PageController.InternalChildren.Contains(page))
				throw new ArgumentException("Page to remove must be contained on this Navigation Page");

			EventHandler<NavigationRequestedEventArgs> handler = RemovePageRequestedInternal;
			if (handler != null)
				handler(this, new NavigationRequestedEventArgs(page, true));

			PageController.InternalChildren.Remove(page);
		}

		void SafePop()
		{
			PopAsync(true).ContinueWith(t =>
			{
				if (t.IsFaulted)
					throw t.Exception;
			});
		}

		class NavigationImpl : NavigationProxy
		{
			readonly Lazy<ReadOnlyCastingList<Page, Element>> _castingList;

			public NavigationImpl(NavigationPage owner)
			{
				Owner = owner;
				_castingList = new Lazy<ReadOnlyCastingList<Page, Element>>(() => new ReadOnlyCastingList<Page, Element>(((IPageController)Owner).InternalChildren));
			}

			NavigationPage Owner { get; }

			protected override IReadOnlyList<Page> GetNavigationStack()
			{
				return _castingList.Value;
			}

			protected override void OnInsertPageBefore(Page page, Page before)
			{
				Owner.InsertPageBefore(page, before);
			}

			protected override Task<Page> OnPopAsync(bool animated)
			{
				return Owner.PopAsync(animated);
			}

			protected override Task OnPopToRootAsync(bool animated)
			{
				return Owner.PopToRootAsync(animated);
			}

			protected override Task OnPushAsync(Page root, bool animated)
			{
				return Owner.PushAsync(root, animated);
			}

			protected override void OnRemovePage(Page page)
			{
				Owner.RemovePage(page);
			}
		}

		readonly Lazy<PlatformConfigurationRegistry<NavigationPage>> _platformConfigurationRegistry;

		public new IPlatformElementConfiguration<T, NavigationPage> On<T>() where T : IConfigPlatform
		{
			return _platformConfigurationRegistry.Value.On<T>();
		}
	}
}