Skip to content

Commit

Permalink
Fix some minor design flaws in new code.
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-englert committed Sep 14, 2024
1 parent 0b477f7 commit da01176
Show file tree
Hide file tree
Showing 19 changed files with 53 additions and 46 deletions.
2 changes: 1 addition & 1 deletion ILSpy.ReadyToRun/ReadyToRunOptionPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void Load(SettingsSnapshot snapshot)

public void LoadDefaults()
{
Options.LoadFromSection(new("empty"));
Options.LoadFromXml(new("empty"));
}
}
}
8 changes: 6 additions & 2 deletions ILSpy.ReadyToRun/ReadyToRunOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public string DisassemblyFormat {

public XName SectionName { get; } = ns + "ReadyToRunOptions";

public void LoadFromSection(XElement e)
public void LoadFromXml(XElement e)
{
XAttribute format = e.Attribute("DisassemblyFormat");
DisassemblyFormat = format == null ? intel : (string)format;
Expand All @@ -79,12 +79,16 @@ public void LoadFromSection(XElement e)
IsShowGCInfo = showGc != null && (bool)showGc;
}

public void SaveToSection(XElement section)
public XElement SaveToXml()
{
var section = new XElement(SectionName);

section.SetAttributeValue("DisassemblyFormat", disassemblyFormat);
section.SetAttributeValue("IsShowUnwindInfo", isShowUnwindInfo);
section.SetAttributeValue("IsShowDebugInfo", isShowDebugInfo);
section.SetAttributeValue("IsShowGCInfo", isShowGCInfo);

return section;
}
}
}
2 changes: 0 additions & 2 deletions ILSpy/Commands/BrowseBackCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ protected override void OnCanExecute(object sender, CanExecuteRoutedEventArgs e)

protected override void OnExecute(object sender, ExecutedRoutedEventArgs e)
{
base.OnExecute(sender, e);

if (assemblyTreeModel.CanNavigateBack)
{
e.Handled = true;
Expand Down
2 changes: 0 additions & 2 deletions ILSpy/Commands/BrowseForwardCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ protected override void OnCanExecute(object sender, CanExecuteRoutedEventArgs e)

protected override void OnExecute(object sender, ExecutedRoutedEventArgs e)
{
base.OnExecute(sender, e);

if (assemblyTreeModel.CanNavigateForward)
{
e.Handled = true;
Expand Down
8 changes: 3 additions & 5 deletions ILSpy/Commands/CommandWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@

namespace ICSharpCode.ILSpy
{
class CommandWrapper : ICommand
abstract class CommandWrapper : ICommand
{
private readonly ICommand wrappedCommand;

public CommandWrapper(ICommand wrappedCommand)
protected CommandWrapper(ICommand wrappedCommand)
{
this.wrappedCommand = wrappedCommand;

Expand Down Expand Up @@ -56,9 +56,7 @@ public bool CanExecute(object parameter)
return wrappedCommand.CanExecute(parameter);
}

protected virtual void OnExecute(object sender, ExecutedRoutedEventArgs e)
{
}
protected abstract void OnExecute(object sender, ExecutedRoutedEventArgs e);

protected virtual void OnCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
Expand Down
2 changes: 0 additions & 2 deletions ILSpy/Commands/OpenCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ public OpenCommand(AssemblyTreeModel assemblyTreeModel)

protected override void OnExecute(object sender, ExecutedRoutedEventArgs e)
{
base.OnExecute(sender, e);

e.Handled = true;
OpenFileDialog dlg = new OpenFileDialog {
Filter = ".NET assemblies|*.dll;*.exe;*.winmd;*.wasm|Nuget Packages (*.nupkg)|*.nupkg|Portable Program Database (*.pdb)|*.pdb|All files|*.*",
Expand Down
2 changes: 0 additions & 2 deletions ILSpy/Commands/RefreshCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ public RefreshCommand(AssemblyTreeModel assemblyTreeModel)

protected override void OnExecute(object sender, ExecutedRoutedEventArgs e)
{
base.OnExecute(sender, e);

assemblyTreeModel.Refresh();
}
}
Expand Down
2 changes: 0 additions & 2 deletions ILSpy/Commands/SaveCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ protected override void OnCanExecute(object sender, CanExecuteRoutedEventArgs e)

protected override void OnExecute(object sender, ExecutedRoutedEventArgs e)
{
base.OnExecute(sender, e);

SaveCodeContextMenuEntry.Execute(assemblyTreeModel.SelectedNodes.ToList());
}
}
Expand Down
2 changes: 1 addition & 1 deletion ILSpy/Languages/Languages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static Language GetLanguage(string name)
return AllLanguages.FirstOrDefault(l => l.Name == name) ?? AllLanguages.First();
}

static ILLanguage ilLanguage;
static ILLanguage? ilLanguage;

public static ILLanguage ILLanguage {
get {
Expand Down
14 changes: 9 additions & 5 deletions ILSpy/Options/DecompilerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ public class DecompilerSettings : Decompiler.DecompilerSettings, ISettingsSectio

public XName SectionName => "DecompilerSettings";

public void SaveToSection(XElement section)
public XElement SaveToXml()
{
var section = new XElement(SectionName);

foreach (var p in properties)
{
section.SetAttributeValue(p.Name, p.GetValue(this));
}

return section;
}

public void LoadFromSection(XElement section)
public void LoadFromXml(XElement section)
{
foreach (var p in properties)
{
Expand All @@ -37,10 +41,10 @@ public void LoadFromSection(XElement section)

public new DecompilerSettings Clone()
{
var section = new XElement("DecompilerSettings");
SaveToSection(section);
var section = SaveToXml();

var newSettings = new DecompilerSettings();
newSettings.LoadFromSection(section);
newSettings.LoadFromXml(section);

return newSettings;
}
Expand Down
2 changes: 1 addition & 1 deletion ILSpy/Options/DecompilerSettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public sealed class DecompilerSettingsItemViewModel(PropertyInfo property, Decom
{
private bool isEnabled = property.GetValue(decompilerSettings) is true;

public PropertyInfo Property { get; } = property;
public PropertyInfo Property => property;

public bool IsEnabled {
get => isEnabled;
Expand Down
8 changes: 6 additions & 2 deletions ILSpy/Options/DisplaySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public bool ShowRawOffsetsAndBytesBeforeInstruction {

public XName SectionName => "DisplaySettings";

public void LoadFromSection(XElement section)
public void LoadFromXml(XElement section)
{
SelectedFont = new FontFamily((string)section.Attribute("Font") ?? "Consolas");
SelectedFontSize = (double?)section.Attribute("FontSize") ?? 10.0 * 4 / 3;
Expand All @@ -174,8 +174,10 @@ public void LoadFromSection(XElement section)
StyleWindowTitleBar = (bool?)section.Attribute("StyleWindowTitleBar") ?? false;
}

public void SaveToSection(XElement section)
public XElement SaveToXml()
{
var section = new XElement(SectionName);

section.SetAttributeValue("Font", SelectedFont.Source);
section.SetAttributeValue("FontSize", SelectedFontSize);
section.SetAttributeValue("ShowLineNumbers", ShowLineNumbers);
Expand All @@ -196,6 +198,8 @@ public void SaveToSection(XElement section)
section.SetAttributeValue("UseNestedNamespaceNodes", UseNestedNamespaceNodes);
section.SetAttributeValue("ShowRawOffsetsAndBytesBeforeInstruction", ShowRawOffsetsAndBytesBeforeInstruction);
section.SetAttributeValue("StyleWindowTitleBar", StyleWindowTitleBar);

return section;
}
}
}
2 changes: 1 addition & 1 deletion ILSpy/Options/DisplaySettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ static FontFamily[] FontLoader()

public void LoadDefaults()
{
Settings.LoadFromSection(new XElement("empty"));
Settings.LoadFromXml(new XElement("empty"));
SessionSettings.Theme = ThemeManager.Current.DefaultTheme;
}
}
Expand Down
8 changes: 6 additions & 2 deletions ILSpy/Options/MiscSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,20 @@ public bool LoadPreviousAssemblies {

public XName SectionName => "MiscSettings";

public void LoadFromSection(XElement e)
public void LoadFromXml(XElement e)
{
AllowMultipleInstances = (bool?)e.Attribute(nameof(AllowMultipleInstances)) ?? false;
LoadPreviousAssemblies = (bool?)e.Attribute(nameof(LoadPreviousAssemblies)) ?? true;
}

public void SaveToSection(XElement section)
public XElement SaveToXml()
{
var section = new XElement(SectionName);

section.SetAttributeValue(nameof(AllowMultipleInstances), AllowMultipleInstances);
section.SetAttributeValue(nameof(LoadPreviousAssemblies), LoadPreviousAssemblies);

return section;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion ILSpy/Options/MiscSettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void Load(SettingsSnapshot settings)

public void LoadDefaults()
{
Settings.LoadFromSection(new XElement("dummy"));
Settings.LoadFromXml(new XElement("dummy"));
}
}
}
2 changes: 0 additions & 2 deletions ILSpy/Search/SearchPane.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,6 @@ public ShowSearchCommand()

protected override void OnExecute(object sender, ExecutedRoutedEventArgs e)
{
base.OnExecute(sender, e);

DockWorkspace.Instance.ShowToolPane(SearchPaneModel.PaneContentId);
}
}
Expand Down
8 changes: 5 additions & 3 deletions ILSpy/SessionSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public sealed class SessionSettings : ISettingsSection
{
public XName SectionName => "SessionSettings";

public void LoadFromSection(XElement section)
public void LoadFromXml(XElement section)
{
XElement filterSettings = section.Element("FilterSettings") ?? new XElement("FilterSettings");

Expand Down Expand Up @@ -103,9 +103,9 @@ public string ActiveAssemblyList {

public DockLayoutSettings DockLayout { get; set; }

public void SaveToSection(XElement section)
public XElement SaveToXml()
{
section.RemoveAll();
var section = new XElement(SectionName);

section.Add(this.LanguageSettings.SaveAsXml());
if (this.ActiveAssemblyList != null)
Expand Down Expand Up @@ -134,6 +134,8 @@ public void SaveToSection(XElement section)
dockLayoutElement.Add(DockLayout.SaveAsXml());
}
section.Add(dockLayoutElement);

return section;
}

static Regex regex = new("\\\\x(?<num>[0-9A-f]{4})");
Expand Down
12 changes: 5 additions & 7 deletions ILSpy/Util/SettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public interface ISettingsSection : INotifyPropertyChanged
{
XName SectionName { get; }

void LoadFromSection(XElement section);
void LoadFromXml(XElement section);

void SaveToSection(XElement section);
XElement SaveToXml();
}

public abstract class SettingsServiceBase
Expand All @@ -47,7 +47,7 @@ protected SettingsServiceBase(ISettingsProvider spySettings)
var sectionElement = SpySettings[section.SectionName];
section.LoadFromSection(sectionElement);
section.LoadFromXml(sectionElement);
section.PropertyChanged += Section_PropertyChanged;
return section;
Expand All @@ -56,9 +56,7 @@ protected SettingsServiceBase(ISettingsProvider spySettings)

protected void SaveSection(ISettingsSection section, XElement root)
{
var element = SpySettings[section.SectionName];

section.SaveToSection(element);
var element = section.SaveToXml();

var existingElement = root.Element(section.SectionName);
if (existingElement != null)
Expand Down Expand Up @@ -150,7 +148,7 @@ public void Reload()
{
var element = SpySettings[section.SectionName];

section.LoadFromSection(element);
section.LoadFromXml(element);
}
}
finally
Expand Down
11 changes: 8 additions & 3 deletions TestPlugin/CustomOptionPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public CustomOptionPage()
}

[ExportOptionPage(Order = 0)]
[PartCreationPolicy(CreationPolicy.NonShared)]
class CustomOptionsViewModel : ObservableObject, IOptionPage
{
private Options options;
Expand All @@ -41,7 +42,7 @@ public void Load(SettingsSnapshot snapshot)

public void LoadDefaults()
{
Options.LoadFromSection(new XElement("dummy"));
Options.LoadFromXml(new XElement("dummy"));
}
}

Expand All @@ -65,16 +66,20 @@ public double UselessOption2 {

public XName SectionName { get; } = ns + "CustomOptions";

public void LoadFromSection(XElement e)
public void LoadFromXml(XElement e)
{
UselessOption1 = (bool?)e.Attribute("useless1") ?? false;
UselessOption2 = (double?)e.Attribute("useless2") ?? 50.0;
}

public void SaveToSection(XElement section)
public XElement SaveToXml()
{
var section = new XElement(SectionName);

section.SetAttributeValue("useless1", UselessOption1);
section.SetAttributeValue("useless2", UselessOption2);

return section;
}
}
}

0 comments on commit da01176

Please sign in to comment.