From d38ec53484c22da79d8d28417bdb5a03b419fa70 Mon Sep 17 00:00:00 2001 From: tom-englert Date: Fri, 16 Aug 2024 14:49:40 +0200 Subject: [PATCH] Decouple some global logic from the MainWindow --- Directory.Packages.props | 1 + ILSpy/Analyzers/AnalyzeCommand.cs | 61 +++++++++++++++++------------ ILSpy/Analyzers/AnalyzerTreeView.cs | 6 +++ ILSpy/ILSpy.csproj | 1 + ILSpy/MainWindow.xaml | 23 +---------- ILSpy/MainWindow.xaml.cs | 9 +---- ILSpy/Search/SearchPane.cs | 4 ++ ILSpy/Views/DebugSteps.xaml.cs | 5 +++ 8 files changed, 56 insertions(+), 54 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index cc543f59b1..0a153070f0 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -46,6 +46,7 @@ + diff --git a/ILSpy/Analyzers/AnalyzeCommand.cs b/ILSpy/Analyzers/AnalyzeCommand.cs index 854aa565cd..00e6b03dbb 100644 --- a/ILSpy/Analyzers/AnalyzeCommand.cs +++ b/ILSpy/Analyzers/AnalyzeCommand.cs @@ -16,21 +16,30 @@ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -using System; using System.ComponentModel.Composition; using System.Linq; +using System.Windows; -using ICSharpCode.Decompiler.Metadata; using ICSharpCode.Decompiler.TypeSystem; using ICSharpCode.ILSpy.Properties; using ICSharpCode.ILSpy.TreeNodes; +using TomsToolbox.Composition; + namespace ICSharpCode.ILSpy.Analyzers { [ExportContextMenuEntry(Header = nameof(Resources.Analyze), Icon = "Images/Search", Category = nameof(Resources.Analyze), InputGestureText = "Ctrl+R", Order = 100)] [PartCreationPolicy(CreationPolicy.Shared)] internal sealed class AnalyzeCommand : SimpleCommand, IContextMenuEntry { + private static readonly IExport analyzerTreeViewExport = App.ExportProvider.GetExports().Single(); + + private static AnalyzerTreeView AnalyzerTreeView { + get { + return Application.Current?.MainWindow?.IsLoaded != true ? null : analyzerTreeViewExport.Value; + } + } + public bool IsVisible(TextViewContext context) { if (context.TreeView is AnalyzerTreeView && context.SelectedTreeNodes != null && context.SelectedTreeNodes.All(n => n.Parent.IsRoot)) @@ -43,14 +52,12 @@ public bool IsVisible(TextViewContext context) public bool IsEnabled(TextViewContext context) { if (context.SelectedTreeNodes == null) - return context.Reference != null && context.Reference.Reference is IEntity; - foreach (IMemberTreeNode node in context.SelectedTreeNodes) { - if (!IsValidReference(node.Member)) - return false; + return context.Reference is { Reference: IEntity }; } - - return true; + return context.SelectedTreeNodes + .OfType() + .All(node => IsValidReference(node.Member)); } bool IsValidReference(object reference) @@ -60,52 +67,56 @@ bool IsValidReference(object reference) public void Execute(TextViewContext context) { - AnalyzerTreeView analyzerTreeView = MainWindow.Instance.AnalyzerTreeView; - if (analyzerTreeView == null) + if (AnalyzerTreeView is null) { return; } if (context.SelectedTreeNodes != null) { - foreach (IMemberTreeNode node in context.SelectedTreeNodes) + foreach (var node in context.SelectedTreeNodes.OfType().ToArray()) { - analyzerTreeView.Analyze(node.Member); + AnalyzerTreeView.Analyze(node.Member); } } - else if (context.Reference != null && context.Reference.Reference is IEntity entity) + else if (context.Reference is { Reference: IEntity entity }) { - analyzerTreeView.Analyze(entity); + AnalyzerTreeView.Analyze(entity); } } public override bool CanExecute(object parameter) { - AnalyzerTreeView analyzerTreeView = MainWindow.Instance.AnalyzerTreeView; - if (analyzerTreeView != null && analyzerTreeView.IsKeyboardFocusWithin) + if (AnalyzerTreeView is null) { - return analyzerTreeView.SelectedItems.OfType().All(n => n is IMemberTreeNode); + return false; } - else + + if (AnalyzerTreeView is { IsKeyboardFocusWithin: true }) { - return MainWindow.Instance.SelectedNodes.All(n => n is IMemberTreeNode); + return AnalyzerTreeView.SelectedItems.OfType().All(n => n is IMemberTreeNode); } + + return MainWindow.Instance.SelectedNodes.All(n => n is IMemberTreeNode); } public override void Execute(object parameter) { - AnalyzerTreeView analyzerTreeView = MainWindow.Instance.AnalyzerTreeView; - if (analyzerTreeView != null && analyzerTreeView.IsKeyboardFocusWithin) + if (AnalyzerTreeView is null) + { + return; + } + if (AnalyzerTreeView.IsKeyboardFocusWithin) { - foreach (IMemberTreeNode node in MainWindow.Instance.AnalyzerTreeView.SelectedItems.OfType().ToArray()) + foreach (var node in AnalyzerTreeView.SelectedItems.OfType().ToArray()) { - MainWindow.Instance.AnalyzerTreeView.Analyze(node.Member); + AnalyzerTreeView.Analyze(node.Member); } } else { - foreach (IMemberTreeNode node in MainWindow.Instance.SelectedNodes) + foreach (var node in MainWindow.Instance.SelectedNodes.OfType()) { - MainWindow.Instance.AnalyzerTreeView.Analyze(node.Member); + AnalyzerTreeView.Analyze(node.Member); } } } diff --git a/ILSpy/Analyzers/AnalyzerTreeView.cs b/ILSpy/Analyzers/AnalyzerTreeView.cs index 4e40239ea8..cdc8fcbdb2 100644 --- a/ILSpy/Analyzers/AnalyzerTreeView.cs +++ b/ILSpy/Analyzers/AnalyzerTreeView.cs @@ -19,6 +19,7 @@ using System; using System.Collections.Generic; using System.Collections.Specialized; +using System.ComponentModel.Composition; using System.Linq; using System.Windows; @@ -30,11 +31,16 @@ using ICSharpCode.ILSpy.Controls.TreeView; using ICSharpCode.ILSpyX.TreeView; +using TomsToolbox.Wpf.Composition.Mef; + namespace ICSharpCode.ILSpy.Analyzers { /// /// Analyzer tree view. /// + [DataTemplate(typeof(AnalyzerPaneModel))] + [PartCreationPolicy(CreationPolicy.Shared)] + [Export] public class AnalyzerTreeView : SharpTreeView { FilterSettings filterSettings; diff --git a/ILSpy/ILSpy.csproj b/ILSpy/ILSpy.csproj index b7657c8915..36a8e7b443 100644 --- a/ILSpy/ILSpy.csproj +++ b/ILSpy/ILSpy.csproj @@ -51,6 +51,7 @@ + diff --git a/ILSpy/MainWindow.xaml b/ILSpy/MainWindow.xaml index 1f505652bb..f2f4bac4f8 100644 --- a/ILSpy/MainWindow.xaml +++ b/ILSpy/MainWindow.xaml @@ -2,8 +2,8 @@ - - - - - - - - - - - - - - - - - - - diff --git a/ILSpy/MainWindow.xaml.cs b/ILSpy/MainWindow.xaml.cs index 26f04b11c8..b6f92d433c 100644 --- a/ILSpy/MainWindow.xaml.cs +++ b/ILSpy/MainWindow.xaml.cs @@ -31,7 +31,6 @@ using System.Windows.Controls; using System.Windows.Data; using System.Windows.Input; -using System.Windows.Interop; using System.Windows.Media; using System.Windows.Navigation; using System.Windows.Threading; @@ -98,15 +97,9 @@ public SharpTreeView AssemblyTreeView { } } - public AnalyzerTreeView AnalyzerTreeView { - get { - return !IsLoaded ? null : FindResource("AnalyzerTreeView") as AnalyzerTreeView; - } - } - public SearchPane SearchPane { get { - return FindResource("SearchPane") as SearchPane; + return App.ExportProvider.GetExportedValue(); } } diff --git a/ILSpy/Search/SearchPane.cs b/ILSpy/Search/SearchPane.cs index 2f7d25d3fd..7f920599fa 100644 --- a/ILSpy/Search/SearchPane.cs +++ b/ILSpy/Search/SearchPane.cs @@ -40,11 +40,15 @@ using ICSharpCode.ILSpyX.Extensions; using ICSharpCode.ILSpyX.Search; +using TomsToolbox.Wpf.Composition.Mef; + namespace ICSharpCode.ILSpy.Search { /// /// Search pane /// + [DataTemplate(typeof(SearchPaneModel))] + [PartCreationPolicy(CreationPolicy.Shared)] public partial class SearchPane : UserControl { const int MAX_RESULTS = 1000; diff --git a/ILSpy/Views/DebugSteps.xaml.cs b/ILSpy/Views/DebugSteps.xaml.cs index dbe96c3f28..2c3e2e2e21 100644 --- a/ILSpy/Views/DebugSteps.xaml.cs +++ b/ILSpy/Views/DebugSteps.xaml.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.ComponentModel.Composition; using System.Windows; using System.Windows.Controls; using System.Windows.Input; @@ -9,8 +10,12 @@ using ICSharpCode.ILSpy.Docking; using ICSharpCode.ILSpy.ViewModels; +using TomsToolbox.Wpf.Composition.Mef; + namespace ICSharpCode.ILSpy { + [DataTemplate(typeof(DebugStepsPaneModel))] + [PartCreationPolicy(CreationPolicy.NonShared)] public partial class DebugSteps : UserControl { static readonly ILAstWritingOptions writingOptions = new ILAstWritingOptions {