Skip to content

Commit 6554a4a

Browse files
committed
add a 'customize' item to the context menu of the bar itself
1 parent cac0048 commit 6554a4a

File tree

10 files changed

+88
-13
lines changed

10 files changed

+88
-13
lines changed

src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/OpenSettingsCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation
1+
// Copyright (c) Microsoft Corporation
22
// The Microsoft Corporation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

@@ -19,7 +19,7 @@ public OpenSettingsCommand()
1919

2020
public override ICommandResult Invoke()
2121
{
22-
WeakReferenceMessenger.Default.Send<OpenSettingsMessage>();
22+
WeakReferenceMessenger.Default.Send<OpenSettingsMessage>(new());
2323
return CommandResult.KeepOpen();
2424
}
2525
}

src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Dock/DockViewModel.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using ManagedCommon;
88
using Microsoft.CmdPal.Core.Common;
99
using Microsoft.CmdPal.Core.ViewModels;
10+
using Microsoft.CmdPal.UI.Messages;
1011
using Microsoft.CmdPal.UI.ViewModels.Messages;
1112
using Microsoft.CmdPal.UI.ViewModels.Settings;
1213
using Microsoft.CommandPalette.Extensions.Toolkit;
@@ -122,6 +123,35 @@ private void DoOnUiThread(Action action)
122123
TaskCreationOptions.None,
123124
Scheduler);
124125
}
126+
127+
public CommandItemViewModel GetContextMenuForDock()
128+
{
129+
var model = new DockContextMenuItem();
130+
var vm = new CommandItemViewModel(new(model), new(this));
131+
vm.SlowInitializeProperties();
132+
return vm;
133+
}
134+
135+
private sealed partial class DockContextMenuItem : CommandItem
136+
{
137+
public DockContextMenuItem()
138+
{
139+
var openSettingsCommand = new AnonymousCommand(
140+
action: () =>
141+
{
142+
WeakReferenceMessenger.Default.Send(new OpenSettingsMessage("Dock"));
143+
})
144+
{
145+
Name = "Customize", // TODO!Loc
146+
Icon = Icons.SettingsIcon,
147+
};
148+
149+
MoreCommands = new CommandContextItem[]
150+
{
151+
new CommandContextItem(openSettingsCommand),
152+
};
153+
}
154+
}
125155
}
126156

127157
#pragma warning restore SA1402 // File may only contain a single type

src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Icons.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ internal sealed class Icons
1111
internal static IconInfo PinIcon => new("\uE718"); // Pin icon
1212

1313
internal static IconInfo UnpinIcon => new("\uE77A"); // Unpin icon
14+
15+
internal static IconInfo SettingsIcon => new("\uE713"); // Settings icon
1416
}

src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Messages/OpenSettingsMessage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
namespace Microsoft.CmdPal.UI.Messages;
66

7-
public record OpenSettingsMessage()
7+
public record OpenSettingsMessage(string? Page = null)
88
{
99
}

src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/CommandBar.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ private void SecondaryButton_Clicked(object sender, RoutedEventArgs e)
126126

127127
private void SettingsIcon_Clicked(object sender, RoutedEventArgs e)
128128
{
129-
WeakReferenceMessenger.Default.Send<OpenSettingsMessage>();
129+
WeakReferenceMessenger.Default.Send<OpenSettingsMessage>(new());
130130
}
131131

132132
private void MoreCommandsButton_Clicked(object sender, RoutedEventArgs e)

src/modules/cmdpal/Microsoft.CmdPal.UI/Dock/DockControl.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@
232232
<Grid
233233
x:Name="RootGrid"
234234
BorderBrush="{ThemeResource SurfaceStrokeColorDefaultBrush}"
235+
RightTapped="RootGrid_RightTapped"
235236
BorderThickness="0,0,0,1">
236237
<Grid x:Name="ContentGrid" Padding="0,0,0,0">
237238
<Grid.ColumnDefinitions>

src/modules/cmdpal/Microsoft.CmdPal.UI/Dock/DockControl.xaml.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,25 @@ public void Receive(CloseContextMenuMessage message)
243243
ContextMenuFlyout.Hide();
244244
}
245245
}
246+
247+
private void RootGrid_RightTapped(object sender, Microsoft.UI.Xaml.Input.RightTappedRoutedEventArgs e)
248+
{
249+
var pos = e.GetPosition(null);
250+
var item = this.ViewModel.GetContextMenuForDock();
251+
if (item.HasMoreCommands)
252+
{
253+
ContextControl.ViewModel.SelectedItem = item;
254+
ContextMenuFlyout.ShowAt(
255+
this.RootGrid,
256+
new FlyoutShowOptions()
257+
{
258+
ShowMode = FlyoutShowMode.Standard,
259+
Placement = FlyoutPlacementMode.TopEdgeAlignedRight,
260+
Position = pos,
261+
});
262+
e.Handled = true;
263+
}
264+
}
246265
}
247266

248267
#pragma warning restore SA1402 // File may only contain a single type

src/modules/cmdpal/Microsoft.CmdPal.UI/Helpers/TrayIconService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation
1+
// Copyright (c) Microsoft Corporation
22
// The Microsoft Corporation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

@@ -152,7 +152,7 @@ private LRESULT WindowProc(
152152
{
153153
if (wParam == PInvoke.WM_USER + 1)
154154
{
155-
WeakReferenceMessenger.Default.Send<OpenSettingsMessage>();
155+
WeakReferenceMessenger.Default.Send<OpenSettingsMessage>(new());
156156
}
157157
else if (wParam == PInvoke.WM_USER + 2)
158158
{

src/modules/cmdpal/Microsoft.CmdPal.UI/Pages/ShellPage.xaml.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,17 +257,23 @@ public void Receive(OpenSettingsMessage message)
257257
{
258258
_ = DispatcherQueue.TryEnqueue(() =>
259259
{
260-
OpenSettings();
260+
OpenSettings(message.Page);
261261
});
262262
}
263263

264-
public void OpenSettings()
264+
public void OpenSettings(string? page = null)
265265
{
266266
if (_settingsWindow is null)
267267
{
268268
_settingsWindow = new SettingsWindow();
269269
}
270270

271+
if (page is not null)
272+
{
273+
_settingsWindow.OpenToPage = page;
274+
_settingsWindow.Navigate(page);
275+
}
276+
271277
_settingsWindow.Activate();
272278
_settingsWindow.BringToFront();
273279
}

src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/SettingsWindow.xaml.cs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using Microsoft.CmdPal.UI.ViewModels.Messages;
1111
using Microsoft.UI.Windowing;
1212
using Microsoft.UI.Xaml;
13-
using Microsoft.UI.Xaml.Automation.Peers;
1413
using Microsoft.UI.Xaml.Controls;
1514
using WinUIEx;
1615
using RS_ = Microsoft.CmdPal.UI.Helpers.ResourceLoaderInstance;
@@ -25,7 +24,9 @@ public sealed partial class SettingsWindow : WindowEx,
2524
public ObservableCollection<Crumb> BreadCrumbs { get; } = [];
2625

2726
// Gets or sets optional action invoked after NavigationView is loaded.
28-
public Action NavigationViewLoaded { get; set; } = () => { };
27+
public Action? NavigationViewLoaded { get; set; }
28+
29+
internal string? OpenToPage { get; set; }
2930

3031
public SettingsWindow()
3132
{
@@ -50,7 +51,9 @@ private void NavView_Loaded(object sender, RoutedEventArgs e)
5051
Task.Delay(500).ContinueWith(_ => this.NavigationViewLoaded?.Invoke(), TaskScheduler.FromCurrentSynchronizationContext());
5152

5253
NavView.SelectedItem = NavView.MenuItems[0];
53-
Navigate("General");
54+
55+
Navigate(OpenToPage);
56+
OpenToPage = null;
5457

5558
if (sender is NavigationView navigationView)
5659
{
@@ -77,20 +80,34 @@ private void NavView_ItemInvoked(NavigationView sender, NavigationViewItemInvoke
7780
Navigate((selectedItem.Tag as string)!);
7881
}
7982

80-
private void Navigate(string page)
83+
internal void Navigate(string? page)
8184
{
8285
var pageType = page switch
8386
{
87+
null => typeof(GeneralPage),
8488
"General" => typeof(GeneralPage),
8589
"Extensions" => typeof(ExtensionsPage),
8690
"Dock" => typeof(DockSettingsPage),
8791
_ => null,
8892
};
93+
var actualPage = page ?? "General";
8994
if (pageType is not null)
9095
{
9196
BreadCrumbs.Clear();
92-
BreadCrumbs.Add(new(page, page));
97+
BreadCrumbs.Add(new(actualPage, actualPage));
9398
NavFrame.Navigate(pageType);
99+
100+
// Now, make sure to actually select the correct menu item too
101+
foreach (var obj in NavView.MenuItems)
102+
{
103+
if (obj is NavigationViewItem item)
104+
{
105+
if (item.Tag is string s && s == page)
106+
{
107+
NavView.SelectedItem = item;
108+
}
109+
}
110+
}
94111
}
95112
}
96113

0 commit comments

Comments
 (0)