Skip to content

Commit

Permalink
Refactor AnalyzerTreeView into View/ViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-englert committed Aug 19, 2024
1 parent 9fb3400 commit d4df855
Show file tree
Hide file tree
Showing 11 changed files with 250 additions and 218 deletions.
27 changes: 10 additions & 17 deletions ILSpy/Analyzers/AnalyzeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ 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
internal sealed class AnalyzeContextMenuCommand : IContextMenuEntry
{
private static readonly AnalyzerTreeView AnalyzerTreeView = App.ExportProvider.GetExportedValue<AnalyzerTreeView>();
private static readonly AnalyzerTreeViewModel AnalyzerTreeView = App.ExportProvider.GetExportedValue<AnalyzerTreeViewModel>();

public bool IsVisible(TextViewContext context)
{
Expand Down Expand Up @@ -70,29 +70,22 @@ public void Execute(TextViewContext context)
AnalyzerTreeView.Analyze(entity);
}
}
}

internal sealed class AnalyzeCommand : SimpleCommand
{
private static readonly AnalyzerTreeViewModel AnalyzerTreeView = App.ExportProvider.GetExportedValue<AnalyzerTreeViewModel>();

public override bool CanExecute(object parameter)
{
return AnalyzerTreeView.IsKeyboardFocusWithin
? AnalyzerTreeView.SelectedItems.OfType<object>().All(n => n is IMemberTreeNode)
: MainWindow.Instance.SelectedNodes.All(n => n is IMemberTreeNode);
return MainWindow.Instance.SelectedNodes.All(n => n is IMemberTreeNode);
}

public override void Execute(object parameter)
{
if (AnalyzerTreeView.IsKeyboardFocusWithin)
foreach (var node in MainWindow.Instance.SelectedNodes.OfType<IMemberTreeNode>())
{
foreach (var node in AnalyzerTreeView.SelectedItems.OfType<IMemberTreeNode>().ToArray())
{
AnalyzerTreeView.Analyze(node.Member);
}
}
else
{
foreach (var node in MainWindow.Instance.SelectedNodes.OfType<IMemberTreeNode>())
{
AnalyzerTreeView.Analyze(node.Member);
}
AnalyzerTreeView.Analyze(node.Member);
}
}
}
Expand Down
42 changes: 42 additions & 0 deletions ILSpy/Analyzers/AnalyzerRootNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;

using ICSharpCode.ILSpy.Util;
using ICSharpCode.ILSpyX;
using ICSharpCode.ILSpyX.TreeView;

namespace ICSharpCode.ILSpy.Analyzers;

public sealed class AnalyzerRootNode : AnalyzerTreeNode
{
public AnalyzerRootNode()
{
MessageBus<CurrentAssemblyListChangedEventArgs>.Subscribers += (sender, e) => CurrentAssemblyList_Changed(sender, e);
}

void CurrentAssemblyList_Changed(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Reset)
{
this.Children.Clear();
}
else
{
var removedAssemblies = e.OldItems?.Cast<LoadedAssembly>().ToArray() ?? [];
var addedAssemblies = e.NewItems?.Cast<LoadedAssembly>().ToArray() ?? [];

HandleAssemblyListChanged(removedAssemblies, addedAssemblies);
}
}

public override bool HandleAssemblyListChanged(ICollection<LoadedAssembly> removedAssemblies, ICollection<LoadedAssembly> addedAssemblies)
{
this.Children.RemoveAll(
delegate (SharpTreeNode n) {
AnalyzerTreeNode an = n as AnalyzerTreeNode;
return an == null || !an.HandleAssemblyListChanged(removedAssemblies, addedAssemblies);
});
return true;
}
}
158 changes: 0 additions & 158 deletions ILSpy/Analyzers/AnalyzerTreeView.cs

This file was deleted.

23 changes: 23 additions & 0 deletions ILSpy/Analyzers/AnalyzerTreeView.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<treeView:SharpTreeView
x:Class="ICSharpCode.ILSpy.Analyzers.AnalyzerTreeView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:toms="urn:TomsToolbox"
xmlns:treeView="clr-namespace:ICSharpCode.ILSpy.Controls.TreeView"
xmlns:analyzers="clr-namespace:ICSharpCode.ILSpy.Analyzers"
mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"
d:DataContext="{d:DesignInstance analyzers:AnalyzerTreeViewModel}"
ShowRoot="False"
BorderThickness="0"
Root="{Binding Root}"
toms:MultiSelectorExtensions.SelectionBinding="{Binding SelectedItems}"
SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
SelectionChanged="AnalyzerTreeView_OnSelectionChanged">

<UIElement.InputBindings>
<KeyBinding Key="R" Modifiers="Control" Command="{Binding AnalyzeCommand}" />
</UIElement.InputBindings>

</treeView:SharpTreeView>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019 AlphaSierraPapa for the SharpDevelop Team
// Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
Expand All @@ -17,22 +17,34 @@
// DEALINGS IN THE SOFTWARE.

using System.ComponentModel.Composition;
using System.Windows.Input;
using System.Windows.Controls;

namespace ICSharpCode.ILSpy.ViewModels
using ICSharpCode.ILSpyX.TreeView;

using TomsToolbox.Wpf.Composition.Mef;

namespace ICSharpCode.ILSpy.Analyzers
{
[ExportToolPane]
[PartCreationPolicy(CreationPolicy.Shared)]
public class AnalyzerPaneModel : ToolPaneModel
/// <summary>
/// Interaction logic for AnalyzerTreeView.xaml
/// </summary>
[DataTemplate(typeof(AnalyzerTreeViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
[Export]
public partial class AnalyzerTreeView
{
public const string PaneContentId = "analyzerPane";
public AnalyzerTreeView()
{
InitializeComponent();
ContextMenuProvider.Add(this);
}

public AnalyzerPaneModel()
private void AnalyzerTreeView_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
ContentId = PaneContentId;
Title = Properties.Resources.Analyze;
ShortcutKey = new KeyGesture(Key.R, ModifierKeys.Control);
AssociatedCommand = ILSpyCommands.Analyze;
if (SelectedItem is SharpTreeNode sharpTreeNode)
{
FocusNode(sharpTreeNode);
}
}
}
}
Loading

0 comments on commit d4df855

Please sign in to comment.