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

Fix transparent default modal background #23985

Merged
merged 3 commits into from
Aug 5, 2024
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,6 +16,7 @@
using AndroidX.AppCompat.App;
using Microsoft.Maui.LifecycleEvents;
using AndroidX.Activity;
using System.ComponentModel;

namespace Microsoft.Maui.Controls.Platform
{
Expand Down Expand Up @@ -241,6 +242,8 @@ internal class ModalFragment : DialogFragment
public ModalFragment(IMauiContext mauiContext, Page modal)
{
_modal = modal;
_modal.PropertyChanged += OnModalPagePropertyChanged;
_modal.HandlerChanged += OnPageHandlerChanged;
_mauiWindowContext = mauiContext;
}

Expand All @@ -256,6 +259,53 @@ public ModalFragment(IMauiContext mauiContext, Page modal)
return dialog;
}

void OnPageHandlerChanged(object? sender, EventArgs e)
{
if (sender is Page page)
{
page.HandlerChanged -= OnPageHandlerChanged;
}

UpdateBackgroundColor();
}

void OnModalPagePropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (_modal is null)
{
if (sender is Page page)
{
page.PropertyChanged -= OnModalPagePropertyChanged;
page.HandlerChanged -= OnPageHandlerChanged;
}

return;
}


if (e.IsOneOf(Page.BackgroundColorProperty, Page.BackgroundProperty))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think BackgroundColor will always fire Background, so this is allocating an array twice and then firing twice for each time the Color is set.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the Background Mapper will fire but the Background INPC won't fire

Here's a quick test
image

{
UpdateBackgroundColor();
}
}

void UpdateBackgroundColor()
{
if (_modal is not IView view || view.Handler is not IPlatformViewHandler platformViewHandler)
{
return;
}

var pageView = platformViewHandler.PlatformView;

if (pageView is null)
return;

var modalBkgndColor = view.Background;
if (modalBkgndColor is null)
pageView.SetWindowBackground();
}

public override AView OnCreateView(LayoutInflater inflater, ViewGroup? container, Bundle? savedInstanceState)
{
var modalContext = _mauiWindowContext
Expand All @@ -264,6 +314,8 @@ public override AView OnCreateView(LayoutInflater inflater, ViewGroup? container
_navigationRootManager = modalContext.GetNavigationRootManager();
_navigationRootManager.Connect(_modal, modalContext);

UpdateBackgroundColor();

return _navigationRootManager?.RootView ??
throw new InvalidOperationException("Root view not initialized");
}
Expand Down Expand Up @@ -309,6 +361,9 @@ void OnAnimationEnded(object? sender, AAnimation.AnimationEndEventArgs e)

public override void OnDismiss(IDialogInterface dialog)
{
_modal.PropertyChanged -= OnModalPagePropertyChanged;
_modal.HandlerChanged -= OnPageHandlerChanged;

if (_modal.Toolbar?.Handler is not null)
{
_modal.Toolbar.Handler = null;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
75 changes: 75 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue23973.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
namespace Maui.Controls.Sample.Issues;

[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.Github, "23973", "Default Modal Page Is Not Transparent", PlatformAffected.All)]
public partial class Issue23973 : ContentPage
{
public Issue23973()
{
Content = new VerticalStackLayout()
{
new Label(){
Text = "Label 1"
},
new Button()
{
Text = "Click to show opaque default modal page",
Command = new Command(async () => await Navigation.PushModalAsync(new ModalPage())),
LineBreakMode = LineBreakMode.WordWrap,
AutomationId = "PushModal"
},
new Button()
{
Text = "Click to show transparent modal page",
Command = new Command(async () => await Navigation.PushModalAsync(new TransparentModalPage())),
LineBreakMode = LineBreakMode.WordWrap,
AutomationId = "PushTransparentModal"
},
new Label(){
Text = "Label 2"
},
new Label(){
Text = "Label 3"
},
};
}

public class TransparentModalPage : ContentPage
{
public TransparentModalPage()
{
Title = "Transparent Modal Page";
BackgroundColor = Colors.Transparent;

Content = new VerticalStackLayout()
{
new Button()
{
AutomationId = "PopModal",
Text = "If you do not see through to the underlying page, this test has failed",
LineBreakMode = LineBreakMode.WordWrap,
Command = new Command(async () => await Navigation.PopModalAsync())
}
};
}
}

public class ModalPage : ContentPage
{
public ModalPage()
{
Title = "Opaque Modal Page";

Content = new VerticalStackLayout()
{
new Button()
{
AutomationId = "PopModal",
Text = "If you see through to the underlying page, this test has failed",
LineBreakMode = LineBreakMode.WordWrap,
Command = new Command(async () => await Navigation.PopModalAsync())
}
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#if !MACCATALYST // MACCATALYST doesn't support VerifyScreenshot tests
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues
{
public class Issue23973(TestDevice device) : _IssuesUITest(device)
{
protected override bool ResetAfterEachTest => true;
public override string Issue => "Default Modal Page Is Not Transparent";

[Test]
[Category(UITestCategories.Navigation)]
public void VerifyOpaqueModalDefault()
{
App.WaitForElement("PushModal");
App.Tap("PushModal");
VerifyScreenshot();
}

#if !WINDOWS
[Test]
[Category(UITestCategories.Navigation)]
public void VerifyTransparentModalShowsPageBeneathModal()
{
App.WaitForElement("PushTransparentModal");
App.Tap("PushTransparentModal");
VerifyScreenshot();
}
#endif
}
}
#endif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading