This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 471
/
PopupRenderer.uwp.cs
309 lines (251 loc) · 9.94 KB
/
PopupRenderer.uwp.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
using System;
using System.ComponentModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Xamarin.CommunityToolkit.UI.Views;
using Xamarin.Forms;
using Xamarin.Forms.Internals;
using Xamarin.Forms.Platform.UWP;
using Specific = Xamarin.CommunityToolkit.PlatformConfiguration.WindowsSpecific.PopUp;
using UWPThickness = Windows.UI.Xaml.Thickness;
using XamlStyle = Windows.UI.Xaml.Style;
[assembly: ExportRenderer(typeof(BasePopup), typeof(PopupRenderer))]
namespace Xamarin.CommunityToolkit.UI.Views
{
public class PopupRenderer : Flyout, IVisualElementRenderer
{
const double defaultBorderThickness = 2;
const double defaultSize = 600;
bool isDisposed = false;
XamlStyle? flyoutStyle;
XamlStyle? panelStyle;
public BasePopup? Element { get; private set; }
internal ViewToRendererConverter.WrapperControl? Control { get; private set; }
FrameworkElement? IVisualElementRenderer.ContainerElement => null;
VisualElement? IVisualElementRenderer.Element => Element;
public event EventHandler<VisualElementChangedEventArgs>? ElementChanged;
public event EventHandler<PropertyChangedEventArgs>? ElementPropertyChanged;
public PopupRenderer()
{
}
void IVisualElementRenderer.SetElement(VisualElement element)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
if (element is not BasePopup popup)
throw new ArgumentNullException("Element is not of type " + typeof(BasePopup), nameof(element));
var oldElement = Element;
Element = popup;
CreateControl();
if (oldElement != null)
oldElement.PropertyChanged -= OnElementPropertyChanged;
element.PropertyChanged += OnElementPropertyChanged;
OnElementChanged(new ElementChangedEventArgs<BasePopup?>(oldElement, Element));
}
void CreateControl()
{
if (Control == null && Element?.Content != null)
{
Control = new ViewToRendererConverter.WrapperControl(Element.Content);
Content = Control;
}
}
void InitializeStyles()
{
flyoutStyle = new XamlStyle { TargetType = typeof(FlyoutPresenter) };
panelStyle = new XamlStyle { TargetType = typeof(Panel) };
}
protected virtual void OnElementChanged(ElementChangedEventArgs<BasePopup?> e)
{
if (e.NewElement != null && !isDisposed)
{
ConfigureControl();
Show();
}
ElementChanged?.Invoke(this, new VisualElementChangedEventArgs(e.OldElement, e.NewElement));
}
protected virtual void OnElementPropertyChanged(object? sender, PropertyChangedEventArgs args)
{
if (Element is BasePopup basePopup && !isDisposed)
{
if (args.PropertyName == BasePopup.VerticalOptionsProperty.PropertyName ||
args.PropertyName == BasePopup.HorizontalOptionsProperty.PropertyName ||
args.PropertyName == BasePopup.SizeProperty.PropertyName ||
args.PropertyName == BasePopup.ColorProperty.PropertyName)
{
ConfigureControl();
}
}
ElementPropertyChanged?.Invoke(this, args);
}
void ConfigureControl()
{
InitializeStyles();
SetEvents();
SetColor();
SetBorderColor();
SetSize();
SetLayout();
ApplyStyles();
}
void SetEvents()
{
Closing += OnClosing;
if (Element != null)
Element.Dismissed += OnDismissed;
Opened += OnOpened;
}
void SetSize()
{
_ = Element ?? throw new InvalidOperationException($"{nameof(Element)} cannot be null");
_ = Control ?? throw new InvalidOperationException($"{nameof(Control)} cannot be null");
_ = flyoutStyle ?? throw new InvalidOperationException($"{nameof(flyoutStyle)} cannot be null");
var standardSize = new Size { Width = defaultSize, Height = defaultSize / 2 };
var currentSize = Element.Size != default(Size) ? Element.Size : standardSize;
Control.Width = currentSize.Width;
Control.Height = currentSize.Height;
flyoutStyle.Setters.Add(new Windows.UI.Xaml.Setter(FlyoutPresenter.MinHeightProperty, currentSize.Height + (defaultBorderThickness * 2)));
flyoutStyle.Setters.Add(new Windows.UI.Xaml.Setter(FlyoutPresenter.MinWidthProperty, currentSize.Width + (defaultBorderThickness * 2)));
flyoutStyle.Setters.Add(new Windows.UI.Xaml.Setter(FlyoutPresenter.MaxHeightProperty, currentSize.Height + (defaultBorderThickness * 2)));
flyoutStyle.Setters.Add(new Windows.UI.Xaml.Setter(FlyoutPresenter.MaxWidthProperty, currentSize.Width + (defaultBorderThickness * 2)));
}
void SetLayout()
{
LightDismissOverlayMode = LightDismissOverlayMode.On;
if (Element != null)
SetDialogPosition(Element.VerticalOptions, Element.HorizontalOptions);
}
void SetBorderColor()
{
_ = flyoutStyle ?? throw new NullReferenceException();
flyoutStyle.Setters.Add(new Windows.UI.Xaml.Setter(FlyoutPresenter.PaddingProperty, 0));
flyoutStyle.Setters.Add(new Windows.UI.Xaml.Setter(FlyoutPresenter.BorderThicknessProperty, new UWPThickness(defaultBorderThickness)));
if (Element == null)
{
Log.Warning("warning", "The PopUpView is null.");
return;
}
var borderColor = Specific.GetBorderColor(Element);
if (borderColor == default(Color))
flyoutStyle.Setters.Add(new Windows.UI.Xaml.Setter(FlyoutPresenter.BorderBrushProperty, Color.FromHex("#2e6da0").ToWindowsColor()));
else
flyoutStyle.Setters.Add(new Windows.UI.Xaml.Setter(FlyoutPresenter.BorderBrushProperty, borderColor.ToWindowsColor()));
}
void SetColor()
{
_ = Element?.Content ?? throw new NullReferenceException();
_ = panelStyle ?? throw new NullReferenceException();
_ = flyoutStyle ?? throw new NullReferenceException();
if (Element.Content.BackgroundColor == default(Color))
panelStyle.Setters.Add(new Windows.UI.Xaml.Setter(Panel.BackgroundProperty, Element.Color.ToWindowsColor()));
flyoutStyle.Setters.Add(new Windows.UI.Xaml.Setter(FlyoutPresenter.BackgroundProperty, Element.Color.ToWindowsColor()));
if (Element.Color == Color.Transparent)
flyoutStyle.Setters.Add(new Windows.UI.Xaml.Setter(FlyoutPresenter.IsDefaultShadowEnabledProperty, false));
}
void ApplyStyles()
{
_ = Control ?? throw new NullReferenceException();
Control.Style = panelStyle;
FlyoutPresenterStyle = flyoutStyle;
}
void Show()
{
if (Element?.Anchor != null)
{
var anchor = Platform.GetRenderer(Element.Anchor).ContainerElement;
FlyoutBase.SetAttachedFlyout(anchor, this);
FlyoutBase.ShowAttachedFlyout(anchor);
}
else
{
var frameworkElement = Platform.GetRenderer(Element?.Parent as VisualElement)?.ContainerElement;
FlyoutBase.SetAttachedFlyout(frameworkElement, this);
FlyoutBase.ShowAttachedFlyout(frameworkElement);
}
Element?.OnOpened();
}
void SetDialogPosition(LayoutOptions verticalOptions, LayoutOptions horizontalOptions)
{
if (IsTopLeft())
Placement = FlyoutPlacementMode.TopEdgeAlignedLeft;
else if (IsTop())
Placement = FlyoutPlacementMode.Top;
else if (IsTopRight())
Placement = FlyoutPlacementMode.TopEdgeAlignedRight;
else if (IsRight())
Placement = FlyoutPlacementMode.Right;
else if (IsBottomRight())
Placement = FlyoutPlacementMode.BottomEdgeAlignedRight;
else if (IsBottom())
Placement = FlyoutPlacementMode.Bottom;
else if (IsBottomLeft())
Placement = FlyoutPlacementMode.BottomEdgeAlignedLeft;
else if (IsLeft())
Placement = FlyoutPlacementMode.Left;
else if (Element != null && Element.Anchor == null)
Placement = FlyoutPlacementMode.Full;
else
Placement = FlyoutPlacementMode.Top;
bool IsTopLeft() => verticalOptions.Alignment == LayoutAlignment.Start && horizontalOptions.Alignment == LayoutAlignment.Start;
bool IsTop() => verticalOptions.Alignment == LayoutAlignment.Start && horizontalOptions.Alignment == LayoutAlignment.Center;
bool IsTopRight() => verticalOptions.Alignment == LayoutAlignment.Start && horizontalOptions.Alignment == LayoutAlignment.End;
bool IsRight() => verticalOptions.Alignment == LayoutAlignment.Center && horizontalOptions.Alignment == LayoutAlignment.End;
bool IsBottomRight() => verticalOptions.Alignment == LayoutAlignment.End && horizontalOptions.Alignment == LayoutAlignment.End;
bool IsBottom() => verticalOptions.Alignment == LayoutAlignment.End && horizontalOptions.Alignment == LayoutAlignment.Center;
bool IsBottomLeft() => verticalOptions.Alignment == LayoutAlignment.End && horizontalOptions.Alignment == LayoutAlignment.Start;
bool IsLeft() => verticalOptions.Alignment == LayoutAlignment.Center && horizontalOptions.Alignment == LayoutAlignment.Start;
}
SizeRequest IVisualElementRenderer.GetDesiredSize(double widthConstraint, double heightConstraint)
{
if (isDisposed || Control == null)
return default(SizeRequest);
var constraint = new Windows.Foundation.Size(widthConstraint, heightConstraint);
Control.Measure(constraint);
var size = new Size(Math.Ceiling(Control.DesiredSize.Width), Math.Ceiling(Control.DesiredSize.Height));
return new SizeRequest(size);
}
UIElement? IVisualElementRenderer.GetNativeElement() => Control;
// The UWP PopupRenderer needs to maintain it's own version of
// `isOpen` because our popup lifecycle differs slightly from
// the UWP version of `IsOpen`. Without this variable usages
// in OnDismissed and OnClosing will not work as expected.
bool isOpen = true;
void OnDismissed(object? sender, PopupDismissedEventArgs e)
{
if (!isOpen)
return;
isOpen = false;
Hide();
}
void OnClosing(object? sender, object e)
{
if (isOpen && Element?.IsLightDismissEnabled is true)
Element.LightDismiss();
if (isOpen && e is FlyoutBaseClosingEventArgs args)
args.Cancel = true;
}
void OnOpened(object sender, object e) =>
isOpen = true;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!isDisposed && disposing)
{
if (Element != null)
Element.Dismissed -= OnDismissed;
if (Control != null)
Control.CleanUp();
Element = null;
Control = null;
Closing -= OnClosing;
Opened -= OnOpened;
}
isDisposed = true;
}
}
}