diff --git a/WinUIGallery/MainWindow.xaml.cs b/WinUIGallery/MainWindow.xaml.cs index 0dda5d96e..38238f184 100644 --- a/WinUIGallery/MainWindow.xaml.cs +++ b/WinUIGallery/MainWindow.xaml.cs @@ -32,12 +32,42 @@ public NavigationView NavigationView public Action NavigationViewLoaded { get; set; } +#nullable enable + private OverlappedPresenter? WindowPresenter { get; } +#nullable disable + + private OverlappedPresenterState CurrentWindowState { get; set; } + public MainWindow() { this.InitializeComponent(); SetWindowProperties(); RootGrid.ActualThemeChanged += (_, _) => TitleBarHelper.ApplySystemThemeToCaptionButtons(this, RootGrid.ActualTheme); dispatcherQueue = Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread(); + + 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 is null || + (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)