From caa7114e6f6feeadf926403508d7c38952a6c964 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Pol=C3=A1=C5=A1ek?= Date: Wed, 3 Sep 2025 18:30:57 +0200 Subject: [PATCH] CmdPal: Add keyboard shortcuts to items in File Search extension (#41413) ## Summary of the Pull Request Adds keyboard shortcuts to `IndexerListItem` commands to mirror those in **All Apps**. - Introduces a new `KeyChords` class that defines and manages key chords for all actions within individual projects, improving code organization and maintainability (similar to the `Icons` class). - Adds a `WellKnownKeyChords` class in the shared project that defines common shortcuts to be used consistently across the entire app. - Updates `IndexerListItem` to include new command context items with `RequestedShortcut` properties for: - Show in folder (`Ctrl+Shift+E`) - Copy path (`Ctrl+Shift+C`) - Open path in console (`Ctrl+Shift+R`) - Updates `AppListItem`, `UWPApplication`, and `Win32Program` to use the new key chord properties from the `KeyChords` class, ensuring consistency and maintainability. ## PR Checklist - [ ] Closes: #xxx - [ ] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [ ] **Tests:** Added/updated and all pass - [ ] **Localization:** All end-user-facing strings can be localized - [ ] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx ## Detailed Description of the Pull Request / Additional comments ## Validation Steps Performed --- .../Helpers/WellKnownKeyChords.cs | 51 +++++++++++++++++++ .../Microsoft.CmdPal.Ext.Apps/AppListItem.cs | 8 +-- .../Microsoft.CmdPal.Ext.Apps/KeyChords.cs | 23 +++++++++ .../Microsoft.CmdPal.Ext.Apps.csproj | 1 + .../Programs/UWPApplication.cs | 8 +-- .../Programs/Win32Program.cs | 10 ++-- .../Data/IndexerListItem.cs | 6 +-- .../Microsoft.CmdPal.Ext.Indexer/KeyChords.cs | 17 +++++++ 8 files changed, 106 insertions(+), 18 deletions(-) create mode 100644 src/modules/cmdpal/Microsoft.CmdPal.Common/Helpers/WellKnownKeyChords.cs create mode 100644 src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/KeyChords.cs create mode 100644 src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/KeyChords.cs diff --git a/src/modules/cmdpal/Microsoft.CmdPal.Common/Helpers/WellKnownKeyChords.cs b/src/modules/cmdpal/Microsoft.CmdPal.Common/Helpers/WellKnownKeyChords.cs new file mode 100644 index 0000000000..8e481b936c --- /dev/null +++ b/src/modules/cmdpal/Microsoft.CmdPal.Common/Helpers/WellKnownKeyChords.cs @@ -0,0 +1,51 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.CommandPalette.Extensions; +using Microsoft.CommandPalette.Extensions.Toolkit; + +using Windows.System; + +namespace Microsoft.CmdPal.Common.Helpers; + +/// +/// Well-known key chords used in the Command Palette and extensions. +/// +/// +/// Assigned key chords should not conflict with system or application shortcuts. +/// However, the key chords in this class are not guaranteed to be unique and may conflict +/// with each other, especially when commands appear together in the same menu. +/// +public static class WellKnownKeyChords +{ + /// + /// Gets the well-known key chord for opening the file location. Shortcut: Ctrl+Shift+E. + /// + public static KeyChord OpenFileLocation { get; } = KeyChordHelpers.FromModifiers(ctrl: true, shift: true, vkey: (int)VirtualKey.E); + + /// + /// Gets the well-known key chord for copying the file path. Shortcut: Ctrl+Shift+C. + /// + public static KeyChord CopyFilePath { get; } = KeyChordHelpers.FromModifiers(ctrl: true, shift: true, vkey: (int)VirtualKey.C); + + /// + /// Gets the well-known key chord for opening the current location in a console. Shortcut: Ctrl+Shift+R. + /// + public static KeyChord OpenInConsole { get; } = KeyChordHelpers.FromModifiers(ctrl: true, shift: true, vkey: (int)VirtualKey.R); + + /// + /// Gets the well-known key chord for running the selected item as administrator. Shortcut: Ctrl+Shift+Enter. + /// + public static KeyChord RunAsAdministrator { get; } = KeyChordHelpers.FromModifiers(ctrl: true, shift: true, vkey: (int)VirtualKey.Enter); + + /// + /// Gets the well-known key chord for running the selected item as a different user. Shortcut: Ctrl+Shift+U. + /// + public static KeyChord RunAsDifferentUser { get; } = KeyChordHelpers.FromModifiers(ctrl: true, shift: true, vkey: (int)VirtualKey.U); + + /// + /// Gets the well-known key chord for toggling the pin state. Shortcut: Ctrl+P. + /// + public static KeyChord TogglePin { get; } = KeyChordHelpers.FromModifiers(ctrl: true, vkey: (int)VirtualKey.P); +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/AppListItem.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/AppListItem.cs index ba8dfc1213..d91c195552 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/AppListItem.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/AppListItem.cs @@ -133,17 +133,13 @@ internal sealed partial class AppListItem : ListItem newCommands.Add(new Separator()); - // 0x50 = P - // Full key chord would be Ctrl+P - var pinKeyChord = KeyChordHelpers.FromModifiers(true, false, false, false, 0x50, 0); - if (isPinned) { newCommands.Add( new CommandContextItem( new UnpinAppCommand(this.AppIdentifier)) { - RequestedShortcut = pinKeyChord, + RequestedShortcut = KeyChords.TogglePin, }); } else @@ -152,7 +148,7 @@ internal sealed partial class AppListItem : ListItem new CommandContextItem( new PinAppCommand(this.AppIdentifier)) { - RequestedShortcut = pinKeyChord, + RequestedShortcut = KeyChords.TogglePin, }); } diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/KeyChords.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/KeyChords.cs new file mode 100644 index 0000000000..6b4063f35d --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/KeyChords.cs @@ -0,0 +1,23 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.CmdPal.Common.Helpers; +using Microsoft.CommandPalette.Extensions; + +namespace Microsoft.CmdPal.Ext.Apps; + +internal static class KeyChords +{ + internal static KeyChord OpenFileLocation { get; } = WellKnownKeyChords.OpenFileLocation; + + internal static KeyChord CopyFilePath { get; } = WellKnownKeyChords.CopyFilePath; + + internal static KeyChord OpenInConsole { get; } = WellKnownKeyChords.OpenInConsole; + + internal static KeyChord RunAsAdministrator { get; } = WellKnownKeyChords.RunAsAdministrator; + + internal static KeyChord RunAsDifferentUser { get; } = WellKnownKeyChords.RunAsDifferentUser; + + internal static KeyChord TogglePin { get; } = WellKnownKeyChords.TogglePin; +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Microsoft.CmdPal.Ext.Apps.csproj b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Microsoft.CmdPal.Ext.Apps.csproj index 311a725e4a..fb074407f8 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Microsoft.CmdPal.Ext.Apps.csproj +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Microsoft.CmdPal.Ext.Apps.csproj @@ -25,6 +25,7 @@ + diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Programs/UWPApplication.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Programs/UWPApplication.cs index 541de2eee1..525e5b3ca8 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Programs/UWPApplication.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Programs/UWPApplication.cs @@ -87,7 +87,7 @@ public class UWPApplication : IUWPApplication new CommandContextItem( new RunAsAdminCommand(UniqueIdentifier, string.Empty, true)) { - RequestedShortcut = KeyChordHelpers.FromModifiers(ctrl: true, shift: true, vkey: VirtualKey.Enter), + RequestedShortcut = KeyChords.RunAsAdministrator, }); // We don't add context menu to 'run as different user', because UWP applications normally installed per user and not for all users. @@ -97,7 +97,7 @@ public class UWPApplication : IUWPApplication new CommandContextItem( new CopyTextCommand(Location) { Name = Resources.copy_path }) { - RequestedShortcut = KeyChordHelpers.FromModifiers(ctrl: true, shift: true, vkey: VirtualKey.C), + RequestedShortcut = KeyChords.CopyFilePath, }); commands.Add( @@ -107,14 +107,14 @@ public class UWPApplication : IUWPApplication Name = Resources.open_containing_folder, }) { - RequestedShortcut = KeyChordHelpers.FromModifiers(ctrl: true, shift: true, vkey: VirtualKey.E), + RequestedShortcut = KeyChords.OpenFileLocation, }); commands.Add( new CommandContextItem( new OpenInConsoleCommand(Package.Location)) { - RequestedShortcut = KeyChordHelpers.FromModifiers(ctrl: true, shift: true, vkey: VirtualKey.R), + RequestedShortcut = KeyChords.OpenInConsole, }); commands.Add( diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Programs/Win32Program.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Programs/Win32Program.cs index dea7fb1e20..151a6ee6d7 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Programs/Win32Program.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Programs/Win32Program.cs @@ -191,32 +191,32 @@ public class Win32Program : IProgram commands.Add(new CommandContextItem( new RunAsAdminCommand(!string.IsNullOrEmpty(LnkFilePath) ? LnkFilePath : FullPath, ParentDirectory, false)) { - RequestedShortcut = KeyChordHelpers.FromModifiers(ctrl: true, shift: true, vkey: VirtualKey.Enter), + RequestedShortcut = KeyChords.RunAsAdministrator, }); commands.Add(new CommandContextItem( new RunAsUserCommand(!string.IsNullOrEmpty(LnkFilePath) ? LnkFilePath : FullPath, ParentDirectory)) { - RequestedShortcut = KeyChordHelpers.FromModifiers(ctrl: true, shift: true, vkey: VirtualKey.U), + RequestedShortcut = KeyChords.RunAsDifferentUser, }); } commands.Add(new CommandContextItem( new CopyTextCommand(FullPath) { Name = Resources.copy_path }) { - RequestedShortcut = KeyChordHelpers.FromModifiers(ctrl: true, shift: true, vkey: VirtualKey.C), + RequestedShortcut = KeyChords.CopyFilePath, }); commands.Add(new CommandContextItem( new OpenPathCommand(ParentDirectory)) { - RequestedShortcut = KeyChordHelpers.FromModifiers(ctrl: true, shift: true, vkey: VirtualKey.E), + RequestedShortcut = KeyChords.OpenFileLocation, }); commands.Add(new CommandContextItem( new OpenInConsoleCommand(ParentDirectory)) { - RequestedShortcut = KeyChordHelpers.FromModifiers(ctrl: true, shift: true, vkey: VirtualKey.R), + RequestedShortcut = KeyChords.OpenInConsole, }); if (AppType == ApplicationType.ShortcutApplication || AppType == ApplicationType.ApprefApplication || AppType == ApplicationType.Win32Application) diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/Data/IndexerListItem.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/Data/IndexerListItem.cs index 9e4d3a4387..25ac912c40 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/Data/IndexerListItem.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/Data/IndexerListItem.cs @@ -91,9 +91,9 @@ internal sealed partial class IndexerListItem : ListItem } commands.Add(new CommandContextItem(new OpenWithCommand(fullPath))); - commands.Add(new CommandContextItem(new ShowFileInFolderCommand(fullPath) { Name = Resources.Indexer_Command_ShowInFolder })); - commands.Add(new CommandContextItem(new CopyPathCommand(fullPath) { Name = Resources.Indexer_Command_CopyPath })); - commands.Add(new CommandContextItem(new OpenInConsoleCommand(fullPath))); + commands.Add(new CommandContextItem(new ShowFileInFolderCommand(fullPath) { Name = Resources.Indexer_Command_ShowInFolder }) { RequestedShortcut = KeyChords.OpenFileLocation }); + commands.Add(new CommandContextItem(new CopyPathCommand(fullPath) { Name = Resources.Indexer_Command_CopyPath }) { RequestedShortcut = KeyChords.CopyFilePath }); + commands.Add(new CommandContextItem(new OpenInConsoleCommand(fullPath)) { RequestedShortcut = KeyChords.OpenInConsole }); commands.Add(new CommandContextItem(new OpenPropertiesCommand(fullPath))); if (IsActionsFeatureEnabled && ApiInformation.IsApiContractPresent("Windows.AI.Actions.ActionsContract", 4)) diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/KeyChords.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/KeyChords.cs new file mode 100644 index 0000000000..39ac4ae627 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/KeyChords.cs @@ -0,0 +1,17 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.CmdPal.Common.Helpers; +using Microsoft.CommandPalette.Extensions; + +namespace Microsoft.CmdPal.Ext.Indexer; + +internal static class KeyChords +{ + internal static KeyChord OpenFileLocation { get; } = WellKnownKeyChords.OpenFileLocation; + + internal static KeyChord CopyFilePath { get; } = WellKnownKeyChords.CopyFilePath; + + internal static KeyChord OpenInConsole { get; } = WellKnownKeyChords.OpenInConsole; +}