// Copyright (C) 2025 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 using System; using System.Collections.Generic; using System.ComponentModel.Design; using Microsoft.VisualStudio.Shell; namespace QtVsTools { using Core; using Core.MsBuild; using VisualStudio; /// /// Command handler /// internal sealed class QtSolutionContextMenu { /// /// Gets the instance of the command. /// private static QtSolutionContextMenu Instance { get; set; } /// /// Initializes the singleton instance of the command. /// public static void Initialize() { Instance = new QtSolutionContextMenu(); } /// /// Command ID. /// TODO: Remove, take form QtMenus.Package /// private enum CommandId { LUpdateOnSolution = QtMenus.Package.lUpdateOnSolution, LReleaseOnSolution = QtMenus.Package.lReleaseOnSolution, SolutionConvertToQtMsBuild = QtMenus.Package.SolutionConvertToQtMsBuild, SolutionEnableProjectTracking = QtMenus.Package.SolutionEnableProjectTracking } /// /// Initializes a new instance of the class. /// Adds our command handlers for menu (commands must exist in the command table file) /// private QtSolutionContextMenu() { var commandService = VsServiceProvider .GetService(); if (commandService == null) return; foreach (int id in Enum.GetValues(typeof(CommandId))) { var command = new OleMenuCommand(ExecHandler, new CommandID(QtMenus.Package.Guid, id)); commandService.AddCommand(command); } } private static void ExecHandler(object sender, EventArgs e) { ThreadHelper.ThrowIfNotOnUIThread(); if (sender is not OleMenuCommand command) return; var properties = new Dictionary { {"Command", Enum.GetName(typeof(CommandId), command.CommandID.ID)} }; Telemetry.TrackEvent(typeof(QtSolutionContextMenu) + ".ExecHandler", properties); var dte = QtVsToolsPackage.Instance.Dte; switch (command.CommandID.ID) { case QtMenus.Package.lUpdateOnSolution: Translation.RunLUpdate(QtVsToolsPackage.Instance.Dte.Solution); break; case QtMenus.Package.lReleaseOnSolution: Translation.RunLRelease(QtVsToolsPackage.Instance.Dte.Solution); break; case QtMenus.Package.SolutionConvertToQtMsBuild: MsBuildProjectConverter.SolutionToQtMsBuild(); break; case QtMenus.Package.SolutionEnableProjectTracking: foreach (var vcProject in HelperFunctions.ProjectsInSolution(dte)) MsBuildProject.GetOrAdd(vcProject); break; } } } }