Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Windows] Hide/show titlebar on modal pages #26071

Merged
merged 2 commits into from
Nov 26, 2024
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 @@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure -- lemme add that to the existing modal push/pop Windows tests

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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,35 @@ await CreateHandlerAndAddToWindow<IWindowHandler>(window,
var rootView = handler.PlatformView.Content;
ContentPage modalPage = new ContentPage();

var previousPageNavigationRootManager = navPage.CurrentPage
.FindMauiContext()
.GetNavigationRootManager();

var previousWindowRootView = previousPageNavigationRootManager.RootView as WindowRootView;

await navPage.CurrentPage.Navigation.PushModalAsync(modalPage);
await OnLoadedAsync(modalPage);

var modalNavigationRootManager = modalPage
.FindMauiContext()
.GetNavigationRootManager();


var mauiWindow = (MauiWinUIWindow)handler.PlatformView;
Assert.Equal("Original Title", mauiWindow.Title);
Assert.Equal(modalNavigationRootManager.WindowTitle, mauiWindow.Title);

// Ensure previous page has hidden its titlebar
Assert.True(previousWindowRootView.AppTitleBarContainer.Visibility == UI.Xaml.Visibility.Collapsed);
Assert.True(previousWindowRootView.NavigationViewControl?.ButtonHolderGrid.Visibility == UI.Xaml.Visibility.Collapsed);

window.Title = "Update Title";
Assert.Equal("Update Title", mauiWindow.Title);
Assert.Equal(modalNavigationRootManager.WindowTitle, mauiWindow.Title);

// Ensure titlebar is visible after popping modal
var modalWindowRootView = modalNavigationRootManager.RootView as WindowRootView;
Assert.True(modalWindowRootView.AppTitleBarContainer.Visibility == UI.Xaml.Visibility.Visible);
Assert.True(modalWindowRootView.NavigationViewControl.ButtonHolderGrid.Visibility == UI.Xaml.Visibility.Visible);
});
}

Expand Down Expand Up @@ -130,6 +144,11 @@ await CreateHandlerAndAddToWindow(window,
Assert.Equal(UpdatedTitle, mauiWindow.Title);
Assert.Equal(UpdatedTitle, currentNavigationRootManager.WindowTitle);
Assert.Equal(UpdatedTitle, modalNavigationRootManager.WindowTitle);

// Ensure titlebar is visible after popping modal
var windowRootView = currentNavigationRootManager.RootView as WindowRootView;
Assert.True(windowRootView.AppTitleBarContainer.Visibility == UI.Xaml.Visibility.Visible);
Assert.True(windowRootView.NavigationViewControl.ButtonHolderGrid.Visibility == UI.Xaml.Visibility.Visible);
}));
}
}
Expand Down
41 changes: 16 additions & 25 deletions src/Core/src/Platform/Windows/WindowRootView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ internal FrameworkElement? AppTitleBar
_appTitleBar = cp;
}

UpdateAppTitleBarTransparency();

return _appTitleBar;
}
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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),
Expand Down
Loading