-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[macOS] Display alert by Window #14361
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,17 +16,17 @@ internal void Subscribe(Window window) | |
{ | ||
var platformWindow = window.MauiContext.GetPlatformWindow(); | ||
|
||
if (Subscriptions.Any(s => s.Window == platformWindow)) | ||
if (Subscriptions.Any(s => s.PlatformView == platformWindow)) | ||
return; | ||
|
||
Subscriptions.Add(new AlertRequestHelper(platformWindow, window.MauiContext)); | ||
Subscriptions.Add(new AlertRequestHelper(window, platformWindow)); | ||
} | ||
|
||
internal void Unsubscribe(Window window) | ||
{ | ||
var platformWindow = window.MauiContext.GetPlatformWindow(); | ||
|
||
var toRemove = Subscriptions.Where(s => s.Window == platformWindow).ToList(); | ||
var toRemove = Subscriptions.Where(s => s.PlatformView == platformWindow).ToList(); | ||
|
||
foreach (AlertRequestHelper alertRequestHelper in toRemove) | ||
{ | ||
|
@@ -37,32 +37,33 @@ internal void Unsubscribe(Window window) | |
|
||
internal sealed class AlertRequestHelper : IDisposable | ||
{ | ||
static Task<bool>? CurrentAlert; | ||
static Task<string?>? CurrentPrompt; | ||
Task<bool>? CurrentAlert; | ||
Task<string?>? CurrentPrompt; | ||
Comment on lines
-40
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @PureWeen this was causing the issue. No need for statics anymore! We have multiple windows! |
||
|
||
internal AlertRequestHelper(UI.Xaml.Window window, IMauiContext mauiContext) | ||
internal AlertRequestHelper(Window virtualView, UI.Xaml.Window platformView) | ||
{ | ||
Window = window; | ||
MauiContext = mauiContext; | ||
VirtualView = virtualView; | ||
PlatformView = platformView; | ||
|
||
#pragma warning disable CS0618 // TODO: Remove when we internalize/replace MessagingCenter | ||
MessagingCenter.Subscribe<Page, bool>(Window, Page.BusySetSignalName, OnPageBusy); | ||
MessagingCenter.Subscribe<Page, AlertArguments>(Window, Page.AlertSignalName, OnAlertRequested); | ||
MessagingCenter.Subscribe<Page, PromptArguments>(Window, Page.PromptSignalName, OnPromptRequested); | ||
MessagingCenter.Subscribe<Page, ActionSheetArguments>(Window, Page.ActionSheetSignalName, OnActionSheetRequested); | ||
MessagingCenter.Subscribe<Page, bool>(PlatformView, Page.BusySetSignalName, OnPageBusy); | ||
MessagingCenter.Subscribe<Page, AlertArguments>(PlatformView, Page.AlertSignalName, OnAlertRequested); | ||
MessagingCenter.Subscribe<Page, PromptArguments>(PlatformView, Page.PromptSignalName, OnPromptRequested); | ||
MessagingCenter.Subscribe<Page, ActionSheetArguments>(PlatformView, Page.ActionSheetSignalName, OnActionSheetRequested); | ||
#pragma warning restore CS0618 // Type or member is obsolete | ||
} | ||
|
||
public UI.Xaml.Window Window { get; } | ||
public IMauiContext MauiContext { get; } | ||
public Window VirtualView { get; } | ||
|
||
public UI.Xaml.Window PlatformView { get; } | ||
|
||
public void Dispose() | ||
{ | ||
#pragma warning disable CS0618 // TODO: Remove when we internalize/replace MessagingCenter | ||
MessagingCenter.Unsubscribe<Page, bool>(Window, Page.BusySetSignalName); | ||
MessagingCenter.Unsubscribe<Page, AlertArguments>(Window, Page.AlertSignalName); | ||
MessagingCenter.Unsubscribe<Page, PromptArguments>(Window, Page.PromptSignalName); | ||
MessagingCenter.Unsubscribe<Page, ActionSheetArguments>(Window, Page.ActionSheetSignalName); | ||
MessagingCenter.Unsubscribe<Page, bool>(PlatformView, Page.BusySetSignalName); | ||
MessagingCenter.Unsubscribe<Page, AlertArguments>(PlatformView, Page.AlertSignalName); | ||
MessagingCenter.Unsubscribe<Page, PromptArguments>(PlatformView, Page.PromptSignalName); | ||
MessagingCenter.Unsubscribe<Page, ActionSheetArguments>(PlatformView, Page.ActionSheetSignalName); | ||
#pragma warning restore CS0618 // Type or member is obsolete | ||
} | ||
|
||
|
@@ -112,7 +113,7 @@ async void OnAlertRequested(Page sender, AlertArguments arguments) | |
alertDialog.PrimaryButtonText = arguments.Accept; | ||
|
||
// This is a temporary workaround | ||
alertDialog.XamlRoot = Window.Content.XamlRoot; | ||
alertDialog.XamlRoot = PlatformView.Content.XamlRoot; | ||
|
||
var currentAlert = CurrentAlert; | ||
|
||
|
@@ -157,7 +158,7 @@ async void OnPromptRequested(Page sender, PromptArguments arguments) | |
} | ||
|
||
// This is a temporary workaround | ||
promptDialog.XamlRoot = Window.Content.XamlRoot; | ||
promptDialog.XamlRoot = PlatformView.Content.XamlRoot; | ||
|
||
CurrentPrompt = ShowPrompt(promptDialog); | ||
arguments.SetResult(await CurrentPrompt.ConfigureAwait(false)); | ||
|
@@ -205,7 +206,7 @@ void OnActionSheetRequested(Page sender, ActionSheetArguments arguments) | |
|
||
try | ||
{ | ||
var current = sender.ToPlatform(MauiContext); | ||
var current = sender.ToPlatform(VirtualView.RequireMauiContext()); | ||
var pageParent = current?.Parent as FrameworkElement; | ||
|
||
if (pageParent != null) | ||
|
@@ -244,16 +245,8 @@ static async Task<bool> ShowAlert(ContentDialog alert) | |
return null; | ||
} | ||
|
||
bool PageIsInThisWindow(Page page) | ||
{ | ||
var window = page?.Window; | ||
var platformWindow = window?.MauiContext.GetPlatformWindow(); | ||
|
||
if (window is null || platformWindow is null) | ||
return false; | ||
|
||
return platformWindow == Window; | ||
} | ||
bool PageIsInThisWindow(Page page) => | ||
page?.Window == VirtualView; | ||
Comment on lines
-247
to
+249
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The alert manager already knows about the window, so this is now possible. The iOS code had everything set up, but Windows just needed an extra constructor argument for the helper to simplify all this. |
||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PureWeen I though that the use of LINQ was limited, should we remove it from here?