microsoft/TypeAgent
Publicmirrored fromhttps://github.com/microsoft/TypeAgentAvailable
dotnet/visualStudioTypeAgent/ChatToolWindowCommand.cs
55lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System; |
| 5 | using System.ComponentModel.Design; |
| 6 | using Microsoft.VisualStudio.Shell; |
| 7 | using Microsoft.VisualStudio.Shell.Interop; |
| 8 | using Microsoft.VisualStudio.Threading; |
| 9 | using Task = System.Threading.Tasks.Task; |
| 10 | |
| 11 | namespace Microsoft.TypeAgent.VisualStudio; |
| 12 | |
| 13 | internal sealed class ChatToolWindowCommand |
| 14 | { |
| 15 | public const int CommandId = 0x0100; |
| 16 | public static readonly Guid CommandSet = new Guid("c2a30f9d-8c64-4f3e-ad2a-af2f3b4e6f12"); |
| 17 | |
| 18 | private readonly AsyncPackage _package; |
| 19 | |
| 20 | private ChatToolWindowCommand(AsyncPackage package, OleMenuCommandService commandService) |
| 21 | { |
| 22 | _package = package ?? throw new ArgumentNullException(nameof(package)); |
| 23 | commandService = commandService ?? throw new ArgumentNullException(nameof(commandService)); |
| 24 | |
| 25 | var menuCommandID = new CommandID(CommandSet, CommandId); |
| 26 | var menuItem = new MenuCommand(Execute, menuCommandID); |
| 27 | commandService.AddCommand(menuItem); |
| 28 | } |
| 29 | |
| 30 | public static async Task InitializeAsync(AsyncPackage package) |
| 31 | { |
| 32 | await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken); |
| 33 | var commandService = (OleMenuCommandService?)await package.GetServiceAsync(typeof(IMenuCommandService)); |
| 34 | if (commandService is null) |
| 35 | { |
| 36 | return; |
| 37 | } |
| 38 | _ = new ChatToolWindowCommand(package, commandService); |
| 39 | } |
| 40 | |
| 41 | private void Execute(object sender, EventArgs e) |
| 42 | { |
| 43 | _package.JoinableTaskFactory.RunAsync(async () => |
| 44 | { |
| 45 | // Marshal to the UI thread explicitly so the VSTHRD010 analyzer |
| 46 | // can see the IVsWindowFrame access is main-thread-safe. |
| 47 | await _package.JoinableTaskFactory.SwitchToMainThreadAsync(_package.DisposalToken); |
| 48 | var window = await _package.ShowToolWindowAsync(typeof(ChatToolWindow), 0, create: true, cancellationToken: _package.DisposalToken); |
| 49 | if (window?.Frame is IVsWindowFrame frame) |
| 50 | { |
| 51 | Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(frame.Show()); |
| 52 | } |
| 53 | }).FileAndForget("typeagent/vsix/show-tool-window"); |
| 54 | } |
| 55 | } |
| 56 | |