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] Fix titlebar not persisting when page is swapped #27192

Merged
merged 3 commits into from
Jan 28, 2025
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
10 changes: 10 additions & 0 deletions src/Controls/src/Core/TitleBar/TitleBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,16 @@ public TitleBar()
}
}

internal void Cleanup()
PureWeen marked this conversation as resolved.
Show resolved Hide resolved
{
PropertyChanged -= TitleBar_PropertyChanged;
if (Window is not null)
{
Window.Activated -= Window_Activated;
Window.Deactivated -= Window_Deactivated;
}
}

private void TitleBar_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(Window) && Window is not null)
Expand Down
1 change: 1 addition & 0 deletions src/Controls/src/Core/Window/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ static void TitleBarChanged(BindableObject bindable, object oldValue, object new
{
if (oldValue is TitleBar prevTitleBar)
{
prevTitleBar.Cleanup();
self.RemoveLogicalChild(prevTitleBar);
}

Expand Down
42 changes: 42 additions & 0 deletions src/Controls/tests/Core.UnitTests/TitleBarTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.Maui.Controls.Core.UnitTests
{

public class TitleBarTests : BaseTestFixture
{
[Fact, Category(TestCategory.Memory)]
public async Task TitleBarDoesNotLeak()
{
var application = new Application();

WeakReference CreateReference()
{
var window = new Window { Page = new ContentPage() };
var firstTitleBar = new TitleBar();
var secondTitleBar = new TitleBar();
var reference = new WeakReference(firstTitleBar);

window.TitleBar = firstTitleBar;

application.OpenWindow(window);

window.TitleBar = secondTitleBar;

((IWindow)window).Destroying();
return reference;
}

var reference = CreateReference();

// GC
await TestHelpers.Collect();

Assert.False(reference.IsAlive, "TitleBar should not be alive!");

GC.KeepAlive(application);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Xunit;
using WPanel = Microsoft.UI.Xaml.Controls.Panel;
using static Microsoft.Maui.DeviceTests.AssertHelpers;
using Windows.UI;

namespace Microsoft.Maui.DeviceTests
{
Expand Down Expand Up @@ -239,6 +240,45 @@ await CreateHandlerAndAddToWindow<WindowHandlerStub>(window, async (handler) =>
});
}

[Theory]
[ClassData(typeof(WindowPageSwapTestCases))]
public async Task TitlebarWorksWhenSwitchingPage(WindowPageSwapTestCase swapOrder)
{
SetupBuilder();

var firstRootPage = swapOrder.GetNextPageType();
var window = new Window(firstRootPage)
{
TitleBar = new TitleBar()
{
Title = "Hello World",
BackgroundColor = Colors.CornflowerBlue
}
};

await CreateHandlerAndAddToWindow<WindowHandlerStub>(window, async (handler) =>
{
await OnLoadedAsync(swapOrder.Page);
while (!swapOrder.IsFinished())
{
var nextRootPage = swapOrder.GetNextPageType();
window.Page = nextRootPage;

try
{
await OnLoadedAsync(swapOrder.Page);

var navView = GetWindowRootView(handler);
Assert.NotNull(navView.TitleBar);
}
catch (Exception exc)
{
throw new Exception($"Failed to swap to {nextRootPage}", exc);
}
}
});
}

[Collection(ControlsHandlerTestBase.RunInNewWindowCollection)]
[Category(TestCategory.Lifecycle)]
public class WindowTestsRunInNewWindowCollection : ControlsHandlerTestBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ void SetupBuilder()
handlers.AddHandler(typeof(FlyoutPage), typeof(PhoneFlyoutPageRenderer));
#endif

handlers.AddHandler<IContentView, ContentViewHandler>();

handlers.AddHandler<Button, ButtonHandler>();
handlers.AddHandler<Entry, EntryHandler>();
handlers.AddHandler<Editor, EditorHandler>();
Expand Down
14 changes: 2 additions & 12 deletions src/Core/src/Handlers/Window/WindowHandler.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ protected override void DisconnectHandler(UI.Xaml.Window platformView)
if (windowRootContentManager is not null)
{
windowRootContentManager.OnApplyTemplateFinished -= WindowRootContentManagerOnApplyTemplateFinished;
windowRootContentManager.SetTitleBar(null, null);
windowRootContentManager.Disconnect();
}

Expand All @@ -78,18 +79,7 @@ public static void MapContent(IWindowHandler handler, IWindow window)
_ = handler.MauiContext ?? throw new InvalidOperationException($"{nameof(MauiContext)} should have been set by base class.");

var windowManager = handler.MauiContext.GetNavigationRootManager();
var previousRootView = windowManager.RootView;

windowManager.Disconnect();
windowManager.Connect(handler.VirtualView.Content?.ToPlatform(handler.MauiContext));

if (handler.PlatformView.Content is WindowRootViewContainer container)
{
if (previousRootView != null && previousRootView != windowManager.RootView)
container.RemovePage(previousRootView);

container.AddPage(windowManager.RootView);
}
windowManager.Connect(handler);

window.VisualDiagnosticsOverlay?.Initialize();
}
Expand Down
27 changes: 26 additions & 1 deletion src/Core/src/Platform/Windows/NavigationRootManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,32 @@ void OnBackRequested(NavigationView sender, NavigationViewBackRequestedEventArgs
internal MauiToolbar? Toolbar => _rootView.Toolbar;
public FrameworkElement RootView => _rootView;

internal void Connect(IWindowHandler handler)
{
_ = handler.MauiContext ?? throw new InvalidOperationException($"{nameof(MauiContext)} should have been set by base class.");

var platformWindow = _platformWindow.GetTargetOrDefault();
if (platformWindow is null)
{
return;
}

var previousRootView = RootView;

Disconnect();
Connect(handler.VirtualView.Content?.ToPlatform(handler.MauiContext));

if (platformWindow.Content is WindowRootViewContainer container)
{
if (previousRootView is not null && previousRootView != RootView)
{
container.RemovePage(previousRootView);
}

container.AddPage(RootView);
}
}

public virtual void Connect(UIElement? platformView)
{
if (_rootView.Content != null)
Expand Down Expand Up @@ -120,7 +146,6 @@ public virtual void Disconnect()
}

SetToolbar(null);
SetTitleBar(null, null);
Copy link
Member

Choose a reason for hiding this comment

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

Can we add this call up into the WindowHandler.Disconnect code?

I think it's still useful to set the TitleBar to null if the handler itself is being disconnected

so we can add it here
image

after the disconnect call


if (_rootView.Content is RootNavigationView navView)
navView.Content = null;
Expand Down
2 changes: 2 additions & 0 deletions src/Core/src/Platform/Windows/WindowRootView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ internal MenuBar? MenuBar
}
}

internal ITitleBar? TitleBar => _titleBar;

public DataTemplate? AppTitleBarTemplate
{
get => (DataTemplate?)GetValue(AppTitleBarTemplateProperty);
Expand Down
Loading