Skip to content

Commit a3bc984

Browse files
authored
Merge pull request #1293 from microsoft/niels9001/gallery-titlebar-fixes
[UX] Titlebar improvements
2 parents 831f3c3 + 17de6a9 commit a3bc984

3 files changed

Lines changed: 73 additions & 48 deletions

File tree

WinUIGallery/Helper/NavigationOrientationHelper.cs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
using Windows.ApplicationModel.Core;
7-
using Windows.Storage;
8-
using Microsoft.UI;
9-
using Microsoft.UI.Windowing;
101
using Microsoft.UI.Xaml;
2+
using Microsoft.UI.Xaml.Controls;
3+
using Windows.Storage;
114

125

136
namespace AppUIBasics.Helper
@@ -38,26 +31,26 @@ public static bool IsLeftMode()
3831

3932
public static void IsLeftModeForElement(bool isLeftMode, UIElement element)
4033
{
41-
UpdateTitleBarForElement(isLeftMode, element);
34+
UpdateNavigationViewForElement(isLeftMode, element);
4235
#if !UNPACKAGED
4336
ApplicationData.Current.LocalSettings.Values[IsLeftModeKey] = isLeftMode;
4437
#else
4538
_isLeftMode = isLeftMode;
4639
#endif
4740
}
4841

49-
public static void UpdateTitleBarForElement(bool isLeftMode, UIElement element)
42+
public static void UpdateNavigationViewForElement(bool isLeftMode, UIElement element)
5043
{
51-
var window = WindowHelper.GetWindowForElement(element);
52-
window.ExtendsContentIntoTitleBar = isLeftMode;
53-
44+
NavigationView _navView = NavigationRootPage.GetForElement(element).NavigationView;
5445
if (isLeftMode)
5546
{
56-
NavigationRootPage.GetForElement(element).NavigationView.PaneDisplayMode = Microsoft.UI.Xaml.Controls.NavigationViewPaneDisplayMode.Auto;
47+
_navView.PaneDisplayMode = NavigationViewPaneDisplayMode.Auto;
48+
Grid.SetRow(_navView, 0);
5749
}
5850
else
5951
{
60-
NavigationRootPage.GetForElement(element).NavigationView.PaneDisplayMode = Microsoft.UI.Xaml.Controls.NavigationViewPaneDisplayMode.Top;
52+
_navView.PaneDisplayMode = NavigationViewPaneDisplayMode.Top;
53+
Grid.SetRow(_navView, 1);
6154
}
6255
}
6356
}

WinUIGallery/Navigation/NavigationRootPage.xaml

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,33 @@
2020
<ColumnDefinition Width="Auto" />
2121
<ColumnDefinition />
2222
</Grid.ColumnDefinitions>
23+
<Grid.RowDefinitions>
24+
<RowDefinition Height="Auto" />
25+
<!-- TitleBar -->
26+
<RowDefinition Height="*" />
27+
<!-- NavView -->
28+
</Grid.RowDefinitions>
2329
<VisualStateManager.VisualStateGroups>
2430
<VisualStateGroup x:Name="TitleBarStates">
25-
<VisualState x:Name="WideLayout">
26-
<VisualState.StateTriggers>
27-
<AdaptiveTrigger MinWindowWidth="{StaticResource Breakpoint640Plus}" />
28-
</VisualState.StateTriggers>
29-
</VisualState>
30-
<VisualState x:Name="SmallLayout">
31-
<VisualState.StateTriggers>
32-
<AdaptiveTrigger MinWindowWidth="0" />
33-
</VisualState.StateTriggers>
31+
<VisualState x:Name="Default" />
32+
<VisualState x:Name="Compact">
3433
<VisualState.Setters>
3534
<Setter Target="AppTitleBar.Margin" Value="96,0,0,0" />
3635
</VisualState.Setters>
3736
</VisualState>
37+
<VisualState x:Name="Top">
38+
<VisualState.Setters>
39+
<Setter Target="AppTitleBar.Margin" Value="16,0,0,0" />
40+
</VisualState.Setters>
41+
</VisualState>
42+
</VisualStateGroup>
43+
<VisualStateGroup x:Name="WindowActivationStates">
44+
<VisualState x:Name="Activated" />
45+
<VisualState x:Name="Deactivated">
46+
<VisualState.Setters>
47+
<Setter Target="AppTitle.Foreground" Value="{ThemeResource TextFillColorTertiaryBrush}" />
48+
</VisualState.Setters>
49+
</VisualState>
3850
</VisualStateGroup>
3951
</VisualStateManager.VisualStateGroups>
4052

@@ -43,15 +55,15 @@
4355
Grid.Column="1"
4456
Height="{Binding ElementName=NavigationViewControl, Path=CompactPaneLength}"
4557
Margin="48,0,0,0"
46-
VerticalAlignment="Top"
58+
VerticalAlignment="Stretch"
4759
AutomationProperties.AutomationId="AppTitleBar"
4860
Canvas.ZIndex="1"
4961
IsHitTestVisible="True">
50-
<StackPanel Orientation="Horizontal">
62+
<StackPanel VerticalAlignment="Center" Orientation="Horizontal">
5163
<Image Width="18" Source="ms-appx:///Assets/Tiles/TitlebarLogo.png" />
5264
<TextBlock
5365
x:Name="AppTitle"
54-
Margin="16,0,0,0"
66+
Margin="12,0,0,0"
5567
VerticalAlignment="Center"
5668
Style="{StaticResource CaptionTextBlockStyle}"
5769
Text="{x:Bind AppTitleText}" />
@@ -140,8 +152,10 @@
140152
-->
141153
<NavigationView
142154
x:Name="NavigationViewControl"
155+
Grid.RowSpan="2"
143156
Grid.Column="1"
144157
Canvas.ZIndex="0"
158+
DisplayModeChanged="OnPaneDisplayModeChanged"
145159
IsTabStop="False"
146160
IsTitleBarAutoPaddingEnabled="True"
147161
Loaded="OnNavigationViewControlLoaded"

WinUIGallery/Navigation/NavigationRootPage.xaml.cs

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
using Microsoft.UI.Xaml.Controls;
2323
using Microsoft.UI.Xaml.Input;
2424
using Microsoft.UI.Xaml.Navigation;
25-
using Windows.Devices.Input;
2625
using Windows.Foundation;
2726
using Windows.System.Profile;
2827
using Windows.UI.ViewManagement;
@@ -34,9 +33,6 @@ public sealed partial class NavigationRootPage : Page
3433
{
3534
public Windows.System.VirtualKey ArrowKey;
3635
public Microsoft.UI.Dispatching.DispatcherQueue dispatcherQueue;
37-
38-
private RootFrameNavigationHelper _navHelper;
39-
private bool _isKeyboardConnected;
4036
private UISettings _settings;
4137

4238

@@ -77,8 +73,6 @@ public NavigationRootPage()
7773
this.InitializeComponent();
7874
dispatcherQueue = Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread();
7975

80-
_navHelper = new RootFrameNavigationHelper(rootFrame, NavigationViewControl);
81-
8276
SetDeviceFamily();
8377
AddNavigationMenuItems();
8478

@@ -91,27 +85,56 @@ public NavigationRootPage()
9185
}
9286
};
9387

94-
_isKeyboardConnected = Convert.ToBoolean(new KeyboardCapabilities().KeyboardPresent);
95-
9688
// remove the solid-colored backgrounds behind the caption controls and system back button if we are in left mode
9789
// This is done when the app is loaded since before that the actual theme that is used is not "determined" yet
9890
Loaded += delegate (object sender, RoutedEventArgs e)
9991
{
100-
NavigationOrientationHelper.UpdateTitleBarForElement(NavigationOrientationHelper.IsLeftMode(), this);
92+
NavigationOrientationHelper.UpdateNavigationViewForElement(NavigationOrientationHelper.IsLeftMode(), this);
10193

10294
Window window = WindowHelper.GetWindowForElement(sender as UIElement);
10395
window.Title = AppTitleText;
10496
window.ExtendsContentIntoTitleBar = true;
97+
window.Activated += Window_Activated;
10598
window.SetTitleBar(this.AppTitleBar);
10699

107100
AppWindow appWindow = WindowHelper.GetAppWindow(window);
108101
appWindow.SetIcon("Assets/Tiles/GalleryIcon.ico");
109102
_settings = new UISettings();
110103
_settings.ColorValuesChanged += _settings_ColorValuesChanged; // cannot use FrameworkElement.ActualThemeChanged event because the triggerTitleBarRepaint workaround no longer works
111104
};
105+
}
106+
107+
private void Window_Activated(object sender, WindowActivatedEventArgs args)
108+
{
109+
if (args.WindowActivationState == WindowActivationState.Deactivated)
110+
{
111+
VisualStateManager.GoToState(this, "Deactivated", true);
112+
}
113+
else
114+
{
115+
VisualStateManager.GoToState(this, "Activated", true);
116+
}
117+
}
112118

113-
NavigationViewControl.RegisterPropertyChangedCallback(NavigationView.PaneDisplayModeProperty, new DependencyPropertyChangedCallback(OnPaneDisplayModeChanged));
119+
private void OnPaneDisplayModeChanged(NavigationView sender, NavigationViewDisplayModeChangedEventArgs args)
120+
{
121+
if (sender.PaneDisplayMode == NavigationViewPaneDisplayMode.Top)
122+
{
123+
VisualStateManager.GoToState(this, "Top", true);
124+
}
125+
else
126+
{
127+
if (args.DisplayMode == NavigationViewDisplayMode.Minimal)
128+
{
129+
VisualStateManager.GoToState(this, "Compact", true);
130+
}
131+
else
132+
{
133+
VisualStateManager.GoToState(this, "Default", true);
134+
}
135+
}
114136
}
137+
115138
// this handles updating the caption button colors correctly when indows system theme is changed
116139
// while the app is open
117140
private void _settings_ColorValuesChanged(UISettings sender, object args)
@@ -123,12 +146,6 @@ private void _settings_ColorValuesChanged(UISettings sender, object args)
123146
});
124147
}
125148

126-
private void OnPaneDisplayModeChanged(DependencyObject sender, DependencyProperty dp)
127-
{
128-
var navigationView = sender as NavigationView;
129-
NavigationRootPage.GetForElement(this).AppTitleBar.Visibility = navigationView.PaneDisplayMode == NavigationViewPaneDisplayMode.Top ? Visibility.Collapsed : Visibility.Visible;
130-
}
131-
132149
// Wraps a call to rootFrame.Navigate to give the Page a way to know which NavigationRootPage is navigating.
133150
// Please call this function rather than rootFrame.Navigate to navigate the rootFrame.
134151
public void Navigate(
@@ -186,7 +203,7 @@ private void AddNavigationMenuItems()
186203
{
187204
foreach (var group in ControlInfoDataSource.Instance.Groups.OrderBy(i => i.Title).Where(i => !i.IsSpecialSection))
188205
{
189-
var itemGroup = new Microsoft.UI.Xaml.Controls.NavigationViewItem() { Content = group.Title, Tag = group.UniqueId, DataContext = group, Icon = GetIcon(group.ImageIconPath) };
206+
var itemGroup = new NavigationViewItem() { Content = group.Title, Tag = group.UniqueId, DataContext = group, Icon = GetIcon(group.ImageIconPath) };
190207

191208
var groupMenuFlyoutItem = new MenuFlyoutItem() { Text = $"Copy Link to {group.Title} samples", Icon = new FontIcon() { Glyph = "\uE8C8" }, Tag = group };
192209
groupMenuFlyoutItem.Click += this.OnMenuFlyoutItemClick;
@@ -196,7 +213,7 @@ private void AddNavigationMenuItems()
196213

197214
foreach (var item in group.Items)
198215
{
199-
var itemInGroup = new Microsoft.UI.Xaml.Controls.NavigationViewItem() { IsEnabled = item.IncludedInBuild, Content = item.Title, Tag = item.UniqueId, DataContext = item };
216+
var itemInGroup = new NavigationViewItem() { IsEnabled = item.IncludedInBuild, Content = item.Title, Tag = item.UniqueId, DataContext = item };
200217

201218
var itemInGroupMenuFlyoutItem = new MenuFlyoutItem() { Text = $"Copy Link to {item.Title} sample", Icon = new FontIcon() { Glyph = "\uE8C8" }, Tag = item };
202219
itemInGroupMenuFlyoutItem.Click += this.OnMenuFlyoutItemClick;
@@ -249,7 +266,7 @@ private void SetDeviceFamily()
249266

250267
private void OnNewControlsMenuItemLoaded(object sender, RoutedEventArgs e)
251268
{
252-
if ( NavigationViewControl.DisplayMode == Microsoft.UI.Xaml.Controls.NavigationViewDisplayMode.Expanded)
269+
if ( NavigationViewControl.DisplayMode == NavigationViewDisplayMode.Expanded)
253270
{
254271
controlsSearchBox.Focus(FocusState.Keyboard);
255272
}
@@ -261,7 +278,7 @@ private void OnNavigationViewControlLoaded(object sender, RoutedEventArgs e)
261278
Task.Delay(500).ContinueWith(_ => this.NavigationViewLoaded?.Invoke(), TaskScheduler.FromCurrentSynchronizationContext());
262279
}
263280

264-
private void OnNavigationViewSelectionChanged(Microsoft.UI.Xaml.Controls.NavigationView sender, Microsoft.UI.Xaml.Controls.NavigationViewSelectionChangedEventArgs args)
281+
private void OnNavigationViewSelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
265282
{
266283
if (args.IsSettingsSelected)
267284
{
@@ -554,6 +571,7 @@ private void WaitForDebuggerInvokerButton_Click(object sender, RoutedEventArgs e
554571
private static extern void DebugBreak();
555572

556573
#endregion
574+
557575
}
558576

559577
public class NavigationRootPageArgs

0 commit comments

Comments
 (0)