/
FlyoutBase.cs
337 lines (275 loc) · 8.98 KB
/
FlyoutBase.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
330
331
332
333
334
335
336
337
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using Uno.Disposables;
using Uno.UI;
using Windows.Foundation;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml.Media;
#if XAMARIN_IOS
using View = UIKit.UIView;
#elif XAMARIN_ANDROID
using View = Android.Views.View;
#else
using View = Windows.UI.Xaml.UIElement;
#endif
namespace Windows.UI.Xaml.Controls.Primitives
{
public partial class FlyoutBase : DependencyObject
{
public event EventHandler Opened;
public event EventHandler Closed;
public event EventHandler Opening;
public event TypedEventHandler<FlyoutBase, FlyoutBaseClosingEventArgs> Closing;
private bool _isOpen = false;
internal bool m_isPositionedAtPoint;
protected internal Windows.UI.Xaml.Controls.Popup _popup;
private bool _isLightDismissEnabled = true;
private Point? _popupPositionInTarget;
private readonly SerialDisposable _sizeChangedDisposable = new SerialDisposable();
public FlyoutBase()
{
}
private void EnsurePopupCreated()
{
if (_popup == null)
{
LightDismissOverlayBackground = Application.Current?.Resources["FlyoutLightDismissOverlayBackground"] as Brush ??
// This is normally a no-op - the above line should retrieve the framework-level resource. This is purely to fail the build when
// Resources/Styles are overhauled (and the above will no longer be valid)
Uno.UI.GlobalStaticResources.FlyoutLightDismissOverlayBackground as Brush;
_popup = new Windows.UI.Xaml.Controls.Popup()
{
Child = CreatePresenter(),
IsLightDismissEnabled = _isLightDismissEnabled,
};
_popup.Opened += OnPopupOpened;
_popup.Closed += OnPopupClosed;
_popup.BindToEquivalentProperty(this, nameof(LightDismissOverlayMode));
_popup.BindToEquivalentProperty(this, nameof(LightDismissOverlayBackground));
InitializePopupPanel();
SynchronizeDataContext();
}
}
/// <summary>
/// Controls the appeareance of <see cref="MenuFlyout"/>, when true the native popups and appearance
/// is used, otherwise the UWP appeareance is used. The default value is provided by <see cref="FeatureConfiguration.Style.UseUWPDefaultStyles"/>.
/// </summary>
public bool UseNativePopup { get; set; } = !FeatureConfiguration.Style.UseUWPDefaultStyles;
protected virtual void InitializePopupPanel()
{
InitializePopupPanelPartial();
}
private protected bool IsLightDismissOverlayEnabled
{
get => _isLightDismissEnabled;
set
{
_isLightDismissEnabled = value;
if (_popup != null)
{
_popup.IsLightDismissEnabled = value;
}
}
}
partial void InitializePopupPanelPartial();
private void OnPopupOpened(object sender, object e)
{
if (_popup.Child is FrameworkElement child)
{
SizeChangedEventHandler handler = (_, __) => SetPopupPositionPartial(Target, _popupPositionInTarget);
child.SizeChanged += handler;
_sizeChangedDisposable.Disposable = Disposable
.Create(() => child.SizeChanged -= handler);
}
}
#region Placement
/// <summary>
/// Preferred placement of the flyout.
/// </summary>
/// <remarks>
/// If there's not enough place, the following logic will be used:
/// https://docs.microsoft.com/en-us/previous-versions/windows/apps/dn308515(v=win.10)#placing-a-flyout
/// </remarks>
public FlyoutPlacementMode Placement
{
get { return (FlyoutPlacementMode)GetValue(PlacementProperty); }
set { SetValue(PlacementProperty, value); }
}
public static DependencyProperty PlacementProperty { get; } =
DependencyProperty.Register(
"Placement",
typeof(FlyoutPlacementMode),
typeof(FlyoutBase),
new FrameworkPropertyMetadata(default(FlyoutPlacementMode))
);
#endregion
public LightDismissOverlayMode LightDismissOverlayMode
{
get
{
return (LightDismissOverlayMode)this.GetValue(LightDismissOverlayModeProperty);
}
set
{
this.SetValue(LightDismissOverlayModeProperty, value);
}
}
public static DependencyProperty LightDismissOverlayModeProperty { get; } =
Windows.UI.Xaml.DependencyProperty.Register(
"LightDismissOverlayMode", typeof(LightDismissOverlayMode),
typeof(FlyoutBase),
new FrameworkPropertyMetadata(default(LightDismissOverlayMode)));
/// <summary>
/// Sets the light-dismiss colour, if the overlay is enabled. The external API for modifying this is to override the PopupLightDismissOverlayBackground, etc, static resource values.
/// </summary>
internal Brush LightDismissOverlayBackground
{
get { return (Brush)GetValue(LightDismissOverlayBackgroundProperty); }
set { SetValue(LightDismissOverlayBackgroundProperty, value); }
}
internal static readonly DependencyProperty LightDismissOverlayBackgroundProperty =
DependencyProperty.Register("LightDismissOverlayBackground", typeof(Brush), typeof(FlyoutBase), new PropertyMetadata(null));
public FrameworkElement Target { get; private set; }
/// <summary>
/// Defines an optional position of the popup in the <see cref="Target"/> element.
/// </summary>
internal Point? PopupPositionInTarget => _popupPositionInTarget;
public void Hide()
{
Hide(canCancel: true);
}
internal void Hide(bool canCancel)
{
if (!_isOpen)
{
return;
}
if (canCancel)
{
bool cancel = false;
OnClosing(ref cancel);
var closing = new FlyoutBaseClosingEventArgs();
Closing?.Invoke(this, closing);
if (cancel || closing.Cancel)
{
return;
}
}
Close();
_isOpen = false;
OnClosed();
Closed?.Invoke(this, EventArgs.Empty);
}
public void ShowAt(FrameworkElement placementTarget)
{
ShowAtCore(placementTarget, null);
}
public void ShowAt(DependencyObject placementTarget, FlyoutShowOptions showOptions)
{
if (placementTarget is FrameworkElement fe)
{
ShowAtCore(fe, showOptions);
}
}
private protected virtual void ShowAtCore(FrameworkElement placementTarget, FlyoutShowOptions showOptions)
{
EnsurePopupCreated();
if (_isOpen)
{
if (placementTarget == Target)
{
return;
}
else
{
// Close at previous placement target before opening at new one (without raising Closing)
Hide(canCancel: false);
}
}
Target = placementTarget;
if(showOptions != null)
{
_popupPositionInTarget = showOptions.Position;
}
OnOpening();
Opening?.Invoke(this, EventArgs.Empty);
Open();
_isOpen = true;
OnOpened();
Opened?.Invoke(this, EventArgs.Empty);
}
private protected virtual void OnOpening() { }
private protected virtual void OnClosing(ref bool cancel) { }
private protected virtual void OnClosed() { }
private protected virtual void OnOpened() { }
protected virtual Control CreatePresenter()
{
return null;
}
private void OnPopupClosed(object sender, object e)
{
Hide(canCancel: false);
_sizeChangedDisposable.Disposable = null;
}
protected internal virtual void Close()
{
if (_popup != null)
{
_popup.IsOpen = false;
}
}
protected internal virtual void Open()
{
SetPopupPositionPartial(Target, _popupPositionInTarget);
_popup.IsOpen = true;
}
partial void SetPopupPositionPartial(UIElement placementTarget, Point? absolutePosition);
partial void OnDataContextChangedPartial(DependencyPropertyChangedEventArgs e)
{
SynchronizeDataContext();
}
private void SynchronizeDataContext() =>
// This is present to force the dataContext to be passed to the popup of the flyout since it is not directly a child in the visual tree of the flyout.
_popup?.SetValue(Popup.DataContextProperty, this.DataContext, precedence: DependencyPropertyValuePrecedences.Local);
partial void OnTemplatedParentChangedPartial(DependencyPropertyChangedEventArgs e)
{
_popup?.SetValue(Popup.TemplatedParentProperty, TemplatedParent, precedence: DependencyPropertyValuePrecedences.Local);
}
public static FlyoutBase GetAttachedFlyout(FrameworkElement element)
{
return (FlyoutBase)element.GetValue(AttachedFlyoutProperty);
}
public static void SetAttachedFlyout(FrameworkElement element, FlyoutBase value)
{
element.SetValue(AttachedFlyoutProperty, value);
}
public static void ShowAttachedFlyout(FrameworkElement flyoutOwner)
{
var flyout = GetAttachedFlyout(flyoutOwner);
flyout?.ShowAt(flyoutOwner);
}
internal static Rect CalculateAvailableWindowRect(bool isMenuFlyout, Controls.Popup popup, object placementTarget, bool hasTargetPosition, Point positionPoint, bool isFull)
{
// UNO TODO: UWP also uses values coming from the input pane and app bars, if any.
// Make sure of migrate to XamlRoot: https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.xamlroot
return ApplicationView.GetForCurrentView().VisibleBounds;
}
internal void SetPresenterStyle(
Control pPresenter,
Style pStyle)
{
Debug.Assert(pPresenter != null);
if (pStyle != null)
{
pPresenter.Style = pStyle;
}
else
{
pPresenter.ClearValue(Control.StyleProperty);
}
}
internal Control GetPresenter() => _popup?.Child as Control;
}
}