Skip to content
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 @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Maui.Graphics;
using Microsoft.UI.Windowing;

namespace Microsoft.Maui.Controls.Platform
{
Expand All @@ -13,11 +14,39 @@ _window.NativeWindow.Content as WindowRootViewContainer ??

bool _firstActivated;

// Tracks the AppWindow subscription used to update the top modal's title bar
// when the user enters or exits full-screen while a modal is visible.
AppWindow? _appWindowForPresenterChanges;

// Captured at subscription time from the WinUI Window (a DependencyObject with a
// guaranteed non-null DispatcherQueue). AppWindow.DispatcherQueue returns null because
// AppWindow does not implement IDispatcherQueueObject, so we cannot use sender.DispatcherQueue
// in the Changed event handler.
Microsoft.UI.Dispatching.DispatcherQueue? _windowDispatcherQueue;

partial void InitializePlatform()
{
_window.Created += (_, _) => SyncModalStackWhenPlatformIsReady();
_window.Destroying += (_, _) => _firstActivated = false;
_window.Destroying += (_, _) =>
{
_firstActivated = false;
UnsubscribeFromPresenterChanges();
};
_window.Activated += OnWindowActivated;
_window.HandlerChanging += OnPlatformWindowHandlerChanging;
}

void OnPlatformWindowHandlerChanging(object? sender, HandlerChangingEventArgs e)
{
// When the WinUI window handler is recreated (e.g., activity recreation on Windows),
// the AppWindow instance may change. Clear the subscription to the old AppWindow so
// that SubscribeToPresenterChanges() can attach to the new one when the modal stack
// is resynced after the handler is reconnected.
if (e.OldHandler is not null)
{
_firstActivated = false;
UnsubscribeFromPresenterChanges();
}
}

void OnWindowActivated(object? sender, EventArgs e)
Expand Down Expand Up @@ -118,6 +147,19 @@ void SetCurrent(
var windowManager = modalContext.GetNavigationRootManager();
if (windowManager is not null)
{
// The NavigationRootManager constructor unconditionally reserves 32px for the
// title bar based on ExtendsContentIntoTitleBar (true by default). Suppress
// this reservation when the app has hidden the title bar (IsVisible = false)
// or when running in full-screen mode (no OS window controls to avoid).
// This also clears the InputNonClientPointerSource passthrough regions, fixing
// the unclickable top area that would otherwise remain in full-screen mode.
// Use the same predicate as the pop/presenter-change paths so all paths agree.
bool showTitleBarOnPush = !IsWindowFullScreen() && ((_window.TitleBar as TitleBar)?.IsVisible ?? true);
if (!showTitleBarOnPush)
{
windowManager.SetTitleBarVisibility(false);
}
Comment thread
devanathan-vaithiyanathan marked this conversation as resolved.
Resolved

// Set the titlebar on the new navigation root
if (previousPage is not null &&
previousPage.GetParentWindow() is Window window &&
Expand All @@ -130,22 +172,48 @@ void SetCurrent(
_waitingForIncomingPage = platform.OnLoaded(() => completedCallback?.Invoke());
windowManager.Connect(platform);
Container.AddPage(windowManager.RootView);

// Subscribe once (when first modal is pushed) so that full-screen
// entry/exit while the modal is shown updates the modal's title bar.
if (_platformModalPages.Count == 1)
{
SubscribeToPresenterChanges();
}
}
}
// popping modal
else
{
// Unsubscribe from presenter changes once the last modal is popped.
if (_platformModalPages.Count == 0)
{
UnsubscribeFromPresenterChanges();
}

var context = newPage.FindMauiContext();
var windowManager = context?.GetNavigationRootManager() ??
throw new InvalidOperationException("Previous Page Has Lost its MauiContext");

// Toggle the titlebar visibility on the new page
var navRoot = context.GetNavigationRootManager();
// Combine presenter state with the app's explicit TitleBar.IsVisible setting.
// Casting to the concrete TitleBar type is safe — ModalNavigationManager is in the
// same assembly and TitleBar is the only platform implementation on Windows.
// Defaults to true (show title bar) when no custom TitleBar is set.
bool showTitleBar = !IsWindowFullScreen() && ((_window.TitleBar as TitleBar)?.IsVisible ?? true);

if (navRoot.RootView is WindowRootView wrv && wrv.AppTitleBarContainer is not null)
{
// Always restore the visual containers to Visible. The reserved height
// (WindowTitleBarContentControlMinHeight via navRoot.SetTitleBarVisibility below)
// will be 0 in full-screen, so no space is taken. Keeping the container Visible
// avoids a permanent collapse when the window later exits full-screen after
// the modal has already been dismissed. Height is restored by the
// WM_STYLECHANGING handler in MauiWinUIWindow when the window leaves full-screen.
wrv.SetTitleBarVisibility(UI.Xaml.Visibility.Visible);
}

navRoot?.SetTitleBarVisibility(showTitleBar);

Comment thread
devanathan-vaithiyanathan marked this conversation as resolved.
Resolved
// Restore the titlebar
if (previousPage is not null &&
previousPage.GetParentWindow() is Window window &&
Expand All @@ -169,5 +237,97 @@ void SetCurrent(
"Please ensure that the new page is in the same UI thread as the current page.", error);
}
}

// Returns true when the window is currently in full-screen presentation mode.
bool IsWindowFullScreen()
{
var platformWindow = WindowMauiContext?.GetPlatformWindow();
if (platformWindow is null)
{
return false;
}

return platformWindow.GetAppWindow()?.Presenter?.Kind == AppWindowPresenterKind.FullScreen;
}

// Subscribes to AppWindow.Changed (once) when the first modal is pushed so that
// entering or exiting full-screen while a modal is visible is handled correctly.
void SubscribeToPresenterChanges()
{
if (_appWindowForPresenterChanges is not null)
Comment thread
devanathan-vaithiyanathan marked this conversation as resolved.
Resolved
{
return;
}

var platformWindow = WindowMauiContext?.GetPlatformWindow();
if (platformWindow is null)
{
return;
}

var appWindow = platformWindow.GetAppWindow();
if (appWindow is null)
{
return;
}

_windowDispatcherQueue = platformWindow.DispatcherQueue;
_appWindowForPresenterChanges = appWindow;
_appWindowForPresenterChanges.Changed += OnAppWindowPresenterChanged;
}

// Removes the AppWindow.Changed subscription when the last modal is popped or
// the window is being destroyed.
void UnsubscribeFromPresenterChanges()
{
if (_appWindowForPresenterChanges is null)
{
return;
}

_appWindowForPresenterChanges.Changed -= OnAppWindowPresenterChanged;
_appWindowForPresenterChanges = null;
_windowDispatcherQueue = null;
}

// Fires whenever the window's AppWindow.Presenter changes (e.g., windowed ↔ full-screen).
// Updates the top-most modal's NavigationRootManager so its title bar height and margins
// match the new presenter mode.
void OnAppWindowPresenterChanged(AppWindow sender, AppWindowChangedEventArgs args)
{
if (!args.DidPresenterChange)
{
return;
}

// AppWindow.Changed is not guaranteed to fire on the UI thread, so marshal ALL
// MAUI/WinUI state reads (modal list, MauiContext, Presenter) onto the dispatcher.
// This avoids races with modal pop/disconnect and COM apartment violations.
// Re-check that a modal is still present at dispatch time before acting.
// NOTE: sender.DispatcherQueue is null for AppWindow (it does not implement
// IDispatcherQueueObject), so we use the window's DispatcherQueue captured at
// subscription time instead.
_windowDispatcherQueue?.TryEnqueue(() =>
{
if (_platformModalPages.Count == 0)
{
return;
}

var topModal = _platformModalPages[_platformModalPages.Count - 1];
var rootManager = topModal.FindMauiContext()?.GetNavigationRootManager();
if (rootManager is null)
{
return;
}

bool isFullScreen = sender.Presenter?.Kind == AppWindowPresenterKind.FullScreen;
// Combine presenter state with the app's explicit TitleBar.IsVisible setting so that
// an app that intentionally hides its TitleBar does not have the reservation forced
// back on when leaving full-screen. Defaults to true when no TitleBar is set.
bool showTitleBar = !isFullScreen && ((_window.TitleBar as TitleBar)?.IsVisible ?? true);
rootManager.SetTitleBarVisibility(showTitleBar);
});
}
}
}
147 changes: 147 additions & 0 deletions src/Controls/tests/DeviceTests/Elements/Modal/ModalTests.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Platform;
using Microsoft.UI.Windowing;
using Xunit;
using static Microsoft.Maui.DeviceTests.AssertHelpers;
using WPanel = Microsoft.UI.Xaml.Controls.Panel;

namespace Microsoft.Maui.DeviceTests
Expand Down Expand Up @@ -242,6 +244,151 @@ await CreateHandlerAndAddToWindow<IWindowHandler>(new Window(navPage),
});
}

// In full-screen mode, the NavigationRootManager constructor unconditionally reserves 32px
// for the title bar. ModalNavigationManager must suppress this for modal pages.
[Fact]
public async Task ModalPageInFullScreenModeHasNoTitleBarReservation()
{
SetupBuilder();

var navPage = new NavigationPage(new ContentPage());
var window = new Window(navPage);

await CreateHandlerAndAddToWindow<IWindowHandler>(window,
async (handler) =>
{
var platformWindow = handler.PlatformView;
var appWindow = platformWindow.GetAppWindow();

appWindow.SetPresenter(AppWindowPresenterKind.FullScreen);
Comment thread
devanathan-vaithiyanathan marked this conversation as resolved.
Resolved

try
{
var modalPage = new ContentPage { BackgroundColor = Colors.Red };
await navPage.CurrentPage.Navigation.PushModalAsync(modalPage);
await OnLoadedAsync(modalPage);

var modalWindowRootView = modalPage
.FindMauiContext()
.GetNavigationRootManager()
.RootView as WindowRootView;

Assert.NotNull(modalWindowRootView);

// In full-screen mode, the modal must NOT reserve any space for the title bar.
// Before the fix this was 32px, causing both a visual gap and an unclickable area.
Assert.Equal(0d, modalWindowRootView.WindowTitleBarContentControlMinHeight);

await navPage.CurrentPage.Navigation.PopModalAsync();
await OnUnloadedAsync(modalPage);
}
finally
{
// Restore windowed mode so subsequent tests are not affected.
appWindow.SetPresenter(AppWindowPresenterKind.Default);
}
});
}

// The full-screen fix must not suppress the title bar reservation in windowed mode.
[Fact]
public async Task ModalPageInWindowedModeRetainsTitleBarState()
{
SetupBuilder();

var navPage = new NavigationPage(new ContentPage());
var window = new Window(navPage);

await CreateHandlerAndAddToWindow<IWindowHandler>(window,
async (handler) =>
{
var modalPage = new ContentPage { BackgroundColor = Colors.Red };
await navPage.CurrentPage.Navigation.PushModalAsync(modalPage);
await OnLoadedAsync(modalPage);

var modalWindowRootView = modalPage
.FindMauiContext()
.GetNavigationRootManager()
.RootView as WindowRootView;

Assert.NotNull(modalWindowRootView);

// In windowed mode, the modal's title bar container must remain visible.
// The fix must not affect this path.
Assert.Equal(
UI.Xaml.Visibility.Visible,
modalWindowRootView.AppTitleBarContainer?.Visibility);

await navPage.CurrentPage.Navigation.PopModalAsync();
await OnUnloadedAsync(modalPage);
});
}

// Verifies that the AppWindow.Changed subscription fires and updates the modal's
// title bar reservation when the presenter changes while a modal is already visible.
[Fact]
public async Task ModalTitleBarUpdatesWhenPresenterChangesWhileModalIsVisible()
Comment thread
devanathan-vaithiyanathan marked this conversation as resolved.
Resolved
{
SetupBuilder();

var navPage = new NavigationPage(new ContentPage());
var window = new Window(navPage);

await CreateHandlerAndAddToWindow<IWindowHandler>(window,
async (handler) =>
{
var platformWindow = handler.PlatformView;
var appWindow = platformWindow.GetAppWindow();

// Push a modal while the window is in windowed mode.
var modalPage = new ContentPage { BackgroundColor = Colors.Red };
await navPage.CurrentPage.Navigation.PushModalAsync(modalPage);
await OnLoadedAsync(modalPage);

var modalWindowRootView = modalPage
.FindMauiContext()
.GetNavigationRootManager()
.RootView as WindowRootView;

Assert.NotNull(modalWindowRootView);

// In windowed mode the modal must reserve space for the title bar.
Assert.True(
modalWindowRootView.WindowTitleBarContentControlMinHeight > 0,
"Modal should reserve title bar space in windowed mode");

try
{
// Switch to full-screen while the modal is already visible.
// This exercises the AppWindow.Changed subscription path added by the fix.
appWindow.SetPresenter(AppWindowPresenterKind.FullScreen);

// Wait deterministically for the AppWindow.Changed event to propagate and
// the DispatcherQueue.TryEnqueue callback to update WindowTitleBarContentControlMinHeight.
await AssertEventually(
() => modalWindowRootView.WindowTitleBarContentControlMinHeight == 0d,
timeout: 2000,
message: "Modal should clear title bar space after switching to full-screen");

// Restore windowed mode while the modal is still open.
appWindow.SetPresenter(AppWindowPresenterKind.Default);

// Wait deterministically for the AppWindow.Changed event to propagate and
// the DispatcherQueue.TryEnqueue callback to restore WindowTitleBarContentControlMinHeight.
await AssertEventually(
() => modalWindowRootView.WindowTitleBarContentControlMinHeight > 0,
timeout: 2000,
message: "Modal should restore title bar space when returning to windowed mode");
}
finally
{
appWindow.SetPresenter(AppWindowPresenterKind.Default);
await navPage.CurrentPage.Navigation.PopModalAsync();
await OnUnloadedAsync(modalPage);
}
});
}

[Fact]
public async Task NestedModalPagesMaintainHitTestVisibilityAndFocusTrap()
{
Expand Down
Loading