Skip to content
Closed
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 @@ -73,6 +74,8 @@ void IAppearanceObserver.OnAppearanceChanged(ShellAppearance appearance)
IShellToolbarTracker _toolbarTracker;
bool _disposed;
bool _destroyed;
GlobalWindowInsetListener _localInsetListener;
CoordinatorLayout _managedCoordinatorLayout;
Comment on lines +77 to +78
Copy link

Copilot AI Oct 29, 2025

Choose a reason for hiding this comment

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

These fields should be marked with ? to indicate nullability since they are assigned conditionally in OnCreateView() and set to null in Destroy(). This should be GlobalWindowInsetListener? _localInsetListener; and CoordinatorLayout? _managedCoordinatorLayout;.

Suggested change
GlobalWindowInsetListener _localInsetListener;
CoordinatorLayout _managedCoordinatorLayout;
GlobalWindowInsetListener? _localInsetListener;
CoordinatorLayout? _managedCoordinatorLayout;

Copilot uses AI. Check for mistakes.

public ShellContentFragment(IShellContext shellContext, ShellContent shellContent)
{
Expand Down Expand Up @@ -135,6 +138,13 @@ 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)
{
_localInsetListener = new GlobalWindowInsetListener();
_managedCoordinatorLayout = rootLayout;
_root = GlobalWindowInsetListener.SetupCoordinatorLayoutWithLocalListener(rootLayout, _localInsetListener);
}
var shellContentMauiContext = _shellContext.Shell.Handler.MauiContext.MakeScoped(layoutInflater: inflater, fragmentManager: ChildFragmentManager);

Maui.IElement parentElement = (_shellContent as Maui.IElement) ?? _page;
Expand All @@ -143,9 +153,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 Expand Up @@ -183,6 +190,12 @@ void Destroy()
// to avoid the navigation `TaskCompletionSource` to be stuck forever.
AnimationFinished?.Invoke(this, EventArgs.Empty);

// Clean up the coordinator layout and local listener first
if (_managedCoordinatorLayout is not null && _localInsetListener is not null)
{
GlobalWindowInsetListener.RemoveCoordinatorLayoutWithLocalListener(_managedCoordinatorLayout, _localInsetListener);
}

(_shellContext?.Shell as IShellController)?.RemoveAppearanceObserver(this);

if (_shellContent != null)
Expand Down Expand Up @@ -214,6 +227,8 @@ void Destroy()
_viewhandler = null;
_shellContent = null;
_shellPageContainer = null;
_localInsetListener = null;
_managedCoordinatorLayout = null;
}

protected override void Dispose(bool disposing)
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 @@ -75,6 +77,8 @@ void AView.IOnClickListener.OnClick(AView v)
IShellToolbarTracker _toolbarTracker;
ViewPager2 _viewPager;
bool _disposed;
GlobalWindowInsetListener _localInsetListener;
CoordinatorLayout _managedCoordinatorLayout;
Comment on lines +80 to +81
Copy link

Copilot AI Oct 29, 2025

Choose a reason for hiding this comment

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

These fields should be marked with ? to indicate nullability since they are assigned after construction and set to null in Destroy(). This should be GlobalWindowInsetListener? _localInsetListener; and CoordinatorLayout? _managedCoordinatorLayout;.

Suggested change
GlobalWindowInsetListener _localInsetListener;
CoordinatorLayout _managedCoordinatorLayout;
GlobalWindowInsetListener? _localInsetListener;
CoordinatorLayout? _managedCoordinatorLayout;

Copilot uses AI. Check for mistakes.
IShellController ShellController => _shellContext.Shell;
public event EventHandler AnimationFinished;
Fragment IShellObservableFragment.Fragment => this;
Expand All @@ -101,7 +105,12 @@ 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
_localInsetListener = new GlobalWindowInsetListener();
_managedCoordinatorLayout = root;
root = GlobalWindowInsetListener.SetupCoordinatorLayoutWithLocalListener(root, _localInsetListener);

int actionBarHeight = context.GetActionBarHeight();

var shellToolbar = new Toolbar(shellSection);
Expand Down Expand Up @@ -194,6 +203,12 @@ void Destroy()
{
if (_rootView != null)
{
// Clean up the coordinator layout and local listener first
if (_managedCoordinatorLayout is not null && _localInsetListener is not null)
{
GlobalWindowInsetListener.RemoveCoordinatorLayoutWithLocalListener(_managedCoordinatorLayout, _localInsetListener);
}

UnhookEvents();

_shellContext?.Shell?.Toolbar?.Handler?.DisconnectHandler();
Expand All @@ -220,6 +235,8 @@ void Destroy()
_toolbar = null;
_viewPager = null;
_rootView = null;
_localInsetListener = null;
_managedCoordinatorLayout = null;

}

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
36 changes: 21 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,32 @@ 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);

// 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
4 changes: 2 additions & 2 deletions src/Core/src/Platform/Android/ContentViewGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ protected override void OnConfigurationChanged(Configuration? newConfig)
{
base.OnConfigurationChanged(newConfig);

Context?.GetGlobalWindowInsetListener()?.ResetView(this);
GlobalWindowInsetListener.FindListenerForView(this)?.ResetView(this);
_didSafeAreaEdgeConfigurationChange = true;
}

Expand Down Expand Up @@ -231,7 +231,7 @@ internal IBorderStroke? Clip
{
_originalPadding = (PaddingLeft, PaddingTop, PaddingRight, PaddingBottom);
_hasStoredOriginalPadding = true;
}
}

return SafeAreaExtensions.ApplyAdjustedSafeAreaInsetsPx(insets, CrossPlatformLayout, _context, view);
}
Expand Down
Loading
Loading