Browse Source

Allow extending the toolbar using MEF.

pull/70/head
Daniel Grunwald 15 years ago
parent
commit
2c0baaf2b3
  1. 53
      ILSpy/Commands.cs
  2. 35
      ILSpy/ExportCommandAttribute.cs
  3. 2
      ILSpy/ILSpy.csproj
  4. 12
      ILSpy/Images/Images.cs
  5. 14
      ILSpy/MainWindow.xaml
  6. 41
      ILSpy/MainWindow.xaml.cs

53
ILSpy/Commands.cs

@ -0,0 +1,53 @@ @@ -0,0 +1,53 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using System;
using System.Windows.Input;
namespace ICSharpCode.ILSpy
{
[ExportToolbarCommand(ToolTip = "Back", Icon = "Images/Back.png", Category = "Navigation")]
sealed class BrowseBackCommand : CommandWrapper {
public BrowseBackCommand() : base(NavigationCommands.BrowseBack) {}
}
[ExportToolbarCommand(ToolTip = "Forward", Icon = "Images/Forward.png", Category = "Navigation", Order = 1)]
sealed class BrowseForwardCommand : CommandWrapper {
public BrowseForwardCommand() : base(NavigationCommands.BrowseForward) {}
}
[ExportToolbarCommand(ToolTip = "Open", Icon = "Images/Open.png", Category = "Open")]
sealed class OpenCommand : CommandWrapper {
public OpenCommand() : base(ApplicationCommands.Open) {}
}
[ExportToolbarCommand(ToolTip = "Reload all assemblies", Icon = "Images/Refresh.png", Category = "Open", Order = 1)]
sealed class RefreshCommand : CommandWrapper {
public RefreshCommand() : base(NavigationCommands.Refresh) {}
}
class CommandWrapper : ICommand
{
ICommand wrappedCommand;
public CommandWrapper(ICommand wrappedCommand)
{
this.wrappedCommand = wrappedCommand;
}
public event EventHandler CanExecuteChanged {
add { wrappedCommand.CanExecuteChanged += value; }
remove { wrappedCommand.CanExecuteChanged -= value; }
}
public void Execute(object parameter)
{
wrappedCommand.Execute(parameter);
}
public bool CanExecute(object parameter)
{
return wrappedCommand.CanExecute(parameter);
}
}
}

35
ILSpy/ExportCommandAttribute.cs

@ -0,0 +1,35 @@ @@ -0,0 +1,35 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using System;
using System.ComponentModel.Composition;
using System.Windows.Input;
namespace ICSharpCode.ILSpy
{
#region Toolbar
public interface IToolbarCommandMetadata
{
string Icon { get; }
string ToolTip { get; }
string Category { get; }
double Order { get; }
}
[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class, AllowMultiple=false)]
public class ExportToolbarCommandAttribute : ExportAttribute
{
public ExportToolbarCommandAttribute()
: base(typeof(ICommand))
{
}
public string ToolTip { get; set; }
public string Icon { get; set; }
public string Category { get; set; }
public double Order { get; set; }
}
#endregion
}

2
ILSpy/ILSpy.csproj

@ -88,6 +88,8 @@ @@ -88,6 +88,8 @@
<Compile Include="AssemblyList.cs" />
<Compile Include="AssemblyListManager.cs" />
<Compile Include="BamlDecompiler.cs" />
<Compile Include="Commands.cs" />
<Compile Include="ExportCommandAttribute.cs" />
<Compile Include="Controls\SearchBox.cs" />
<Compile Include="Controls\SortableGridViewColumn.cs" />
<Compile Include="CSharpLanguage.cs" />

12
ILSpy/Images/Images.cs

@ -68,5 +68,17 @@ namespace ICSharpCode.ILSpy @@ -68,5 +68,17 @@ namespace ICSharpCode.ILSpy
public static readonly BitmapImage Delete = LoadBitmap("Delete");
public static readonly BitmapImage Search = LoadBitmap("Search");
public static BitmapImage LoadImage(object part, string icon)
{
Uri uri;
if (part.GetType().Assembly == typeof(Images).Assembly)
uri = new Uri("pack://application:,,,/" + icon);
else
throw new NotImplementedException();
BitmapImage image = new BitmapImage(uri);
image.Freeze();
return image;
}
}
}

14
ILSpy/MainWindow.xaml

@ -83,19 +83,9 @@ @@ -83,19 +83,9 @@
</Style.Triggers>
</Style>
</ToolBar.Resources>
<Button Command="BrowseBack" ToolTip="Back">
<Image Width="16" Height="16" Source="Images/Back.png" />
</Button>
<Button Command="BrowseForward" ToolTip="Forward">
<Image Width="16" Height="16" Source="Images/Forward.png" />
</Button>
<!-- 'Navigation' toolbar category is inserted here -->
<Separator />
<Button Command="Open" ToolTip="Open">
<Image Width="16" Height="16" Source="Images/Open.png" />
</Button>
<Button Command="Refresh" ToolTip="Reload all assemblies">
<Image Width="16" Height="16" Source="Images/Refresh.png" />
</Button>
<!-- 'Open' toolbar category is inserted here -->
<Separator />
<CheckBox IsChecked="{Binding FilterSettings.ShowInternalApi}" ToolTip="Show internal types and members">
<Image Width="16" Height="16" Source="Images/PrivateInternal.png" />

41
ILSpy/MainWindow.xaml.cs

@ -18,8 +18,10 @@ @@ -18,8 +18,10 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
@ -84,9 +86,46 @@ namespace ICSharpCode.ILSpy @@ -84,9 +86,46 @@ namespace ICSharpCode.ILSpy
}
sessionSettings.FilterSettings.PropertyChanged += filterSettings_PropertyChanged;
App.CompositionContainer.ComposeParts(this);
int navigationPos = 0;
int openPos = 1;
foreach (var commandGroup in ToolbarCommands.GroupBy(c => c.Metadata.Category)) {
if (commandGroup.Key == "Navigation") {
foreach (var command in commandGroup.OrderBy(c => c.Metadata.Order)) {
toolBar.Items.Insert(navigationPos++, MakeToolbarItem(command));
openPos++;
}
} else if (commandGroup.Key == "Open") {
foreach (var command in commandGroup.OrderBy(c => c.Metadata.Order)) {
toolBar.Items.Insert(openPos++, MakeToolbarItem(command));
}
} else {
toolBar.Items.Add(new Separator());
foreach (var command in commandGroup.OrderBy(c => c.Metadata.Order)) {
toolBar.Items.Add(MakeToolbarItem(command));
}
}
}
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
Button MakeToolbarItem(Lazy<ICommand, IToolbarCommandMetadata> command)
{
return new Button {
Command = command.Value,
ToolTip = command.Metadata.ToolTip,
Content = new Image {
Width = 16,
Height = 16,
Source = Images.LoadImage(command.Value, command.Metadata.Icon)
}
};
}
[ImportMany]
internal Lazy<ICommand, IToolbarCommandMetadata>[] ToolbarCommands { get; set; }
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
ILSpySettings spySettings = this.spySettings;

Loading…
Cancel
Save