Skip to content

Commit fae5431

Browse files
PureWeengithub-actions
authored andcommitted
- fix insetting on MaterialToolBar so its content doesn't go into the display cutout
1 parent baf5b21 commit fae5431

File tree

8 files changed

+113
-4
lines changed

8 files changed

+113
-4
lines changed
78.9 KB
Loading
-41.6 KB
Loading
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace Maui.Controls.Sample.Issues;
2+
3+
[Issue(IssueTracker.Github, 28986, "Test SafeArea Flyout Page for per-edge safe area control", PlatformAffected.Android | PlatformAffected.iOS, issueTestNumber: 8)]
4+
public partial class Issue28986_FlyoutPage : FlyoutPage
5+
{
6+
public Issue28986_FlyoutPage() : base()
7+
{
8+
var page = new Issue28986_ContentPage() { Title = "SafeArea Flyout Test" };
9+
page.ToolbarItems.Add(new ToolbarItem { Text = "Item 1" });
10+
Shell.SetBackgroundColor(page, Colors.Blue);
11+
Detail = new NavigationPage(page);
12+
Flyout = new ContentPage()
13+
{
14+
Title = "Flyout",
15+
Content = new StackLayout
16+
{
17+
Children =
18+
{
19+
new Label { Text = "This is the flyout content" }
20+
}
21+
}
22+
};
23+
}
24+
}
Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using System.Drawing;
2-
31
namespace Maui.Controls.Sample.Issues;
42

53
[Issue(IssueTracker.Github, 28986, "Test SafeArea Shell Page for per-edge safe area control", PlatformAffected.Android | PlatformAffected.iOS, issueTestNumber: 6)]
@@ -8,7 +6,32 @@ public partial class Issue28986_Shell : Shell
86
public Issue28986_Shell() : base()
97
{
108
var page = new Issue28986_ContentPage();
9+
page.ToolbarItems.Add(new ToolbarItem { Text = "Item 1" });
10+
page.Title = "SafeArea Shell Test";
1111
Shell.SetBackgroundColor(page, Colors.Blue);
12-
CurrentItem = page;
12+
Items.Add(new FlyoutItem()
13+
{
14+
Items =
15+
{
16+
new ShellContent()
17+
{
18+
Content = page,
19+
}
20+
}
21+
});
22+
23+
Items.Add(new FlyoutItem()
24+
{
25+
Items =
26+
{
27+
new ShellContent()
28+
{
29+
Content = new ContentPage()
30+
{
31+
Title = "Page 2"
32+
},
33+
}
34+
}
35+
});
1336
}
1437
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#if IOS || ANDROID
2+
using NUnit.Framework;
3+
using UITest.Appium;
4+
using UITest.Core;
5+
6+
namespace Microsoft.Maui.TestCases.Tests.Issues;
7+
8+
public class Issue28986_FlyoutPage : _IssuesUITest
9+
{
10+
public override string Issue => "Test SafeArea Flyout Page for per-edge safe area control";
11+
12+
public Issue28986_FlyoutPage(TestDevice device) : base(device)
13+
{
14+
}
15+
16+
[Test]
17+
[Category(UITestCategories.SafeAreaEdges)]
18+
public void ToolbarExtendsAllTheWayLeftAndRight_FlyoutPage()
19+
{
20+
// 1. Test loads - verify essential elements are present
21+
App.WaitForElement("ContentGrid");
22+
App.SetOrientationLandscape();
23+
App.WaitForElement("ContentGrid");
24+
VerifyScreenshot();
25+
}
26+
}
27+
#endif

src/Core/src/Handlers/Toolbar/ToolbarHandler.Android.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,39 @@ protected override MaterialToolbar CreatePlatformElement()
3030
return Microsoft.Maui.PlatformInterop.CreateToolbar(context, context.GetActionBarHeight(), -1);
3131
}
3232

33+
protected override void ConnectHandler(MaterialToolbar platformView)
34+
{
35+
base.ConnectHandler(platformView);
36+
platformView.ViewAttachedToWindow += OnViewAttachedToWindow;
37+
platformView.ViewDetachedFromWindow += OnViewDetachedFromWindow;
38+
}
39+
40+
41+
void OnViewDetachedFromWindow(object? sender, View.ViewDetachedFromWindowEventArgs e)
42+
{
43+
if (sender is MaterialToolbar mt && mt.IsAlive() && mt.Context is not null)
44+
{
45+
GlobalWindowInsetListenerExtensions.RemoveGlobalWindowInsetListener(mt, mt.Context);
46+
}
47+
}
48+
49+
void OnViewAttachedToWindow(object? sender, View.ViewAttachedToWindowEventArgs e)
50+
{
51+
var context = MauiContext?.Context ?? throw new InvalidOperationException("Context cannot be null");
52+
GlobalWindowInsetListenerExtensions.TrySetGlobalWindowInsetListener(PlatformView, context);
53+
}
54+
3355
private protected override void OnDisconnectHandler(object platformView)
3456
{
3557
base.OnDisconnectHandler(platformView);
58+
3659
if (platformView is MaterialToolbar mt && mt.IsAlive())
60+
{
3761
mt.RemoveFromParent();
62+
mt.ViewAttachedToWindow -= OnViewAttachedToWindow;
63+
mt.ViewDetachedFromWindow -= OnViewDetachedFromWindow;
64+
65+
};
3866
}
3967

4068
public static void MapTitle(IToolbarHandler arg1, IToolbar arg2)

src/Core/src/Platform/Android/GlobalWindowInsetListener.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ public GlobalWindowInsetListener() : base(DispatchModeStop)
5555
var rightInset = Math.Max(systemBars?.Right ?? 0, displayCutout?.Right ?? 0);
5656
var bottomInset = Math.Max(systemBars?.Bottom ?? 0, displayCutout?.Bottom ?? 0);
5757

58+
if (v is MaterialToolbar)
59+
{
60+
v.SetPadding(leftInset, 0, rightInset, 0);
61+
return WindowInsetsCompat.Consumed;
62+
}
63+
5864
// Handle special cases
5965
var appBarLayout = v.FindViewById<AppBarLayout>(Resource.Id.navigationlayout_appbar);
6066

@@ -305,7 +311,7 @@ internal static class GlobalWindowInsetListenerExtensions
305311
/// <param name="context">The Android context to get the listener from</param>
306312
public static bool TrySetGlobalWindowInsetListener(this View view, Context context)
307313
{
308-
if (view.FindParent(
314+
if (view is not MaterialToolbar && view.FindParent(
309315
(parent) =>
310316
parent is NestedScrollView ||
311317
parent is AppBarLayout ||

src/Core/src/PublicAPI/net-android/PublicAPI.Unshipped.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Microsoft.Maui.Handlers.OpenWindowRequest.OpenWindowRequest(Microsoft.Maui.Handl
2424
Microsoft.Maui.Hosting.MauiAppBuilder.Environment.get -> Microsoft.Maui.Hosting.MauiHostEnvironment!
2525
Microsoft.Maui.Hosting.MauiAppBuilder.Properties.get -> System.Collections.Generic.IDictionary<object!, object!>!
2626
Microsoft.Maui.PlatformViewGroup
27+
override Microsoft.Maui.Handlers.ToolbarHandler.ConnectHandler(Google.Android.Material.AppBar.MaterialToolbar! platformView) -> void
2728
override Microsoft.Maui.PlatformViewGroup.JniPeerMembers.get -> Java.Interop.JniPeerMembers!
2829
override Microsoft.Maui.PlatformViewGroup.ThresholdClass.get -> nint
2930
override Microsoft.Maui.PlatformViewGroup.ThresholdType.get -> System.Type!

0 commit comments

Comments
 (0)