From 1d14364c5f027a08ab789315331917706e275644 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 25 Nov 2024 16:37:54 -0800 Subject: [PATCH] Hide/show titlebar on modal pages w/ Windows (#26111) --- .../ModalNavigationManager.Windows.cs | 23 ++++++++--- .../src/Platform/Windows/WindowRootView.cs | 41 ++++++++----------- 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/src/Controls/src/Core/Platform/ModalNavigationManager/ModalNavigationManager.Windows.cs b/src/Controls/src/Core/Platform/ModalNavigationManager/ModalNavigationManager.Windows.cs index 5d9405e54e2f..1fbeb11faa49 100644 --- a/src/Controls/src/Core/Platform/ModalNavigationManager/ModalNavigationManager.Windows.cs +++ b/src/Controls/src/Core/Platform/ModalNavigationManager/ModalNavigationManager.Windows.cs @@ -104,13 +104,18 @@ void SetCurrent( newPage.Toolbar ??= new Toolbar(newPage); _ = newPage.Toolbar.ToPlatform(modalContext); - var windowManager = modalContext.GetNavigationRootManager(); - - if (windowManager.RootView is WindowRootView wrv) + // Hide titlebar on previous page + var previousContext = previousPage.FindMauiContext(); + if (previousContext is not null) { - wrv.SetTitleBarBackgroundToTransparent(false); + var navRoot = previousContext.GetNavigationRootManager(); + if (navRoot.RootView is WindowRootView wrv && wrv.AppTitleBarContainer is not null) + { + wrv.SetTitleBarVisibility(UI.Xaml.Visibility.Collapsed); + } } + var windowManager = modalContext.GetNavigationRootManager(); var platform = newPage.ToPlatform(modalContext); _waitingForIncomingPage = platform.OnLoaded(() => completedCallback?.Invoke()); windowManager.Connect(platform); @@ -119,9 +124,17 @@ void SetCurrent( // popping modal else { - var windowManager = newPage.FindMauiContext()?.GetNavigationRootManager() ?? + 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(); + if (navRoot.RootView is WindowRootView wrv && wrv.AppTitleBarContainer is not null) + { + wrv.SetTitleBarVisibility(UI.Xaml.Visibility.Visible); + } + var platform = newPage.ToPlatform(); _waitingForIncomingPage = platform.OnLoaded(() => completedCallback?.Invoke()); Container.AddPage(windowManager.RootView); diff --git a/src/Core/src/Platform/Windows/WindowRootView.cs b/src/Core/src/Platform/Windows/WindowRootView.cs index 1e12e398b213..7670a08ef7bc 100644 --- a/src/Core/src/Platform/Windows/WindowRootView.cs +++ b/src/Core/src/Platform/Windows/WindowRootView.cs @@ -108,8 +108,6 @@ internal FrameworkElement? AppTitleBar _appTitleBar = cp; } - UpdateAppTitleBarTransparency(); - return _appTitleBar; } } @@ -477,6 +475,22 @@ internal void SetTitleBar(ITitleBar? titlebar, IMauiContext? mauiContext) } } + internal void SetTitleBarVisibility(UI.Xaml.Visibility visibility) + { + // Set default and custom titlebar container visibility + if (AppTitleBarContainer is not null) + { + AppTitleBarContainer.Visibility = visibility; + } + + // Set the back/flyout button container visibility + if (NavigationViewControl is not null && + NavigationViewControl.ButtonHolderGrid is not null) + { + NavigationViewControl.ButtonHolderGrid.Visibility = visibility; + } + } + private void TitlebarPropChanged_PropertyChanged(object? sender, PropertyChangedEventArgs e) { if (_titleBar is not null && _titleBar.Handler?.MauiContext is not null) @@ -553,29 +567,6 @@ internal String? WindowTitle set => SetValue(TitleProperty, value); } - // Once we switch the TitleBar over to using replaceable IViews we won't need this - // but for the sake of first just getting us converted over to the new TitleBar - // APIs - bool _setTitleBarBackgroundToTransparent = true; - internal void SetTitleBarBackgroundToTransparent(bool value) - { - _setTitleBarBackgroundToTransparent = value; - if (value) - { - UpdateAppTitleBarTransparency(); - } - else - { - _appTitleBar?.RefreshThemeResources(); - } - } - - void UpdateAppTitleBarTransparency() - { - if (_setTitleBarBackgroundToTransparent && _appTitleBar is Border border) - border.Background = null; - } - internal static readonly DependencyProperty WindowTitleBarContentProperty = DependencyProperty.Register( nameof(WindowTitleBarContent),