/
FlyoutPage.cs
329 lines (270 loc) · 11 KB
/
FlyoutPage.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Microsoft.Maui.Controls.Internals;
using Microsoft.Maui.Devices;
using Microsoft.Maui.Graphics;
namespace Microsoft.Maui.Controls
{
/// <include file="../../docs/Microsoft.Maui.Controls/FlyoutPage.xml" path="Type[@FullName='Microsoft.Maui.Controls.FlyoutPage']/Docs" />
[ContentProperty(nameof(Detail))]
public partial class FlyoutPage : Page, IFlyoutPageController, IElementConfiguration<FlyoutPage>
{
/// <include file="../../docs/Microsoft.Maui.Controls/FlyoutPage.xml" path="//Member[@MemberName='IsGestureEnabledProperty']/Docs" />
public static readonly BindableProperty IsGestureEnabledProperty = BindableProperty.Create(nameof(IsGestureEnabled), typeof(bool), typeof(FlyoutPage), true);
/// <include file="../../docs/Microsoft.Maui.Controls/FlyoutPage.xml" path="//Member[@MemberName='IsPresentedProperty']/Docs" />
public static readonly BindableProperty IsPresentedProperty = BindableProperty.Create(nameof(IsPresented), typeof(bool), typeof(FlyoutPage), default(bool),
propertyChanged: OnIsPresentedPropertyChanged, propertyChanging: OnIsPresentedPropertyChanging, defaultValueCreator: GetDefaultValue);
/// <include file="../../docs/Microsoft.Maui.Controls/FlyoutPage.xml" path="//Member[@MemberName='FlyoutLayoutBehaviorProperty']/Docs" />
public static readonly BindableProperty FlyoutLayoutBehaviorProperty = BindableProperty.Create(nameof(FlyoutLayoutBehavior), typeof(FlyoutLayoutBehavior), typeof(FlyoutPage), default(FlyoutLayoutBehavior),
propertyChanged: OnFlyoutLayoutBehaviorPropertyChanged);
Page _detail;
Rect _detailBounds;
Page _flyout;
Rect _flyoutBounds;
IFlyoutPageController FlyoutPageController => this;
/// <include file="../../docs/Microsoft.Maui.Controls/FlyoutPage.xml" path="//Member[@MemberName='Detail']/Docs" />
public Page Detail
{
get { return _detail; }
set
{
if (_detail != null && value == null)
throw new ArgumentNullException("value", "Detail cannot be set to null once a value is set.");
if (_detail == value)
return;
if (value.RealParent != null)
throw new InvalidOperationException("Detail must not already have a parent.");
var previousDetail = _detail;
// TODO MAUI refine this to fire earlier
_detail?.SendNavigatingFrom(new NavigatingFromEventArgs());
OnPropertyChanging();
if (_detail != null)
InternalChildren.Remove(_detail);
_detail = value;
InternalChildren.Add(_detail);
OnPropertyChanged();
if (this.HasAppeared)
{
previousDetail?.SendDisappearing();
_detail?.SendAppearing();
}
previousDetail?.SendNavigatedFrom(new NavigatedFromEventArgs(_detail));
_detail?.SendNavigatedTo(new NavigatedToEventArgs(previousDetail));
}
}
/// <include file="../../docs/Microsoft.Maui.Controls/FlyoutPage.xml" path="//Member[@MemberName='IsGestureEnabled']/Docs" />
public bool IsGestureEnabled
{
get { return (bool)GetValue(IsGestureEnabledProperty); }
set { SetValue(IsGestureEnabledProperty, value); }
}
/// <include file="../../docs/Microsoft.Maui.Controls/FlyoutPage.xml" path="//Member[@MemberName='IsPresented']/Docs" />
public bool IsPresented
{
get { return (bool)GetValue(IsPresentedProperty); }
set { SetValue(IsPresentedProperty, value); }
}
/// <include file="../../docs/Microsoft.Maui.Controls/FlyoutPage.xml" path="//Member[@MemberName='Flyout']/Docs" />
public Page Flyout
{
get { return _flyout; }
set
{
if (_flyout != null && value == null)
throw new ArgumentNullException("value", "Flyout cannot be set to null once a value is set");
if (string.IsNullOrEmpty(value.Title))
throw new InvalidOperationException("Title property must be set on Flyout page");
if (_flyout == value)
return;
if (value.RealParent != null)
throw new InvalidOperationException("Flyout must not already have a parent.");
// TODO MAUI refine this to fire earlier
var previousFlyout = _flyout;
// TODO MAUI refine this to fire earlier
_flyout?.SendNavigatingFrom(new NavigatingFromEventArgs());
OnPropertyChanging();
if (_flyout != null)
InternalChildren.Remove(_flyout);
_flyout = value;
InternalChildren.Add(_flyout);
OnPropertyChanged();
if (this.HasAppeared)
{
previousFlyout?.SendDisappearing();
_flyout?.SendAppearing();
}
previousFlyout?.SendNavigatedFrom(new NavigatedFromEventArgs(_flyout));
_flyout?.SendNavigatedTo(new NavigatedToEventArgs(previousFlyout));
}
}
/// <include file="../../docs/Microsoft.Maui.Controls/FlyoutPage.xml" path="//Member[@MemberName='FlyoutLayoutBehavior']/Docs" />
public FlyoutLayoutBehavior FlyoutLayoutBehavior
{
get { return (FlyoutLayoutBehavior)GetValue(FlyoutLayoutBehaviorProperty); }
set { SetValue(FlyoutLayoutBehaviorProperty, value); }
}
bool IFlyoutPageController.CanChangeIsPresented { get; set; } = true;
Rect IFlyoutPageController.DetailBounds
{
get { return _detailBounds; }
set
{
_detailBounds = value;
if (_detail == null)
throw new InvalidOperationException("Detail must be set before using a FlyoutPage");
_detail.Layout(value);
}
}
Rect IFlyoutPageController.FlyoutBounds
{
get { return _flyoutBounds; }
set
{
_flyoutBounds = value;
if (_flyout == null)
throw new InvalidOperationException("Flyout must be set before using a FlyoutPage");
_flyout.Layout(value);
}
}
bool IFlyoutPageController.ShouldShowSplitMode
{
get
{
if (DeviceInfo.Idiom == DeviceIdiom.Phone)
return false;
FlyoutLayoutBehavior behavior = FlyoutLayoutBehavior;
var orientation = Window.GetOrientation();
bool isSplitOnLandscape = (behavior == FlyoutLayoutBehavior.SplitOnLandscape || behavior == FlyoutLayoutBehavior.Default) && orientation.IsLandscape();
bool isSplitOnPortrait = behavior == FlyoutLayoutBehavior.SplitOnPortrait && orientation.IsPortrait();
return behavior == FlyoutLayoutBehavior.Split || isSplitOnLandscape || isSplitOnPortrait;
}
}
public event EventHandler IsPresentedChanged;
/// <include file="../../docs/Microsoft.Maui.Controls/FlyoutPage.xml" path="//Member[@MemberName='ShouldShowToolbarButton']/Docs" />
public virtual bool ShouldShowToolbarButton()
{
if (DeviceInfo.Idiom == DeviceIdiom.Phone)
return true;
FlyoutLayoutBehavior behavior = FlyoutLayoutBehavior;
var orientation = DeviceDisplay.MainDisplayInfo.Orientation;
bool isSplitOnLandscape = (behavior == FlyoutLayoutBehavior.SplitOnLandscape || behavior == FlyoutLayoutBehavior.Default) && orientation.IsLandscape();
bool isSplitOnPortrait = behavior == FlyoutLayoutBehavior.SplitOnPortrait && orientation.IsPortrait();
return behavior != FlyoutLayoutBehavior.Split && !isSplitOnLandscape && !isSplitOnPortrait;
}
protected override void LayoutChildren(double x, double y, double width, double height)
{
if (Flyout == null || Detail == null)
throw new InvalidOperationException("Flyout and Detail must be set before using a FlyoutPage");
#if !ANDROID
_flyout.Layout(_flyoutBounds);
_detail.Layout(_detailBounds);
#endif
}
protected override void OnAppearing()
{
Flyout?.SendAppearing();
Detail?.SendAppearing();
FlyoutPageController.CanChangeIsPresented = true;
UpdateFlyoutLayoutBehavior(this);
base.OnAppearing();
}
protected override bool OnBackButtonPressed()
{
if (IsPresented)
{
if (Flyout.SendBackButtonPressed())
return true;
}
EventHandler<BackButtonPressedEventArgs> handler = BackButtonPressed;
if (handler != null)
{
var args = new BackButtonPressedEventArgs();
handler(this, args);
if (args.Handled)
return true;
}
if (Detail.SendBackButtonPressed())
{
return true;
}
return base.OnBackButtonPressed();
}
protected override void OnParentSet()
{
if (RealParent != null && (Flyout == null || Detail == null))
throw new InvalidOperationException("Flyout and Detail must be set before adding FlyoutPage to a container");
base.OnParentSet();
}
[EditorBrowsable(EditorBrowsableState.Never)]
public event EventHandler<BackButtonPressedEventArgs> BackButtonPressed;
/// <include file="../../docs/Microsoft.Maui.Controls/FlyoutPage.xml" path="//Member[@MemberName='UpdateFlyoutLayoutBehavior']/Docs" />
[EditorBrowsable(EditorBrowsableState.Never)]
public void UpdateFlyoutLayoutBehavior()
{
UpdateFlyoutLayoutBehavior(this);
}
internal static void UpdateFlyoutLayoutBehavior(FlyoutPage page)
{
if (page is IFlyoutPageController fpc && fpc.ShouldShowSplitMode)
{
page.SetValueCore(IsPresentedProperty, true);
if (page.FlyoutLayoutBehavior != FlyoutLayoutBehavior.Default)
fpc.CanChangeIsPresented = false;
}
}
static void OnIsPresentedPropertyChanged(BindableObject sender, object oldValue, object newValue)
=> ((FlyoutPage)sender).IsPresentedChanged?.Invoke(sender, EventArgs.Empty);
static void OnIsPresentedPropertyChanging(BindableObject sender, object oldValue, object newValue)
{
if (sender is Maui.IElement element && element.IsShimmed())
{
if (sender is FlyoutPage fp && fp is IFlyoutPageController fpc && !fpc.CanChangeIsPresented)
throw new InvalidOperationException(string.Format("Can't change IsPresented when setting {0}", fp.FlyoutLayoutBehavior));
}
else
{
if ((!(bool)newValue) && sender is IFlyoutPageController fpc && fpc.ShouldShowSplitMode && sender is FlyoutPage fp)
{
throw new InvalidOperationException(string.Format("Can't change IsPresented when setting {0}", fp.FlyoutLayoutBehavior));
}
}
}
static void OnFlyoutLayoutBehaviorPropertyChanged(BindableObject sender, object oldValue, object newValue)
{
var page = (FlyoutPage)sender;
UpdateFlyoutLayoutBehavior(page);
}
static object GetDefaultValue(BindableObject bindable)
{
return DeviceInfo.Platform == DevicePlatform.macOS;
}
/// <include file="../../docs/Microsoft.Maui.Controls/FlyoutPage.xml" path="//Member[@MemberName='.ctor']/Docs" />
public FlyoutPage()
{
_platformConfigurationRegistry = new Lazy<PlatformConfigurationRegistry<FlyoutPage>>(() => new PlatformConfigurationRegistry<FlyoutPage>(this));
this.Loaded += OnLoaded;
this.Unloaded += OnUnloaded;
}
readonly Lazy<PlatformConfigurationRegistry<FlyoutPage>> _platformConfigurationRegistry;
/// <include file="../../docs/Microsoft.Maui.Controls/FlyoutPage.xml" path="//Member[@MemberName='On']/Docs" />
public new IPlatformElementConfiguration<T, FlyoutPage> On<T>() where T : IConfigPlatform
{
return _platformConfigurationRegistry.Value.On<T>();
}
void OnUnloaded(object sender, EventArgs e)
{
DeviceDisplay.MainDisplayInfoChanged -= OnMainDisplayInfoChanged;
}
void OnLoaded(object sender, EventArgs e)
{
DeviceDisplay.MainDisplayInfoChanged += OnMainDisplayInfoChanged;
Handler?.UpdateValue(nameof(FlyoutBehavior));
}
void OnMainDisplayInfoChanged(object sender, DisplayInfoChangedEventArgs e)
{
Handler?.UpdateValue(nameof(FlyoutBehavior));
}
}
}