Browse Source

Remove usage of custom template selector with magic ids, use standard WPF pattern to locate data templates

pull/3257/head
tom-englert 1 year ago
parent
commit
00ff9c8816
  1. 10
      ILSpy/Commands/ExportCommandAttribute.cs
  2. 48
      ILSpy/Docking/PaneTemplateSelector.cs
  3. 15
      ILSpy/MainWindow.xaml
  4. 22
      ILSpy/MainWindow.xaml.cs
  5. 5
      ILSpy/ViewModels/AnalyzerPaneModel.cs
  6. 4
      ILSpy/ViewModels/AssemblyListPaneModel.cs
  7. 4
      ILSpy/ViewModels/DebugStepsPaneModel.cs
  8. 2
      ILSpy/ViewModels/LegacyToolPaneModel.cs
  9. 4
      ILSpy/ViewModels/SearchPaneModel.cs
  10. 3
      ILSpy/ViewModels/ToolPaneModel.cs

10
ILSpy/Commands/ExportCommandAttribute.cs

@ -99,21 +99,15 @@ namespace ICSharpCode.ILSpy
#endregion #endregion
#region Tool Panes #region Tool Panes
public interface IToolPaneMetadata
{
string ContentId { get; }
}
[MetadataAttribute] [MetadataAttribute]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] [AttributeUsage(AttributeTargets.Class)]
public class ExportToolPaneAttribute : ExportAttribute, IToolPaneMetadata public class ExportToolPaneAttribute : ExportAttribute
{ {
public ExportToolPaneAttribute() public ExportToolPaneAttribute()
: base("ToolPane", typeof(ViewModels.ToolPaneModel)) : base("ToolPane", typeof(ViewModels.ToolPaneModel))
{ {
} }
public string ContentId { get; set; }
} }
#endregion #endregion
} }

48
ILSpy/Docking/PaneTemplateSelector.cs

@ -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<TemplateMapping> Mappings { get; set; } = new Collection<TemplateMapping>();
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);
}
}
}

15
ILSpy/MainWindow.xaml

@ -23,6 +23,7 @@
xmlns:b="http://schemas.microsoft.com/xaml/behaviors" xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
xmlns:themes="clr-namespace:ICSharpCode.ILSpy.Themes" xmlns:themes="clr-namespace:ICSharpCode.ILSpy.Themes"
xmlns:toms="urn:TomsToolbox" xmlns:toms="urn:TomsToolbox"
xmlns:viewModels="clr-namespace:ICSharpCode.ILSpy.ViewModels"
d:DataContext="{d:DesignInstance local:MainWindowViewModel}" d:DataContext="{d:DesignInstance local:MainWindowViewModel}"
> >
<Window.Resources> <Window.Resources>
@ -69,29 +70,33 @@
</tv:SharpTreeView.ItemContainerStyle> </tv:SharpTreeView.ItemContainerStyle>
</tv:SharpTreeView> </tv:SharpTreeView>
<DataTemplate x:Key="AssemblyListPaneTemplate"> <DataTemplate DataType="{x:Type viewModels:AssemblyListPaneModel}">
<ContentControl Content="{StaticResource AssemblyTreeView}" /> <ContentControl Content="{StaticResource AssemblyTreeView}" />
</DataTemplate> </DataTemplate>
<local:DebugSteps x:Key="DebugSteps" /> <local:DebugSteps x:Key="DebugSteps" />
<DataTemplate x:Key="DebugStepsPaneTemplate"> <DataTemplate DataType="{x:Type viewModels:DebugStepsPaneModel}">
<ContentControl Content="{StaticResource DebugSteps}" /> <ContentControl Content="{StaticResource DebugSteps}" />
</DataTemplate> </DataTemplate>
<search:SearchPane x:Key="SearchPane" /> <search:SearchPane x:Key="SearchPane" />
<DataTemplate x:Key="SearchPaneTemplate"> <DataTemplate DataType="{x:Type viewModels:SearchPaneModel}">
<ContentControl Content="{StaticResource SearchPane}" /> <ContentControl Content="{StaticResource SearchPane}" />
</DataTemplate> </DataTemplate>
<analyzers:AnalyzerTreeView x:Key="AnalyzerTreeView" /> <analyzers:AnalyzerTreeView x:Key="AnalyzerTreeView" />
<DataTemplate x:Key="AnalyzerPaneTemplate"> <DataTemplate DataType="{x:Type viewModels:AnalyzerPaneModel}">
<ContentControl Content="{StaticResource AnalyzerTreeView}" /> <ContentControl Content="{StaticResource AnalyzerTreeView}" />
</DataTemplate> </DataTemplate>
<DataTemplate x:Key="DefaultContentTemplate"> <DataTemplate DataType="{x:Type viewModels:TabPageModel}">
<ContentPresenter Content="{Binding Content}" />
</DataTemplate>
<DataTemplate DataType="{x:Type viewModels:LegacyToolPaneModel}">
<ContentPresenter Content="{Binding Content}" /> <ContentPresenter Content="{Binding Content}" />
</DataTemplate> </DataTemplate>

22
ILSpy/MainWindow.xaml.cs

@ -58,6 +58,7 @@ using ICSharpCode.ILSpyX;
using ICSharpCode.ILSpyX.FileLoaders; using ICSharpCode.ILSpyX.FileLoaders;
using ICSharpCode.ILSpyX.Settings; using ICSharpCode.ILSpyX.Settings;
using ICSharpCode.ILSpy.Controls.TreeView; using ICSharpCode.ILSpy.Controls.TreeView;
using ICSharpCode.ILSpyX.Extensions;
using Microsoft.Win32; using Microsoft.Win32;
using ICSharpCode.ILSpyX.TreeView; using ICSharpCode.ILSpyX.TreeView;
@ -365,25 +366,12 @@ namespace ICSharpCode.ILSpy
#endregion #endregion
#region Tool Pane extensibility #region Tool Pane extensibility
private void InitToolPanes() private void InitToolPanes()
{ {
var toolPanes = App.ExportProvider.GetExports<ToolPaneModel, IToolPaneMetadata>("ToolPane"); var toolPanes = App.ExportProvider.GetExportedValues<ToolPaneModel>("ToolPane");
var templateSelector = new PaneTemplateSelector();
templateSelector.Mappings.Add(new TemplateMapping { DockWorkspace.Instance.ToolPanes.AddRange(toolPanes);
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;
} }
private void InitWindowMenu() private void InitWindowMenu()

5
ILSpy/ViewModels/AnalyzerPaneModel.cs

@ -17,12 +17,11 @@
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
using System.ComponentModel.Composition; using System.ComponentModel.Composition;
using System.Windows;
using System.Windows.Input; using System.Windows.Input;
namespace ICSharpCode.ILSpy.ViewModels namespace ICSharpCode.ILSpy.ViewModels
{ {
[ExportToolPane(ContentId = PaneContentId)] [ExportToolPane]
[PartCreationPolicy(CreationPolicy.Shared)] [PartCreationPolicy(CreationPolicy.Shared)]
public class AnalyzerPaneModel : ToolPaneModel public class AnalyzerPaneModel : ToolPaneModel
{ {
@ -35,7 +34,5 @@ namespace ICSharpCode.ILSpy.ViewModels
ShortcutKey = new KeyGesture(Key.R, ModifierKeys.Control); ShortcutKey = new KeyGesture(Key.R, ModifierKeys.Control);
AssociatedCommand = ILSpyCommands.Analyze; AssociatedCommand = ILSpyCommands.Analyze;
} }
public override DataTemplate Template => (DataTemplate)MainWindow.Instance.FindResource("AnalyzerPaneTemplate");
} }
} }

4
ILSpy/ViewModels/AssemblyListPaneModel.cs

@ -24,7 +24,7 @@ using ICSharpCode.ILSpy.Properties;
namespace ICSharpCode.ILSpy.ViewModels namespace ICSharpCode.ILSpy.ViewModels
{ {
[ExportToolPane(ContentId = PaneContentId)] [ExportToolPane]
[PartCreationPolicy(CreationPolicy.Shared)] [PartCreationPolicy(CreationPolicy.Shared)]
public class AssemblyListPaneModel : ToolPaneModel public class AssemblyListPaneModel : ToolPaneModel
{ {
@ -37,7 +37,5 @@ namespace ICSharpCode.ILSpy.ViewModels
IsCloseable = false; IsCloseable = false;
ShortcutKey = new KeyGesture(Key.F6); ShortcutKey = new KeyGesture(Key.F6);
} }
public override DataTemplate Template => (DataTemplate)MainWindow.Instance.FindResource("AssemblyListPaneTemplate");
} }
} }

4
ILSpy/ViewModels/DebugStepsPaneModel.cs

@ -22,7 +22,7 @@ using System.Windows;
namespace ICSharpCode.ILSpy.ViewModels namespace ICSharpCode.ILSpy.ViewModels
{ {
#if DEBUG #if DEBUG
[ExportToolPane(ContentId = PaneContentId)] [ExportToolPane]
[PartCreationPolicy(CreationPolicy.Shared)] [PartCreationPolicy(CreationPolicy.Shared)]
#endif #endif
public class DebugStepsPaneModel : ToolPaneModel public class DebugStepsPaneModel : ToolPaneModel
@ -34,7 +34,5 @@ namespace ICSharpCode.ILSpy.ViewModels
ContentId = PaneContentId; ContentId = PaneContentId;
Title = Properties.Resources.DebugSteps; Title = Properties.Resources.DebugSteps;
} }
public override DataTemplate Template => (DataTemplate)MainWindow.Instance.FindResource("DebugStepsPaneTemplate");
} }
} }

2
ILSpy/ViewModels/LegacyToolPaneModel.cs

@ -39,8 +39,6 @@ namespace ICSharpCode.ILSpy.ViewModels
public object Content { get; } public object Content { get; }
public override DataTemplate Template => throw new NotSupportedException();
public LegacyToolPaneLocation Location { get; } public LegacyToolPaneLocation Location { get; }
} }
} }

4
ILSpy/ViewModels/SearchPaneModel.cs

@ -22,7 +22,7 @@ using System.Windows.Input;
namespace ICSharpCode.ILSpy.ViewModels namespace ICSharpCode.ILSpy.ViewModels
{ {
[ExportToolPane(ContentId = PaneContentId)] [ExportToolPane]
[PartCreationPolicy(CreationPolicy.Shared)] [PartCreationPolicy(CreationPolicy.Shared)]
public class SearchPaneModel : ToolPaneModel public class SearchPaneModel : ToolPaneModel
{ {
@ -42,7 +42,5 @@ namespace ICSharpCode.ILSpy.ViewModels
base.Show(); base.Show();
MainWindow.Instance.SearchPane.Show(); MainWindow.Instance.SearchPane.Show();
} }
public override DataTemplate Template => (DataTemplate)MainWindow.Instance.FindResource("SearchPaneTemplate");
} }
} }

3
ILSpy/ViewModels/ToolPaneModel.cs

@ -16,7 +16,6 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
using System.Windows;
using System.Windows.Input; using System.Windows.Input;
namespace ICSharpCode.ILSpy.ViewModels namespace ICSharpCode.ILSpy.ViewModels
@ -29,8 +28,6 @@ namespace ICSharpCode.ILSpy.ViewModels
this.IsVisible = true; this.IsVisible = true;
} }
public abstract DataTemplate Template { get; }
public KeyGesture ShortcutKey { get; protected set; } public KeyGesture ShortcutKey { get; protected set; }
public string Icon { get; protected set; } public string Icon { get; protected set; }

Loading…
Cancel
Save