From 44aa41d933be3d5f2e50d8996486368876ea71f8 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Sat, 14 May 2022 10:39:48 +0200 Subject: [PATCH] Fix #2686: Introduce ToolPaneModel.AssociatedCommand to allow overriding Window-menu action. --- ILSpy/Analyzers/AnalyzeCommand.cs | 17 ++++++++++++----- ILSpy/MainWindow.xaml.cs | 4 ++-- ILSpy/ViewModels/AnalyzerPaneModel.cs | 1 + ILSpy/ViewModels/ToolPaneModel.cs | 2 ++ 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/ILSpy/Analyzers/AnalyzeCommand.cs b/ILSpy/Analyzers/AnalyzeCommand.cs index 71a041539..e221235e8 100644 --- a/ILSpy/Analyzers/AnalyzeCommand.cs +++ b/ILSpy/Analyzers/AnalyzeCommand.cs @@ -58,24 +58,30 @@ namespace ICSharpCode.ILSpy.Analyzers public void Execute(TextViewContext context) { + AnalyzerTreeView analyzerTreeView = MainWindow.Instance.AnalyzerTreeView; + if (analyzerTreeView == null) + { + return; + } if (context.SelectedTreeNodes != null) { foreach (IMemberTreeNode node in context.SelectedTreeNodes) { - MainWindow.Instance.AnalyzerTreeView.Analyze(node.Member); + analyzerTreeView.Analyze(node.Member); } } else if (context.Reference != null && context.Reference.Reference is IEntity entity) { - MainWindow.Instance.AnalyzerTreeView.Analyze(entity); + analyzerTreeView.Analyze(entity); } } public override bool CanExecute(object parameter) { - if (MainWindow.Instance.AnalyzerTreeView.IsKeyboardFocusWithin) + AnalyzerTreeView analyzerTreeView = MainWindow.Instance.AnalyzerTreeView; + if (analyzerTreeView != null && analyzerTreeView.IsKeyboardFocusWithin) { - return MainWindow.Instance.AnalyzerTreeView.SelectedItems.OfType().All(n => n is IMemberTreeNode); + return analyzerTreeView.SelectedItems.OfType().All(n => n is IMemberTreeNode); } else { @@ -85,7 +91,8 @@ namespace ICSharpCode.ILSpy.Analyzers public override void Execute(object parameter) { - if (MainWindow.Instance.AnalyzerTreeView.IsKeyboardFocusWithin) + AnalyzerTreeView analyzerTreeView = MainWindow.Instance.AnalyzerTreeView; + if (analyzerTreeView != null && analyzerTreeView.IsKeyboardFocusWithin) { foreach (IMemberTreeNode node in MainWindow.Instance.AnalyzerTreeView.SelectedItems.OfType().ToArray()) { diff --git a/ILSpy/MainWindow.xaml.cs b/ILSpy/MainWindow.xaml.cs index c6331c4ec..69920b960 100644 --- a/ILSpy/MainWindow.xaml.cs +++ b/ILSpy/MainWindow.xaml.cs @@ -89,7 +89,7 @@ namespace ICSharpCode.ILSpy public AnalyzerTreeView AnalyzerTreeView { get { - return FindResource("AnalyzerTreeView") as AnalyzerTreeView; + return !IsLoaded ? null : FindResource("AnalyzerTreeView") as AnalyzerTreeView; } } @@ -428,7 +428,7 @@ namespace ICSharpCode.ILSpy MenuItem CreateMenuItem(ToolPaneModel pane) { MenuItem menuItem = new MenuItem(); - menuItem.Command = new ToolPaneCommand(pane.ContentId); + menuItem.Command = pane.AssociatedCommand ?? new ToolPaneCommand(pane.ContentId); menuItem.Header = pane.Title; menuItem.Tag = pane; var shortcutKey = pane.ShortcutKey; diff --git a/ILSpy/ViewModels/AnalyzerPaneModel.cs b/ILSpy/ViewModels/AnalyzerPaneModel.cs index d86558ee6..a91f27e3f 100644 --- a/ILSpy/ViewModels/AnalyzerPaneModel.cs +++ b/ILSpy/ViewModels/AnalyzerPaneModel.cs @@ -31,6 +31,7 @@ namespace ICSharpCode.ILSpy.ViewModels ContentId = PaneContentId; Title = Properties.Resources.Analyze; ShortcutKey = new KeyGesture(Key.R, ModifierKeys.Control); + AssociatedCommand = ILSpyCommands.Analyze; } public override DataTemplate Template => (DataTemplate)MainWindow.Instance.FindResource("AnalyzerPaneTemplate"); diff --git a/ILSpy/ViewModels/ToolPaneModel.cs b/ILSpy/ViewModels/ToolPaneModel.cs index 547385b1b..15a79250b 100644 --- a/ILSpy/ViewModels/ToolPaneModel.cs +++ b/ILSpy/ViewModels/ToolPaneModel.cs @@ -34,5 +34,7 @@ namespace ICSharpCode.ILSpy.ViewModels public KeyGesture ShortcutKey { get; protected set; } public string Icon { get; protected set; } + + public ICommand AssociatedCommand { get; set; } } }