From e4162eeeb6919d39bef0e3af3b81ff747b857621 Mon Sep 17 00:00:00 2001 From: Andrew KeepCoding Date: Wed, 17 Sep 2025 17:52:29 +0900 Subject: [PATCH 1/4] Fix titlebar gap under caption buttons --- WinUIGallery/MainWindow.xaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/WinUIGallery/MainWindow.xaml b/WinUIGallery/MainWindow.xaml index 6a7d12f06..da5768639 100644 --- a/WinUIGallery/MainWindow.xaml +++ b/WinUIGallery/MainWindow.xaml @@ -13,6 +13,9 @@ + + 46 + From b148634fb16e294499bd4f46a0fa6991527bdb8b Mon Sep 17 00:00:00 2001 From: Andrew KeepCoding Date: Fri, 19 Sep 2025 11:03:52 +0900 Subject: [PATCH 2/4] Revert "Fix titlebar gap under caption buttons" This reverts commit e4162eeeb6919d39bef0e3af3b81ff747b857621. --- WinUIGallery/MainWindow.xaml | 3 --- 1 file changed, 3 deletions(-) diff --git a/WinUIGallery/MainWindow.xaml b/WinUIGallery/MainWindow.xaml index da5768639..6a7d12f06 100644 --- a/WinUIGallery/MainWindow.xaml +++ b/WinUIGallery/MainWindow.xaml @@ -13,9 +13,6 @@ - - 46 - From 0f16190426799313164b95c11498caefcc4f628d Mon Sep 17 00:00:00 2001 From: Andrew KeepCoding Date: Fri, 19 Sep 2025 11:49:26 +0900 Subject: [PATCH 3/4] Adjust NavigationView margin based on window state Added `WindowPresenter` and `CurrentWindowState` properties to track and manage the window's presentation state. Updated the `MainWindow` constructor to initialize these properties and adjust the `NavigationView` margin dynamically. Introduced the `AdjustNavigationViewMargin` method to handle margin adjustments when the window state changes, ensuring proper alignment of the `NavigationView` with the caption buttons. --- WinUIGallery/MainWindow.xaml.cs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/WinUIGallery/MainWindow.xaml.cs b/WinUIGallery/MainWindow.xaml.cs index 0dda5d96e..95ba6e764 100644 --- a/WinUIGallery/MainWindow.xaml.cs +++ b/WinUIGallery/MainWindow.xaml.cs @@ -32,12 +32,35 @@ public NavigationView NavigationView public Action NavigationViewLoaded { get; set; } + private OverlappedPresenter WindowPresenter { get; init; } + + private OverlappedPresenterState CurrentWindowState { get; set; } + public MainWindow() { this.InitializeComponent(); SetWindowProperties(); RootGrid.ActualThemeChanged += (_, _) => TitleBarHelper.ApplySystemThemeToCaptionButtons(this, RootGrid.ActualTheme); dispatcherQueue = Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread(); + + WindowPresenter = AppWindow.Presenter as OverlappedPresenter; + CurrentWindowState = WindowPresenter.State; + AdjustNavigationViewMargin(force: true); + AppWindow.Changed += (_, _) => AdjustNavigationViewMargin(); + } + + // Adjusts the NavigationView margin based on the window state to fix the gap between the caption buttons and the NavigationView. + private void AdjustNavigationViewMargin(bool? force = null) + { + if (WindowPresenter.State == CurrentWindowState && force is not true) + { + return; + } + + NavigationView.Margin = WindowPresenter.State == OverlappedPresenterState.Maximized + ? new Thickness(0, -1, 0, 0) + : new Thickness(0, -2, 0, 0); + CurrentWindowState = WindowPresenter.State; } private void RootGrid_Loaded(object sender, RoutedEventArgs e) From 02679031f499278516176811bf4f5d22d6e4353e Mon Sep 17 00:00:00 2001 From: Andrew KeepCoding Date: Mon, 6 Oct 2025 17:34:01 +0900 Subject: [PATCH 4/4] Handle nullable WindowPresenter --- WinUIGallery/MainWindow.xaml.cs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/WinUIGallery/MainWindow.xaml.cs b/WinUIGallery/MainWindow.xaml.cs index 95ba6e764..38238f184 100644 --- a/WinUIGallery/MainWindow.xaml.cs +++ b/WinUIGallery/MainWindow.xaml.cs @@ -32,7 +32,9 @@ public NavigationView NavigationView public Action NavigationViewLoaded { get; set; } - private OverlappedPresenter WindowPresenter { get; init; } +#nullable enable + private OverlappedPresenter? WindowPresenter { get; } +#nullable disable private OverlappedPresenterState CurrentWindowState { get; set; } @@ -43,16 +45,21 @@ public MainWindow() RootGrid.ActualThemeChanged += (_, _) => TitleBarHelper.ApplySystemThemeToCaptionButtons(this, RootGrid.ActualTheme); dispatcherQueue = Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread(); - WindowPresenter = AppWindow.Presenter as OverlappedPresenter; - CurrentWindowState = WindowPresenter.State; - AdjustNavigationViewMargin(force: true); - AppWindow.Changed += (_, _) => AdjustNavigationViewMargin(); + if (AppWindow.Presenter is OverlappedPresenter windowPresenter) + { + WindowPresenter = windowPresenter; + CurrentWindowState = WindowPresenter.State; + AdjustNavigationViewMargin(force: true); + AppWindow.Changed += (_, _) => AdjustNavigationViewMargin(); + } } // Adjusts the NavigationView margin based on the window state to fix the gap between the caption buttons and the NavigationView. + // Set force to true to force the adjustment on the first call. private void AdjustNavigationViewMargin(bool? force = null) { - if (WindowPresenter.State == CurrentWindowState && force is not true) + if (WindowPresenter is null || + (WindowPresenter.State == CurrentWindowState && force is not true)) { return; }