Skip to content
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

Merged
merged 4 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Member

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?


foreach (AlertRequestHelper alertRequestHelper in toRemove)
{
Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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
}

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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.

}
}
}
22 changes: 17 additions & 5 deletions src/Controls/src/Core/Platform/AlertManager/AlertManager.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,26 +72,35 @@ public void Dispose()
#pragma warning restore CS0618 // Type or member is obsolete
}

void OnPageBusy(IView sender, bool enabled)
void OnPageBusy(Page sender, bool enabled)
{
_busyCount = Math.Max(0, enabled ? _busyCount + 1 : _busyCount - 1);
#pragma warning disable CA1416, CA1422 // TODO: 'UIApplication.NetworkActivityIndicatorVisible' is unsupported on: 'ios' 13.0 and later
UIApplication.SharedApplication.NetworkActivityIndicatorVisible = _busyCount > 0;
#pragma warning restore CA1416, CA1422
}

void OnAlertRequested(IView sender, AlertArguments arguments)
void OnAlertRequested(Page sender, AlertArguments arguments)
{
if (!PageIsInThisWindow(sender))
return;

PresentAlert(arguments);
}

void OnPromptRequested(IView sender, PromptArguments arguments)
void OnPromptRequested(Page sender, PromptArguments arguments)
{
if (!PageIsInThisWindow(sender))
return;

PresentPrompt(arguments);
}

void OnActionSheetRequested(IView sender, ActionSheetArguments arguments)
void OnActionSheetRequested(Page sender, ActionSheetArguments arguments)
{
if (!PageIsInThisWindow(sender))
return;

PresentActionSheet(arguments);
}

Expand Down Expand Up @@ -189,7 +198,7 @@ static void PresentPopUp(Window virtualView, UIWindow platformView, UIAlertContr
if (modalStack != null && modalStack.Count > 0)
{
var topPage = modalStack[modalStack.Count - 1];
var pageController = topPage.ToUIViewController(topPage.FindMauiContext());
var pageController = topPage.ToUIViewController(topPage.RequireMauiContext());

if (pageController != null)
{
Expand All @@ -206,6 +215,9 @@ static void PresentPopUp(Window virtualView, UIWindow platformView, UIAlertContr
_ = platformView.RootViewController.PresentViewControllerAsync(alert, true);
});
}

bool PageIsInThisWindow(Page page) =>
page?.Window == VirtualView;
}
}
}