From 8258f2ab6f51d0abaa6f9e7347fa68d92ca4dd7e Mon Sep 17 00:00:00 2001 From: PesBandi <127593627+PesBandi@users.noreply.github.com> Date: Mon, 25 Aug 2025 21:48:48 +0200 Subject: [PATCH] [CmdPal] Eliminate `CopyTextCommand` duplicates (#41347) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary of the Pull Request Removes redundant CopyCommands and replaces their usage with `CopyTextCommand` from the toolkit. This is actually recommend by the [docs](https://learn.microsoft.com/en-us/windows/powertoys/command-palette/command-results#showtoast-command-result), we should probably lead by example 😄 > Consider the CopyTextCommand in the helpers - this command will show a toast with the text "Copied to clipboard", then dismiss the palette. Only functionality change is that they now display *Copied to clipboard!* after copying. ## PR Checklist - [x] Closes: #41346 - [ ] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [x] **Tests:** All pass - [x] **Localization:** All end-user-facing strings can be localized - [x] **Dev docs:** No need - [x] **New binaries:** None - [x] **Documentation updated:** No need ## Detailed Description of the Pull Request / Additional comments Custom command names (e.g. *Copy path* instead of *Copy*) are preserved. Strings put out of use have been deleted. CopyCommands from the Registry and Clipboard plugins are left untouched, as they contain special logic. The error handling in `CopyPathCommand` from Apps was unnecessary, since it's already taken care of by `ClipboardHelper`. ## Validation Steps Performed Manual testing --- .../Commands/CopyPathCommand.cs | 47 ------------------- .../Programs/UWPApplication.cs | 2 +- .../Programs/Win32Program.cs | 2 +- .../Properties/Resources.Designer.cs | 18 ------- .../Properties/Resources.resx | 7 --- .../Commands/CopySettingCommand.cs | 39 --------------- .../Helpers/ContextMenuHelper.cs | 2 +- 7 files changed, 3 insertions(+), 114 deletions(-) delete mode 100644 src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Commands/CopyPathCommand.cs delete mode 100644 src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowsSettings/Commands/CopySettingCommand.cs diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Commands/CopyPathCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Commands/CopyPathCommand.cs deleted file mode 100644 index 9fca0304aa..0000000000 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Commands/CopyPathCommand.cs +++ /dev/null @@ -1,47 +0,0 @@ -// 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 System; -using System.Globalization; -using System.Text; -using ManagedCommon; -using Microsoft.CmdPal.Ext.Apps.Properties; -using Microsoft.CommandPalette.Extensions.Toolkit; - -namespace Microsoft.CmdPal.Ext.Apps.Commands; - -internal sealed partial class CopyPathCommand : InvokableCommand -{ - private readonly string _target; - - public CopyPathCommand(string target) - { - Name = Resources.copy_path; - Icon = Icons.CopyIcon; - - _target = target; - } - - private static readonly CompositeFormat CopyFailedFormat = CompositeFormat.Parse(Resources.copy_failed); - - public override CommandResult Invoke() - { - try - { - ClipboardHelper.SetText(_target); - } - catch (Exception ex) - { - Logger.LogError("Copy failed: " + ex.Message); - return CommandResult.ShowToast( - new ToastArgs - { - Message = string.Format(CultureInfo.CurrentCulture, CopyFailedFormat, ex.Message), - Result = CommandResult.KeepOpen(), - }); - } - - return CommandResult.ShowToast(Resources.copied_to_clipboard); - } -} 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 23b428447b..9b5c663082 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 @@ -95,7 +95,7 @@ public class UWPApplication : IUWPApplication commands.Add( new CommandContextItem( - new Commands.CopyPathCommand(Location)) + new CopyTextCommand(Location) { Name = Resources.copy_path }) { RequestedShortcut = KeyChordHelpers.FromModifiers(ctrl: true, shift: true, vkey: VirtualKey.C), }); 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 bf9e5f7334..32455b5ea4 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 @@ -202,7 +202,7 @@ public class Win32Program : IProgram } commands.Add(new CommandContextItem( - new Commands.CopyPathCommand(FullPath)) + new CopyTextCommand(FullPath) { Name = Resources.copy_path }) { RequestedShortcut = KeyChordHelpers.FromModifiers(ctrl: true, shift: true, vkey: VirtualKey.C), }); diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Properties/Resources.Designer.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Properties/Resources.Designer.cs index 419bba3580..da93e318a5 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Properties/Resources.Designer.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Properties/Resources.Designer.cs @@ -78,24 +78,6 @@ namespace Microsoft.CmdPal.Ext.Apps.Properties { } } - /// - /// Looks up a localized string similar to Copied to clipboard!. - /// - internal static string copied_to_clipboard { - get { - return ResourceManager.GetString("copied_to_clipboard", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Copy failed ({0}). Please try again.. - /// - internal static string copy_failed { - get { - return ResourceManager.GetString("copy_failed", resourceCulture); - } - } - /// /// Looks up a localized string similar to Copy path. /// diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Properties/Resources.resx b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Properties/Resources.resx index 1e4c8fa4bd..7eaae14520 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Properties/Resources.resx +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Properties/Resources.resx @@ -172,13 +172,6 @@ Run as different user - - Copy failed ({0}). Please try again. - {0} is the error message - - - Copied to clipboard! - Include apps found in the Start Menu diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowsSettings/Commands/CopySettingCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowsSettings/Commands/CopySettingCommand.cs deleted file mode 100644 index d992a5fa6b..0000000000 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowsSettings/Commands/CopySettingCommand.cs +++ /dev/null @@ -1,39 +0,0 @@ -// 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 System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Resources; -using System.Text; -using System.Threading.Tasks; -using Microsoft.CmdPal.Ext.WindowsSettings.Classes; -using Microsoft.CmdPal.Ext.WindowsSettings.Properties; -using Microsoft.CommandPalette.Extensions; -using Microsoft.CommandPalette.Extensions.Toolkit; -using Windows.ApplicationModel.DataTransfer; -using Windows.Networking.NetworkOperators; -using Windows.UI; - -namespace Microsoft.CmdPal.Ext.WindowsSettings.Commands; - -internal sealed partial class CopySettingCommand : InvokableCommand -{ - private readonly WindowsSetting _entry; - - internal CopySettingCommand(WindowsSetting entry) - { - Name = Resources.CopyCommand; - Icon = Icons.CopyIcon; - _entry = entry; - } - - public override CommandResult Invoke() - { - ClipboardHelper.SetText(_entry.Command); - - return CommandResult.Dismiss(); - } -} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowsSettings/Helpers/ContextMenuHelper.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowsSettings/Helpers/ContextMenuHelper.cs index 83a0d2eb8c..855a47e1a7 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowsSettings/Helpers/ContextMenuHelper.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowsSettings/Helpers/ContextMenuHelper.cs @@ -23,7 +23,7 @@ internal static class ContextMenuHelper { var list = new List(1) { - new(new CopySettingCommand(entry)), + new(new CopyTextCommand(entry.Command) { Name = Resources.CopyCommand }), }; return list;