summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/Native/MasterDetailPage.cs
blob: e7e1f243b2e4d4b6ba25ff05b7dbedb1be36273c (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
using System;
using ElmSharp;

namespace Xamarin.Forms.Platform.Tizen.Native
{
	/// <summary>
	/// The native widget which provides Xamarin.MasterDetailPage features.
	/// </summary>
	public class MasterDetailPage : Box
	{
		/// <summary>
		/// The portion of the screen that the MasterPage takes in Split mode.
		/// </summary>
		static readonly double s_splitRatio = 0.35;

		/// <summary>
		/// The portion of the screen that the MasterPage takes in Popover mode.
		/// </summary>
		static readonly double s_popoverRatio = 0.8;

		/// <summary>
		/// The default master behavior (a.k.a mode).
		/// </summary>
		static readonly MasterBehavior s_defaultMasterBehavior = (Device.Idiom == TargetIdiom.Phone) ? MasterBehavior.Popover : MasterBehavior.SplitOnLandscape;

		/// <summary>
		/// The MasterPage native container.
		/// </summary>
		readonly Canvas _masterCanvas;

		/// <summary>
		/// The DetailPage native container.
		/// </summary>
		readonly Canvas _detailCanvas;

		/// <summary>
		/// The container for <c>_masterCanvas</c> and <c>_detailCanvas</c> used in split mode.
		/// </summary>
		readonly Panes _splitPane;

		/// <summary>
		/// The container for <c>_masterCanvas</c> used in popover mode.
		/// </summary>
		readonly Panel _drawer;

		/// <summary>
		/// The <see cref="MasterBehavior"/> property value.
		/// </summary>
		MasterBehavior _masterBehavior = s_defaultMasterBehavior;

		/// <summary>
		/// The actual MasterDetailPage mode - either split or popover. It depends on <c>_masterBehavior</c> and screen orientation.
		/// </summary>
		MasterBehavior _internalMasterBehavior = MasterBehavior.Popover;

		/// <summary>
		/// The <see cref="Master"/> property value.
		/// </summary>
		EvasObject _master;

		/// <summary>
		/// The <see cref="Detail"/> property value.
		/// </summary>
		EvasObject _detail;

		/// <summary>
		/// The main widget - either <see cref="_splitPlane"/> or <see cref="_detailPage"/>, depending on the mode.
		/// </summary>
		EvasObject _mainWidget;

		/// <summary>
		/// The <see cref="IsGestureEnabled"/> property value.
		/// </summary>
		bool _isGestureEnabled;

		/// <summary>
		/// Initializes a new instance of the <see cref="Xamarin.Forms.Platform.Tizen.Native.MasterDetailPage"/> class.
		/// </summary>
		/// <param name="parent">Parent evas object.</param>
		public MasterDetailPage(EvasObject parent) : base(parent)
		{
			// we control the layout ourselves
			Resized += (sender, e) =>
			{
				var g = Geometry;

				// main widget should fill the area of the MasterDetailPage
				if (_mainWidget != null)
				{
					_mainWidget.Geometry = g;
				}

				g.Width = (int)((s_popoverRatio * (double)g.Width));
				_drawer.Geometry = g;
			};

			// create the controls which will hold the master and detail pages
			_masterCanvas = new Canvas(this);
			_masterCanvas.SetAlignment(-1.0, -1.0);  // fill
			_masterCanvas.SetWeight(1.0, 1.0);  // expand
			_masterCanvas.Resized += (sender, e) =>
			{
				UpdatePageGeometry(_master);
			};

			_detailCanvas = new Canvas(this);
			_detailCanvas.SetAlignment(-1.0, -1.0);  // fill
			_detailCanvas.SetWeight(1.0, 1.0);  // expand
			_detailCanvas.Resized += (sender, e) =>
			{
				UpdatePageGeometry(_detail);
			};

			_splitPane = new Panes(this)
			{
				IsFixed = true,
				IsHorizontal = false,
				Proportion = s_splitRatio,
			};

			_drawer = new Panel(Forms.Context.MainWindow)
			{
				Direction = PanelDirection.Left,
			};
			_drawer.SetScrollable(_isGestureEnabled);
			_drawer.SetScrollableArea(1.0);
			_drawer.Toggled += (object sender, EventArgs e) =>
			{
				IsPresentedChanged?.Invoke(this, EventArgs.Empty);
			};

			ConfigureLayout();

			// in case of the screen rotation we may need to update the choice between split
			// and popover behaviors and reconfigure the layout
			Forms.Context.MainWindow.RotationChanged += (sender, e) =>
			{
				UpdateMasterBehavior();
			};
		}

		/// <summary>
		/// Occurs when the MasterPage is shown or hidden.
		/// </summary>
		public event EventHandler IsPresentedChanged;

		/// <summary>
		/// Gets or sets the MasterDetailPage behavior.
		/// </summary>
		/// <value>The behavior of the <c>MasterDetailPage</c> requested by the user.</value>
		public MasterBehavior MasterBehavior
		{
			get
			{
				return _masterBehavior;
			}

			set
			{
				if (_masterBehavior != value)
				{
					_masterBehavior = value;

					UpdateMasterBehavior();
				}
			}
		}

		/// <summary>
		/// Gets or sets the content of the MasterPage.
		/// </summary>
		/// <value>The MasterPage.</value>
		public EvasObject Master
		{
			get
			{
				return _master;
			}

			set
			{
				if (_master != value)
				{
					_master = value;
					UpdatePageGeometry(_master);
					_masterCanvas.Children.Clear();
					_masterCanvas.Children.Add(_master);
				}
			}
		}

		/// <summary>
		/// Gets or sets the content of the DetailPage.
		/// </summary>
		/// <value>The DetailPage.</value>
		public EvasObject Detail
		{
			get
			{
				return _detail;
			}

			set
			{
				if (_detail != value)
				{
					_detail = value;
					UpdatePageGeometry(_detail);
					_detailCanvas.Children.Clear();
					_detailCanvas.Children.Add(_detail);
				}
			}
		}

		/// <summary>
		/// Gets or sets a value indicating whether the MasterPage is shown.
		/// </summary>
		/// <value><c>true</c> if the MasterPage is presented.</value>
		public bool IsPresented
		{
			get
			{
				return _drawer.IsOpen;
			}

			set
			{
				if (_drawer.IsOpen != value)
				{
					_drawer.IsOpen = value;
				}
			}
		}

		/// <summary>
		/// Gets or sets a value indicating whether a MasterDetailPage allows showing MasterPage with swipe gesture.
		/// </summary>
		/// <value><c>true</c> if the MasterPage can be revealed with a gesture.</value>
		public bool IsGestureEnabled
		{
			get
			{
				return _isGestureEnabled;
			}

			set
			{
				if (_isGestureEnabled != value)
				{
					_isGestureEnabled = value;
					_drawer.SetScrollable(_isGestureEnabled);
				}
			}
		}

		/// <summary>
		/// Provides destruction for native element and contained elements.
		/// </summary>
		protected override void OnUnrealize()
		{
			_drawer.Unrealize();

			base.OnUnrealize();
		}

		/// <summary>
		/// Updates the geometry of the selected page.
		/// </summary>
		/// <param name="page">Master or Detail page to be updated.</param>
		void UpdatePageGeometry(EvasObject page)
		{
			if (page != null)
			{
				if (_master == page)
				{
					// update the geometry of the master page
					page.Geometry = _masterCanvas.Geometry;
				}
				else if (_detail == page)
				{
					// update the geometry of the detail page
					page.Geometry = _detailCanvas.Geometry;
				}
			}
		}

		/// <summary>
		/// Updates <see cref="_internalMasterBehavior"/> according to <see cref="MasterDetailBehavior"/> set by the user and current screen orientation.
		/// </summary>
		void UpdateMasterBehavior()
		{
			var behavior = (_masterBehavior == MasterBehavior.Default) ? s_defaultMasterBehavior : _masterBehavior;

			// Screen orientation affects those 2 behaviors
			if (behavior == MasterBehavior.SplitOnLandscape ||
				behavior == MasterBehavior.SplitOnPortrait)
			{
				var orientation = Forms.Context.MainWindow.CurrentOrientation;

				if (((orientation == DisplayOrientations.Landscape || orientation == DisplayOrientations.LandscapeFlipped) && behavior == MasterBehavior.SplitOnLandscape) ||
					((orientation == DisplayOrientations.Portrait || orientation == DisplayOrientations.PortraitFlipped) && behavior == MasterBehavior.SplitOnPortrait))
				{
					behavior = MasterBehavior.Split;
				}
				else
				{
					behavior = MasterBehavior.Popover;
				}
			}

			if (behavior != _internalMasterBehavior)
			{
				_internalMasterBehavior = behavior;

				ConfigureLayout();
			}
		}

		/// <summary>
		/// Composes the structure of all the necessary widgets.
		/// </summary>
		void ConfigureLayout()
		{
			_drawer.SetContent(null, true);
			_drawer.Hide();

			_splitPane.SetPartContent("left", null, true);
			_splitPane.SetPartContent("right", null, true);
			_splitPane.Hide();

			UnPackAll();

			// the structure for split mode and for popover mode looks differently
			if (_internalMasterBehavior == MasterBehavior.Split)
			{
				_splitPane.SetPartContent("left", _masterCanvas, true);
				_splitPane.SetPartContent("right", _detailCanvas, true);
				PackEnd(_splitPane);

				_splitPane.Show();

				_mainWidget = _splitPane;

				IsPresented = true;
			}
			else
			{
				_drawer.SetContent(_masterCanvas, true);
				PackEnd(_detailCanvas);
				_drawer.Show();

				_mainWidget = _detailCanvas;
				_drawer.IsOpen = IsPresented;
			}

			_masterCanvas.Show();
			_detailCanvas.Show();
		}
	}
}