예제 #1
0
        public override bool CanExecute(object parameter)
        {
            var e = parameter as KeyEventArgs;

            if (e == null)
            {
                return(false);
            }
            if (!removalKeys.Contains(e.Key))
            {
                return(false);
            }

            var area           = selectionViewReader.GetCurrentSelectionArea();
            var lastLineLength = textViewReader.GetLineLength(caretViewReader.CaretPosition.Line);

            // if user pressed delete key and the caret is at the last position in the document and nothing is selected, then no text should be removed
            if (e.Key == Key.Delete && caretViewReader.CaretPosition.Line == textViewReader.LinesCount - 1 && caretViewReader.CaretPosition.Column == lastLineLength && area == null)
            {
                return(false);
            }
            // if user pressed backspace key and caret is at the first position in the document and nothing is selected, then no text should be removed
            if (e.Key == Key.Back && caretViewReader.CaretPosition.Line == 0 && caretViewReader.CaretPosition.Column == 0 && area == null)
            {
                return(false);
            }
            if (area?.StartPosition != null && area.EndPosition != null)
            {
                return(area.StartPosition != area.EndPosition);
            }

            return(true);
        }
예제 #2
0
        public override void Execute(object parameter)
        {
            var e                 = parameter as TextCompositionEventArgs;
            var selectionArea     = selectionViewReader.GetCurrentSelectionArea();
            var prevCaretPosition = caretViewReader.CaretPosition;

            UpdateCommandState(BeforeCommandExecutedState);

            if (selectionArea == null)
            {
                textView.EnterText(e.Text);
            }
            else
            {
                textView.ReplaceText(e.Text, selectionArea);
            }

            UpdateCommandState(AfterCommandExecutedState);

            caretView.HandleTextChange(e.Text);
            textView.Postbox.Put(new TextAddedMessage {
                Text = e.Text,
                PrevCaretPosition = prevCaretPosition,
                NewCaretPosition  = caretViewReader.CaretPosition
            });
        }