Skip to content

Commit

Permalink
Revert "Shortcuts (flutter#21083)" (flutter#21556)
Browse files Browse the repository at this point in the history
This reverts commit 6e7f3e6.
  • Loading branch information
jonahwilliams authored Sep 7, 2018
1 parent 6e7f3e6 commit 11943ce
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 386 deletions.
98 changes: 3 additions & 95 deletions packages/flutter/lib/src/rendering/editable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,14 @@ class RenderEditable extends RenderBox {
Locale locale,
double cursorWidth = 1.0,
Radius cursorRadius,
@required this.textSelectionDelegate,
}) : assert(textAlign != null),
assert(textDirection != null, 'RenderEditable created without a textDirection.'),
assert(maxLines == null || maxLines > 0),
assert(textScaleFactor != null),
assert(offset != null),
assert(ignorePointer != null),
assert(obscureText != null),
assert(textSelectionDelegate != null),
_textPainter = new TextPainter(
_textPainter = new TextPainter(
text: text,
textAlign: textAlign,
textDirection: textDirection,
Expand Down Expand Up @@ -198,24 +196,12 @@ class RenderEditable extends RenderBox {
markNeedsSemanticsUpdate();
}

/// The object that controls the text selection, used by this render object
/// for implementing cut, copy, and paste keyboard shortcuts.
///
/// It must not be null. It will make cut, copy and paste functionality work
/// with the most recently set [TextSelectionDelegate].
TextSelectionDelegate textSelectionDelegate;

Rect _lastCaretRect;

static const int _kLeftArrowCode = 21;
static const int _kRightArrowCode = 22;
static const int _kUpArrowCode = 19;
static const int _kDownArrowCode = 20;
static const int _kXKeyCode = 52;
static const int _kCKeyCode = 31;
static const int _kVKeyCode = 50;
static const int _kAKeyCode = 29;
static const int _kDelKeyCode = 112;

// The extent offset of the current selection
int _extentOffset = -1;
Expand All @@ -235,7 +221,7 @@ class RenderEditable extends RenderBox {
static const int _kControlMask = 1 << 12; // https://developer.android.com/reference/android/view/KeyEvent.html#META_CTRL_ON

// TODO(goderbauer): doesn't handle extended grapheme clusters with more than one Unicode scalar value (https://github.com/flutter/flutter/issues/13404).
void _handleKeyEvent(RawKeyEvent keyEvent) {
void _handleKeyEvent(RawKeyEvent keyEvent){
if (defaultTargetPlatform != TargetPlatform.android)
return;

Expand All @@ -260,11 +246,6 @@ class RenderEditable extends RenderBox {
final bool upArrow = pressedKeyCode == _kUpArrowCode;
final bool downArrow = pressedKeyCode == _kDownArrowCode;
final bool arrow = leftArrow || rightArrow || upArrow || downArrow;
final bool aKey = pressedKeyCode == _kAKeyCode;
final bool xKey = pressedKeyCode == _kXKeyCode;
final bool vKey = pressedKeyCode == _kVKeyCode;
final bool cKey = pressedKeyCode == _kCKeyCode;
final bool del = pressedKeyCode == _kDelKeyCode;

// We will only move select or more the caret if an arrow is pressed
if (arrow) {
Expand All @@ -281,12 +262,7 @@ class RenderEditable extends RenderBox {
newOffset = _handleShift(rightArrow, leftArrow, shift, newOffset);

_extentOffset = newOffset;
} else if (ctrl && (xKey || vKey || cKey || aKey)) {
// _handleShortcuts depends on being started in the same stack invocation as the _handleKeyEvent method
_handleShortcuts(pressedKeyCode);
}
if (del)
_handleDelete();
}

// Handles full word traversal using control.
Expand Down Expand Up @@ -392,7 +368,7 @@ class RenderEditable extends RenderBox {
onSelectionChanged(
new TextSelection.fromPosition(
new TextPosition(
offset: newOffset
offset: newOffset
)
),
this,
Expand All @@ -402,74 +378,6 @@ class RenderEditable extends RenderBox {
return newOffset;
}

// Handles shortcut functionality including cut, copy, paste and select all
// using control + (X, C, V, A).
void _handleShortcuts(int pressedKeyCode) async {
switch (pressedKeyCode) {
case _kCKeyCode:
if (!selection.isCollapsed) {
Clipboard.setData(
new ClipboardData(text: selection.textInside(text.text)));
}
break;
case _kXKeyCode:
if (!selection.isCollapsed) {
Clipboard.setData(
new ClipboardData(text: selection.textInside(text.text)));
textSelectionDelegate.textEditingValue = new TextEditingValue(
text: selection.textBefore(text.text)
+ selection.textAfter(text.text),
selection: new TextSelection.collapsed(offset: selection.start),
);
}
break;
case _kVKeyCode:
// Snapshot the input before using `await`.
// See https://github.com/flutter/flutter/issues/11427
final TextEditingValue value = textSelectionDelegate.textEditingValue;
final ClipboardData data = await Clipboard.getData(Clipboard.kTextPlain);
if (data != null) {
textSelectionDelegate.textEditingValue = new TextEditingValue(
text: value.selection.textBefore(value.text)
+ data.text
+ value.selection.textAfter(value.text),
selection: new TextSelection.collapsed(
offset: value.selection.start + data.text.length
),
);
}
break;
case _kAKeyCode:
_baseOffset = 0;
_extentOffset = textSelectionDelegate.textEditingValue.text.length;
onSelectionChanged(
new TextSelection(
baseOffset: 0,
extentOffset: textSelectionDelegate.textEditingValue.text.length,
),
this,
SelectionChangedCause.keyboard,
);
break;
default:
assert(false);
}
}

int _handleDelete() {
if (selection.textAfter(text.text).isNotEmpty) {
textSelectionDelegate.textEditingValue = new TextEditingValue(
text: selection.textBefore(text.text)
+ selection.textAfter(text.text).substring(1),
selection: new TextSelection.collapsed(offset: selection.start));
} else {
textSelectionDelegate.textEditingValue = new TextEditingValue(
text: selection.textBefore(text.text),
selection: new TextSelection.collapsed(offset: selection.start)
);
}
}

/// Marks the render object as needing to be laid out again and have its text
/// metrics recomputed.
///
Expand Down
17 changes: 0 additions & 17 deletions packages/flutter/lib/src/services/text_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -534,23 +534,6 @@ class TextEditingValue {
);
}

/// An interface for manipulating the selection, to be used by the implementor
/// of the toolbar widget.
abstract class TextSelectionDelegate {
/// Gets the current text input.
TextEditingValue get textEditingValue;

/// Sets the current text input (replaces the whole line).
set textEditingValue(TextEditingValue value);

/// Hides the text selection toolbar.
void hideToolbar();

/// Brings the provided [TextPosition] into the visible area of the text
/// input.
void bringIntoView(TextPosition position);
}

/// An interface to receive information from [TextInput].
///
/// See also:
Expand Down
7 changes: 1 addition & 6 deletions packages/flutter/lib/src/widgets/editable_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,6 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
rendererIgnoresPointer: widget.rendererIgnoresPointer,
cursorWidth: widget.cursorWidth,
cursorRadius: widget.cursorRadius,
textSelectionDelegate: this,
),
),
);
Expand Down Expand Up @@ -968,7 +967,6 @@ class _Editable extends LeafRenderObjectWidget {
this.rendererIgnoresPointer = false,
this.cursorWidth,
this.cursorRadius,
this.textSelectionDelegate,
}) : assert(textDirection != null),
assert(rendererIgnoresPointer != null),
super(key: key);
Expand All @@ -992,7 +990,6 @@ class _Editable extends LeafRenderObjectWidget {
final bool rendererIgnoresPointer;
final double cursorWidth;
final Radius cursorRadius;
final TextSelectionDelegate textSelectionDelegate;

@override
RenderEditable createRenderObject(BuildContext context) {
Expand All @@ -1015,7 +1012,6 @@ class _Editable extends LeafRenderObjectWidget {
obscureText: obscureText,
cursorWidth: cursorWidth,
cursorRadius: cursorRadius,
textSelectionDelegate: textSelectionDelegate,
);
}

Expand All @@ -1039,7 +1035,6 @@ class _Editable extends LeafRenderObjectWidget {
..ignorePointer = rendererIgnoresPointer
..obscureText = obscureText
..cursorWidth = cursorWidth
..cursorRadius = cursorRadius
..textSelectionDelegate = textSelectionDelegate;
..cursorRadius = cursorRadius;
}
}
19 changes: 17 additions & 2 deletions packages/flutter/lib/src/widgets/text_selection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import 'gesture_detector.dart';
import 'overlay.dart';
import 'transitions.dart';

export 'package:flutter/services.dart' show TextSelectionDelegate;

/// Which type of selection handle to be displayed.
///
/// With mixed-direction text, both handles may be the same type. Examples:
Expand Down Expand Up @@ -62,6 +60,23 @@ enum _TextSelectionHandlePosition { start, end }
/// Used by [TextSelectionOverlay.onSelectionOverlayChanged].
typedef void TextSelectionOverlayChanged(TextEditingValue value, Rect caretRect);

/// An interface for manipulating the selection, to be used by the implementor
/// of the toolbar widget.
abstract class TextSelectionDelegate {
/// Gets the current text input.
TextEditingValue get textEditingValue;

/// Sets the current text input (replaces the whole line).
set textEditingValue(TextEditingValue value);

/// Hides the text selection toolbar.
void hideToolbar();

/// Brings the provided [TextPosition] into the visible area of the text
/// input.
void bringIntoView(TextPosition position);
}

/// An interface for building the selection UI, to be provided by the
/// implementor of the toolbar widget.
///
Expand Down
Loading

0 comments on commit 11943ce

Please sign in to comment.