| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- using Microsoft.VisualStudio.Shell;
- using System;
- using System.ComponentModel.Design;
- using Quadarax.Application.Tools.Vsix.FilteredSolutionsExtension.Views;
- using Quadarax.Application.Tools.Vsix.FilteredSolutionsExtension.Helpers;
- using Quadarax.Application.Tools.Vsix.FilteredSolutionsExtension.Models;
- namespace Quadarax.Application.Tools.Vsix.FilteredSolutionsExtension.Commands
- {
- internal sealed class ManualFilterCommand
- {
- public const int CommandId = 0x0101;
- public static readonly Guid CommandSet = new Guid("12345678-1234-1234-1234-123456789abd");
- private readonly AsyncPackage _package;
- private ManualFilterCommand(AsyncPackage package, OleMenuCommandService commandService)
- {
- _package = package ?? throw new ArgumentNullException(nameof(package));
- commandService = commandService ?? throw new ArgumentNullException(nameof(commandService));
- var menuCommandID = new CommandID(CommandSet, CommandId);
- var menuItem = new OleMenuCommand(Execute, menuCommandID);
- menuItem.BeforeQueryStatus += OnBeforeQueryStatus;
- commandService.AddCommand(menuItem);
- }
- public static ManualFilterCommand Instance { get; private set; }
- private IAsyncServiceProvider ServiceProvider => _package;
- public static async System.Threading.Tasks.Task InitializeAsync(AsyncPackage package)
- {
- await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken);
- var commandService = await package.GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService;
- Instance = new ManualFilterCommand(package, commandService);
- }
- private async void OnBeforeQueryStatus(object sender, EventArgs e)
- {
- await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
-
- var command = sender as OleMenuCommand;
- if (command != null)
- {
- var dte = await _package.GetServiceAsync(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
- command.Visible = command.Enabled =
- dte?.Solution != null &&
- !string.IsNullOrEmpty(dte.Solution.FullName) &&
- dte.Solution.Projects?.Count > 1;
- }
- }
- private async void Execute(object sender, EventArgs e)
- {
- await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
- try
- {
- DebugHelper.Log("Manual filter command executed");
-
- var dte = await _package.GetServiceAsync(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
- var solutionInfo = SolutionInfo.FromCurrentSolution(dte);
-
- if (solutionInfo != null)
- {
- DebugHelper.Log($"Opening filter dialog for: {solutionInfo.FullPath}");
-
- var dialog = new ProjectFilterDialog(solutionInfo);
- var result = dialog.ShowDialog();
- if (result == true && !string.IsNullOrEmpty(dialog.FilteredSolutionPath))
- {
- DebugHelper.Log($"Opening filtered solution: {dialog.FilteredSolutionPath}");
- dte.Solution.Close();
- dte.Solution.Open(dialog.FilteredSolutionPath);
- }
- else
- {
- DebugHelper.Log("Filter dialog cancelled or no filtered solution created");
- }
- }
- else
- {
- DebugHelper.ShowMessage("Solution Filter", "No solution is currently open.");
- }
- }
- catch (Exception ex)
- {
- DebugHelper.Log($"Error in manual filter command: {ex}");
- DebugHelper.ShowMessage("Error", $"Error filtering solution: {ex.Message}");
- }
- }
- }
- }
|