ManualFilterCommand.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using Microsoft.VisualStudio.Shell;
  2. using System;
  3. using System.ComponentModel.Design;
  4. using Quadarax.Application.Tools.Vsix.FilteredSolutionsExtension.Views;
  5. using Quadarax.Application.Tools.Vsix.FilteredSolutionsExtension.Helpers;
  6. using Quadarax.Application.Tools.Vsix.FilteredSolutionsExtension.Models;
  7. namespace Quadarax.Application.Tools.Vsix.FilteredSolutionsExtension.Commands
  8. {
  9. internal sealed class ManualFilterCommand
  10. {
  11. public const int CommandId = 0x0101;
  12. public static readonly Guid CommandSet = new Guid("12345678-1234-1234-1234-123456789abd");
  13. private readonly AsyncPackage _package;
  14. private ManualFilterCommand(AsyncPackage package, OleMenuCommandService commandService)
  15. {
  16. _package = package ?? throw new ArgumentNullException(nameof(package));
  17. commandService = commandService ?? throw new ArgumentNullException(nameof(commandService));
  18. var menuCommandID = new CommandID(CommandSet, CommandId);
  19. var menuItem = new OleMenuCommand(Execute, menuCommandID);
  20. menuItem.BeforeQueryStatus += OnBeforeQueryStatus;
  21. commandService.AddCommand(menuItem);
  22. }
  23. public static ManualFilterCommand Instance { get; private set; }
  24. private IAsyncServiceProvider ServiceProvider => _package;
  25. public static async System.Threading.Tasks.Task InitializeAsync(AsyncPackage package)
  26. {
  27. await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken);
  28. var commandService = await package.GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService;
  29. Instance = new ManualFilterCommand(package, commandService);
  30. }
  31. private async void OnBeforeQueryStatus(object sender, EventArgs e)
  32. {
  33. await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
  34. var command = sender as OleMenuCommand;
  35. if (command != null)
  36. {
  37. var dte = await _package.GetServiceAsync(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
  38. command.Visible = command.Enabled =
  39. dte?.Solution != null &&
  40. !string.IsNullOrEmpty(dte.Solution.FullName) &&
  41. dte.Solution.Projects?.Count > 1;
  42. }
  43. }
  44. private async void Execute(object sender, EventArgs e)
  45. {
  46. await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
  47. try
  48. {
  49. DebugHelper.Log("Manual filter command executed");
  50. var dte = await _package.GetServiceAsync(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
  51. var solutionInfo = SolutionInfo.FromCurrentSolution(dte);
  52. if (solutionInfo != null)
  53. {
  54. DebugHelper.Log($"Opening filter dialog for: {solutionInfo.FullPath}");
  55. var dialog = new ProjectFilterDialog(solutionInfo);
  56. var result = dialog.ShowDialog();
  57. if (result == true && !string.IsNullOrEmpty(dialog.FilteredSolutionPath))
  58. {
  59. DebugHelper.Log($"Opening filtered solution: {dialog.FilteredSolutionPath}");
  60. dte.Solution.Close();
  61. dte.Solution.Open(dialog.FilteredSolutionPath);
  62. }
  63. else
  64. {
  65. DebugHelper.Log("Filter dialog cancelled or no filtered solution created");
  66. }
  67. }
  68. else
  69. {
  70. DebugHelper.ShowMessage("Solution Filter", "No solution is currently open.");
  71. }
  72. }
  73. catch (Exception ex)
  74. {
  75. DebugHelper.Log($"Error in manual filter command: {ex}");
  76. DebugHelper.ShowMessage("Error", $"Error filtering solution: {ex.Message}");
  77. }
  78. }
  79. }
  80. }