|
| 1 | +using JeffPires.VisualChatGPTStudio.ToolWindows; |
| 2 | +using Microsoft.VisualStudio.Shell; |
| 3 | +using System; |
| 4 | +using System.ComponentModel.Design; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +namespace JeffPires.VisualChatGPTStudio.Commands |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Command handler |
| 11 | + /// </summary> |
| 12 | + internal sealed class TerminalWindowCodeReviewCommand |
| 13 | + { |
| 14 | + /// <summary> |
| 15 | + /// Command ID. |
| 16 | + /// </summary> |
| 17 | + public const int CommandId = 259; |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Command menu group (command set GUID). |
| 21 | + /// </summary> |
| 22 | + public static readonly Guid CommandSet = new("8b0b1a54-4655-4dae-8984-022f82a739f2"); |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// VS Package that provides this command, not null. |
| 26 | + /// </summary> |
| 27 | + private readonly AsyncPackage package; |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// This field holds a reference to the TerminalWindow object. |
| 31 | + /// </summary> |
| 32 | + private static TerminalWindowCodeReview window; |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Initializes a new instance of the <see cref="TerminalWindowCommand"/> class. |
| 36 | + /// Adds our command handlers for menu (commands must exist in the command table file) |
| 37 | + /// </summary> |
| 38 | + /// <param name="package">Owner package, not null.</param> |
| 39 | + /// <param name="commandService">Command service to add command to, not null.</param> |
| 40 | + private TerminalWindowCodeReviewCommand(AsyncPackage package, OleMenuCommandService commandService) |
| 41 | + { |
| 42 | + this.package = package ?? throw new ArgumentNullException(nameof(package)); |
| 43 | + commandService = commandService ?? throw new ArgumentNullException(nameof(commandService)); |
| 44 | + |
| 45 | + CommandID menuCommandID = new(CommandSet, CommandId); |
| 46 | + MenuCommand menuItem = new(this.Execute, menuCommandID); |
| 47 | + commandService.AddCommand(menuItem); |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Gets the instance of the command. |
| 52 | + /// </summary> |
| 53 | + public static TerminalWindowCodeReviewCommand Instance |
| 54 | + { |
| 55 | + get; |
| 56 | + private set; |
| 57 | + } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Initializes the singleton instance of the command. |
| 61 | + /// </summary> |
| 62 | + /// <param name="package">Owner package, not null.</param> |
| 63 | + public static async System.Threading.Tasks.Task InitializeAsync(AsyncPackage package) |
| 64 | + { |
| 65 | + // Switch to the main thread - the call to AddCommand in TerminalWindowCommand's constructor requires |
| 66 | + // the UI thread. |
| 67 | + await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken); |
| 68 | + |
| 69 | + OleMenuCommandService commandService = await package.GetServiceAsync((typeof(IMenuCommandService))) as OleMenuCommandService; |
| 70 | + Instance = new TerminalWindowCodeReviewCommand(package, commandService); |
| 71 | + |
| 72 | + await InitializeToolWindowAsync(package); |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Shows the tool window when the menu item is clicked. |
| 77 | + /// </summary> |
| 78 | + /// <param name="sender">The event sender.</param> |
| 79 | + /// <param name="e">The event args.</param> |
| 80 | + private void Execute(object sender, EventArgs e) |
| 81 | + { |
| 82 | + _ = this.package.JoinableTaskFactory.RunAsync(async delegate |
| 83 | + { |
| 84 | + await package.ShowToolWindowAsync(typeof(TerminalWindowCodeReview), 0, true, package.DisposalToken); |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Initializes the ToolWindow with the specified <paramref name="package"/>. |
| 90 | + /// </summary> |
| 91 | + /// <param name="package">The AsyncPackage to be initialized.</param> |
| 92 | + /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> |
| 93 | + private static async System.Threading.Tasks.Task InitializeToolWindowAsync(AsyncPackage package) |
| 94 | + { |
| 95 | + window = await package.FindToolWindowAsync(typeof(TerminalWindowCodeReview), 0, true, package.DisposalToken) as TerminalWindowCodeReview; |
| 96 | + |
| 97 | + if ((null == window) || (null == window.Frame)) |
| 98 | + { |
| 99 | + throw new NotSupportedException("Cannot create tool window"); |
| 100 | + } |
| 101 | + |
| 102 | + window.SetTerminalWindowProperties(((VisuallChatGPTStudioPackage)package).OptionsGeneral, package); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments