Skip to content

Commit

Permalink
Decouple some global logic from the MainWindow
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-englert committed Aug 19, 2024
1 parent b6ad02d commit d38ec53
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 54 deletions.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<PackageVersion Include="System.Resources.Extensions" Version="8.0.0" />
<PackageVersion Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
<PackageVersion Include="TomsToolbox.Wpf.Composition" Version="2.18.1" />
<PackageVersion Include="TomsToolbox.Wpf.Composition.Mef" Version="2.18.1" />
<PackageVersion Include="TomsToolbox.Wpf.Styles" Version="2.18.1" />
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
</ItemGroup>
Expand Down
61 changes: 36 additions & 25 deletions ILSpy/Analyzers/AnalyzeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AnalyzerTreeView> analyzerTreeViewExport = App.ExportProvider.GetExports<AnalyzerTreeView>().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))
Expand All @@ -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<IMemberTreeNode>()
.All(node => IsValidReference(node.Member));
}

bool IsValidReference(object reference)
Expand All @@ -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<IMemberTreeNode>().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<object>().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<object>().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<IMemberTreeNode>().ToArray())
foreach (var node in AnalyzerTreeView.SelectedItems.OfType<IMemberTreeNode>().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<IMemberTreeNode>())
{
MainWindow.Instance.AnalyzerTreeView.Analyze(node.Member);
AnalyzerTreeView.Analyze(node.Member);
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions ILSpy/Analyzers/AnalyzerTreeView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel.Composition;
using System.Linq;
using System.Windows;

Expand All @@ -30,11 +31,16 @@
using ICSharpCode.ILSpy.Controls.TreeView;
using ICSharpCode.ILSpyX.TreeView;

using TomsToolbox.Wpf.Composition.Mef;

namespace ICSharpCode.ILSpy.Analyzers
{
/// <summary>
/// Analyzer tree view.
/// </summary>
[DataTemplate(typeof(AnalyzerPaneModel))]
[PartCreationPolicy(CreationPolicy.Shared)]
[Export]
public class AnalyzerTreeView : SharpTreeView
{
FilterSettings filterSettings;
Expand Down
1 change: 1 addition & 0 deletions ILSpy/ILSpy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" />
<PackageReference Include="NaturalSort.Extension" />
<PackageReference Include="TomsToolbox.Wpf.Composition" />
<PackageReference Include="TomsToolbox.Wpf.Composition.Mef" />
<PackageReference Include="TomsToolbox.Wpf.Styles" />
</ItemGroup>

Expand Down
23 changes: 2 additions & 21 deletions ILSpy/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<Window
x:Class="ICSharpCode.ILSpy.MainWindow"
x:ClassModifier="public"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tv="clr-namespace:ICSharpCode.ILSpy.Controls.TreeView"
xmlns:local="clr-namespace:ICSharpCode.ILSpy"
xmlns:search="clr-namespace:ICSharpCode.ILSpy.Search"
Expand Down Expand Up @@ -67,29 +67,10 @@
</Style>
</tv:SharpTreeView.ItemContainerStyle>
</tv:SharpTreeView>

<DataTemplate DataType="{x:Type viewModels:AssemblyListPaneModel}">
<ContentControl Content="{StaticResource AssemblyTreeView}" />
</DataTemplate>

<local:DebugSteps x:Key="DebugSteps" />

<DataTemplate DataType="{x:Type viewModels:DebugStepsPaneModel}">
<ContentControl Content="{StaticResource DebugSteps}" />
</DataTemplate>

<search:SearchPane x:Key="SearchPane" />

<DataTemplate DataType="{x:Type viewModels:SearchPaneModel}">
<ContentControl Content="{StaticResource SearchPane}" />
</DataTemplate>

<analyzers:AnalyzerTreeView x:Key="AnalyzerTreeView" />

<DataTemplate DataType="{x:Type viewModels:AnalyzerPaneModel}">
<ContentControl Content="{StaticResource AnalyzerTreeView}" />
</DataTemplate>

<DataTemplate DataType="{x:Type viewModels:TabPageModel}">
<ContentPresenter Content="{Binding Content}" />
</DataTemplate>
Expand Down
9 changes: 1 addition & 8 deletions ILSpy/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<SearchPane>();
}
}

Expand Down
4 changes: 4 additions & 0 deletions ILSpy/Search/SearchPane.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,15 @@
using ICSharpCode.ILSpyX.Extensions;
using ICSharpCode.ILSpyX.Search;

using TomsToolbox.Wpf.Composition.Mef;

namespace ICSharpCode.ILSpy.Search
{
/// <summary>
/// Search pane
/// </summary>
[DataTemplate(typeof(SearchPaneModel))]
[PartCreationPolicy(CreationPolicy.Shared)]
public partial class SearchPane : UserControl
{
const int MAX_RESULTS = 1000;
Expand Down
5 changes: 5 additions & 0 deletions ILSpy/Views/DebugSteps.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
Expand All @@ -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 {
Expand Down

0 comments on commit d38ec53

Please sign in to comment.