|
| 1 | +using System; |
| 2 | +using System.ComponentModel.Design; |
| 3 | +using System.Runtime.InteropServices; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Microsoft.VisualStudio; |
| 6 | +using Microsoft.VisualStudio.OLE.Interop; |
| 7 | +using Microsoft.VisualStudio.Shell; |
| 8 | +using Microsoft.VisualStudio.Shell.Interop; |
| 9 | + |
| 10 | +namespace Community.VisualStudio.Toolkit |
| 11 | +{ |
| 12 | + /// <summary>A collection of services related to the command system.</summary> |
| 13 | + public class Commands |
| 14 | + { |
| 15 | + internal Commands() |
| 16 | + { } |
| 17 | + |
| 18 | + /// <summary>Provides methods to manage the global designer verbs and menu commands available in design mode, and to show some types of shortcut menus.</summary> |
| 19 | + public Task<IMenuCommandService> GetCommandServiceAsync() => VS.GetRequiredServiceAsync<IMenuCommandService, IMenuCommandService>(); |
| 20 | + |
| 21 | + /// <summary>Used to register and unregister a command target as a high priority command handler.</summary> |
| 22 | + public Task<IVsRegisterPriorityCommandTarget> GetPriorityCommandTargetAsync() => VS.GetRequiredServiceAsync<SVsRegisterPriorityCommandTarget, IVsRegisterPriorityCommandTarget>(); |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Executes a command by name |
| 26 | + /// </summary> |
| 27 | + /// <returns>Returns <see langword="true"/> if the command was succesfully executed; otherwise <see langword="false"/>.</returns> |
| 28 | + public async Task<bool> ExecuteAsync(string name, string argument = "") |
| 29 | + { |
| 30 | + await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); |
| 31 | + IVsCommandWindow cw = await VS.Windows.GetCommandWindowAsync(); |
| 32 | + |
| 33 | + return cw.ExecuteCommand($"{name} {argument}") == VSConstants.S_OK; |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Executes a command by guid and ID |
| 38 | + /// </summary> |
| 39 | + /// <returns>Returns <see langword="true"/> if the command was succesfully executed; otherwise <see langword="false"/>.</returns> |
| 40 | + public async Task<bool> ExecuteAsync(Guid menuGroup, uint commandId, string argument = "") |
| 41 | + { |
| 42 | + await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); |
| 43 | + IOleCommandTarget cs = await VS.GetRequiredServiceAsync<SUIHostCommandDispatcher, IOleCommandTarget>(); |
| 44 | + |
| 45 | + IntPtr inArgPtr = Marshal.AllocCoTaskMem(200); |
| 46 | + Marshal.GetNativeVariantForObject(argument, inArgPtr); |
| 47 | + |
| 48 | + var result = cs.Exec(menuGroup, commandId, (uint)OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, inArgPtr, IntPtr.Zero); |
| 49 | + |
| 50 | + return result == VSConstants.S_OK; |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Executes a command by guid and ID |
| 55 | + /// </summary> |
| 56 | + /// <returns>Returns <see langword="true"/> if the command was succesfully executed; otherwise <see langword="false"/>.</returns> |
| 57 | + public Task<bool> ExecuteAsync(VSConstants.VSStd97CmdID command, string argument = "") |
| 58 | + { |
| 59 | + return ExecuteAsync(typeof(VSConstants.VSStd97CmdID).GUID, (uint)command, argument); |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Executes a command by guid and ID |
| 64 | + /// </summary> |
| 65 | + /// <returns>Returns <see langword="true"/> if the command was succesfully executed; otherwise <see langword="false"/>.</returns> |
| 66 | + public Task<bool> ExecuteAsync(VSConstants.VSStd2KCmdID command, string argument = "") |
| 67 | + { |
| 68 | + return ExecuteAsync(typeof(VSConstants.VSStd2KCmdID).GUID, (uint)command, argument); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments