Skip to content

Commit

Permalink
Merge pull request #3266 from tom-englert/dev/WpfRefactoring
Browse files Browse the repository at this point in the history
WPF Refactoring
  • Loading branch information
siegfriedpammer committed Aug 25, 2024
2 parents 524ab60 + dde581a commit 6a84a81
Show file tree
Hide file tree
Showing 37 changed files with 486 additions and 462 deletions.
1 change: 1 addition & 0 deletions ICSharpCode.ILSpyX/AssemblyListManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public AssemblyListManager(ISettingsProvider settingsProvider)
}

public bool ApplyWinRTProjections { get; set; }

public bool UseDebugSymbols { get; set; }

public ObservableCollection<string> AssemblyLists { get; } = new ObservableCollection<string>();
Expand Down
4 changes: 2 additions & 2 deletions ICSharpCode.ILSpyX/Search/AbstractSearchStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public struct SearchRequest
public AssemblySearchKind AssemblySearchKind;
public MemberSearchKind MemberSearchKind;
public string[] Keywords;
public Regex RegEx;
public Regex? RegEx;
public bool FullNameSearch;
public bool OmitGenerics;
public string InNamespace;
Expand All @@ -62,7 +62,7 @@ public struct SearchRequest
public abstract class AbstractSearchStrategy
{
protected readonly string[] searchTerm;
protected readonly Regex regex;
protected readonly Regex? regex;
protected readonly bool fullNameSearch;
protected readonly bool omitGenerics;
protected readonly SearchRequest searchRequest;
Expand Down
3 changes: 2 additions & 1 deletion ILSpy.ReadyToRun/ReadyToRunLanguage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.Solution;
using ICSharpCode.Decompiler.TypeSystem;
using ICSharpCode.ILSpy.Util;
using ICSharpCode.ILSpyX;

using ILCompiler.Reflection.ReadyToRun;
Expand Down Expand Up @@ -174,7 +175,7 @@ public override void DecompileMethod(IMethod method, ITextOutput output, Decompi
.GroupBy(m => m.MethodHandle)
.ToDictionary(g => g.Key, g => g.ToArray());
}
var displaySettings = MainWindow.Instance.CurrentDisplaySettings;
var displaySettings = SettingsService.Instance.DisplaySettings;
bool showMetadataTokens = displaySettings.ShowMetadataTokens;
bool showMetadataTokensInBase10 = displaySettings.ShowMetadataTokensInBase10;
#if STRESS
Expand Down
3 changes: 2 additions & 1 deletion ILSpy/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ private static async Task InitializeMef()
protected override void OnStartup(StartupEventArgs e)
{
var output = new StringBuilder();
if (ILSpy.MainWindow.FormatExceptions(StartupExceptions.ToArray(), output))

if (StartupExceptions.FormatExceptions(output))
{
MessageBox.Show(output.ToString(), "Sorry we crashed!");
Environment.Exit(1);
Expand Down
13 changes: 11 additions & 2 deletions ILSpy/Commands/ScopeSearchToAssembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using ICSharpCode.Decompiler.TypeSystem;
using ICSharpCode.ILSpy.AppEnv;
using ICSharpCode.ILSpy.Properties;
using ICSharpCode.ILSpy.Search;
using ICSharpCode.ILSpy.TreeNodes;

namespace ICSharpCode.ILSpy
Expand All @@ -31,11 +32,19 @@ namespace ICSharpCode.ILSpy
[PartCreationPolicy(CreationPolicy.Shared)]
public class ScopeSearchToAssembly : IContextMenuEntry
{
private readonly SearchPaneModel searchPane;

[ImportingConstructor]
public ScopeSearchToAssembly(SearchPaneModel searchPane)
{
this.searchPane = searchPane;
}

public void Execute(TextViewContext context)
{
// asmName cannot be null here, because Execute is only called if IsEnabled/IsVisible return true.
string asmName = GetAssembly(context)!;
string searchTerm = MainWindow.Instance.SearchPane.SearchTerm;
string searchTerm = searchPane.SearchTerm;
string[] args = CommandLineTools.CommandLineToArgumentArray(searchTerm);
bool replaced = false;
for (int i = 0; i < args.Length; i++)
Expand All @@ -55,7 +64,7 @@ public void Execute(TextViewContext context)
{
searchTerm = CommandLineTools.ArgumentArrayToCommandLine(args);
}
MainWindow.Instance.SearchPane.SearchTerm = searchTerm;
searchPane.SearchTerm = searchTerm;
}

public bool IsEnabled(TextViewContext context)
Expand Down
14 changes: 12 additions & 2 deletions ILSpy/Commands/ScopeSearchToNamespace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using ICSharpCode.Decompiler.TypeSystem;
using ICSharpCode.ILSpy.AppEnv;
using ICSharpCode.ILSpy.Properties;
using ICSharpCode.ILSpy.Search;
using ICSharpCode.ILSpy.TreeNodes;

namespace ICSharpCode.ILSpy
Expand All @@ -29,10 +30,18 @@ namespace ICSharpCode.ILSpy
[PartCreationPolicy(CreationPolicy.Shared)]
public class ScopeSearchToNamespace : IContextMenuEntry
{
private readonly SearchPaneModel searchPane;

[ImportingConstructor]
public ScopeSearchToNamespace(SearchPaneModel searchPane)
{
this.searchPane = searchPane;
}

public void Execute(TextViewContext context)
{
string ns = GetNamespace(context);
string searchTerm = MainWindow.Instance.SearchPane.SearchTerm;
string searchTerm = searchPane.SearchTerm;
string[] args = CommandLineTools.CommandLineToArgumentArray(searchTerm);
bool replaced = false;
for (int i = 0; i < args.Length; i++)
Expand All @@ -52,7 +61,8 @@ public void Execute(TextViewContext context)
{
searchTerm = CommandLineTools.ArgumentArrayToCommandLine(args);
}
MainWindow.Instance.SearchPane.SearchTerm = searchTerm;

searchPane.SearchTerm = searchTerm;
}

public bool IsEnabled(TextViewContext context)
Expand Down
8 changes: 7 additions & 1 deletion ILSpy/Controls/CultureSelectionConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Markup;

namespace ICSharpCode.ILSpy.Controls
{
public class CultureSelectionConverter : IValueConverter
public class CultureSelectionConverter : MarkupExtension, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Expand All @@ -37,5 +38,10 @@ public object ConvertBack(object value, Type targetType, object parameter, Cultu
return parameter;
return Binding.DoNothing;
}

public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
}
46 changes: 0 additions & 46 deletions ILSpy/Controls/TreeView/Converters.cs

This file was deleted.

5 changes: 3 additions & 2 deletions ILSpy/Controls/TreeView/SharpTreeView.xaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Default="clr-namespace:ICSharpCode.ILSpy.Controls.TreeView"
xmlns:styles="urn:TomsToolbox.Wpf.Styles">
xmlns:styles="urn:TomsToolbox.Wpf.Styles"
xmlns:toms="urn:TomsToolbox">

<Style x:Key="ExpandCollapseToggleStyle"
TargetType="{x:Type ToggleButton}" BasedOn="{StaticResource {x:Type ToggleButton}}">
Expand Down Expand Up @@ -227,7 +228,7 @@
<Grid>
<Default:LinesRenderer x:Name="linesRenderer"
ClipToBounds="True"
Visibility="{Binding ShowLines, RelativeSource={RelativeSource AncestorType={x:Type Default:SharpTreeView}}, Converter={Default:CollapsedWhenFalse}}" />
Visibility="{Binding ShowLines, RelativeSource={RelativeSource AncestorType={x:Type Default:SharpTreeView}}, Converter={toms:BooleanToVisibilityConverter}}" />
<StackPanel Orientation="Horizontal">
<FrameworkElement Name="spacer" />
<ToggleButton Name="expander"
Expand Down
6 changes: 3 additions & 3 deletions ILSpy/DecompilationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class DecompilationOptions
/// <summary>
/// Gets the settings for the decompiler.
/// </summary>
public Decompiler.DecompilerSettings DecompilerSettings { get; private set; }
public DecompilerSettings DecompilerSettings { get; private set; }

/// <summary>
/// Gets/sets an optional state of a decompiler text view.
Expand All @@ -83,7 +83,7 @@ public class DecompilationOptions
internal int StepLimit = int.MaxValue;
internal bool IsDebug = false;

public DecompilationOptions(LanguageVersion version, Decompiler.DecompilerSettings settings, DisplaySettingsViewModel displaySettings)
public DecompilationOptions(LanguageVersion version, DecompilerSettings settings, DisplaySettings displaySettings)
{
if (!Enum.TryParse(version?.Version, out Decompiler.CSharp.LanguageVersion languageVersion))
languageVersion = Decompiler.CSharp.LanguageVersion.Latest;
Expand All @@ -96,7 +96,7 @@ public DecompilationOptions(LanguageVersion version, Decompiler.DecompilerSettin
newSettings.CSharpFormattingOptions.IndentationString = GetIndentationString(displaySettings);
}

private string GetIndentationString(DisplaySettingsViewModel displaySettings)
private string GetIndentationString(DisplaySettings displaySettings)
{
if (displaySettings.IndentationUseTabs)
{
Expand Down
11 changes: 5 additions & 6 deletions ILSpy/Docking/DockWorkspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,12 @@
// DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Navigation;
using System.Windows.Threading;

using AvalonDock;
Expand All @@ -36,6 +31,7 @@

using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.ILSpy.Analyzers;
using ICSharpCode.ILSpy.Search;
using ICSharpCode.ILSpy.TextView;
using ICSharpCode.ILSpy.Util;
using ICSharpCode.ILSpy.ViewModels;
Expand Down Expand Up @@ -219,7 +215,7 @@ internal void ResetLayout()
}
CloseAllTabs();
SessionSettings.DockLayout.Reset();
InitializeLayout(MainWindow.Instance.DockManager);
InitializeLayout(MainWindow.Instance.dockManager);
MainWindow.Instance.Dispatcher.BeginInvoke(DispatcherPriority.Background, (Action)MainWindow.Instance.RefreshDecompiledView);
}

Expand Down Expand Up @@ -268,5 +264,8 @@ public bool BeforeInsertDocument(LayoutRoot layout, LayoutDocument anchorableToS
public void AfterInsertDocument(LayoutRoot layout, LayoutDocument anchorableShown)
{
}

// Dummy property to make the XAML designer happy, the model is provided by the AvalonDock PaneStyleSelectors, not by the DockWorkspace, but the designer assumes the data context in the PaneStyleSelectors is the DockWorkspace.
public PaneModel Model { get; } = null;
}
}
33 changes: 32 additions & 1 deletion ILSpy/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.TypeSystem;
using ICSharpCode.ILSpy.Util;
using ICSharpCode.ILSpyX;

namespace ICSharpCode.ILSpy
Expand Down Expand Up @@ -75,7 +77,7 @@ public static U[] SelectArray<T, U>(this ICollection<T> collection, Func<T, U> f
public static ICompilation? GetTypeSystemWithCurrentOptionsOrNull(this MetadataFile file)
{
return LoadedAssemblyExtensions.GetLoadedAssembly(file)
.GetTypeSystemOrNull(DecompilerTypeSystem.GetOptions(MainWindow.Instance.CurrentDecompilerSettings));
.GetTypeSystemOrNull(DecompilerTypeSystem.GetOptions(SettingsService.Instance.DecompilerSettings));
}

#region DPI independence
Expand Down Expand Up @@ -162,5 +164,34 @@ public static double ToGray(this Color? color)
{
return color?.R * 0.3 + color?.G * 0.6 + color?.B * 0.1 ?? 0.0;
}

internal static bool FormatExceptions(this IList<App.ExceptionData> exceptions, StringBuilder output)
{
if (exceptions.Count == 0)
return false;
bool first = true;

foreach (var item in exceptions)
{
if (first)
first = false;
else
output.AppendLine("-------------------------------------------------");
output.AppendLine("Error(s) loading plugin: " + item.PluginName);
if (item.Exception is System.Reflection.ReflectionTypeLoadException)
{
var e = (System.Reflection.ReflectionTypeLoadException)item.Exception;
foreach (var ex in e.LoaderExceptions)
{
output.AppendLine(ex.ToString());

Check warning on line 186 in ILSpy/ExtensionMethods.cs

View workflow job for this annotation

GitHub Actions / Build (Debug)

Dereference of a possibly null reference.

Check warning on line 186 in ILSpy/ExtensionMethods.cs

View workflow job for this annotation

GitHub Actions / Build (Debug)

Dereference of a possibly null reference.

Check warning on line 186 in ILSpy/ExtensionMethods.cs

View workflow job for this annotation

GitHub Actions / Build (Debug)

Dereference of a possibly null reference.

Check warning on line 186 in ILSpy/ExtensionMethods.cs

View workflow job for this annotation

GitHub Actions / Build (Debug)

Dereference of a possibly null reference.

Check warning on line 186 in ILSpy/ExtensionMethods.cs

View workflow job for this annotation

GitHub Actions / Build (Release)

Dereference of a possibly null reference.

Check warning on line 186 in ILSpy/ExtensionMethods.cs

View workflow job for this annotation

GitHub Actions / Build (Release)

Dereference of a possibly null reference.

Check warning on line 186 in ILSpy/ExtensionMethods.cs

View workflow job for this annotation

GitHub Actions / Build (Release)

Dereference of a possibly null reference.

Check warning on line 186 in ILSpy/ExtensionMethods.cs

View workflow job for this annotation

GitHub Actions / Build (Release)

Dereference of a possibly null reference.
output.AppendLine();
}
}
else
output.AppendLine(item.Exception.ToString());
}

return true;
}
}
}
3 changes: 2 additions & 1 deletion ILSpy/Languages/CSharpILMixedLanguage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.TypeSystem;
using ICSharpCode.Decompiler.Util;
using ICSharpCode.ILSpy.Util;
using ICSharpCode.ILSpyX;
using ICSharpCode.ILSpyX.Extensions;

Expand All @@ -49,7 +50,7 @@ class CSharpILMixedLanguage : ILLanguage

protected override ReflectionDisassembler CreateDisassembler(ITextOutput output, DecompilationOptions options)
{
var displaySettings = MainWindow.Instance.CurrentDisplaySettings;
var displaySettings = SettingsService.Instance.DisplaySettings;
return new ReflectionDisassembler(output,
new MixedMethodBodyDisassembler(output, options) {
DetectControlStructure = detectControlStructure,
Expand Down
Loading

0 comments on commit 6a84a81

Please sign in to comment.