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 private KeyGesture _shortcut; /// /// Gets or sets the keyboard shortcut of this command /// public KeyGesture Shortcut { get { if (_shortcut == null) { string str = SettingsProvider.GetSetting(Name + "_Shortcut"); return GetKeyGestureFromString(str); } return _shortcut; } set { _shortcut = value; } } /// /// Gets the text representation of the keyboard shortcut /// public string ShortcutText { get { // Safety check if (Shortcut == null) return null; // Build string string text = String.Empty; 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; } set { Shortcut = GetKeyGestureFromString(value); } } private KeyGesture GetKeyGestureFromString(string k) { // Safety check if (k == null) return null; // Variables ModifierKeys mods = ModifierKeys.None; Key key = Key.None; // Parse each field foreach (var field in k.Split('+')) { // Trim surrounding white space string trimmed = field.Trim(); // Parse if (trimmed.Equals("Win", StringComparison.InvariantCultureIgnoreCase)) mods |= ModifierKeys.Windows; if (trimmed.Equals("Ctrl", StringComparison.InvariantCultureIgnoreCase)) mods |= ModifierKeys.Control; if (trimmed.Equals("Alt", StringComparison.InvariantCultureIgnoreCase)) mods |= ModifierKeys.Alt; if (trimmed.Equals("Shift", StringComparison.InvariantCultureIgnoreCase)) mods |= ModifierKeys.Shift; else Enum.TryParse(field, out key); } return new KeyGesture(key, mods); } #endregion #endregion public event EventHandler CanExecuteChanged; 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 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)); } } }