Skip to content

Commit

Permalink
Fix navigation history
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-englert committed Sep 22, 2024
1 parent 4b7377a commit 5b8e7ce
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
38 changes: 21 additions & 17 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 @@ -692,6 +692,15 @@ void TreeView_SelectionChanged()
{
if (SelectedItems.Count > 0)
{
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)
Expand Down Expand Up @@ -721,18 +730,10 @@ void ContextMenuClosed(object sender, EventArgs e)
}
}

public void DecompileSelectedNodes(DecompilerTextViewState newState = null, bool recordHistory = true)
private void DecompileSelectedNodes(DecompilerTextViewState newState = null)
{
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 @@ -742,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 @@ -770,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 @@ -779,7 +783,6 @@ public void NavigateHistory(bool forward)
DockWorkspace.Instance.ActiveTabPage = newState.TabPage;

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

public bool CanNavigateBack => history.CanNavigateBack;
Expand All @@ -802,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
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 5b8e7ce

Please sign in to comment.