Skip to content

Commit

Permalink
Get selection boxes from textPainter and send them over the TextInput…
Browse files Browse the repository at this point in the history
…Connection
  • Loading branch information
fbcouch committed Jan 26, 2021
1 parent 84f3d28 commit 7b39621
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/flutter/lib/src/rendering/editable.dart
Expand Up @@ -850,6 +850,7 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
/// The text to display.
TextSpan? get text => _textPainter.text as TextSpan?;
final TextPainter _textPainter;
TextPainter? get textPainter => _textPainter;
set text(TextSpan? value) {
if (_textPainter.text == value)
return;
Expand Down
21 changes: 21 additions & 0 deletions packages/flutter/lib/src/services/text_input.dart
Expand Up @@ -909,6 +909,20 @@ class TextInputConnection {
}
}

List cachedTextBoxes = [];

void setSelectionRects(List textBoxes) {
var equal = cachedTextBoxes.length == textBoxes.length &&
List.generate(textBoxes.length, (i) => textBoxes[i] == cachedTextBoxes[i]).reduce((a, b) => a && b);
if (!equal) {
cachedTextBoxes = textBoxes;
TextInput._instance._setSelectionRects(textBoxes.map((box) {
var rect = box.toRect();
return <double>[rect.left, rect.top, rect.width, rect.height];
}).toList());
}
}

/// Send text styling information.
///
/// This information is used by the Flutter Web Engine to change the style
Expand Down Expand Up @@ -1234,6 +1248,13 @@ class TextInput {
);
}

void _setSelectionRects(List<List<double>> args) {
_channel.invokeMethod<void>(
'TextInput.setSelectionRects',
args,
);
}

void _setStyle(Map<String, dynamic> args) {
_channel.invokeMethod<void>(
'TextInput.setStyle',
Expand Down
4 changes: 4 additions & 0 deletions packages/flutter/lib/src/widgets/editable_text.dart
Expand Up @@ -2282,6 +2282,10 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
final Size size = renderEditable.size;
final Matrix4 transform = renderEditable.getTransformTo(null);
_textInputConnection.setEditableSizeAndTransform(size, transform);
var textSpan = buildTextSpan();
var rects = List.generate(
textSpan.text.length, (i) => renderEditable.textPainter.getBoxesForSelection(TextSelection(baseOffset: i, extentOffset: i + 1)).first);
_textInputConnection.setSelectionRects(rects);
SchedulerBinding.instance
.addPostFrameCallback((Duration _) => _updateSizeAndTransform());
}
Expand Down

0 comments on commit 7b39621

Please sign in to comment.