Browse Source

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

pull/3257/head
tom-englert 12 months 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 @@ -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
}

48
ILSpy/Docking/PaneTemplateSelector.cs

@ -1,48 +0,0 @@ @@ -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 @@ @@ -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}"
>
<Window.Resources>
@ -69,29 +70,33 @@ @@ -69,29 +70,33 @@
</tv:SharpTreeView.ItemContainerStyle>
</tv:SharpTreeView>
<DataTemplate x:Key="AssemblyListPaneTemplate">
<DataTemplate DataType="{x:Type viewModels:AssemblyListPaneModel}">
<ContentControl Content="{StaticResource AssemblyTreeView}" />
</DataTemplate>
<local:DebugSteps x:Key="DebugSteps" />
<DataTemplate x:Key="DebugStepsPaneTemplate">
<DataTemplate DataType="{x:Type viewModels:DebugStepsPaneModel}">
<ContentControl Content="{StaticResource DebugSteps}" />
</DataTemplate>
<search:SearchPane x:Key="SearchPane" />
<DataTemplate x:Key="SearchPaneTemplate">
<DataTemplate DataType="{x:Type viewModels:SearchPaneModel}">
<ContentControl Content="{StaticResource SearchPane}" />
</DataTemplate>
<analyzers:AnalyzerTreeView x:Key="AnalyzerTreeView" />
<DataTemplate x:Key="AnalyzerPaneTemplate">
<DataTemplate DataType="{x:Type viewModels:AnalyzerPaneModel}">
<ContentControl Content="{StaticResource AnalyzerTreeView}" />
</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}" />
</DataTemplate>

22
ILSpy/MainWindow.xaml.cs

@ -58,6 +58,7 @@ using ICSharpCode.ILSpyX; @@ -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 @@ -365,25 +366,12 @@ namespace ICSharpCode.ILSpy
#endregion
#region Tool Pane extensibility
private void InitToolPanes()
{
var toolPanes = App.ExportProvider.GetExports<ToolPaneModel, IToolPaneMetadata>("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<ToolPaneModel>("ToolPane");
DockWorkspace.Instance.ToolPanes.AddRange(toolPanes);
}
private void InitWindowMenu()

5
ILSpy/ViewModels/AnalyzerPaneModel.cs

@ -17,12 +17,11 @@ @@ -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 @@ -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");
}
}

4
ILSpy/ViewModels/AssemblyListPaneModel.cs

@ -24,7 +24,7 @@ using ICSharpCode.ILSpy.Properties; @@ -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 @@ -37,7 +37,5 @@ namespace ICSharpCode.ILSpy.ViewModels
IsCloseable = false;
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; @@ -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 @@ -34,7 +34,5 @@ namespace ICSharpCode.ILSpy.ViewModels
ContentId = PaneContentId;
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 @@ -39,8 +39,6 @@ namespace ICSharpCode.ILSpy.ViewModels
public object Content { get; }
public override DataTemplate Template => throw new NotSupportedException();
public LegacyToolPaneLocation Location { get; }
}
}

4
ILSpy/ViewModels/SearchPaneModel.cs

@ -22,7 +22,7 @@ using System.Windows.Input; @@ -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 @@ -42,7 +42,5 @@ namespace ICSharpCode.ILSpy.ViewModels
base.Show();
MainWindow.Instance.SearchPane.Show();
}
public override DataTemplate Template => (DataTemplate)MainWindow.Instance.FindResource("SearchPaneTemplate");
}
}

3
ILSpy/ViewModels/ToolPaneModel.cs

@ -16,7 +16,6 @@ @@ -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 @@ -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; }

Loading…
Cancel
Save