using System; using System.Windows.Input; using System.Windows.Media; using RainmeterStudio.Business; using RainmeterStudio.Core.Utils; namespace RainmeterStudio.UI { public class Command : ICommand { #region Private members private Action _execute; private Func _canExecute; private Action _executeNoParam; private Func _canExecuteNoParam; #endregion #region Public properties /// /// Gets or sets the name of the command /// public string Name { get; set; } /// /// Gets or sets the display text of the command /// public string DisplayText { get { return ResourceProvider.GetString("Command_" + Name + "_DisplayText"); } } /// /// Gets or sets the tooltip /// public string ToolTip { get { return ResourceProvider.GetString("Command_" + Name + "_ToolTip"); } } /// /// Gets or sets the command's icon /// public ImageSource Icon { get { return ResourceProvider.GetImage("Command_" + Name + "_Icon"); } } /// /// Gets or sets the keyboard shortcut of this command /// public KeyGesture Shortcut { get { string str = SettingsProvider.GetSetting("Command_" + Name + "_Shortcut"); return InputHelper.GetKeyGesture(str); } } /// /// Gets the text representation of the keyboard shortcut /// public string ShortcutText { get { return SettingsProvider.GetSetting("Command_" + Name + "_Shortcut"); } } #endregion /// /// Event triggered when the command execution status changes /// public event EventHandler CanExecuteChanged; /// /// Triggers the can execute changed event /// public void NotifyCanExecuteChanged() { if (CanExecuteChanged != null) CanExecuteChanged(this, new EventArgs()); } /// /// Initializes this command /// /// The name of the command /// Callback function to execute when the command is triggered /// Function that can be queried if the command can execute public Command(string name, Action execute, Func canExecute = null) { Name = name; _execute = execute; _canExecute = canExecute; } /// /// Initializes this command /// /// The name of the command /// Callback function to execute when the command is triggered /// Function that can be queried if the command can execute public Command(string name, Action execute, Func canExecute = null) { Name = name; _executeNoParam = execute; _canExecuteNoParam = canExecute; } /// /// Function that can be queried if the command can be executed /// /// Command parameter /// True if the function can be executed public virtual bool CanExecute(object parameter) { if (_canExecute != null) return _canExecute(parameter); else if (_canExecuteNoParam != null) return _canExecuteNoParam(); return true; } /// /// Executes the command /// /// Command parameter public virtual void Execute(object parameter) { if (_execute != null) _execute(parameter); else if (_executeNoParam != null) _executeNoParam(); } } public static partial class UIElementExtensions { /// /// Adds a keyboard shortcut to an UI element /// /// UI element /// Command public static void AddKeyBinding(this System.Windows.UIElement uiElement, Command command) { if (command.Shortcut != null) uiElement.InputBindings.Add(new KeyBinding(command, command.Shortcut)); } } }