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