summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WP8/NavigationPageRenderer.cs
blob: 35a77f21c8bd4f6ef98f0cb98c904688271934e2 (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
´╗┐using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace Xamarin.Forms.Platform.WinPhone
{
	public class NavigationPageRenderer : VisualElementRenderer<NavigationPage, FrameworkElement>
	{
		Page _currentRoot;
		bool _isRemoving;

		public NavigationPageRenderer()
		{
			AutoPackage = false;
		}

		protected override void OnElementChanged(ElementChangedEventArgs<NavigationPage> e)
		{
			base.OnElementChanged(e);

			Debug.WriteLine("Warning, Windows Phone backend does not support NavigationPage, falling back to global navigation.");

			Action init = () =>
			{
				Element.PushRequested += PageOnPushed;
				Element.PopRequested += PageOnPopped;
				Element.PopToRootRequested += PageOnPoppedToRoot;
				Element.RemovePageRequested += RemovePageRequested;
				Element.InsertPageBeforeRequested += ElementOnInsertPageBeforeRequested;
				Element.PropertyChanged += OnElementPropertyChanged;

				var platform = (Platform)Element.Platform;
				Element.ContainerArea = new Rectangle(new Point(0, 0), platform.Size);

				platform.SizeChanged += (sender, args) => Element.ContainerArea = new Rectangle(new Point(0, 0), platform.Size);

				List<Page> stack = GetStack();
				if (stack.Count > 0)
					UpdateRootPage(stack);
				else
					return;

				Device.BeginInvokeOnMainThread(() =>
				{
					for (var i = 0; i < stack.Count; i++)
						PageOnPushed(this, new NavigationRequestedEventArgs(stack[i], false, i != 0));
				});
			};

			if (Element.Platform == null)
				Element.PlatformSet += (sender, args) => init();
			else
				init();

			Loaded += (sender, args) => Element.SendAppearing();
			Unloaded += OnUnloaded;
		}

		void ElementOnInsertPageBeforeRequested(object sender, NavigationRequestedEventArgs eventArgs)
		{
			if (Element.Platform == null)
				return;
			var platform = Element.Platform as Platform;
			if (platform != null)
				((INavigation)platform).InsertPageBefore(eventArgs.Page, eventArgs.BeforePage);

			List<Page> stack = GetStack();
			stack.Insert(stack.IndexOf(eventArgs.BeforePage), eventArgs.Page);

			UpdateRootPage(stack);
		}

		List<Page> GetStack()
		{
			int count = Element.InternalChildren.Count;
			var stack = new List<Page>(count);
			for (var i = 0; i < count; i++)
				stack.Add((Page)Element.InternalChildren[i]);

			return stack;
		}

		protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			if (e.PropertyName != "Parent" || Element.RealParent != null)
				return;

			var platform = Element.Platform as Platform;

			if (platform == null)
				return;

			for (var i = 0; i < Element.LogicalChildren.Count; i++)
			{
				var page = Element.LogicalChildren[i] as Page;
				if (page != null)
					platform.RemovePage(page, false);
			}
		}

		void OnUnloaded(object sender, RoutedEventArgs args)
		{
			Element.SendDisappearing();
		}

		void PageOnPopped(object sender, NavigationRequestedEventArgs eventArg)
		{
			if (Element.Platform == null)
				return;
			var platform = Element.Platform as Platform;
			if (platform != null)
				eventArg.Task = platform.Pop(Element, eventArg.Animated).ContinueWith((t, o) => true, null);
		}

		void PageOnPoppedToRoot(object sender, NavigationRequestedEventArgs eventArgs)
		{
			if (Element.Platform == null)
				return;
			var platform = Element.Platform as Platform;
			if (platform != null)
				eventArgs.Task = platform.PopToRoot(Element, eventArgs.Animated).ContinueWith((t, o) => true, null);
		}

		void PageOnPushed(object sender, NavigationRequestedEventArgs e)
		{
			if (Element.Platform == null)
				return;
			var platform = Element.Platform as Platform;
			if (platform != null)
			{
				if (e.Page == Element.StackCopy.LastOrDefault())
					e.Page.IgnoresContainerArea = true;
				e.Task = platform.PushCore(e.Page, Element, e.Animated, e.Realize).ContinueWith((t, o) => true, null);
			}
		}

		void RemovePageRequested(object sender, NavigationRequestedEventArgs eventArgs)
		{
			if (Element.Platform == null)
				return;
			var platform = Element.Platform as Platform;
			if (platform != null)
				((INavigation)platform).RemovePage(eventArgs.Page);

			List<Page> stack = GetStack();
			stack.Remove(eventArgs.Page);
			_isRemoving = true;
			UpdateRootPage(stack);
			_isRemoving = false;
		}

		void UpdateRootPage(IReadOnlyList<Page> stack)
		{
			Page first = stack.FirstOrDefault();
			if (first == _currentRoot)
				return;

			if (Children.Count > 0)
			{
				var renderer = Children[0] as IVisualElementRenderer;
				if (renderer != null)
				{
					Children.RemoveAt(0);

					var page = renderer.Element as Page;
					if (page != null)
						page.IgnoresContainerArea = false;

					if (!stack.Contains(renderer.Element))
						Platform.SetRenderer(renderer.Element, null);
				}
			}

			_currentRoot = first;

			if (first == null)
				return;

			first.IgnoresContainerArea = true;

			IVisualElementRenderer firstRenderer = Platform.GetRenderer(first);
			if (firstRenderer == null)
			{
				firstRenderer = Platform.CreateRenderer(first);
				Platform.SetRenderer(first, firstRenderer);
			}
			var uiElement = (UIElement)firstRenderer;
			var platform = Element.Platform as Platform;
			Canvas canvas = platform?.GetCanvas();

			//We could be swapping the visible page,
			//so let's make sure we remove it
			if (canvas.Children.Contains(uiElement))
				canvas.Children.Remove(uiElement);
			Children.Add(uiElement);

			// we removed the previous root page, and the new root page is the one being presented
			// at this time there's only 1 page now on the stack (the navigationpage with root)
			// we need to update the platform to set this root page as the visible again
			bool updateRoot = Element.CurrentPage == first && _isRemoving;
			if (updateRoot)
				platform.SetCurrent(Element, false);
		}
	}
}