예제 #1
0
        public bool HasShortcut(IGlobalCommand command)
        {
            command.AssertParameterNotNull(nameof(command));
            if (!CommandManager.IsRegistered(command))
            {
                throw new Exception("Error : Requesting shortcut information of an unknown command : The command is not registered in the command manager.");
            }

            return(command.Metadata.OfType <KeyShortcut>().Any());
        }
예제 #2
0
        public void ClearShortcut(IGlobalCommand command)
        {
            command.AssertParameterNotNull(nameof(command));
            if (!CommandManager.IsRegistered(command))
            {
                throw new Exception($"Error : Requesting shortcut cleanup on an unknown command : The command is not registered in the CommandManager.");
            }

            if (HasShortcut(command))
            {
                var shortcut = GetShortcut(command);
                command.Metadata.Remove(shortcut);
                NotifyCommandShortcutsChanged(command);
            }
        }
예제 #3
0
        public static GlobalCommandShortcutInformation CreateFromGlobalCommand(IGlobalCommand command)
        {
            command.AssertParameterNotNull(nameof(command));

            var guid        = command.Metadata.OfType <CommandGuid>().Single().Guid;
            var hasShortcut = command.Metadata.OfType <KeyShortcut>().Any();

            return(new GlobalCommandShortcutInformation()
            {
                CommandGuid = guid,
                HasShortcut = hasShortcut,
                IsDefault = true,
                ModifierKeys = hasShortcut ? command.Metadata.OfType <KeyShortcut>().Single().ModifierKeys : ModifierKeys.None,
                Key = hasShortcut ? command.Metadata.OfType <KeyShortcut>().Single().Key : Key.None
            });
        }
예제 #4
0
        public KeyShortcut GetShortcut(IGlobalCommand command)
        {
            command.AssertParameterNotNull(nameof(command));

            if (!CommandManager.IsRegistered(command))
            {
                throw new Exception("Error : Requesting shortcut information of an unknown command : The command is not registered in the command manager.");
            }

            try
            {
                return(command.Metadata.OfType <KeyShortcut>().Single());
            }

            catch (InvalidOperationException)
            {
                throw new Exception("Error : The specified command doesn't have an associated shortcut.");
            }
        }
예제 #5
0
        public void SetShortcut(IGlobalCommand command, ModifierKeys modifierKeys, Key key)
        {
            command.AssertParameterNotNull(nameof(command));
            if (!CommandManager.IsRegistered(command))
            {
                throw new Exception("Error : Cannot set the shortcut for the specified command : The command is not registered in the command manager.");
            }

            if (!LoadingScope.IsInScope)
            {
                var matchingElements = GetElementsMatchingShortcut(modifierKeys, key).Where(o => !(o == command));
                if (matchingElements.Any())
                {
                    throw new Exception($"Error setting the shortcut {modifierKeys.ToString()}+{key.ToString()} on command {GetElementName(command)} " +
                                        $"because element(s) {string.Join(",", matchingElements.Select(o => GetElementName(o)))} already have a shortcut with the same " +
                                        $"key combination.");
                }
            }

            if (HasShortcut(command))
            {
                var shortcut = GetShortcut(command);
                if (shortcut.ModifierKeys == modifierKeys && shortcut.Key == key)
                {
                    return;
                }
                shortcut.ModifierKeys = modifierKeys;
                shortcut.Key          = key;
            }
            else
            {
                command.Metadata.Add(new KeyShortcut(modifierKeys, key));
            }

            NotifyCommandShortcutsChanged(command);
        }
예제 #6
0
 public bool Matches(IGlobalCommand command)
 {
     command.AssertParameterNotNull(nameof(command));
     return(CommandGuid == command.Metadata.OfType <CommandGuid>().Single().Guid);
 }