From 2c0baaf2b3e01f3f018447a31e77b8ba45adb42f Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Fri, 4 Mar 2011 22:57:37 +0100 Subject: [PATCH] Allow extending the toolbar using MEF. --- ILSpy/Commands.cs | 53 +++++++++++++++++++++++++++++++++ ILSpy/ExportCommandAttribute.cs | 35 ++++++++++++++++++++++ ILSpy/ILSpy.csproj | 2 ++ ILSpy/Images/Images.cs | 12 ++++++++ ILSpy/MainWindow.xaml | 14 ++------- ILSpy/MainWindow.xaml.cs | 41 ++++++++++++++++++++++++- 6 files changed, 144 insertions(+), 13 deletions(-) create mode 100644 ILSpy/Commands.cs create mode 100644 ILSpy/ExportCommandAttribute.cs diff --git a/ILSpy/Commands.cs b/ILSpy/Commands.cs new file mode 100644 index 000000000..eb9e0ca02 --- /dev/null +++ b/ILSpy/Commands.cs @@ -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); + } + } +} diff --git a/ILSpy/ExportCommandAttribute.cs b/ILSpy/ExportCommandAttribute.cs new file mode 100644 index 000000000..33538524c --- /dev/null +++ b/ILSpy/ExportCommandAttribute.cs @@ -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 +} diff --git a/ILSpy/ILSpy.csproj b/ILSpy/ILSpy.csproj index 1110bf481..41598a0a1 100644 --- a/ILSpy/ILSpy.csproj +++ b/ILSpy/ILSpy.csproj @@ -88,6 +88,8 @@ + + diff --git a/ILSpy/Images/Images.cs b/ILSpy/Images/Images.cs index b144b6ef9..dfe0f681a 100644 --- a/ILSpy/Images/Images.cs +++ b/ILSpy/Images/Images.cs @@ -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; + } } } diff --git a/ILSpy/MainWindow.xaml b/ILSpy/MainWindow.xaml index 4406128e3..aa7058009 100644 --- a/ILSpy/MainWindow.xaml +++ b/ILSpy/MainWindow.xaml @@ -83,19 +83,9 @@ - - + - - + diff --git a/ILSpy/MainWindow.xaml.cs b/ILSpy/MainWindow.xaml.cs index 22af195cd..7ede290dd 100644 --- a/ILSpy/MainWindow.xaml.cs +++ b/ILSpy/MainWindow.xaml.cs @@ -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 } 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 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[] ToolbarCommands { get; set; } + void MainWindow_Loaded(object sender, RoutedEventArgs e) { ILSpySettings spySettings = this.spySettings;