Esempio n. 1
0
        private static ICommand WrapCommand(ICommand command, IMenuCommandItem cmd)
        {
            if (!cmd.Shortcut.HasValue || !Enum.TryParse(cmd.Shortcut.Value.Key, out Key key))
            {
                return(command);
            }

            // ok, so this is terrible, but TextBox gestures handling is inside OnKeyDown
            // which is executed AFTER handling application wise shortcuts
            // However application wise shortcuts take higher priority
            // and effectively TextBox doesn't handle copy/paste/cut/undo/redo -.-
            var original = command;

            command = OverrideCommand <ICustomCopyPaste>(command, Key.C, key, cmd, tb => tb.DoCopy((IClipboard)AvaloniaLocator.Current.GetService(typeof(IClipboard))));
            command = OverrideCommand <ICustomCopyPaste>(command, Key.V, key, cmd, tb => tb.DoPaste().ListenErrors());
            command = OverrideCommand <TextBox>(command, Key.C, key, cmd, tb => tb.Copy());
            command = OverrideCommand <TextBox>(command, Key.X, key, cmd, tb => tb.Cut());
            command = OverrideCommand <TextBox>(command, Key.V, key, cmd, tb => tb.Paste());
            command = OverrideCommand <TextBox>(command, Key.Z, key, cmd, Undo);
            command = OverrideCommand <TextBox>(command, Key.Y, key, cmd, Redo);
            command = OverrideCommand <FixedTextBox>(command, Key.V, key, cmd, tb => tb.CustomPaste());

            command = OverrideCommand <TextArea>(command, Key.Z, key, cmd, tb =>
            {
                var te = GetTextEditor(tb);
                if (te == null || te.Document.UndoStack.SizeLimit == 0)
                {
                    return(false);
                }
                te.Undo();
                return(true);
            });
            command = OverrideCommand <TextArea>(command, Key.Y, key, cmd, tb =>
            {
                var te = GetTextEditor(tb);
                if (te == null || te.Document.UndoStack.SizeLimit == 0)
                {
                    return(false);
                }
                te.Redo();
                return(true);
            });
            command = OverrideCommand <TextArea>(command, Key.C, key, cmd, tb => ApplicationCommands.Copy.Execute(null, tb));
            command = OverrideCommand <TextArea>(command, Key.X, key, cmd, tb => ApplicationCommands.Cut.Execute(null, tb));
            command = OverrideCommand <TextArea>(command, Key.V, key, cmd, tb => ApplicationCommands.Paste.Execute(null, tb));

            var newCommand = new DelegateCommand(() => command.Execute(null), () => command.CanExecute(null));

            original.CanExecuteChanged += (_, _) => newCommand.RaiseCanExecuteChanged();
            return(newCommand);
        }
Esempio n. 2
0
        private static ICommand OverrideCommand <T>(ICommand command, Key require, Key commandKey, IMenuCommandItem item, Action <T> func)
        {
            if (require != commandKey || !(item.Shortcut?.Control ?? false))
            {
                return(command);
            }

            return(new DelegateCommand(() =>
            {
                if (FocusManager.Instance.Current is T t)
                {
                    func(t);
                }
                else if (command.CanExecute(null))
                {
                    command.Execute(null);
                }
            }, () =>
            {
                if (FocusManager.Instance.Current is T t)
                {
                    return true;
                }
                return command.CanExecute(null);
            }));
        }
Esempio n. 3
0
        private static ICommand OverrideCommand <T>(ICommand command, Key require, KeyModifiers requireModifier, Key commandKey, KeyModifiers commandModifier, IMenuCommandItem item, Func <T, bool> func)
        {
            if (require != commandKey || !(item.Shortcut?.Control ?? false) || requireModifier != commandModifier)
            {
                return(command);
            }

            return(new DelegateCommand(() =>
            {
                bool executed = false;
                if (FocusManager.Instance.Current is T t)
                {
                    executed = func(t);
                }
                if (!executed && command.CanExecute(null))
                {
                    command.Execute(null);
                }
            }, () =>
            {
                if (FocusManager.Instance.Current is T t)
                {
                    return true;
                }
                return command.CanExecute(null);
            }));
        }