|
| 1 | +using System; |
| 2 | +using System.ComponentModel.Design; |
| 3 | +using System.Globalization; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using FineCodeCoverage.Engine; |
| 7 | +using Microsoft.VisualStudio.Shell; |
| 8 | +using Microsoft.VisualStudio.Shell.Interop; |
| 9 | +using Task = System.Threading.Tasks.Task; |
| 10 | + |
| 11 | +namespace FineCodeCoverage.Output |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Command handler |
| 15 | + /// </summary> |
| 16 | + internal sealed class ClearUICommand |
| 17 | + { |
| 18 | + /// <summary> |
| 19 | + /// Command ID. |
| 20 | + /// </summary> |
| 21 | + public const int CommandId = 256; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Command menu group (command set GUID). |
| 25 | + /// </summary> |
| 26 | + public static readonly Guid CommandSet = new Guid("d58a999f-4a1b-42df-839a-cb31a0a4fed7"); |
| 27 | + |
| 28 | + private readonly IFCCEngine fccEngine; |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Initializes a new instance of the <see cref="ClearUICommand"/> class. |
| 32 | + /// Adds our command handlers for menu (commands must exist in the command table file) |
| 33 | + /// </summary> |
| 34 | + /// <param name="package">Owner package, not null.</param> |
| 35 | + /// <param name="commandService">Command service to add command to, not null.</param> |
| 36 | + private ClearUICommand(OleMenuCommandService commandService, IFCCEngine fccEngine) |
| 37 | + { |
| 38 | + this.fccEngine = fccEngine; |
| 39 | + commandService = commandService ?? throw new ArgumentNullException(nameof(commandService)); |
| 40 | + |
| 41 | + var menuCommandID = new CommandID(CommandSet, CommandId); |
| 42 | + var menuItem = new MenuCommand(this.Execute, menuCommandID); |
| 43 | + commandService.AddCommand(menuItem); |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Gets the instance of the command. |
| 48 | + /// </summary> |
| 49 | + public static ClearUICommand Instance |
| 50 | + { |
| 51 | + get; |
| 52 | + private set; |
| 53 | + } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Initializes the singleton instance of the command. |
| 57 | + /// </summary> |
| 58 | + /// <param name="package">Owner package, not null.</param> |
| 59 | + public static async Task InitializeAsync(AsyncPackage package, IFCCEngine fccEngine) |
| 60 | + { |
| 61 | + // Switch to the main thread - the call to AddCommand in ClearUICommand's constructor requires |
| 62 | + // the UI thread. |
| 63 | + await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken); |
| 64 | + |
| 65 | + OleMenuCommandService commandService = await package.GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService; |
| 66 | + Instance = new ClearUICommand(commandService, fccEngine); |
| 67 | + } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// This function is the callback used to execute the command when the menu item is clicked. |
| 71 | + /// See the constructor to see how the menu item is associated with this function using |
| 72 | + /// OleMenuCommandService service and MenuCommand class. |
| 73 | + /// </summary> |
| 74 | + /// <param name="sender">Event sender.</param> |
| 75 | + /// <param name="e">Event args.</param> |
| 76 | + private void Execute(object sender, EventArgs e) |
| 77 | + { |
| 78 | + ThreadHelper.ThrowIfNotOnUIThread(); |
| 79 | + fccEngine.ClearUI(); |
| 80 | + } |
| 81 | + |
| 82 | + } |
| 83 | +} |
0 commit comments