Browse Source

Fix #3293: Right panel shows old info in case all is deleted in the left-hand tree

pull/3297/head
tom-englert 9 months ago
parent
commit
2c6f06e854
  1. 14
      ICSharpCode.ILSpyX/AssemblyListManager.cs
  2. 3
      ILSpy/App.xaml.cs
  3. 36
      ILSpy/AssemblyTree/AssemblyTreeModel.cs
  4. 29
      ILSpy/Commands/RemoveAssembliesWithLoadErrors.cs
  5. 7
      ILSpy/Docking/CloseAllDocumentsCommand.cs
  6. 55
      ILSpy/Docking/DockWorkspace.cs
  7. 10
      ILSpy/MainWindow.xaml.cs
  8. 2
      ILSpy/Util/MenuService.cs
  9. 5
      ILSpy/Util/SettingsService.cs

14
ICSharpCode.ILSpyX/AssemblyListManager.cs

@ -113,14 +113,16 @@ namespace ICSharpCode.ILSpyX
public void SaveList(AssemblyList list) public void SaveList(AssemblyList list)
{ {
this.settingsProvider.Update( this.settingsProvider.Update(
delegate (XElement root) { root => {
XElement? doc = root.Element("AssemblyLists"); XElement? doc = root.Element("AssemblyLists");
if (doc == null) if (doc == null)
{ {
doc = new XElement("AssemblyLists"); doc = new XElement("AssemblyLists");
root.Add(doc); root.Add(doc);
} }
XElement? listElement = doc.Elements("List").FirstOrDefault(e => (string?)e.Attribute("name") == list.ListName);
XElement? listElement = doc.Elements("List")
.FirstOrDefault(e => (string?)e.Attribute("name") == list.ListName);
if (listElement != null) if (listElement != null)
listElement.ReplaceWith(list.SaveAsXml()); listElement.ReplaceWith(list.SaveAsXml());
else else
@ -163,13 +165,9 @@ namespace ICSharpCode.ILSpyX
{ {
AssemblyLists.Clear(); AssemblyLists.Clear();
this.settingsProvider.Update( this.settingsProvider.Update(
delegate (XElement root) { root => {
XElement? doc = root.Element("AssemblyLists"); XElement? doc = root.Element("AssemblyLists");
if (doc == null) doc?.Remove();
{
return;
}
doc.Remove();
}); });
} }

3
ILSpy/App.xaml.cs

@ -216,9 +216,6 @@ namespace ICSharpCode.ILSpy
} }
MainWindow = new MainWindow(); MainWindow = new MainWindow();
MainWindow.Loaded += (sender, args) => {
ExportProvider.GetExportedValue<AssemblyTreeModel>().Initialize();
};
MainWindow.Show(); MainWindow.Show();
} }

36
ILSpy/AssemblyTree/AssemblyTreeModel.cs

@ -86,6 +86,8 @@ namespace ICSharpCode.ILSpy.AssemblyTree
SelectedItems.CollectionChanged += (_, _) => selectionChangeThrottle.Tick(); SelectedItems.CollectionChanged += (_, _) => selectionChangeThrottle.Tick();
refreshThrottle = new DispatcherThrottle(DispatcherPriority.Background, RefreshInternal); refreshThrottle = new DispatcherThrottle(DispatcherPriority.Background, RefreshInternal);
AssemblyList = SettingsService.Instance.CreateEmptyAssemblyList();
} }
private void Settings_PropertyChanged(object? sender, PropertyChangedEventArgs e) private void Settings_PropertyChanged(object? sender, PropertyChangedEventArgs e)
@ -121,7 +123,7 @@ namespace ICSharpCode.ILSpy.AssemblyTree
} }
} }
public AssemblyList? AssemblyList { get; private set; } public AssemblyList AssemblyList { get; private set; }
private SharpTreeNode? root; private SharpTreeNode? root;
public SharpTreeNode? Root { public SharpTreeNode? Root {
@ -205,7 +207,7 @@ namespace ICSharpCode.ILSpy.AssemblyTree
{ {
// FindNamespaceNode() blocks the UI if the assembly is not yet loaded, // FindNamespaceNode() blocks the UI if the assembly is not yet loaded,
// so use an async wait instead. // so use an async wait instead.
await asm.GetMetadataFileAsync().Catch<Exception>(ex => { }); await asm.GetMetadataFileAsync().Catch<Exception>(_ => { });
NamespaceTreeNode nsNode = asmNode.FindNamespaceNode(namespaceName); NamespaceTreeNode nsNode = asmNode.FindNamespaceNode(namespaceName);
if (nsNode != null) if (nsNode != null)
{ {
@ -260,7 +262,7 @@ namespace ICSharpCode.ILSpy.AssemblyTree
else if (spySettings != null) else if (spySettings != null)
{ {
SharpTreeNode? node = null; SharpTreeNode? node = null;
if (activeTreeViewPath?.Length > 0 && AssemblyList != null) if (activeTreeViewPath?.Length > 0)
{ {
foreach (var asm in AssemblyList.GetAssemblies()) foreach (var asm in AssemblyList.GetAssemblies())
{ {
@ -268,7 +270,7 @@ namespace ICSharpCode.ILSpy.AssemblyTree
{ {
// FindNodeByPath() blocks the UI if the assembly is not yet loaded, // FindNodeByPath() blocks the UI if the assembly is not yet loaded,
// so use an async wait instead. // so use an async wait instead.
await asm.GetMetadataFileAsync().Catch<Exception>(ex => { }); await asm.GetMetadataFileAsync().Catch<Exception>(_ => { });
} }
} }
node = FindNodeByPath(activeTreeViewPath, true); node = FindNodeByPath(activeTreeViewPath, true);
@ -401,7 +403,7 @@ namespace ICSharpCode.ILSpy.AssemblyTree
{ {
AssemblyList list = SettingsService.Instance.AssemblyListManager.LoadList(name); AssemblyList list = SettingsService.Instance.AssemblyListManager.LoadList(name);
//Only load a new list when it is a different one //Only load a new list when it is a different one
if (list.ListName != AssemblyList?.ListName) if (list.ListName != AssemblyList.ListName)
{ {
ShowAssemblyList(list); ShowAssemblyList(list);
SelectNode(Root); SelectNode(Root);
@ -411,12 +413,9 @@ namespace ICSharpCode.ILSpy.AssemblyTree
private void ShowAssemblyList(AssemblyList assemblyList) private void ShowAssemblyList(AssemblyList assemblyList)
{ {
history.Clear(); history.Clear();
if (this.AssemblyList != null)
{
this.AssemblyList.CollectionChanged -= assemblyList_CollectionChanged;
}
this.AssemblyList = assemblyList; AssemblyList.CollectionChanged -= assemblyList_CollectionChanged;
AssemblyList = assemblyList;
assemblyList.CollectionChanged += assemblyList_CollectionChanged; assemblyList.CollectionChanged += assemblyList_CollectionChanged;
@ -527,11 +526,6 @@ namespace ICSharpCode.ILSpy.AssemblyTree
return; return;
} }
if (SelectedItems.SequenceEqual(nodesList))
{
return;
}
if (this.isNavigatingHistory) if (this.isNavigatingHistory)
{ {
SelectedItems.Clear(); SelectedItems.Clear();
@ -649,8 +643,6 @@ namespace ICSharpCode.ILSpy.AssemblyTree
MainWindow.OpenLink(opCode.Link); MainWindow.OpenLink(opCode.Link);
break; break;
case EntityReference unresolvedEntity: case EntityReference unresolvedEntity:
if (AssemblyList is null)
break;
string protocol = unresolvedEntity.Protocol; string protocol = unresolvedEntity.Protocol;
var file = unresolvedEntity.ResolveAssembly(AssemblyList); var file = unresolvedEntity.ResolveAssembly(AssemblyList);
if (file == null) if (file == null)
@ -696,8 +688,6 @@ namespace ICSharpCode.ILSpy.AssemblyTree
AssemblyTreeNode? lastNode = null; AssemblyTreeNode? lastNode = null;
var assemblyList = AssemblyList; var assemblyList = AssemblyList;
if (assemblyList is null)
return;
foreach (string file in fileNames) foreach (string file in fileNames)
{ {
@ -907,11 +897,7 @@ namespace ICSharpCode.ILSpy.AssemblyTree
{ {
var path = GetPathForNode(SelectedItem); var path = GetPathForNode(SelectedItem);
if (AssemblyList != null) ShowAssemblyList(SettingsService.Instance.AssemblyListManager.LoadList(AssemblyList.ListName));
{
ShowAssemblyList(SettingsService.Instance.AssemblyListManager.LoadList(AssemblyList.ListName));
}
SelectNode(FindNodeByPath(path, true), inNewTabPage: false); SelectNode(FindNodeByPath(path, true), inNewTabPage: false);
RefreshDecompiledView(); RefreshDecompiledView();
@ -940,7 +926,7 @@ namespace ICSharpCode.ILSpy.AssemblyTree
{ {
using (activeView?.LockUpdates()) using (activeView?.LockUpdates())
{ {
AssemblyList?.Sort(AssemblyComparer.Instance); AssemblyList.Sort(AssemblyComparer.Instance);
} }
} }

29
ILSpy/Commands/RemoveAssembliesWithLoadErrors.cs

@ -19,6 +19,7 @@
using System.ComponentModel.Composition; using System.ComponentModel.Composition;
using System.Linq; using System.Linq;
using ICSharpCode.ILSpy.AssemblyTree;
using ICSharpCode.ILSpy.Properties; using ICSharpCode.ILSpy.Properties;
namespace ICSharpCode.ILSpy namespace ICSharpCode.ILSpy
@ -27,18 +28,26 @@ namespace ICSharpCode.ILSpy
[PartCreationPolicy(CreationPolicy.Shared)] [PartCreationPolicy(CreationPolicy.Shared)]
class RemoveAssembliesWithLoadErrors : SimpleCommand class RemoveAssembliesWithLoadErrors : SimpleCommand
{ {
private readonly AssemblyTreeModel assemblyTreeModel;
[ImportingConstructor]
public RemoveAssembliesWithLoadErrors(AssemblyTreeModel assemblyTreeModel)
{
this.assemblyTreeModel = assemblyTreeModel;
}
public override bool CanExecute(object parameter) public override bool CanExecute(object parameter)
{ {
return MainWindow.Instance.AssemblyTreeModel.AssemblyList?.GetAssemblies().Any(l => l.HasLoadError) == true; return assemblyTreeModel.AssemblyList.GetAssemblies().Any(l => l.HasLoadError);
} }
public override void Execute(object parameter) public override void Execute(object parameter)
{ {
foreach (var asm in MainWindow.Instance.AssemblyTreeModel.AssemblyList.GetAssemblies()) foreach (var assembly in assemblyTreeModel.AssemblyList.GetAssemblies())
{ {
if (!asm.HasLoadError) if (!assembly.HasLoadError)
continue; continue;
var node = MainWindow.Instance.AssemblyTreeModel.FindAssemblyNode(asm); var node = MainWindow.Instance.AssemblyTreeModel.FindAssemblyNode(assembly);
if (node != null && node.CanDelete()) if (node != null && node.CanDelete())
node.Delete(); node.Delete();
} }
@ -49,14 +58,22 @@ namespace ICSharpCode.ILSpy
[PartCreationPolicy(CreationPolicy.Shared)] [PartCreationPolicy(CreationPolicy.Shared)]
class ClearAssemblyList : SimpleCommand class ClearAssemblyList : SimpleCommand
{ {
private readonly AssemblyTreeModel assemblyTreeModel;
[ImportingConstructor]
public ClearAssemblyList(AssemblyTreeModel assemblyTreeModel)
{
this.assemblyTreeModel = assemblyTreeModel;
}
public override bool CanExecute(object parameter) public override bool CanExecute(object parameter)
{ {
return MainWindow.Instance.AssemblyTreeModel.AssemblyList?.Count > 0; return assemblyTreeModel.AssemblyList.Count > 0;
} }
public override void Execute(object parameter) public override void Execute(object parameter)
{ {
MainWindow.Instance.AssemblyTreeModel.AssemblyList?.Clear(); assemblyTreeModel.AssemblyList.Clear();
} }
} }
} }

7
ILSpy/Docking/CloseAllDocumentsCommand.cs

@ -1,9 +1,4 @@
using System; using System.ComponentModel.Composition;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ICSharpCode.ILSpy.Properties; using ICSharpCode.ILSpy.Properties;

55
ILSpy/Docking/DockWorkspace.cs

@ -17,6 +17,7 @@
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
using System; using System;
using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Collections.Specialized; using System.Collections.Specialized;
using System.Linq; using System.Linq;
@ -51,13 +52,17 @@ namespace ICSharpCode.ILSpy.Docking
public static readonly DockWorkspace Instance = new(); public static readonly DockWorkspace Instance = new();
private readonly ObservableCollection<TabPageModel> tabPages = []; private readonly ObservableCollection<TabPageModel> tabPages = [];
private readonly ObservableCollection<ToolPaneModel> toolPanes = [];
private DockWorkspace() private DockWorkspace()
{ {
this.tabPages.CollectionChanged += TabPages_CollectionChanged; this.tabPages.CollectionChanged += TabPages_CollectionChanged;
TabPages = new(tabPages); TabPages = new(tabPages);
ToolPanes = new(toolPanes);
ToolPanes = exportProvider
.GetExportedValues<ToolPaneModel>("ToolPane")
.OrderBy(item => item.Title)
.ToArray()
.AsReadOnly();
// Make sure there is at least one tab open // Make sure there is at least one tab open
AddTabPage(); AddTabPage();
@ -83,11 +88,16 @@ namespace ICSharpCode.ILSpy.Docking
.ExceptNullItems() .ExceptNullItems()
.Any(assemblyNode => !e.OldItems.Contains(assemblyNode.LoadedAssembly)); .Any(assemblyNode => !e.OldItems.Contains(assemblyNode.LoadedAssembly));
if (!found && tabPages.Count > 1) if (!found)
{ {
tabPages.Remove(tab); tabPages.Remove(tab);
} }
} }
if (tabPages.Count == 0)
{
AddTabPage();
}
} }
private void TabPages_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) private void TabPages_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
@ -117,11 +127,11 @@ namespace ICSharpCode.ILSpy.Docking
public ReadOnlyObservableCollection<TabPageModel> TabPages { get; } public ReadOnlyObservableCollection<TabPageModel> TabPages { get; }
public ReadOnlyObservableCollection<ToolPaneModel> ToolPanes { get; } public ReadOnlyCollection<ToolPaneModel> ToolPanes { get; }
public bool ShowToolPane(string contentId) public bool ShowToolPane(string contentId)
{ {
var pane = toolPanes.FirstOrDefault(p => p.ContentId == contentId); var pane = ToolPanes.FirstOrDefault(p => p.ContentId == contentId);
if (pane != null) if (pane != null)
{ {
pane.Show(); pane.Show();
@ -132,10 +142,15 @@ namespace ICSharpCode.ILSpy.Docking
public void Remove(PaneModel model) public void Remove(PaneModel model)
{ {
if (model is TabPageModel document) switch (model)
tabPages.Remove(document); {
if (model is ToolPaneModel tool) case TabPageModel document:
tool.IsVisible = false; tabPages.Remove(document);
break;
case ToolPaneModel tool:
tool.IsVisible = false;
break;
}
} }
private TabPageModel activeTabPage = null; private TabPageModel activeTabPage = null;
@ -166,10 +181,6 @@ namespace ICSharpCode.ILSpy.Docking
public void InitializeLayout(DockingManager manager) public void InitializeLayout(DockingManager manager)
{ {
var panes = exportProvider.GetExportedValues<ToolPaneModel>("ToolPane").OrderBy(item => item.Title);
this.toolPanes.AddRange(panes);
manager.LayoutUpdateStrategy = this; manager.LayoutUpdateStrategy = this;
XmlLayoutSerializer serializer = new XmlLayoutSerializer(manager); XmlLayoutSerializer serializer = new XmlLayoutSerializer(manager);
serializer.LayoutSerializationCallback += LayoutSerializationCallback; serializer.LayoutSerializationCallback += LayoutSerializationCallback;
@ -188,13 +199,13 @@ namespace ICSharpCode.ILSpy.Docking
switch (e.Model) switch (e.Model)
{ {
case LayoutAnchorable la: case LayoutAnchorable la:
e.Content = this.toolPanes.FirstOrDefault(p => p.ContentId == la.ContentId); e.Content = this.ToolPanes.FirstOrDefault(p => p.ContentId == la.ContentId);
e.Cancel = e.Content == null; e.Cancel = e.Content == null;
la.CanDockAsTabbedDocument = false; la.CanDockAsTabbedDocument = false;
if (!e.Cancel) if (e.Content is ToolPaneModel toolPaneModel)
{ {
e.Cancel = ((ToolPaneModel)e.Content).IsVisible; e.Cancel = toolPaneModel.IsVisible;
((ToolPaneModel)e.Content).IsVisible = true; toolPaneModel.IsVisible = true;
} }
break; break;
default: default:
@ -220,16 +231,14 @@ namespace ICSharpCode.ILSpy.Docking
internal void CloseAllTabs() internal void CloseAllTabs()
{ {
foreach (var doc in tabPages.ToArray()) var activePage = ActiveTabPage;
{
if (doc.IsCloseable) tabPages.RemoveWhere(page => page != activePage);
tabPages.Remove(doc);
}
} }
internal void ResetLayout() internal void ResetLayout()
{ {
foreach (var pane in toolPanes) foreach (var pane in ToolPanes)
{ {
pane.IsVisible = false; pane.IsVisible = false;
} }

10
ILSpy/MainWindow.xaml.cs

@ -25,6 +25,7 @@ using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Threading;
using AvalonDock.Layout.Serialization; using AvalonDock.Layout.Serialization;
@ -70,11 +71,14 @@ namespace ICSharpCode.ILSpy
InitializeComponent(); InitializeComponent();
mainWindowViewModel.Workspace.InitializeLayout(dockManager); InitFileLoaders();
MenuService.Instance.Init(mainMenu, toolBar, InputBindings); Dispatcher.BeginInvoke(DispatcherPriority.Background, () => {
mainWindowViewModel.Workspace.InitializeLayout(dockManager);
MenuService.Instance.Init(mainMenu, toolBar, InputBindings);
InitFileLoaders(); Dispatcher.BeginInvoke(DispatcherPriority.Background, AssemblyTreeModel.Initialize);
});
} }
void SetWindowBounds(Rect bounds) void SetWindowBounds(Rect bounds)

2
ILSpy/Util/MenuService.cs

@ -142,7 +142,7 @@ namespace ICSharpCode.ILSpy.Util
windowMenuItem.Items.Clear(); windowMenuItem.Items.Clear();
var toolItems = dockWorkspace.ToolPanes.ObservableSelect(toolPane => CreateMenuItem(toolPane, inputBindings)); var toolItems = dockWorkspace.ToolPanes.Select(toolPane => CreateMenuItem(toolPane, inputBindings)).ToArray();
var tabItems = dockWorkspace.TabPages.ObservableSelect(tabPage => CreateMenuItem(tabPage, dockWorkspace)); var tabItems = dockWorkspace.TabPages.ObservableSelect(tabPage => CreateMenuItem(tabPage, dockWorkspace));
var allItems = new ObservableCompositeCollection<Control>(defaultItems, [new Separator()], toolItems, [new Separator()], tabItems); var allItems = new ObservableCompositeCollection<Control>(defaultItems, [new Separator()], toolItems, [new Separator()], tabItems);

5
ILSpy/Util/SettingsService.cs

@ -152,6 +152,11 @@ namespace ICSharpCode.ILSpy.Util
} }
} }
public AssemblyList CreateEmptyAssemblyList()
{
return AssemblyListManager.CreateList(string.Empty);
}
private bool reloading; private bool reloading;
public void Reload() public void Reload()

Loading…
Cancel
Save