From 86e8325ce4ec7ce82eb91c3e27d88311a1452bbf Mon Sep 17 00:00:00 2001 From: Yimeng Wu Date: Fri, 15 Dec 2023 23:58:36 +0000 Subject: [PATCH] Port microsoft/microsoft-ui-xaml#4779 --- .../NavigationView/NavigationView.cs | 65 ++--- .../NavigationView/NavigationView.xaml | 236 ++++++++++-------- .../NavigationView/NavigationViewItemBase.cs | 2 +- .../NavigationViewItemHeader.cs | 5 + ModernWpf/Styles/NavigationBackButton.xaml | 6 +- ModernWpf/Styles/NavigationView.xaml | 55 ++-- ModernWpf/ThemeResources/Dark.xaml | 9 +- ModernWpf/ThemeResources/HighContrast.xaml | 1 + ModernWpf/ThemeResources/Light.xaml | 9 +- .../NavigationViewTopNavOnlyPage.xaml.cs | 2 +- 10 files changed, 214 insertions(+), 176 deletions(-) diff --git a/ModernWpf.Controls/NavigationView/NavigationView.cs b/ModernWpf.Controls/NavigationView/NavigationView.cs index b8a78f942..6d279bcfa 100644 --- a/ModernWpf.Controls/NavigationView/NavigationView.cs +++ b/ModernWpf.Controls/NavigationView/NavigationView.cs @@ -102,7 +102,7 @@ public partial class NavigationView : ContentControl, IControlProtected const int c_toggleButtonHeightWhenShouldPreserveNavigationViewRS3Behavior = 56; const int c_backButtonRowDefinition = 1; const float c_paneElevationTranslationZ = 32; - const int c_paneItemsSeparatorHeight = 21; + const int c_paneItemsSeparatorHeight = 9; const int c_mainMenuBlockIndex = 0; const int c_footerMenuBlockIndex = 1; @@ -756,6 +756,7 @@ public override void OnApplyTemplate() UpdateVisualState(); UpdatePaneTitleMargins(); UpdatePaneLayout(); + UpdatePaneOverlayGroup(); } void UpdateRepeaterItemsSource(bool forceSelectionModelUpdate) @@ -1572,7 +1573,7 @@ double ItemsContainerMargin() } var availableHeight = paneContentRow.ActualHeight - itemsContainerMargin; - // The -21 below is to account for the separator height that we need to subtract. + // The c_paneItemsSeparatorHeight is to account for the 9px separator height that we need to subtract. if (PaneFooter is { }) { availableHeight -= c_paneItemsSeparatorHeight; @@ -1811,8 +1812,6 @@ void OnSplitViewPaneClosing(DependencyObject sender, SplitViewPaneClosingEventAr VisualStateManager.GoToState(this, "ListSizeCompact", true /*useTransitions*/); UpdatePaneToggleSize(); } - - VisualStateManager.GoToState(this, "PaneNotOverlaying", true /*useTransitions*/); } } } @@ -1829,14 +1828,6 @@ void OnSplitViewPaneOpening(DependencyObject sender, object obj) { // See UpdateIsClosedCompact 'RS3+ animation timing enhancement' for explanation: VisualStateManager.GoToState(this, "ListSizeFull", true /*useTransitions*/); - - if (m_rootSplitView is { } splitView) - { - if (splitView.DisplayMode == SplitViewDisplayMode.CompactOverlay || splitView.DisplayMode == SplitViewDisplayMode.Overlay) - { - VisualStateManager.GoToState(this, "PaneOverlaying", true /*useTransitions*/); - } - } } PaneOpening?.Invoke(this, null); @@ -2008,20 +1999,25 @@ void UpdatePaneTitleFrameworkElementParents() { var isPaneToggleButtonVisible = IsPaneToggleButtonVisible; var isTopNavigationView = IsTopNavigationView(); + var paneTitleSize = PaneTitle?.Length ?? 0; + + m_isLeftPaneTitleEmpty = (isPaneToggleButtonVisible || + isTopNavigationView || + paneTitleSize == 0 || + (PaneDisplayMode == NavigationViewPaneDisplayMode.LeftMinimal && !IsPaneOpen)); - paneTitleHolderFrameworkElement.Visibility = - (isPaneToggleButtonVisible || - isTopNavigationView || - PaneTitle.Length == 0 || - (PaneDisplayMode == NavigationViewPaneDisplayMode.LeftMinimal && !IsPaneOpen)) ? - Visibility.Collapsed : Visibility.Visible; + paneTitleHolderFrameworkElement.Visibility = m_isLeftPaneTitleEmpty ? Visibility.Collapsed : Visibility.Visible; if (m_paneTitleFrameworkElement is { } paneTitleFrameworkElement) { + var paneTitleTopPane = m_paneTitleOnTopPane; + var first = SetPaneTitleFrameworkElementParent(m_paneToggleButton, paneTitleFrameworkElement, isTopNavigationView || !isPaneToggleButtonVisible); var second = SetPaneTitleFrameworkElementParent(m_paneTitlePresenter, paneTitleFrameworkElement, isTopNavigationView || isPaneToggleButtonVisible); - var third = SetPaneTitleFrameworkElementParent(m_paneTitleOnTopPane, paneTitleFrameworkElement, !isTopNavigationView || isPaneToggleButtonVisible); + var third = SetPaneTitleFrameworkElementParent(paneTitleTopPane, paneTitleFrameworkElement, !isTopNavigationView || isPaneToggleButtonVisible); (first ?? second ?? third)?.Invoke(); + + paneTitleTopPane.Visibility = third is { } && paneTitleSize != 0 ? Visibility.Visible : Visibility.Collapsed; } } } @@ -3559,7 +3555,7 @@ void UpdateVisualStateForOverflowButton() void UpdateLeftNavigationOnlyVisualState(bool useTransitions) { bool isToggleButtonVisible = IsPaneToggleButtonVisible; - VisualStateManager.GoToState(this, isToggleButtonVisible ? "TogglePaneButtonVisible" : "TogglePaneButtonCollapsed", false /*useTransitions*/); + VisualStateManager.GoToState(this, isToggleButtonVisible || !m_isLeftPaneTitleEmpty ? "TogglePaneButtonVisible" : "TogglePaneButtonCollapsed", false /*useTransitions*/); } void InvalidateTopNavPrimaryLayout() @@ -4296,6 +4292,8 @@ void OnIsPaneOpenChanged() UpdatePaneTabFocusNavigation(); UpdateSettingsItemToolTip(); UpdatePaneTitleFrameworkElementParents(); + UpdatePaneOverlayGroup(); + UpdatePaneButtonsWidths(); if (SharedHelpers.IsThemeShadowAvailable()) { @@ -4313,7 +4311,6 @@ void OnIsPaneOpenChanged() } } } - UpdatePaneButtonsWidths(); } void UpdatePaneToggleButtonVisibility() @@ -4655,15 +4652,6 @@ void UpdateBackAndCloseButtonsVisibility() paneHeaderCloseButtonColumn.Width = GridLengthHelper.FromValueAndType(paneHeaderPaddingForCloseButton, GridUnitType.Pixel); } - if (m_paneTitleHolderFrameworkElement is { } paneTitleHolderFrameworkElement) - { - if (paneHeaderContentBorderRowMinHeight == 0.00 && paneTitleHolderFrameworkElement.Visibility == Visibility.Visible) - { - // Handling the case where the PaneTottleButton is collapsed and the PaneTitle's height needs to push the rest of the NavigationView's UI down. - paneHeaderContentBorderRowMinHeight = paneTitleHolderFrameworkElement.ActualHeight; - } - } - if (m_paneHeaderContentBorderRow is { } paneHeaderContentBorderRow) { paneHeaderContentBorderRow.MinHeight = paneHeaderContentBorderRowMinHeight; @@ -5027,6 +5015,21 @@ void UpdatePaneShadow() */ } + void UpdatePaneOverlayGroup() + { + if (m_rootSplitView is { } splitView) + { + if (IsPaneOpen && (splitView.DisplayMode == SplitViewDisplayMode.CompactOverlay || splitView.DisplayMode == SplitViewDisplayMode.Overlay)) + { + VisualStateManager.GoToState(this, "PaneOverlaying", true /*useTransitions*/); + } + else + { + VisualStateManager.GoToState(this, "PaneNotOverlaying", true /*useTransitions*/); + } + } + } + T GetContainerForData(object data) where T : class { if (data == null) @@ -5870,6 +5873,8 @@ protected override void OnDpiChanged(DpiScale oldDpi, DpiScale newDpi) bool m_TabKeyPrecedesFocusChange = false; + bool m_isLeftPaneTitleEmpty = false; + GettingFocusHelper m_leftNavRepeaterGettingFocusHelper; GettingFocusHelper m_topNavRepeaterGettingFocusHelper; GettingFocusHelper m_leftNavFooterMenuRepeaterGettingFocusHelper; diff --git a/ModernWpf.Controls/NavigationView/NavigationView.xaml b/ModernWpf.Controls/NavigationView/NavigationView.xaml index 62ca2a5f1..30d157b85 100644 --- a/ModernWpf.Controls/NavigationView/NavigationView.xaml +++ b/ModernWpf.Controls/NavigationView/NavigationView.xaml @@ -161,7 +161,7 @@ MinHeight="{DynamicResource NavigationViewItemOnLeftMinHeight}" Background="{TemplateBinding Background}" CornerRadius="{TemplateBinding CornerRadius}" - Margin="4,2,4,2" + Margin="{DynamicResource NavigationViewItemButtonMargin}" ui:FocusVisualHelper.IsTemplateFocusTarget="True"> @@ -207,7 +207,7 @@ - + @@ -279,7 +279,8 @@ Margin="{DynamicResource NavigationViewItemOnLeftIconBoxMargin}"> + Content="{TemplateBinding Icon}" + TextElement.Foreground="{TemplateBinding Foreground}" /> @@ -290,6 +291,7 @@ Margin="{DynamicResource NavigationViewItemContentPresenterMargin}" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" + TextElement.Foreground="{TemplateBinding Foreground}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}"/> @@ -373,12 +375,17 @@ + - + @@ -400,34 +407,35 @@ - - - - - - - - - - - - - + + + + + + + + + + + + + + + - + @@ -465,13 +473,15 @@