Skip to content

Commit

Permalink
Merge pull request #3283 from tom-englert/dev/WpfRefactoring
Browse files Browse the repository at this point in the history
Fix #3281: Help -> About is broken
  • Loading branch information
siegfriedpammer committed Sep 22, 2024
2 parents bf2cf93 + 5b8e7ce commit f105147
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 58 deletions.
106 changes: 48 additions & 58 deletions ILSpy/AssemblyTree/AssemblyTreeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@
using ICSharpCode.Decompiler.Documentation;
using ICSharpCode.Decompiler.TypeSystem.Implementation;
using System.Reflection.Metadata;
using System.Text;
using System.Windows.Navigation;

using ICSharpCode.ILSpy.AppEnv;
using ICSharpCode.ILSpy.Search;
using ICSharpCode.Decompiler;
using System.Text;

using TomsToolbox.Essentials;
using TomsToolbox.Wpf;
using System.Windows.Navigation;

namespace ICSharpCode.ILSpy.AssemblyTree
{
Expand All @@ -67,6 +67,7 @@ public class AssemblyTreeModel : ToolPaneModel
AssemblyListTreeNode assemblyListTreeNode;

readonly NavigationHistory<NavigationState> history = new();
private bool isNavigatingHistory;

public AssemblyTreeModel()
{
Expand All @@ -78,7 +79,7 @@ public AssemblyTreeModel()
MessageBus<NavigateToReferenceEventArgs>.Subscribers += JumpToReference;
MessageBus<SettingsChangedEventArgs>.Subscribers += (sender, e) => Settings_PropertyChanged(sender, e);

var selectionChangeThrottle = new DispatcherThrottle(DispatcherPriority.Background, TreeView_SelectionChanged);
var selectionChangeThrottle = new DispatcherThrottle(DispatcherPriority.Input, TreeView_SelectionChanged);
SelectedItems.CollectionChanged += (_, _) => selectionChangeThrottle.Tick();
}

Expand All @@ -94,8 +95,7 @@ private void Settings_PropertyChanged(object sender, PropertyChangedEventArgs e)
case nameof(SessionSettings.Theme):
// update syntax highlighting and force reload (AvalonEdit does not automatically refresh on highlighting change)
DecompilerTextView.RegisterHighlighting();
DecompileSelectedNodes(
DockWorkspace.Instance.ActiveTabPage.GetState() as DecompilerTextViewState);
DecompileSelectedNodes(DockWorkspace.Instance.ActiveTabPage.GetState() as DecompilerTextViewState);
break;
case nameof(SessionSettings.CurrentCulture):
MessageBox.Show(Properties.Resources.SettingsChangeRestartRequired, "ILSpy");
Expand All @@ -107,7 +107,7 @@ private void Settings_PropertyChanged(object sender, PropertyChangedEventArgs e)
switch (e.PropertyName)
{
case nameof(LanguageSettings.Language) or nameof(LanguageSettings.LanguageVersion):
DecompileSelectedNodes(recordHistory: false);
DecompileSelectedNodes();
break;
}
}
Expand Down Expand Up @@ -502,36 +502,26 @@ public void SelectNode(SharpTreeNode node, bool inNewTabPage = false)
}
}

internal void SelectNodes(IEnumerable<SharpTreeNode> nodes, bool ignoreCompilationRequests = false)
internal void SelectNodes(IEnumerable<SharpTreeNode> nodes)
{
this.ignoreDecompilationRequests = ignoreCompilationRequests;
// Ensure nodes exist
var nodesList = nodes.Select(n => FindNodeByPath(GetPathForNode(n), true))
.Where(n => n != null)
.ToArray();

try
if (!nodesList.Any() || nodesList.Any(n => n.AncestorsAndSelf().Any(a => a.IsHidden)))
{
// Ensure nodes exist
var nodesList = nodes.Select(n => FindNodeByPath(GetPathForNode(n), true))
.Where(n => n != null)
.ToArray();

if (!nodesList.Any() || nodesList.Any(n => n.AncestorsAndSelf().Any(a => a.IsHidden)))
{
return;
}

if (SelectedItems.SequenceEqual(nodesList))
{
Dispatcher.BeginInvoke(RefreshDecompiledView);
return;
}

SelectedItems.Clear();
SelectedItems.AddRange(nodesList);
return;
}
finally

if (SelectedItems.SequenceEqual(nodesList))
{
this.ignoreDecompilationRequests = false;
Dispatcher.BeginInvoke(RefreshDecompiledView);
return;
}

SelectedItems.Clear();
SelectedItems.AddRange(nodesList);
}

/// <summary>
Expand Down Expand Up @@ -700,15 +690,27 @@ public void LoadAssemblies(IEnumerable<string> fileNames, List<LoadedAssembly> l

void TreeView_SelectionChanged()
{
var delayDecompilationRequestDueToContextMenu = Mouse.RightButton == MouseButtonState.Pressed;

if (!delayDecompilationRequestDueToContextMenu)
{
DecompileSelectedNodes();
}
else
if (SelectedItems.Count > 0)
{
ContextMenuProvider.ContextMenuClosed += ContextMenuClosed;
if (!isNavigatingHistory)
{
var activeTabPage = DockWorkspace.Instance.ActiveTabPage;
var currentState = activeTabPage.GetState();
if (currentState != null)
history.UpdateCurrent(new NavigationState(activeTabPage, currentState));
history.Record(new NavigationState(activeTabPage, SelectedItems));
}

var delayDecompilationRequestDueToContextMenu = Mouse.RightButton == MouseButtonState.Pressed;

if (!delayDecompilationRequestDueToContextMenu)
{
DecompileSelectedNodes();
}
else
{
ContextMenuProvider.ContextMenuClosed += ContextMenuClosed;
}
}

MessageBus.Send(this, new AssemblyTreeSelectionChangedEventArgs());
Expand All @@ -728,23 +730,10 @@ void ContextMenuClosed(object sender, EventArgs e)
}
}

private bool ignoreDecompilationRequests;

public void DecompileSelectedNodes(DecompilerTextViewState newState = null, bool recordHistory = true)
private void DecompileSelectedNodes(DecompilerTextViewState newState = null)
{
if (ignoreDecompilationRequests)
return;

var activeTabPage = DockWorkspace.Instance.ActiveTabPage;

if (recordHistory)
{
var currentState = activeTabPage.GetState();
if (currentState != null)
history.UpdateCurrent(new NavigationState(activeTabPage, currentState));
history.Record(new NavigationState(activeTabPage, SelectedItems));
}

activeTabPage.SupportsLanguageSwitching = true;

if (SelectedItems.Count == 1)
Expand All @@ -754,7 +743,7 @@ public void DecompileSelectedNodes(DecompilerTextViewState newState = null, bool
}
if (newState?.ViewedUri != null)
{
MainWindow.Instance.AssemblyTreeModel.NavigateTo(new(newState.ViewedUri, null), recordHistory: false);
NavigateTo(new(newState.ViewedUri, null), recordHistory: false);
return;
}

Expand Down Expand Up @@ -782,6 +771,9 @@ public IEnumerable<ILSpyTreeNode> SelectedNodes {

public void NavigateHistory(bool forward)
{
isNavigatingHistory = true;
this.Dispatcher.BeginInvoke(DispatcherPriority.Background, () => isNavigatingHistory = false);

TabPageModel tabPage = DockWorkspace.Instance.ActiveTabPage;
var state = tabPage.GetState();
if (state != null)
Expand All @@ -790,8 +782,7 @@ public void NavigateHistory(bool forward)

DockWorkspace.Instance.ActiveTabPage = newState.TabPage;

SelectNodes(newState.TreeNodes, ignoreCompilationRequests: true);
DecompileSelectedNodes(newState.ViewState as DecompilerTextViewState, false);
SelectNodes(newState.TreeNodes);
}

public bool CanNavigateBack => history.CanNavigateBack;
Expand All @@ -814,6 +805,7 @@ internal void NavigateTo(RequestNavigateEventArgs e, bool recordHistory = true,
e.Handled = true;
return;
}

AvalonEditTextOutput output = new AvalonEditTextOutput {
Address = e.Uri,
Title = e.Uri.AbsolutePath,
Expand Down Expand Up @@ -845,7 +837,7 @@ void RecordHistory()
if (currentState != null)
history.UpdateCurrent(new NavigationState(tabPage, currentState));

UnselectAll(ignoreCompilationRequests: true);
UnselectAll();

history.Record(new NavigationState(tabPage, new ViewState { ViewedUri = e.Uri }));
}
Expand All @@ -861,11 +853,9 @@ public void Refresh()
}
}

public void UnselectAll(bool ignoreCompilationRequests = false)
private void UnselectAll()
{
this.ignoreDecompilationRequests = ignoreCompilationRequests;
SelectedItems.Clear();
this.ignoreDecompilationRequests = false;
}

public IEnumerable<SharpTreeNode> GetTopLevelSelection()
Expand Down
1 change: 1 addition & 0 deletions ILSpy/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

<Window.InputBindings>
<KeyBinding Key="R" Modifiers="Control" Command="{x:Static local:ILSpyCommands.Analyze}" />
<KeyBinding Key="Z" Modifiers="Control" Command="{x:Static NavigationCommands.BrowseBack}" />
</Window.InputBindings>

<Window.TaskbarItemInfo>
Expand Down

0 comments on commit f105147

Please sign in to comment.