|
1 | 1 | using System.ComponentModel; |
2 | 2 | using System.Globalization; |
| 3 | +using System.Windows.Input; |
3 | 4 | using CommunityToolkit.Maui.Converters; |
4 | 5 | using CommunityToolkit.Maui.Core; |
5 | 6 | using CommunityToolkit.Maui.Extensions; |
@@ -41,16 +42,14 @@ public PopupPage(Popup popup, IPopupOptions popupOptions) |
41 | 42 | this.popup = popup; |
42 | 43 | this.popupOptions = popupOptions; |
43 | 44 |
|
44 | | - // Only set the content if the parent constructor hasn't set the content already; don't override content if it already exists |
45 | | - base.Content ??= new PopupPageLayout(popup, popupOptions); |
46 | | - |
47 | 45 | tapOutsideOfPopupCommand = new Command(async () => |
48 | 46 | { |
49 | 47 | popupOptions.OnTappingOutsideOfPopup?.Invoke(); |
50 | 48 | await CloseAsync(new PopupResult(true)); |
51 | 49 | }, () => popupOptions.CanBeDismissedByTappingOutsideOfPopup); |
52 | | - |
53 | | - Content.GestureRecognizers.Add(new TapGestureRecognizer { Command = tapOutsideOfPopupCommand }); |
| 50 | + |
| 51 | + // Only set the content if the parent constructor hasn't set the content already; don't override content if it already exists |
| 52 | + base.Content ??= new PopupPageLayout(popup, popupOptions, tapOutsideOfPopupCommand); |
54 | 53 |
|
55 | 54 | if (popupOptions is BindableObject bindablePopupOptions) |
56 | 55 | { |
@@ -104,15 +103,15 @@ public async Task CloseAsync(PopupResult result, CancellationToken token = defau |
104 | 103 |
|
105 | 104 | popupClosedEventManager.HandleEvent(this, result, nameof(PopupClosed)); |
106 | 105 | } |
107 | | - |
| 106 | + |
108 | 107 | protected override bool OnBackButtonPressed() |
109 | 108 | { |
110 | 109 | // Only close the Popup if PopupOptions.CanBeDismissedByTappingOutsideOfPopup is true |
111 | 110 | if (popupOptions.CanBeDismissedByTappingOutsideOfPopup) |
112 | 111 | { |
113 | 112 | CloseAsync(new PopupResult(true), CancellationToken.None).SafeFireAndForget(); |
114 | 113 | } |
115 | | - |
| 114 | + |
116 | 115 | // Always return true to let the Android Operating System know that we are manually handling the Navigation request from the Android Back Button |
117 | 116 | return true; |
118 | 117 | } |
@@ -176,36 +175,42 @@ void IQueryAttributable.ApplyQueryAttributes(IDictionary<string, object> query) |
176 | 175 |
|
177 | 176 | internal sealed partial class PopupPageLayout : Grid |
178 | 177 | { |
179 | | - public PopupPageLayout(in Popup popupContent, in IPopupOptions options) |
| 178 | + public PopupPageLayout(in Popup popupContent, in IPopupOptions options, ICommand tapOutsideOfPopupCommand) |
180 | 179 | { |
181 | 180 | Background = BackgroundColor = null; |
182 | 181 |
|
183 | | - Border = new Border |
| 182 | + var border = new Border |
184 | 183 | { |
185 | 184 | BackgroundColor = popupContent.BackgroundColor ??= PopupDefaults.BackgroundColor, |
186 | 185 | Content = popupContent |
187 | 186 | }; |
188 | | - Border.GestureRecognizers.Add(new TapGestureRecognizer()); // Blocks `tapOutsideOfPopupCommand` from closing the Popup when the content is tapped |
| 187 | + |
| 188 | + var backgroundGrid = new BoxView |
| 189 | + { |
| 190 | + BackgroundColor = Colors.Transparent, |
| 191 | + }; |
| 192 | + |
| 193 | + backgroundGrid.GestureRecognizers.Add(new TapGestureRecognizer { Command = tapOutsideOfPopupCommand }); |
| 194 | + |
| 195 | + Children.Add(backgroundGrid); |
189 | 196 |
|
190 | 197 | // Bind `Popup` values through to Border using OneWay Bindings |
191 | | - Border.SetBinding(Border.MarginProperty, static (Popup popup) => popup.Margin, source: popupContent, mode: BindingMode.OneWay); |
192 | | - Border.SetBinding(Border.PaddingProperty, static (Popup popup) => popup.Padding, source: popupContent, mode: BindingMode.OneWay); |
193 | | - Border.SetBinding(Border.BackgroundProperty, static (Popup popup) => popup.Background, source: popupContent, mode: BindingMode.OneWay); |
194 | | - Border.SetBinding(Border.BackgroundColorProperty, static (Popup popup) => popup.BackgroundColor, source: popupContent, mode: BindingMode.OneWay); |
195 | | - Border.SetBinding(Border.VerticalOptionsProperty, static (Popup popup) => popup.VerticalOptions, source: popupContent, mode: BindingMode.OneWay); |
196 | | - Border.SetBinding(Border.HorizontalOptionsProperty, static (Popup popup) => popup.HorizontalOptions, source: popupContent, mode: BindingMode.OneWay); |
| 198 | + border.SetBinding(Border.MarginProperty, static (Popup popup) => popup.Margin, source: popupContent, mode: BindingMode.OneWay); |
| 199 | + border.SetBinding(Border.PaddingProperty, static (Popup popup) => popup.Padding, source: popupContent, mode: BindingMode.OneWay); |
| 200 | + border.SetBinding(Border.BackgroundProperty, static (Popup popup) => popup.Background, source: popupContent, mode: BindingMode.OneWay); |
| 201 | + border.SetBinding(Border.BackgroundColorProperty, static (Popup popup) => popup.BackgroundColor, source: popupContent, mode: BindingMode.OneWay); |
| 202 | + border.SetBinding(Border.VerticalOptionsProperty, static (Popup popup) => popup.VerticalOptions, source: popupContent, mode: BindingMode.OneWay); |
| 203 | + border.SetBinding(Border.HorizontalOptionsProperty, static (Popup popup) => popup.HorizontalOptions, source: popupContent, mode: BindingMode.OneWay); |
197 | 204 |
|
198 | 205 | // Bind `PopupOptions` values through to Border using OneWay Bindings |
199 | | - Border.SetBinding(Border.ShadowProperty, static (IPopupOptions options) => options.Shadow, source: options, mode: BindingMode.OneWay); |
200 | | - Border.SetBinding(Border.StrokeProperty, static (IPopupOptions options) => options.Shape, source: options, converter: new BorderStrokeConverter(), mode: BindingMode.OneWay); |
201 | | - Border.SetBinding(Border.StrokeShapeProperty, static (IPopupOptions options) => options.Shape, source: options, mode: BindingMode.OneWay); |
202 | | - Border.SetBinding(Border.StrokeThicknessProperty, static (IPopupOptions options) => options.Shape, source: options, converter: new BorderStrokeThicknessConverter(), mode: BindingMode.OneWay); |
| 206 | + border.SetBinding(Border.ShadowProperty, static (IPopupOptions options) => options.Shadow, source: options, mode: BindingMode.OneWay); |
| 207 | + border.SetBinding(Border.StrokeProperty, static (IPopupOptions options) => options.Shape, source: options, converter: new BorderStrokeConverter(), mode: BindingMode.OneWay); |
| 208 | + border.SetBinding(Border.StrokeShapeProperty, static (IPopupOptions options) => options.Shape, source: options, mode: BindingMode.OneWay); |
| 209 | + border.SetBinding(Border.StrokeThicknessProperty, static (IPopupOptions options) => options.Shape, source: options, converter: new BorderStrokeThicknessConverter(), mode: BindingMode.OneWay); |
203 | 210 |
|
204 | | - Children.Add(Border); |
| 211 | + Children.Add(border); |
205 | 212 | } |
206 | 213 |
|
207 | | - public Border Border { get; } |
208 | | - |
209 | 214 | sealed partial class BorderStrokeThicknessConverter : BaseConverterOneWay<Shape?, double> |
210 | 215 | { |
211 | 216 | public override double DefaultConvertReturnValue { get; set; } = PopupOptionsDefaults.BorderStrokeThickness; |
|
0 commit comments