From 00ff9c8816fcc4771bc29f04879cc299a8041b81 Mon Sep 17 00:00:00 2001 From: tom-englert Date: Fri, 16 Aug 2024 13:40:12 +0200 Subject: [PATCH] Remove usage of custom template selector with magic ids, use standard WPF pattern to locate data templates --- ILSpy/Commands/ExportCommandAttribute.cs | 10 +---- ILSpy/Docking/PaneTemplateSelector.cs | 48 ----------------------- ILSpy/MainWindow.xaml | 15 ++++--- ILSpy/MainWindow.xaml.cs | 22 +++-------- ILSpy/ViewModels/AnalyzerPaneModel.cs | 5 +-- ILSpy/ViewModels/AssemblyListPaneModel.cs | 4 +- ILSpy/ViewModels/DebugStepsPaneModel.cs | 4 +- ILSpy/ViewModels/LegacyToolPaneModel.cs | 2 - ILSpy/ViewModels/SearchPaneModel.cs | 4 +- ILSpy/ViewModels/ToolPaneModel.cs | 3 -- 10 files changed, 21 insertions(+), 96 deletions(-) delete mode 100644 ILSpy/Docking/PaneTemplateSelector.cs diff --git a/ILSpy/Commands/ExportCommandAttribute.cs b/ILSpy/Commands/ExportCommandAttribute.cs index 282774bff..19d100fd4 100644 --- a/ILSpy/Commands/ExportCommandAttribute.cs +++ b/ILSpy/Commands/ExportCommandAttribute.cs @@ -99,21 +99,15 @@ namespace ICSharpCode.ILSpy #endregion #region Tool Panes - public interface IToolPaneMetadata - { - string ContentId { get; } - } [MetadataAttribute] - [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] - public class ExportToolPaneAttribute : ExportAttribute, IToolPaneMetadata + [AttributeUsage(AttributeTargets.Class)] + public class ExportToolPaneAttribute : ExportAttribute { public ExportToolPaneAttribute() : base("ToolPane", typeof(ViewModels.ToolPaneModel)) { } - - public string ContentId { get; set; } } #endregion } diff --git a/ILSpy/Docking/PaneTemplateSelector.cs b/ILSpy/Docking/PaneTemplateSelector.cs deleted file mode 100644 index 7890a1dde..000000000 --- a/ILSpy/Docking/PaneTemplateSelector.cs +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright (c) 2019 AlphaSierraPapa for the SharpDevelop Team -// -// Permission is hereby granted, free of charge, to any person obtaining a copy of this -// software and associated documentation files (the "Software"), to deal in the Software -// without restriction, including without limitation the rights to use, copy, modify, merge, -// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons -// to whom the Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in all copies or -// substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR -// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE -// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. - -using System; -using System.Collections.ObjectModel; -using System.Linq; -using System.Windows; -using System.Windows.Controls; - -namespace ICSharpCode.ILSpy.Docking -{ - public class TemplateMapping - { - public Type Type { get; set; } - public DataTemplate Template { get; set; } - } - - public class PaneTemplateSelector : DataTemplateSelector - { - public Collection Mappings { get; set; } = new Collection(); - - public override DataTemplate SelectTemplate(object item, DependencyObject container) - { - if (item == null) - { - return base.SelectTemplate(item, container); - } - - return Mappings.FirstOrDefault(m => m.Type == item.GetType())?.Template - ?? base.SelectTemplate(item, container); - } - } -} diff --git a/ILSpy/MainWindow.xaml b/ILSpy/MainWindow.xaml index 6f423f315..7f6831651 100644 --- a/ILSpy/MainWindow.xaml +++ b/ILSpy/MainWindow.xaml @@ -23,6 +23,7 @@ xmlns:b="http://schemas.microsoft.com/xaml/behaviors" xmlns:themes="clr-namespace:ICSharpCode.ILSpy.Themes" xmlns:toms="urn:TomsToolbox" + xmlns:viewModels="clr-namespace:ICSharpCode.ILSpy.ViewModels" d:DataContext="{d:DesignInstance local:MainWindowViewModel}" > @@ -69,29 +70,33 @@ - + - + - + - + - + + + + + diff --git a/ILSpy/MainWindow.xaml.cs b/ILSpy/MainWindow.xaml.cs index 2f8ec46f7..26f04b11c 100644 --- a/ILSpy/MainWindow.xaml.cs +++ b/ILSpy/MainWindow.xaml.cs @@ -58,6 +58,7 @@ using ICSharpCode.ILSpyX; using ICSharpCode.ILSpyX.FileLoaders; using ICSharpCode.ILSpyX.Settings; using ICSharpCode.ILSpy.Controls.TreeView; +using ICSharpCode.ILSpyX.Extensions; using Microsoft.Win32; using ICSharpCode.ILSpyX.TreeView; @@ -365,25 +366,12 @@ namespace ICSharpCode.ILSpy #endregion #region Tool Pane extensibility + private void InitToolPanes() { - var toolPanes = App.ExportProvider.GetExports("ToolPane"); - var templateSelector = new PaneTemplateSelector(); - templateSelector.Mappings.Add(new TemplateMapping { - Type = typeof(TabPageModel), - Template = (DataTemplate)FindResource("DefaultContentTemplate") - }); - templateSelector.Mappings.Add(new TemplateMapping { - Type = typeof(LegacyToolPaneModel), - Template = (DataTemplate)FindResource("DefaultContentTemplate") - }); - foreach (var toolPane in toolPanes) - { - ToolPaneModel model = toolPane.Value; - templateSelector.Mappings.Add(new TemplateMapping { Type = model.GetType(), Template = model.Template }); - DockWorkspace.Instance.ToolPanes.Add(model); - } - DockManager.LayoutItemTemplateSelector = templateSelector; + var toolPanes = App.ExportProvider.GetExportedValues("ToolPane"); + + DockWorkspace.Instance.ToolPanes.AddRange(toolPanes); } private void InitWindowMenu() diff --git a/ILSpy/ViewModels/AnalyzerPaneModel.cs b/ILSpy/ViewModels/AnalyzerPaneModel.cs index 557113683..ca92e5dae 100644 --- a/ILSpy/ViewModels/AnalyzerPaneModel.cs +++ b/ILSpy/ViewModels/AnalyzerPaneModel.cs @@ -17,12 +17,11 @@ // DEALINGS IN THE SOFTWARE. using System.ComponentModel.Composition; -using System.Windows; using System.Windows.Input; namespace ICSharpCode.ILSpy.ViewModels { - [ExportToolPane(ContentId = PaneContentId)] + [ExportToolPane] [PartCreationPolicy(CreationPolicy.Shared)] public class AnalyzerPaneModel : ToolPaneModel { @@ -35,7 +34,5 @@ namespace ICSharpCode.ILSpy.ViewModels ShortcutKey = new KeyGesture(Key.R, ModifierKeys.Control); AssociatedCommand = ILSpyCommands.Analyze; } - - public override DataTemplate Template => (DataTemplate)MainWindow.Instance.FindResource("AnalyzerPaneTemplate"); } } diff --git a/ILSpy/ViewModels/AssemblyListPaneModel.cs b/ILSpy/ViewModels/AssemblyListPaneModel.cs index bfbc8fa02..d6156cf8f 100644 --- a/ILSpy/ViewModels/AssemblyListPaneModel.cs +++ b/ILSpy/ViewModels/AssemblyListPaneModel.cs @@ -24,7 +24,7 @@ using ICSharpCode.ILSpy.Properties; namespace ICSharpCode.ILSpy.ViewModels { - [ExportToolPane(ContentId = PaneContentId)] + [ExportToolPane] [PartCreationPolicy(CreationPolicy.Shared)] public class AssemblyListPaneModel : ToolPaneModel { @@ -37,7 +37,5 @@ namespace ICSharpCode.ILSpy.ViewModels IsCloseable = false; ShortcutKey = new KeyGesture(Key.F6); } - - public override DataTemplate Template => (DataTemplate)MainWindow.Instance.FindResource("AssemblyListPaneTemplate"); } } diff --git a/ILSpy/ViewModels/DebugStepsPaneModel.cs b/ILSpy/ViewModels/DebugStepsPaneModel.cs index 50a169041..406f047a8 100644 --- a/ILSpy/ViewModels/DebugStepsPaneModel.cs +++ b/ILSpy/ViewModels/DebugStepsPaneModel.cs @@ -22,7 +22,7 @@ using System.Windows; namespace ICSharpCode.ILSpy.ViewModels { #if DEBUG - [ExportToolPane(ContentId = PaneContentId)] + [ExportToolPane] [PartCreationPolicy(CreationPolicy.Shared)] #endif public class DebugStepsPaneModel : ToolPaneModel @@ -34,7 +34,5 @@ namespace ICSharpCode.ILSpy.ViewModels ContentId = PaneContentId; Title = Properties.Resources.DebugSteps; } - - public override DataTemplate Template => (DataTemplate)MainWindow.Instance.FindResource("DebugStepsPaneTemplate"); } } diff --git a/ILSpy/ViewModels/LegacyToolPaneModel.cs b/ILSpy/ViewModels/LegacyToolPaneModel.cs index 4cd2a6bbd..8501b43e3 100644 --- a/ILSpy/ViewModels/LegacyToolPaneModel.cs +++ b/ILSpy/ViewModels/LegacyToolPaneModel.cs @@ -39,8 +39,6 @@ namespace ICSharpCode.ILSpy.ViewModels public object Content { get; } - public override DataTemplate Template => throw new NotSupportedException(); - public LegacyToolPaneLocation Location { get; } } } diff --git a/ILSpy/ViewModels/SearchPaneModel.cs b/ILSpy/ViewModels/SearchPaneModel.cs index e9e0d30d5..ea6fca679 100644 --- a/ILSpy/ViewModels/SearchPaneModel.cs +++ b/ILSpy/ViewModels/SearchPaneModel.cs @@ -22,7 +22,7 @@ using System.Windows.Input; namespace ICSharpCode.ILSpy.ViewModels { - [ExportToolPane(ContentId = PaneContentId)] + [ExportToolPane] [PartCreationPolicy(CreationPolicy.Shared)] public class SearchPaneModel : ToolPaneModel { @@ -42,7 +42,5 @@ namespace ICSharpCode.ILSpy.ViewModels base.Show(); MainWindow.Instance.SearchPane.Show(); } - - public override DataTemplate Template => (DataTemplate)MainWindow.Instance.FindResource("SearchPaneTemplate"); } } diff --git a/ILSpy/ViewModels/ToolPaneModel.cs b/ILSpy/ViewModels/ToolPaneModel.cs index 15a79250b..43ab349f1 100644 --- a/ILSpy/ViewModels/ToolPaneModel.cs +++ b/ILSpy/ViewModels/ToolPaneModel.cs @@ -16,7 +16,6 @@ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -using System.Windows; using System.Windows.Input; namespace ICSharpCode.ILSpy.ViewModels @@ -29,8 +28,6 @@ namespace ICSharpCode.ILSpy.ViewModels this.IsVisible = true; } - public abstract DataTemplate Template { get; } - public KeyGesture ShortcutKey { get; protected set; } public string Icon { get; protected set; }