Skip to content
Draft
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 @@ -9,6 +9,7 @@
using AndroidX.Core.View;
using AndroidX.Fragment.App;
using Google.Android.Material.AppBar;
using Microsoft.Maui.Platform;
using AndroidAnimation = Android.Views.Animations.Animation;
using AnimationSet = Android.Views.Animations.AnimationSet;
using AToolbar = AndroidX.AppCompat.Widget.Toolbar;
Expand Down Expand Up @@ -135,6 +136,12 @@ public override AView OnCreateView(LayoutInflater inflater, ViewGroup container,

_root = inflater.Inflate(Controls.Resource.Layout.shellcontent, null).JavaCast<CoordinatorLayout>();

// Set up the CoordinatorLayout with a local inset listener
if (_root is CoordinatorLayout rootLayout && Context is AndroidX.Fragment.App.FragmentActivity context)
{
var localListener = new GlobalWindowInsetListener();
_root = GlobalWindowInsetListener.SetupCoordinatorLayoutWithLocalListener(rootLayout, localListener);
}
var shellContentMauiContext = _shellContext.Shell.Handler.MauiContext.MakeScoped(layoutInflater: inflater, fragmentManager: ChildFragmentManager);

Maui.IElement parentElement = (_shellContent as Maui.IElement) ?? _page;
Expand All @@ -143,9 +150,6 @@ public override AView OnCreateView(LayoutInflater inflater, ViewGroup container,
_toolbar = (AToolbar)shellToolbar.ToPlatform(shellContentMauiContext);

var appBar = _root.FindViewById<AppBarLayout>(Resource.Id.shellcontent_appbar);

GlobalWindowInsetListenerExtensions.TrySetGlobalWindowInsetListener(_root, this.Context);

appBar.AddView(_toolbar);
_viewhandler = _page.ToHandler(shellContentMauiContext);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
using AndroidX.ViewPager2.Widget;
using Google.Android.Material.Tabs;
using Microsoft.Extensions.Logging;
using Microsoft.Maui.Platform;
using Google.Android.Material.AppBar;
using AToolbar = AndroidX.AppCompat.Widget.Toolbar;
using AView = Android.Views.View;

Expand Down Expand Up @@ -101,7 +103,14 @@ public override AView OnCreateView(LayoutInflater inflater, ViewGroup container,
var context = Context;
var root = PlatformInterop.CreateShellCoordinatorLayout(context);
var appbar = PlatformInterop.CreateShellAppBar(context, Resource.Attribute.appBarLayoutStyle, root);
GlobalWindowInsetListenerExtensions.TrySetGlobalWindowInsetListener(root, this.Context);

// Set up the CoordinatorLayout with a local inset listener
if (context?.GetGlobalWindowInsetListener() is GlobalWindowInsetListener globalListener)
{
var localListener = new GlobalWindowInsetListener();
root = GlobalWindowInsetListener.SetupCoordinatorLayoutWithLocalListener(root, localListener);
}

int actionBarHeight = context.GetActionBarHeight();

var shellToolbar = new Toolbar(shellSection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ internal class ModalFragment : DialogFragment
Page _modal;
IMauiContext _mauiWindowContext;
NavigationRootManager? _navigationRootManager;
GlobalWindowInsetListener? _modalInsetListener;
static readonly ColorDrawable TransparentColorDrawable = new(AColor.Transparent);
bool _pendingAnimation = true;

Expand Down Expand Up @@ -313,15 +312,6 @@ public override AView OnCreateView(LayoutInflater inflater, ViewGroup? container
var rootView = _navigationRootManager?.RootView ??
throw new InvalidOperationException("Root view not initialized");

var context = rootView.Context ?? inflater.Context;
if (context is not null)
{
// Modal pages get their own separate GlobalWindowInsetListener instance
// This prevents cross-contamination with the main window's inset tracking
_modalInsetListener = new GlobalWindowInsetListener();
ViewCompat.SetOnApplyWindowInsetsListener(rootView, _modalInsetListener);
}

if (IsAnimated)
{
_ = new GenericGlobalLayoutListener((listener, view) =>
Expand Down Expand Up @@ -376,20 +366,6 @@ public override void OnDismiss(IDialogInterface dialog)
_modal.Toolbar.Handler = null;
}

// Clean up the modal's separate GlobalWindowInsetListener
if (_modalInsetListener is not null)
{
_modalInsetListener.ResetAllViews();
_modalInsetListener.Dispose();
_modalInsetListener = null;
}

var rootView = _navigationRootManager?.RootView;
if (rootView is not null)
{
ViewCompat.SetOnApplyWindowInsetsListener(rootView, null);
}

_modal.Handler = null;
_modal = null!;
_mauiWindowContext = null!;
Expand Down
35 changes: 20 additions & 15 deletions src/Core/src/Handlers/View/ViewHandler.Android.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using Android.Views;
using AndroidX.Core.View;
using Microsoft.Maui.Platform;
using PlatformView = Android.Views.View;

namespace Microsoft.Maui.Handlers
Expand Down Expand Up @@ -260,27 +261,31 @@ internal static void MapSafeAreaEdges(IViewHandler handler, IView view)
{
return;
}
if (handler.MauiContext?.Context is null || handler.PlatformView is not PlatformView platformView)

if (handler.MauiContext?.Context is null || handler.PlatformView is not View platformView)
{
return;
}

switch (platformView)
// Use our static registry approach to find and reset the appropriate listener
var listener = GlobalWindowInsetListener.FindListenerForView(platformView) ??
handler.MauiContext.Context.GetGlobalWindowInsetListener(); // Check for specific view group types that handle safe area
if (handler.PlatformView is ContentViewGroup cvg)
{
listener?.ResetAppliedSafeAreas(cvg);
cvg.MarkSafeAreaEdgeConfigurationChanged();
}
else if (handler.PlatformView is LayoutViewGroup lvg)
{
case ContentViewGroup cvg:
handler.MauiContext.Context.GetGlobalWindowInsetListener()?.ResetAppliedSafeAreas(cvg);
cvg.MarkSafeAreaEdgeConfigurationChanged();
break;
case LayoutViewGroup lvg:
handler.MauiContext.Context.GetGlobalWindowInsetListener()?.ResetAppliedSafeAreas(lvg);
lvg.MarkSafeAreaEdgeConfigurationChanged();
break;
case MauiScrollView msv:
handler.MauiContext.Context.GetGlobalWindowInsetListener()?.ResetAppliedSafeAreas(msv);
msv.MarkSafeAreaEdgeConfigurationChanged();
break;
listener?.ResetAppliedSafeAreas(lvg);
lvg.MarkSafeAreaEdgeConfigurationChanged();
}
else if (handler.PlatformView is MauiScrollView msv)
{
listener?.ResetAppliedSafeAreas(msv);
msv.MarkSafeAreaEdgeConfigurationChanged();
}

view.InvalidateMeasure();
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/Core/src/Handlers/Window/WindowHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using AndroidX.Core.View;
using AndroidX.Window.Layout;
using Google.Android.Material.AppBar;
using Microsoft.Maui.Platform;
using AView = Android.Views.View;
using AColor = Android.Graphics.Color;
using Android.Content.Res;
Expand Down Expand Up @@ -99,6 +100,10 @@ private protected override void OnDisconnectHandler(object platformView)

if (_rootManager != null)
_rootManager.RootViewChanged -= OnRootViewChanged;

// The MauiCoordinatorLayout will automatically unregister from the static registry
// when it's detached from the window, but we can ensure cleanup here as well
_rootManager = null;
}

void OnRootViewChanged(object? sender, EventArgs e)
Expand All @@ -125,7 +130,12 @@ internal static void DisconnectHandler(NavigationRootManager? navigationRootMana

var rootManager = handler.MauiContext.GetNavigationRootManager();
rootManager.Connect(window.Content);
return rootManager.RootView;

// The NavigationRootManager creates a MauiCoordinatorLayout which automatically
// registers its GlobalWindowInsetListener in the static registry for child views to use
var rootView = rootManager.RootView;

return rootView;
}

void UpdateVirtualViewFrame(Activity activity)
Expand Down
Loading