From 39b036b0279a3134589a52b73b348578cd037d0d Mon Sep 17 00:00:00 2001 From: tom-englert Date: Sat, 7 Sep 2024 07:19:27 +0200 Subject: [PATCH] Remove more code from MainWindow, refactor startup sequence --- ICSharpCode.ILSpyX/AssemblyListManager.cs | 5 +- ICSharpCode.ILSpyX/Settings/ILSpySettings.cs | 48 ++---- .../Settings/ISettingsProvider.cs | 7 +- ICSharpCode.ILSpyX/Settings/MiscSettings.cs | 4 +- ILSpy.ReadyToRun/ReadyToRunDisassembler.cs | 1 + ILSpy.ReadyToRun/ReadyToRunOptions.cs | 25 ++- ILSpy/AboutPage.cs | 4 +- ILSpy/App.xaml | 17 +-- ILSpy/App.xaml.cs | 48 +++--- ILSpy/AssemblyTree/AssemblyListPaneModel.cs | 105 +++++++++++-- ILSpy/Commands/CheckForUpdatesCommand.cs | 2 +- ILSpy/Docking/DockWorkspace.cs | 7 +- ILSpy/ILSpy.csproj | 3 +- ILSpy/Languages/Languages.cs | 3 + ILSpy/MainWindow.xaml | 113 +++++++------- ILSpy/MainWindow.xaml.cs | 143 +++--------------- ILSpy/Options/MiscSettingsPanel.xaml | 2 +- ILSpy/Options/MiscSettingsPanel.xaml.cs | 30 ++-- ILSpy/Options/MiscSettingsViewModel.cs | 67 +++----- ILSpy/Options/OptionsDialog.xaml.cs | 15 +- ILSpy/SessionSettings.cs | 6 +- ILSpy/Updates/UpdateSettings.cs | 2 +- ILSpy/Util/SettingsService.cs | 18 +++ 23 files changed, 304 insertions(+), 371 deletions(-) diff --git a/ICSharpCode.ILSpyX/AssemblyListManager.cs b/ICSharpCode.ILSpyX/AssemblyListManager.cs index 8a886df5f..510af433b 100644 --- a/ICSharpCode.ILSpyX/AssemblyListManager.cs +++ b/ICSharpCode.ILSpyX/AssemblyListManager.cs @@ -39,7 +39,7 @@ namespace ICSharpCode.ILSpyX public const string DotNet35List = ".NET 3.5"; public const string ASPDotNetMVC3List = "ASP.NET (MVC3)"; - private ISettingsProvider settingsProvider; + private readonly ISettingsProvider settingsProvider; public AssemblyListManager(ISettingsProvider settingsProvider) { @@ -69,14 +69,13 @@ namespace ICSharpCode.ILSpyX /// public AssemblyList LoadList(string listName) { - this.settingsProvider = this.settingsProvider.Load(); AssemblyList list = DoLoadList(listName); if (!AssemblyLists.Contains(list.ListName)) AssemblyLists.Add(list.ListName); return list; } - AssemblyList DoLoadList(string listName) + AssemblyList DoLoadList(string? listName) { XElement doc = this.settingsProvider["AssemblyLists"]; if (listName != null) diff --git a/ICSharpCode.ILSpyX/Settings/ILSpySettings.cs b/ICSharpCode.ILSpyX/Settings/ILSpySettings.cs index 63e94299b..c7dc899f1 100644 --- a/ICSharpCode.ILSpyX/Settings/ILSpySettings.cs +++ b/ICSharpCode.ILSpyX/Settings/ILSpySettings.cs @@ -34,16 +34,11 @@ namespace ICSharpCode.ILSpyX.Settings /// public static ISettingsFilePathProvider? SettingsFilePathProvider { get; set; } - readonly XElement root; + XElement root; - ILSpySettings() + ILSpySettings(XElement? root = null) { - this.root = new XElement("ILSpy"); - } - - ILSpySettings(XElement root) - { - this.root = root; + this.root = root ?? new XElement("ILSpy"); } public XElement this[XName section] { @@ -64,13 +59,7 @@ namespace ICSharpCode.ILSpyX.Settings { try { - XDocument doc = LoadWithoutCheckingCharacters(GetConfigFile()); - if (null == doc.Root) - { - return new ILSpySettings(); - } - - return new ILSpySettings(doc.Root); + return new ILSpySettings(LoadFile(GetConfigFile()).Root); } catch (IOException) { @@ -83,7 +72,7 @@ namespace ICSharpCode.ILSpyX.Settings } } - static XDocument LoadWithoutCheckingCharacters(string fileName) + static XDocument LoadFile(string fileName) { return XDocument.Load(fileName, LoadOptions.None); } @@ -91,16 +80,15 @@ namespace ICSharpCode.ILSpyX.Settings /// /// Saves a setting section. /// - public static void SaveSettings(XElement section) + public void SaveSettings(XElement section) { - Update( - delegate (XElement root) { - XElement? existingElement = root.Element(section.Name); - if (existingElement != null) - existingElement.ReplaceWith(section); - else - root.Add(section); - }); + Update(rootElement => { + XElement? existingElement = rootElement.Element(section.Name); + if (existingElement != null) + existingElement.ReplaceWith(section); + else + rootElement.Add(section); + }); } /// @@ -108,7 +96,7 @@ namespace ICSharpCode.ILSpyX.Settings /// We always reload the file on updates to ensure we aren't overwriting unrelated changes performed /// by another ILSpy instance. /// - public static void Update(Action action) + public void Update(Action action) { using (new MutexProtector(ConfigFileMutex)) { @@ -116,7 +104,7 @@ namespace ICSharpCode.ILSpyX.Settings XDocument doc; try { - doc = LoadWithoutCheckingCharacters(config); + doc = LoadFile(config); } catch (IOException) { @@ -131,6 +119,7 @@ namespace ICSharpCode.ILSpyX.Settings doc.Root!.SetAttributeValue("version", DecompilerVersionInfo.Major + "." + DecompilerVersionInfo.Minor + "." + DecompilerVersionInfo.Build + "." + DecompilerVersionInfo.Revision); action(doc.Root); doc.Save(config, SaveOptions.None); + this.root = doc.Root; } } @@ -148,11 +137,6 @@ namespace ICSharpCode.ILSpyX.Settings // return "ILSpy.xml"; } - ISettingsProvider ISettingsProvider.Load() - { - return Load(); - } - const string ConfigFileMutex = "01A91708-49D1-410D-B8EB-4DE2662B3971"; /// diff --git a/ICSharpCode.ILSpyX/Settings/ISettingsProvider.cs b/ICSharpCode.ILSpyX/Settings/ISettingsProvider.cs index 3f62dba76..664acb5e2 100644 --- a/ICSharpCode.ILSpyX/Settings/ISettingsProvider.cs +++ b/ICSharpCode.ILSpyX/Settings/ISettingsProvider.cs @@ -22,8 +22,6 @@ using System.Linq; using System.Reflection; using System.Xml.Linq; -using static System.Collections.Specialized.BitVector32; - namespace ICSharpCode.ILSpyX.Settings { public interface ISettingsProvider @@ -31,9 +29,8 @@ namespace ICSharpCode.ILSpyX.Settings XElement this[XName section] { get; } void Update(Action action); - ISettingsProvider Load(); - public static ICSharpCode.Decompiler.DecompilerSettings LoadDecompilerSettings(ISettingsProvider settingsProvider) + public static Decompiler.DecompilerSettings LoadDecompilerSettings(ISettingsProvider settingsProvider) { XElement e = settingsProvider["DecompilerSettings"]; var newSettings = new Decompiler.DecompilerSettings(); @@ -48,7 +45,7 @@ namespace ICSharpCode.ILSpyX.Settings return newSettings; } - public static void SaveDecompilerSettings(XElement root, ICSharpCode.Decompiler.DecompilerSettings newSettings) + public static void SaveDecompilerSettings(XElement root, Decompiler.DecompilerSettings newSettings) { var properties = typeof(Decompiler.DecompilerSettings).GetProperties() .Where(p => p.GetCustomAttribute()?.Browsable != false); diff --git a/ICSharpCode.ILSpyX/Settings/MiscSettings.cs b/ICSharpCode.ILSpyX/Settings/MiscSettings.cs index d349764d4..c47d5cff2 100644 --- a/ICSharpCode.ILSpyX/Settings/MiscSettings.cs +++ b/ICSharpCode.ILSpyX/Settings/MiscSettings.cs @@ -23,12 +23,12 @@ namespace ICSharpCode.ILSpyX.Settings { public class MiscSettings : IMiscSettings, ISettingsSection { - private MiscSettings() + public MiscSettings() { } public bool AllowMultipleInstances { get; set; } - public bool LoadPreviousAssemblies { get; set; } + public bool LoadPreviousAssemblies { get; set; } = true; public static MiscSettings Load(ISettingsProvider settingsProvider) { diff --git a/ILSpy.ReadyToRun/ReadyToRunDisassembler.cs b/ILSpy.ReadyToRun/ReadyToRunDisassembler.cs index 0b7d0ee6c..e9c11ff93 100644 --- a/ILSpy.ReadyToRun/ReadyToRunDisassembler.cs +++ b/ILSpy.ReadyToRun/ReadyToRunDisassembler.cs @@ -26,6 +26,7 @@ using Iced.Intel; using ICSharpCode.Decompiler; using ICSharpCode.Decompiler.IL; using ICSharpCode.Decompiler.Metadata; +using ICSharpCode.ILSpy.Util; using ILCompiler.Reflection.ReadyToRun; using ILCompiler.Reflection.ReadyToRun.Amd64; diff --git a/ILSpy.ReadyToRun/ReadyToRunOptions.cs b/ILSpy.ReadyToRun/ReadyToRunOptions.cs index 365d1dd9a..1167497bb 100644 --- a/ILSpy.ReadyToRun/ReadyToRunOptions.cs +++ b/ILSpy.ReadyToRun/ReadyToRunOptions.cs @@ -18,6 +18,7 @@ using System.Xml.Linq; +using ICSharpCode.ILSpy.Util; using ICSharpCode.ILSpyX; using ICSharpCode.ILSpyX.Settings; @@ -33,10 +34,8 @@ namespace ICSharpCode.ILSpy.ReadyToRun public static string GetDisassemblyFormat(ILSpySettings settings) { - if (settings == null) - { - settings = ILSpySettings.Load(); - } + settings ??= SettingsService.Instance.SpySettings; + XElement e = settings[ns + "ReadyToRunOptions"]; XAttribute a = e.Attribute("DisassemblyFormat"); if (a == null) @@ -51,10 +50,8 @@ namespace ICSharpCode.ILSpy.ReadyToRun public static bool GetIsShowUnwindInfo(ILSpySettings settings) { - if (settings == null) - { - settings = ILSpySettings.Load(); - } + settings ??= SettingsService.Instance.SpySettings; + XElement e = settings[ns + "ReadyToRunOptions"]; XAttribute a = e.Attribute("IsShowUnwindInfo"); @@ -70,10 +67,8 @@ namespace ICSharpCode.ILSpy.ReadyToRun public static bool GetIsShowDebugInfo(ILSpySettings settings) { - if (settings == null) - { - settings = ILSpySettings.Load(); - } + settings ??= SettingsService.Instance.SpySettings; + XElement e = settings[ns + "ReadyToRunOptions"]; XAttribute a = e.Attribute("IsShowDebugInfo"); @@ -89,10 +84,8 @@ namespace ICSharpCode.ILSpy.ReadyToRun public static bool GetIsShowGCInfo(ILSpySettings settings) { - if (settings == null) - { - settings = ILSpySettings.Load(); - } + settings ??= SettingsService.Instance.SpySettings; + XElement e = settings[ns + "ReadyToRunOptions"]; XAttribute a = e.Attribute("IsShowGCInfo"); diff --git a/ILSpy/AboutPage.cs b/ILSpy/AboutPage.cs index 43303bf88..c9e1989f4 100644 --- a/ILSpy/AboutPage.cs +++ b/ILSpy/AboutPage.cs @@ -42,7 +42,7 @@ namespace ICSharpCode.ILSpy { public override void Execute(object parameter) { - MainWindow.Instance.NavigateTo( + MainWindow.Instance.AssemblyTreeModel.NavigateTo( new RequestNavigateEventArgs(new Uri("resource://aboutpage"), null), inNewTabPage: true ); @@ -76,7 +76,7 @@ namespace ICSharpCode.ILSpy CheckBox checkBox = new CheckBox(); checkBox.Margin = new Thickness(4); checkBox.Content = Resources.AutomaticallyCheckUpdatesEveryWeek; - UpdateSettings settings = new UpdateSettings(ILSpySettings.Load()); + UpdateSettings settings = new UpdateSettings(SettingsService.Instance.SpySettings); checkBox.SetBinding(CheckBox.IsCheckedProperty, new Binding("AutomaticUpdateCheckEnabled") { Source = settings }); return new StackPanel { Margin = new Thickness(0, 4, 0, 0), diff --git a/ILSpy/App.xaml b/ILSpy/App.xaml index 0cfda48ce..ce2e14086 100644 --- a/ILSpy/App.xaml +++ b/ILSpy/App.xaml @@ -1,15 +1,14 @@  + xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" + xmlns:styles="urn:TomsToolbox.Wpf.Styles" + xmlns:toms="urn:TomsToolbox" + xmlns:themes="clr-namespace:ICSharpCode.ILSpy.Themes">