private void OnNewInputGestureTextBoxKeyDown(object sender, KeyEventArgs e) { e.Handled = true; var vm = ViewModel as KeyboardMappingsCustomizationViewModel; if (vm != null) { var modifiers = Keyboard.Modifiers; if (modifiers == ModifierKeys.Shift) { return; } var key = e.Key; if (!IsValidKey(key)) { return; } var inputGesture = new InputGesture(key, modifiers); vm.SelectedCommandNewInputGesture = inputGesture; } }
/// <summary> /// Creates the command inside the command manager. /// <para /> /// If the <paramref name="throwExceptionWhenCommandIsAlreadyCreated"/> is <c>false</c> and the command is already created, only /// the input gesture is updated for the existing command. /// </summary> /// <param name="commandName">Name of the command.</param> /// <param name="inputGesture">The input gesture.</param> /// <param name="compositeCommand">The composite command. If <c>null</c>, this will default to a new instance of <see cref="CompositeCommand" />.</param> /// <param name="throwExceptionWhenCommandIsAlreadyCreated">if set to <c>true</c>, this method will throw an exception when the command is already created.</param> /// <exception cref="ArgumentException">The <paramref name="commandName" /> is <c>null</c> or whitespace.</exception> /// <exception cref="InvalidOperationException">The specified command is already created using the <see cref="CreateCommand" /> method.</exception> public void CreateCommand(string commandName, InputGesture inputGesture = null, ICompositeCommand compositeCommand = null, bool throwExceptionWhenCommandIsAlreadyCreated = true) { Argument.IsNotNullOrWhitespace("commandName", commandName); lock (_lockObject) { Log.Debug("Creating command '{0}' with input gesture '{1}'", commandName, ObjectToStringHelper.ToString(inputGesture)); if (_commands.ContainsKey(commandName)) { var error = $"Command '{commandName}' is already created using the CreateCommand method"; Log.Error(error); if (throwExceptionWhenCommandIsAlreadyCreated) { throw new InvalidOperationException(error); } _commandGestures[commandName] = inputGesture; return; } if (compositeCommand == null) { compositeCommand = new CompositeCommand(); } _commands.Add(commandName, compositeCommand); _originalCommandGestures.Add(commandName, inputGesture); _commandGestures.Add(commandName, inputGesture); CommandCreated.SafeInvoke(this, () => new CommandCreatedEventArgs(compositeCommand, commandName)); } }
public void CorrectlyReturnsStringAfterUpdateModifierKeys() { var inputGesture = new InputGesture(Key.A, ModifierKeys.Control | ModifierKeys.Shift); inputGesture.ToString(); inputGesture.Modifiers |= ModifierKeys.Alt; Assert.AreEqual("Alt + Control + Shift + A", inputGesture.ToString()); }
public void CorrectlyReturnsStringAfterUpdateKey() { var inputGesture = new InputGesture(Key.A, ModifierKeys.Control | ModifierKeys.Shift); inputGesture.ToString(); inputGesture.Key = Key.B; Assert.AreEqual("Control + Shift + B", inputGesture.ToString()); }
public void TheFindCommandsByGestureMethod(Key key, ModifierKeys modifierKeys, bool expectedToBeAvailable) { var commandManager = new CommandManager(); commandManager.CreateCommand("CtrlA", new InputGesture(Key.A, ModifierKeys.Control)); var inputGesture = new InputGesture(key, modifierKeys); var existingCommands = commandManager.FindCommandsByGesture(inputGesture); Assert.AreEqual(expectedToBeAvailable, existingCommands.Any()); }
/// <summary> /// Updates the input gesture for the specified command. /// </summary> /// <param name="commandName">Name of the command.</param> /// <param name="inputGesture">The new input gesture.</param> /// <exception cref="ArgumentException">The <paramref name="commandName"/> is <c>null</c> or whitespace.</exception> /// <exception cref="InvalidOperationException">The specified command is not created using the <see cref="CreateCommand"/> method.</exception> public void UpdateInputGesture(string commandName, InputGesture inputGesture = null) { Argument.IsNotNullOrWhitespace("commandName", commandName); lock (_lockObject) { Log.Debug("Updating input gesture of command '{0}' to '{1}'", commandName, ObjectToStringHelper.ToString(inputGesture)); if (!_commands.ContainsKey(commandName)) { throw Log.ErrorAndCreateException <InvalidOperationException>("Command '{0}' is not yet created using the CreateCommand method", commandName); } _commandGestures[commandName] = inputGesture; } }
public void CorrectlySerializesAndDeserializes() { var inputGesture = new InputGesture(Key.A, ModifierKeys.Control | ModifierKeys.Shift); var xmlSerializer = SerializationFactory.GetXmlSerializer(); using (var memoryStream = new MemoryStream()) { xmlSerializer.Serialize(inputGesture, memoryStream, null); memoryStream.Position = 0L; var finalInputGesture = xmlSerializer.Deserialize(typeof (InputGesture), memoryStream, null); Assert.AreEqual(inputGesture, finalInputGesture); } }
public void CorrectlySerializesAndDeserializes() { var inputGesture = new InputGesture(Key.A, ModifierKeys.Control | ModifierKeys.Shift); var xmlSerializer = SerializationFactory.GetXmlSerializer(); using (var memoryStream = new MemoryStream()) { xmlSerializer.Serialize(inputGesture, memoryStream); memoryStream.Position = 0L; var finalInputGesture = xmlSerializer.Deserialize(typeof(InputGesture), memoryStream); Assert.AreEqual(inputGesture, finalInputGesture); } }
/// <summary> /// Finds the commands inside the <see cref="ICommandManager"/> by gesture. /// </summary> /// <param name="commandManager">The command manager.</param> /// <param name="inputGesture">The input gesture.</param> /// <returns>Dictionary<System.String, ICommand>.</returns> /// <exception cref="ArgumentNullException">The <paramref name="commandManager"/> is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">The <paramref name="inputGesture"/> is <c>null</c>.</exception> public static Dictionary<string, ICommand> FindCommandsByGesture(this ICommandManager commandManager, InputGesture inputGesture) { Argument.IsNotNull("commandManager", commandManager); Argument.IsNotNull("inputGesture", inputGesture); var commands = new Dictionary<string, ICommand>(); foreach (var commandName in commandManager.GetCommands()) { var commandInputGesture = commandManager.GetInputGesture(commandName); if (inputGesture.Equals(commandInputGesture)) { commands[commandName] = commandManager.GetCommand(commandName); } } return commands; }
public void SecondCallExecutesFasterThanFirstOne() { var inputGesture = new InputGesture(Key.A, ModifierKeys.Control | ModifierKeys.Shift); Stopwatch stopwatch1 = new Stopwatch(); stopwatch1.Start(); inputGesture.ToString(); stopwatch1.Stop(); Stopwatch stopwatch2 = new Stopwatch(); stopwatch2.Start(); inputGesture.ToString(); stopwatch2.Stop(); Assert.Less(stopwatch2.Elapsed, stopwatch1.Elapsed); }
private void OnNewInputGestureTextBoxKeyDown(object sender, KeyEventArgs e) { e.Handled = true; var vm = ViewModel as KeyboardMappingsCustomizationViewModel; if (vm != null) { var modifiers = Keyboard.Modifiers; var key = e.Key; if (!IsValidKey(key)) { return; } var inputGesture = new InputGesture(key, modifiers); vm.SelectedCommandNewInputGesture = inputGesture; } }
/// <summary> /// Creates the command inside the command manager. /// </summary> /// <param name="commandName">Name of the command.</param> /// <param name="inputGesture">The input gesture.</param> /// <param name="compositeCommand">The composite command. If <c>null</c>, this will default to a new instance of <see cref="CompositeCommand"/>.</param> /// <exception cref="ArgumentException">The <paramref name="commandName"/> is <c>null</c> or whitespace.</exception> /// <exception cref="InvalidOperationException">The specified command is already created using the <see cref="CreateCommand"/> method.</exception> public void CreateCommand(string commandName, InputGesture inputGesture = null, ICompositeCommand compositeCommand = null) { Argument.IsNotNullOrWhitespace("commandName", commandName); lock (_lockObject) { Log.Debug("Creating command '{0}' with input gesture '{1}'", commandName, ObjectToStringHelper.ToString(inputGesture)); if (_commands.ContainsKey(commandName)) { string error = string.Format("Command '{0}' is already created using the CreateCommand method", commandName); Log.Error(error); throw new InvalidOperationException(error); } _commands.Add(commandName, compositeCommand ?? new CompositeCommand()); _commandGestures.Add(commandName, inputGesture); } }
/// <summary> /// Determines whether the specified input gesture is empty. /// </summary> /// <param name="inputGesture">The input gesture.</param> /// <returns><c>true</c> if the specified input gesture is empty; otherwise, <c>false</c>.</returns> public static bool IsEmpty(this Catel.Windows.Input.InputGesture inputGesture) { if (inputGesture is null) { return(true); } #if NET || NETCORE if (inputGesture.Key != Key.None) { return(false); } if (inputGesture.Modifiers != ModifierKeys.None) { return(false); } #endif return(true); }
/// <summary> /// Creates the command inside the command manager. /// <para /> /// If the <paramref name="throwExceptionWhenCommandIsAlreadyCreated"/> is <c>false</c> and the command is already created, only /// the input gesture is updated for the existing command. /// </summary> /// <param name="commandName">Name of the command.</param> /// <param name="inputGesture">The input gesture.</param> /// <param name="compositeCommand">The composite command. If <c>null</c>, this will default to a new instance of <see cref="CompositeCommand" />.</param> /// <param name="throwExceptionWhenCommandIsAlreadyCreated">if set to <c>true</c>, this method will throw an exception when the command is already created.</param> /// <exception cref="ArgumentException">The <paramref name="commandName" /> is <c>null</c> or whitespace.</exception> /// <exception cref="InvalidOperationException">The specified command is already created using the <see cref="CreateCommand" /> method.</exception> public void CreateCommand(string commandName, InputGesture inputGesture = null, ICompositeCommand compositeCommand = null, bool throwExceptionWhenCommandIsAlreadyCreated = true) { Argument.IsNotNullOrWhitespace("commandName", commandName); lock (_lockObject) { Log.Debug("Creating command '{0}' with input gesture '{1}'", commandName, ObjectToStringHelper.ToString(inputGesture)); if (_commands.ContainsKey(commandName)) { var error = string.Format("Command '{0}' is already created using the CreateCommand method", commandName); Log.Error(error); if (throwExceptionWhenCommandIsAlreadyCreated) { throw new InvalidOperationException(error); } _commandGestures[commandName] = inputGesture; return; } if (compositeCommand == null) { compositeCommand = new CompositeCommand(); } _commands.Add(commandName, compositeCommand); _originalCommandGestures.Add(commandName, inputGesture); _commandGestures.Add(commandName, inputGesture); InvalidateCommands(); CommandCreated.SafeInvoke(this, new CommandCreatedEventArgs(compositeCommand, commandName)); } }
public void TheIsEmptyMethod(Key key, ModifierKeys modifierKeys, bool expectedValue) { var inputGesture = new InputGesture(key, modifierKeys); Assert.AreEqual(expectedValue, inputGesture.IsEmpty()); }
public static void SetTooltip(this System.Windows.UIElement control, Catel.Windows.Input.InputGesture inputGesture, string text) { }
public void CorrectlyReturnsStringWithoutModifiers() { var inputGesture = new InputGesture(Key.A, ModifierKeys.None); Assert.AreEqual("A", inputGesture.ToString()); }
public void CorrectlyReturnsStringWithSingleModifier() { var inputGesture = new InputGesture(Key.A, ModifierKeys.Control); Assert.AreEqual("Control + A", inputGesture.ToString()); }
/// <summary> /// Updates the input gesture for the specified command. /// </summary> /// <param name="commandName">Name of the command.</param> /// <param name="inputGesture">The new input gesture.</param> /// <exception cref="ArgumentException">The <paramref name="commandName"/> is <c>null</c> or whitespace.</exception> /// <exception cref="InvalidOperationException">The specified command is not created using the <see cref="CreateCommand"/> method.</exception> public void UpdateInputGesture(string commandName, InputGesture inputGesture = null) { Argument.IsNotNullOrWhitespace("commandName", commandName); lock (_lockObject) { Log.Debug("Updating input gesture of command '{0}' to '{1}'", commandName, ObjectToStringHelper.ToString(inputGesture)); if (!_commands.ContainsKey(commandName)) { var error = string.Format("Command '{0}' is not yet created using the CreateCommand method", commandName); Log.Error(error); throw new InvalidOperationException(error); } _commandGestures[commandName] = inputGesture; } }
/// <summary> /// Creates a command using a naming convention with the specified gesture. /// </summary> /// <param name="commandManager">The command manager.</param> /// <param name="containerType">Type of the container.</param> /// <param name="commandNameFieldName">Name of the command name field.</param> /// <exception cref="ArgumentNullException">The <paramref name="commandManager"/> is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">The <paramref name="containerType"/> is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">The <paramref name="commandNameFieldName"/> is <c>null</c>.</exception> public static void CreateCommandWithGesture(this ICommandManager commandManager, Type containerType, string commandNameFieldName) { Argument.IsNotNull(() => commandManager); Argument.IsNotNull(() => containerType); Argument.IsNotNullOrWhitespace(() => commandNameFieldName); Log.Debug("Creating command '{0}'", commandNameFieldName); var commandNameField = containerType.GetFieldEx(commandNameFieldName, BindingFlags.Public | BindingFlags.Static); if (commandNameField == null) { Log.ErrorAndThrowException <InvalidOperationException>("Command '{0}' is not available on container type '{1}'", commandNameFieldName, containerType.GetSafeFullName()); } var commandName = (string)commandNameField.GetValue(null); if (commandManager.IsCommandCreated(commandName)) { Log.Debug("Command '{0}' is already created, skipping...", commandName); return; } InputGesture commandInputGesture = null; var inputGestureField = containerType.GetFieldEx(string.Format("{0}InputGesture", commandNameFieldName), BindingFlags.Public | BindingFlags.Static); if (inputGestureField != null) { commandInputGesture = inputGestureField.GetValue(null) as InputGesture; } commandManager.CreateCommand(commandName, commandInputGesture); var commandContainerName = string.Format("{0}CommandContainer", commandName.Replace(".", string.Empty)); var commandContainerType = (from type in TypeCache.GetTypes() where string.Equals(type.Name, commandContainerName, StringComparison.OrdinalIgnoreCase) select type).FirstOrDefault(); if (commandContainerType != null) { Log.Debug("Found command container '{0}', registering it in the ServiceLocator now", commandContainerType.GetSafeFullName()); var serviceLocator = commandManager.GetServiceLocator(); if (!serviceLocator.IsTypeRegistered(commandContainerType)) { var typeFactory = serviceLocator.ResolveType <ITypeFactory>(); var commandContainer = typeFactory.CreateInstance(commandContainerType); if (commandContainer != null) { serviceLocator.RegisterInstance(commandContainer); } else { Log.Warning("Cannot create command container '{0}', skipping registration", commandContainerType.GetSafeFullName()); } } } }
/// <summary> /// Finds the commands inside the <see cref="ICommandManager"/> by gesture. /// </summary> /// <param name="commandManager">The command manager.</param> /// <param name="inputGesture">The input gesture.</param> /// <returns>Dictionary<System.String, ICommand>.</returns> /// <exception cref="ArgumentNullException">The <paramref name="commandManager"/> is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">The <paramref name="inputGesture"/> is <c>null</c>.</exception> public static Dictionary <string, ICommand> FindCommandsByGesture(this ICommandManager commandManager, InputGesture inputGesture) { Argument.IsNotNull(() => commandManager); Argument.IsNotNull(() => inputGesture); var commands = new Dictionary <string, ICommand>(); foreach (var commandName in commandManager.GetCommands()) { var commandInputGesture = commandManager.GetInputGesture(commandName); if (inputGesture.Equals(commandInputGesture)) { commands[commandName] = commandManager.GetCommand(commandName); } } return(commands); }
/// <summary> /// Creates a command using a naming convention with the specified gesture. /// </summary> /// <param name="commandManager">The command manager.</param> /// <param name="containerType">Type of the container.</param> /// <param name="commandNameFieldName">Name of the command name field.</param> /// <exception cref="ArgumentNullException">The <paramref name="commandManager"/> is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">The <paramref name="containerType"/> is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">The <paramref name="commandNameFieldName"/> is <c>null</c>.</exception> public static void CreateCommandWithGesture(this ICommandManager commandManager, Type containerType, string commandNameFieldName) { Argument.IsNotNull("commandManager", commandManager); Argument.IsNotNull("containerType", containerType); Argument.IsNotNullOrWhitespace("commandNameFieldName", commandNameFieldName); Log.Debug("Creating command '{0}'", commandNameFieldName); // Note: we must store bindingflags inside variable otherwise invalid IL will be generated var bindingFlags = BindingFlags.Public | BindingFlags.Static; var commandNameField = containerType.GetFieldEx(commandNameFieldName, bindingFlags); if (commandNameField is null) { throw Log.ErrorAndCreateException <InvalidOperationException>("Command '{0}' is not available on container type '{1}'", commandNameFieldName, containerType.GetSafeFullName(false)); } var commandName = (string)commandNameField.GetValue(null); if (commandManager.IsCommandCreated(commandName)) { Log.Debug("Command '{0}' is already created, skipping...", commandName); return; } InputGesture commandInputGesture = null; var inputGestureField = containerType.GetFieldEx(string.Format("{0}InputGesture", commandNameFieldName), bindingFlags); if (inputGestureField != null) { commandInputGesture = inputGestureField.GetValue(null) as InputGesture; } commandManager.CreateCommand(commandName, commandInputGesture); var commandContainerName = string.Format("{0}CommandContainer", commandName.Replace(".", string.Empty)); // https://github.com/Catel/Catel/issues/1383: CommandManager.CreateCommandWithGesture does not create CommandContainer var commandContainerType = (from type in TypeCache.GetTypes(allowInitialization: true) where string.Equals(type.Name, commandContainerName, StringComparison.OrdinalIgnoreCase) select type).FirstOrDefault(); if (commandContainerType is null) { Log.Debug("Couldn't find command container '{0}', you will need to add a custom action or command manually in order to make the CompositeCommand useful", commandContainerName); return; } Log.Debug("Found command container '{0}', registering it in the ServiceLocator now", commandContainerType.GetSafeFullName(false)); var serviceLocator = commandManager.GetServiceLocator(); if (!serviceLocator.IsTypeRegistered(commandContainerType)) { var typeFactory = serviceLocator.ResolveType <ITypeFactory>(); var commandContainer = typeFactory.CreateInstance(commandContainerType); if (commandContainer != null) { serviceLocator.RegisterInstance(commandContainerType, commandContainer); } else { Log.Warning("Cannot create command container '{0}', skipping registration", commandContainerType.GetSafeFullName(false)); } } }
public void CorrectlyReturnsStringWithMultipleModifier() { var inputGesture = new InputGesture(Key.A, ModifierKeys.Control | ModifierKeys.Shift); Assert.AreEqual("Control + Shift + A", inputGesture.ToString()); }