Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #3284: Focus, Tabs and Title issues #3285

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions ILSpy/AssemblyTree/AssemblyListPane.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.ComponentModel.Composition;
using System.Windows;

using ICSharpCode.ILSpyX.TreeView;
using System.Windows.Threading;

using TomsToolbox.Wpf.Composition.Mef;

Expand Down Expand Up @@ -31,13 +30,16 @@ protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
return;

model.SetActiveView(this);
}
else if (e.Property == SelectedItemProperty)
{
if (e.NewValue is not SharpTreeNode treeNode)
return;

FocusNode(treeNode);
// If there is already a selected item in the model, we need to scroll it into view, so it can be selected in the UI.
var selected = model.SelectedItem;
if (selected != null)
{
this.Dispatcher.BeginInvoke(DispatcherPriority.Background, () => {
ScrollIntoView(selected);
this.SelectedItem = selected;
});
}
}
}
}
Expand Down
68 changes: 47 additions & 21 deletions ILSpy/AssemblyTree/AssemblyTreeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -418,17 +418,22 @@ void ShowAssemblyList(AssemblyList assemblyList)

Root = assemblyListTreeNode;

var mainWindow = Application.Current?.MainWindow;

if (mainWindow == null)
return;

if (assemblyList.ListName == AssemblyListManager.DefaultListName)
#if DEBUG
this.Title = $"ILSpy {DecompilerVersionInfo.FullVersion}";
mainWindow.Title = $"ILSpy {DecompilerVersionInfo.FullVersion}";
#else
this.Title = "ILSpy";
mainWindow.Title = "ILSpy";
#endif
else
#if DEBUG
this.Title = $"ILSpy {DecompilerVersionInfo.FullVersion} - " + assemblyList.ListName;
mainWindow.Title = $"ILSpy {DecompilerVersionInfo.FullVersion} - " + assemblyList.ListName;
#else
this.Title = "ILSpy - " + assemblyList.ListName;
mainWindow.Title = "ILSpy - " + assemblyList.ListName;
#endif
}

Expand Down Expand Up @@ -506,7 +511,7 @@ internal void SelectNodes(IEnumerable<SharpTreeNode> nodes)
{
// Ensure nodes exist
var nodesList = nodes.Select(n => FindNodeByPath(GetPathForNode(n), true))
.Where(n => n != null)
.ExceptNullItems()
.ToArray();

if (!nodesList.Any() || nodesList.Any(n => n.AncestorsAndSelf().Any(a => a.IsHidden)))
Expand All @@ -516,12 +521,22 @@ internal void SelectNodes(IEnumerable<SharpTreeNode> nodes)

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

SelectedItems.Clear();
SelectedItems.AddRange(nodesList);
if (this.isNavigatingHistory)
{
SelectedItems.Clear();
SelectedItems.AddRange(nodesList);
}
else
{
// defer selection change, so it does not interfere with the focus of the tab page.
Dispatcher.BeginInvoke(() => {
SelectedItems.Clear();
SelectedItems.AddRange(nodesList);
});
}
}

/// <summary>
Expand All @@ -543,10 +558,8 @@ public SharpTreeNode FindNodeByPath(string[] path, bool returnBestMatch)
ilSpyTreeNode.EnsureChildrenFiltered();
node = node.Children.FirstOrDefault(c => c.ToString() == element);
}
if (returnBestMatch)
return node ?? bestMatch;
else
return node;

return returnBestMatch ? node ?? bestMatch : node;
}

/// <summary>
Expand Down Expand Up @@ -692,20 +705,28 @@ void TreeView_SelectionChanged()
{
if (SelectedItems.Count > 0)
{
var activeTabPage = DockWorkspace.Instance.ActiveTabPage;

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();
var decompiledNodes = activeTabPage
.GetState()
?.DecompiledNodes
?.Select(n => FindNodeByPath(GetPathForNode(n), true))
.ExceptNullItems()
.ToArray() ?? [];

if (!decompiledNodes.SequenceEqual(SelectedItems))
{
DecompileSelectedNodes();
}
}
else
{
Expand Down Expand Up @@ -743,7 +764,7 @@ private void DecompileSelectedNodes(DecompilerTextViewState newState = null)
}
if (newState?.ViewedUri != null)
{
NavigateTo(new(newState.ViewedUri, null), recordHistory: false);
NavigateTo(new(newState.ViewedUri, null));
return;
}

Expand Down Expand Up @@ -780,7 +801,12 @@ public void NavigateHistory(bool forward)
history.UpdateCurrent(new NavigationState(tabPage, state));
var newState = forward ? history.GoForward() : history.GoBack();

DockWorkspace.Instance.ActiveTabPage = newState.TabPage;
TabPageModel activeTabPage = newState.TabPage;

if (!DockWorkspace.Instance.TabPages.Contains(activeTabPage))
DockWorkspace.Instance.AddTabPage(activeTabPage);
else
DockWorkspace.Instance.ActiveTabPage = activeTabPage;

SelectNodes(newState.TreeNodes);
}
Expand All @@ -789,7 +815,7 @@ public void NavigateHistory(bool forward)

public bool CanNavigateForward => history.CanNavigateForward;

internal void NavigateTo(RequestNavigateEventArgs e, bool recordHistory = true, bool inNewTabPage = false)
internal void NavigateTo(RequestNavigateEventArgs e, bool inNewTabPage = false)
{
if (e.Uri.Scheme == "resource")
{
Expand Down Expand Up @@ -830,7 +856,7 @@ internal void NavigateTo(RequestNavigateEventArgs e, bool recordHistory = true,

void RecordHistory()
{
if (!recordHistory)
if (isNavigatingHistory)
return;
TabPageModel tabPage = DockWorkspace.Instance.ActiveTabPage;
var currentState = tabPage.GetState();
Expand Down
Loading