From 64ad9b56ada7c0319ce574d587338697e721b27e Mon Sep 17 00:00:00 2001 From: PesBandi <127593627+PesBandi@users.noreply.github.com> Date: Mon, 16 Jun 2025 06:10:50 +0200 Subject: [PATCH] [CmdPal][Apps]Add copy path command (#39984) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary of the Pull Request Adds a *Copy path* command to the Ctrl+K menu in the All apps extension ## PR Checklist - [x] **Closes:** #39500 (as mentioned in the discussion, the other requests will be tracked over in #39501) - [x] **Communication:** I've discussed this with core contributors already + has Help wanted - [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 Copies the exe full path for standard programs. Copies the package directory for UWPs. Shows a *Copied to clipboard!* toast on a successful copy and closes. Shows a toast when copying fails, keeps CmdPal open. ![image](https://github.com/user-attachments/assets/ad89a583-13c8-475c-973b-b0f5e7556e31) ## Validation Steps Performed Manually tested copying for UWPs and Win32 programs --- .../Commands/CopyPathCommand.cs | 49 +++++++++++++++++++ .../Programs/UWPApplication.cs | 4 ++ .../Programs/Win32Program.cs | 3 ++ .../Properties/Resources.Designer.cs | 27 ++++++++++ .../Properties/Resources.resx | 10 ++++ 5 files changed, 93 insertions(+) create mode 100644 src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Commands/CopyPathCommand.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 new file mode 100644 index 0000000000..30ad044f37 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Commands/CopyPathCommand.cs @@ -0,0 +1,49 @@ +// 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 static readonly IconInfo TheIcon = new("\ue8c8"); + + private readonly string _target; + + public CopyPathCommand(string target) + { + Name = Resources.copy_path; + Icon = TheIcon; + + _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 793000146d..4619bd70c9 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 @@ -85,6 +85,10 @@ public class UWPApplication : IProgram // We don't add context menu to 'run as different user', because UWP applications normally installed per user and not for all users. } + commands.Add( + new CommandContextItem( + new CopyPathCommand(Location))); + commands.Add( new CommandContextItem( new OpenPathCommand(Location) 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 210febc549..1ccf797d35 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 @@ -198,6 +198,9 @@ public class Win32Program : IProgram new RunAsUserCommand(!string.IsNullOrEmpty(LnkFilePath) ? LnkFilePath : FullPath, ParentDirectory))); } + commands.Add(new CommandContextItem( + new CopyPathCommand(FullPath))); + commands.Add(new CommandContextItem( new OpenPathCommand(ParentDirectory))); 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 33531ba62f..cc07ac86b4 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,6 +78,33 @@ 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. + /// + internal static string copy_path { + get { + return ResourceManager.GetString("copy_path", resourceCulture); + } + } + /// /// Looks up a localized string similar to Include apps found on the desktop. /// 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 98212f1066..ce4fb79689 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 @@ -163,12 +163,22 @@ Open location + + Copy path + Run as administrator 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