using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Input; using System.Windows.Media; using RainmeterStudio.UI.Controller; 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; } #region Display text property private string _displayText = null; /// /// Gets or sets the display text of the command /// public string DisplayText { get { if (_displayText == null) return Resources.Strings.ResourceManager.GetString(Name + "_DisplayText"); return _displayText; } set { _displayText = value; } } #endregion #region ToolTip property private string _toolTip = null; /// /// Gets or sets the tooltip /// public string ToolTip { get { if (_toolTip == null) return Resources.Strings.ResourceManager.GetString(Name + "_ToolTip"); return _toolTip; } set { _toolTip = value; } } #endregion #region Icon property private ImageSource _icon = null; /// /// Gets or sets the command's icon /// public ImageSource Icon { get { if (_icon == null) return IconProvider.GetIcon(Name); return _icon; } set { _icon = value; } } #endregion #region Keyboard shortcut property /// /// Gets or sets the keyboard shortcut of this command /// public KeyGesture Shortcut { get; set; } /// /// Gets the text representation of the keyboard shortcut /// public string ShortcutText { get { string text = String.Empty; if (Shortcut == null) return text; if ((Shortcut.Modifiers & ModifierKeys.Windows) != 0) text += "Win+"; if ((Shortcut.Modifiers & ModifierKeys.Control) != 0) text += "Ctrl+"; if ((Shortcut.Modifiers & ModifierKeys.Alt) != 0) text += "Alt+"; if ((Shortcut.Modifiers & ModifierKeys.Shift) != 0) text += "Shift+"; text += Enum.GetName(typeof(Key), Shortcut.Key); return text; } } #endregion #endregion #pragma warning disable 67 public event EventHandler CanExecuteChanged; #pragma warning restore 67 /// /// 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 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)); } } }