using Microsoft.VisualStudio.Shell; using Microsoft.VisualStudio.Shell.Interop; using PortingAssistantVSExtensionClient.Options; using PortingAssistantVSExtensionClient.Utils; using System; using System.ComponentModel.Design; using System.Globalization; using System.Threading; using System.Threading.Tasks; using Task = System.Threading.Tasks.Task; namespace PortingAssistantVSExtensionClient.Commands { /// /// Command handler /// internal sealed class AutoAssessmentCommand { /// /// Command ID. /// public const int CommandId = PackageIds.cmdidAutoAssessmentCommand; /// /// Command menu group (command set GUID). /// public static readonly Guid CommandSet = new Guid(PackageGuids.guidPortingAssistantVSExtensionClientPackageCmdSetString); /// /// VS Package that provides this command, not null. /// private readonly AsyncPackage package; /// /// Initializes a new instance of the class. /// Adds our command handlers for menu (commands must exist in the command table file) /// /// Owner package, not null. /// Command service to add command to, not null. private AutoAssessmentCommand(AsyncPackage package, OleMenuCommandService commandService) { this.package = package ?? throw new ArgumentNullException(nameof(package)); commandService = commandService ?? throw new ArgumentNullException(nameof(commandService)); var menuCommandID = new CommandID(CommandSet, CommandId); var menuItem = new OleMenuCommand(this.Execute, menuCommandID); menuItem.BeforeQueryStatus += MenuItem_BeforeQueryStatus; commandService.AddCommand(menuItem); } private static void MenuItem_BeforeQueryStatus(object sender, EventArgs e) { var button = (OleMenuCommand)sender; button.Checked = UserSettings.Instance.EnabledContinuousAssessment; } /// /// Gets the instance of the command. /// public static AutoAssessmentCommand Instance { get; private set; } /// /// Gets the service provider from the owner package. /// private Microsoft.VisualStudio.Shell.IAsyncServiceProvider ServiceProvider { get { return this.package; } } /// /// Initializes the singleton instance of the command. /// /// Owner package, not null. public static async Task InitializeAsync(AsyncPackage package) { // Switch to the main thread - the call to AddCommand in AutoAssessmentCommand's constructor requires // the UI thread. await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken); OleMenuCommandService commandService = await package.GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService; Instance = new AutoAssessmentCommand(package, commandService); } /// /// This function is the callback used to execute the command when the menu item is clicked. /// See the constructor to see how the menu item is associated with this function using /// OleMenuCommandService service and MenuCommand class. /// /// Event sender. /// Event args. private void Execute(object sender, EventArgs e) { try { CommandsCommon.SetupPage(); var button = (OleMenuCommand)sender; UserSettings.Instance.EnabledContinuousAssessment = !button.Checked; UserSettings.Instance.UpdateContinuousAssessment(); PortingAssistantLanguageClient.UpdateUserSettingsAsync(); } catch(Exception ex) { NotificationUtils.ShowErrorMessageBox(this.package, ex.Message, "Setting Failed"); } } } }