Browse Source

refactor DecompilerSettings to MVVM model first design

pull/3274/head
tom-englert 10 months ago committed by tom-englert
parent
commit
8fe9514dfe
  1. 10
      ILSpy.ReadyToRun/ReadyToRunOptionPage.xaml.cs
  2. 34
      ILSpy/Options/DecompilerSettingsPanel.xaml.cs
  3. 65
      ILSpy/Options/DecompilerSettingsViewModel.cs
  4. 4
      ILSpy/Options/DisplaySettingsPanel.xaml.cs
  5. 2
      ILSpy/Options/MiscSettingsPanel.xaml.cs
  6. 2
      ILSpy/Options/OptionsDialog.xaml.cs
  7. 2
      ILSpy/Util/SettingsService.cs
  8. 4
      TestPlugin/CustomOptionPage.xaml.cs

10
ILSpy.ReadyToRun/ReadyToRunOptionPage.xaml.cs

@ -37,13 +37,13 @@ namespace ICSharpCode.ILSpy.ReadyToRun @@ -37,13 +37,13 @@ namespace ICSharpCode.ILSpy.ReadyToRun
public string Title => global::ILSpy.ReadyToRun.Properties.Resources.ReadyToRun;
public void Load(ILSpySettings settings)
public void Load(ILSpySettings spySettings)
{
Options s = new Options();
s.DisassemblyFormat = ReadyToRunOptions.GetDisassemblyFormat(settings);
s.IsShowUnwindInfo = ReadyToRunOptions.GetIsShowUnwindInfo(settings);
s.IsShowDebugInfo = ReadyToRunOptions.GetIsShowDebugInfo(settings);
s.IsShowGCInfo = ReadyToRunOptions.GetIsShowGCInfo(settings);
s.DisassemblyFormat = ReadyToRunOptions.GetDisassemblyFormat(spySettings);
s.IsShowUnwindInfo = ReadyToRunOptions.GetIsShowUnwindInfo(spySettings);
s.IsShowDebugInfo = ReadyToRunOptions.GetIsShowDebugInfo(spySettings);
s.IsShowGCInfo = ReadyToRunOptions.GetIsShowGCInfo(spySettings);
this.DataContext = s;
}

34
ILSpy/Options/DecompilerSettingsPanel.xaml.cs

@ -21,46 +21,20 @@ using System.Xml.Linq; @@ -21,46 +21,20 @@ using System.Xml.Linq;
using ICSharpCode.ILSpyX.Settings;
using TomsToolbox.Wpf.Composition.Mef;
namespace ICSharpCode.ILSpy.Options
{
/// <summary>
/// Interaction logic for DecompilerSettingsPanel.xaml
/// </summary>
[ExportOptionPage(Order = 10)]
[DataTemplate(typeof(DecompilerSettingsViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
internal partial class DecompilerSettingsPanel : IOptionPage
internal partial class DecompilerSettingsPanel
{
public DecompilerSettingsPanel()
{
InitializeComponent();
}
public string Title => Properties.Resources.Decompiler;
public static Decompiler.DecompilerSettings LoadDecompilerSettings(ILSpySettings settings)
{
return ISettingsProvider.LoadDecompilerSettings(settings);
}
public void Load(ILSpySettings settings)
{
this.DataContext = new DecompilerSettingsViewModel(LoadDecompilerSettings(settings));
}
public void Save(XElement root)
{
var newSettings = ((DecompilerSettingsViewModel)this.DataContext).ToDecompilerSettings();
ISettingsProvider.SaveDecompilerSettings(root, newSettings);
SettingsService.Instance.DecompilerSettings = newSettings;
SettingsService.Instance.AssemblyListManager.ApplyWinRTProjections = newSettings.ApplyWindowsRuntimeProjections;
SettingsService.Instance.AssemblyListManager.UseDebugSymbols = newSettings.UseDebugSymbols;
}
public void LoadDefaults()
{
SettingsService.Instance.DecompilerSettings = new();
this.DataContext = new DecompilerSettingsViewModel(SettingsService.Instance.DecompilerSettings);
}
}
}

65
ILSpy/Options/DecompilerSettingsViewModel.cs

@ -18,54 +18,87 @@ @@ -18,54 +18,87 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;
using ICSharpCode.Decompiler;
using ICSharpCode.ILSpy.Properties;
using ICSharpCode.ILSpy.TreeNodes;
using ICSharpCode.ILSpyX.Settings;
using TomsToolbox.Wpf;
namespace ICSharpCode.ILSpy.Options
{
public sealed class DecompilerSettingsViewModel : ObservableObjectBase
[ExportOptionPage(Order = 10)]
[PartCreationPolicy(CreationPolicy.NonShared)]
public sealed class DecompilerSettingsViewModel : ObservableObjectBase, IOptionPage
{
public DecompilerSettingsGroupViewModel[] Settings { get; }
private DecompilerSettingsGroupViewModel[] settings;
public DecompilerSettingsViewModel(Decompiler.DecompilerSettings settings)
public string Title => Resources.Decompiler;
public DecompilerSettingsGroupViewModel[] Settings {
get => settings;
set => SetProperty(ref settings, value);
}
public void Load(ILSpySettings spySettings)
{
Settings = typeof(Decompiler.DecompilerSettings).GetProperties()
Load(ISettingsProvider.LoadDecompilerSettings(spySettings));
}
private void Load(DecompilerSettings decompilerSettings)
{
this.Settings = typeof(Decompiler.DecompilerSettings).GetProperties()
.Where(p => p.GetCustomAttribute<BrowsableAttribute>()?.Browsable != false)
.Select(p => new DecompilerSettingsItemViewModel(p) { IsEnabled = p.GetValue(settings) is true })
.Select(p => new DecompilerSettingsItemViewModel(p) { IsEnabled = p.GetValue(decompilerSettings) is true })
.OrderBy(item => item.Category, NaturalStringComparer.Instance)
.GroupBy(p => p.Category)
.Select(g => new DecompilerSettingsGroupViewModel(g.Key, g.OrderBy(i => i.Description).ToArray()))
.ToArray();
}
public Decompiler.DecompilerSettings ToDecompilerSettings()
public void Save(XElement root)
{
var newSettings = ToDecompilerSettings();
ISettingsProvider.SaveDecompilerSettings(root, newSettings);
SettingsService.Instance.DecompilerSettings = newSettings;
SettingsService.Instance.AssemblyListManager.ApplyWinRTProjections = newSettings.ApplyWindowsRuntimeProjections;
SettingsService.Instance.AssemblyListManager.UseDebugSymbols = newSettings.UseDebugSymbols;
}
public void LoadDefaults()
{
Load(new DecompilerSettings());
}
private DecompilerSettings ToDecompilerSettings()
{
var settings = new Decompiler.DecompilerSettings();
var newSettings = new DecompilerSettings();
foreach (var item in Settings.SelectMany(group => group.Settings))
{
item.Property.SetValue(settings, item.IsEnabled);
item.Property.SetValue(newSettings, item.IsEnabled);
}
return settings;
return newSettings;
}
}
public sealed class DecompilerSettingsGroupViewModel : ObservableObjectBase
{
private bool? _areAllItemsChecked;
private bool? areAllItemsChecked;
public DecompilerSettingsGroupViewModel(string category, DecompilerSettingsItemViewModel[] settings)
{
Settings = settings;
Category = category;
_areAllItemsChecked = GetAreAllItemsChecked(Settings);
areAllItemsChecked = GetAreAllItemsChecked(Settings);
foreach (DecompilerSettingsItemViewModel viewModel in settings)
{
@ -74,9 +107,9 @@ namespace ICSharpCode.ILSpy.Options @@ -74,9 +107,9 @@ namespace ICSharpCode.ILSpy.Options
}
public bool? AreAllItemsChecked {
get => _areAllItemsChecked;
get => areAllItemsChecked;
set {
SetProperty(ref _areAllItemsChecked, value);
SetProperty(ref areAllItemsChecked, value);
if (!value.HasValue)
return;
@ -116,13 +149,13 @@ namespace ICSharpCode.ILSpy.Options @@ -116,13 +149,13 @@ namespace ICSharpCode.ILSpy.Options
public sealed class DecompilerSettingsItemViewModel(PropertyInfo property) : ObservableObjectBase
{
private bool _isEnabled;
private bool isEnabled;
public PropertyInfo Property { get; } = property;
public bool IsEnabled {
get => _isEnabled;
set => SetProperty(ref _isEnabled, value);
get => isEnabled;
set => SetProperty(ref isEnabled, value);
}
public string Description { get; set; } = GetResourceString(property.GetCustomAttribute<DescriptionAttribute>()?.Description ?? property.Name);

4
ILSpy/Options/DisplaySettingsPanel.xaml.cs

@ -69,9 +69,9 @@ namespace ICSharpCode.ILSpy.Options @@ -69,9 +69,9 @@ namespace ICSharpCode.ILSpy.Options
public string Title => Properties.Resources.Display;
public void Load(ILSpySettings settings)
public void Load(ILSpySettings spySettings)
{
this.DataContext = LoadDisplaySettings(settings);
this.DataContext = LoadDisplaySettings(spySettings);
}
static bool IsSymbolFont(FontFamily fontFamily)

2
ILSpy/Options/MiscSettingsPanel.xaml.cs

@ -38,7 +38,7 @@ namespace ICSharpCode.ILSpy.Options @@ -38,7 +38,7 @@ namespace ICSharpCode.ILSpy.Options
public string Title => Properties.Resources.Misc;
public void Load(ILSpySettings settings)
public void Load(ILSpySettings spySettings)
{
this.DataContext = new MiscSettingsViewModel(SettingsService.Instance.MiscSettings);
}

2
ILSpy/Options/OptionsDialog.xaml.cs

@ -45,7 +45,7 @@ namespace ICSharpCode.ILSpy.Options @@ -45,7 +45,7 @@ namespace ICSharpCode.ILSpy.Options
public interface IOptionPage
{
string Title { get; }
void Load(ILSpySettings settings);
void Load(ILSpySettings spySettings);
void Save(XElement root);
void LoadDefaults();
}

2
ILSpy/Util/SettingsService.cs

@ -16,7 +16,7 @@ namespace ICSharpCode.ILSpy.Util @@ -16,7 +16,7 @@ namespace ICSharpCode.ILSpy.Util
{
SpySettings = ILSpySettings.Load();
SessionSettings = new(SpySettings);
DecompilerSettings = DecompilerSettingsPanel.LoadDecompilerSettings(SpySettings);
DecompilerSettings = ISettingsProvider.LoadDecompilerSettings(SpySettings);
DisplaySettings = DisplaySettingsPanel.LoadDisplaySettings(SpySettings, SessionSettings);
MiscSettings = MiscSettings.Load(SpySettings);
AssemblyListManager = new(SpySettings) {

4
TestPlugin/CustomOptionPage.xaml.cs

@ -24,11 +24,11 @@ namespace TestPlugin @@ -24,11 +24,11 @@ namespace TestPlugin
public string Title => "TestPlugin";
public void Load(ILSpySettings settings)
public void Load(ILSpySettings spySettings)
{
// For loading options, use ILSpySetting's indexer.
// If the specified section does exist, the indexer will return a new empty element.
XElement e = settings[ns + "CustomOptions"];
XElement e = spySettings[ns + "CustomOptions"];
// Now load the options from the XML document:
Options s = new Options();
s.UselessOption1 = (bool?)e.Attribute("useless1") ?? s.UselessOption1;

Loading…
Cancel
Save