From ae027c194ffa39e9bc3490f197f2b04cec3915b4 Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Fri, 28 Feb 2020 09:36:03 -0800 Subject: [PATCH] Use strut box heights to calculate selection rectangles in order to ensure that they remain within visible bounds (#50354) --- .../lib/src/material/selectable_text.dart | 2 +- .../flutter/lib/src/painting/text_painter.dart | 8 ++++++-- .../flutter/lib/src/widgets/editable_text.dart | 2 +- .../flutter/test/cupertino/text_field_test.dart | 6 ++++-- .../flutter/test/material/text_field_test.dart | 2 -- .../test/painting/text_painter_test.dart | 17 +++++++++++++++++ .../test/widgets/editable_text_cursor_test.dart | 1 - .../test/widgets/selectable_text_test.dart | 2 -- 8 files changed, 29 insertions(+), 11 deletions(-) diff --git a/packages/flutter/lib/src/material/selectable_text.dart b/packages/flutter/lib/src/material/selectable_text.dart index d9ac6ec66f..253529550c 100644 --- a/packages/flutter/lib/src/material/selectable_text.dart +++ b/packages/flutter/lib/src/material/selectable_text.dart @@ -591,7 +591,7 @@ class _SelectableTextState extends State with AutomaticKeepAlive showCursor: widget.showCursor, controller: _controller, focusNode: focusNode, - strutStyle: widget.strutStyle ?? StrutStyle.disabled, + strutStyle: widget.strutStyle ?? const StrutStyle(), textAlign: widget.textAlign ?? defaultTextStyle.textAlign ?? TextAlign.start, textDirection: widget.textDirection, textScaleFactor: widget.textScaleFactor, diff --git a/packages/flutter/lib/src/painting/text_painter.dart b/packages/flutter/lib/src/painting/text_painter.dart index a615c64892..a65949cd64 100644 --- a/packages/flutter/lib/src/painting/text_painter.dart +++ b/packages/flutter/lib/src/painting/text_painter.dart @@ -641,7 +641,9 @@ class TextPainter { List boxes = []; while (boxes.isEmpty && flattenedText != null) { final int prevRuneOffset = offset - graphemeClusterLength; - boxes = _paragraph.getBoxesForRange(prevRuneOffset, offset); + // Use BoxHeightStyle.strut to ensure that the caret's height fits within + // the line's height and is consistent throughout the line. + boxes = _paragraph.getBoxesForRange(prevRuneOffset, offset, boxHeightStyle: ui.BoxHeightStyle.strut); // When the range does not include a full cluster, no boxes will be returned. if (boxes.isEmpty) { // When we are at the beginning of the line, a non-surrogate position will @@ -690,7 +692,9 @@ class TextPainter { List boxes = []; while (boxes.isEmpty && flattenedText != null) { final int nextRuneOffset = offset + graphemeClusterLength; - boxes = _paragraph.getBoxesForRange(offset, nextRuneOffset); + // Use BoxHeightStyle.strut to ensure that the caret's height fits within + // the line's height and is consistent throughout the line. + boxes = _paragraph.getBoxesForRange(offset, nextRuneOffset, boxHeightStyle: ui.BoxHeightStyle.strut); // When the range does not include a full cluster, no boxes will be returned. if (boxes.isEmpty) { // When we are at the end of the line, a non-surrogate position will diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index 4847b4bb26..1b7a48406c 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -564,7 +564,7 @@ class EditableText extends StatefulWidget { /// [TextStyle] instead. See [StrutStyle.inheritFromTextStyle]. StrutStyle get strutStyle { if (_strutStyle == null) { - return style != null ? StrutStyle.fromTextStyle(style, forceStrutHeight: true) : StrutStyle.disabled; + return style != null ? StrutStyle.fromTextStyle(style, forceStrutHeight: true) : const StrutStyle(); } return _strutStyle.inheritFromTextStyle(style); } diff --git a/packages/flutter/test/cupertino/text_field_test.dart b/packages/flutter/test/cupertino/text_field_test.dart index eb13dfd01d..e472c6814c 100644 --- a/packages/flutter/test/cupertino/text_field_test.dart +++ b/packages/flutter/test/cupertino/text_field_test.dart @@ -4028,7 +4028,8 @@ void main() { await tester.longPressAt(textfieldStart + const Offset(50.0, 2.0)); await tester.pump(const Duration(milliseconds: 150)); - await tester.tapAt(textfieldStart + const Offset(20.0, 146.0)); + // Tap the Select All button. + await tester.tapAt(textfieldStart + const Offset(20.0, 100.0)); await tester.pump(const Duration(milliseconds: 300)); await expectLater( @@ -4074,7 +4075,8 @@ void main() { await tester.longPressAt(textfieldStart + const Offset(50.0, 2.0)); await tester.pump(const Duration(milliseconds: 150)); - await tester.tapAt(textfieldStart + const Offset(20.0, 146.0)); + // Tap the Select All button. + await tester.tapAt(textfieldStart + const Offset(20.0, 100.0)); await tester.pump(const Duration(milliseconds: 300)); await expectLater( diff --git a/packages/flutter/test/material/text_field_test.dart b/packages/flutter/test/material/text_field_test.dart index 8ea107ed6a..b851ccab89 100644 --- a/packages/flutter/test/material/text_field_test.dart +++ b/packages/flutter/test/material/text_field_test.dart @@ -2352,7 +2352,6 @@ void main() { controller: controller, style: const TextStyle(color: Colors.black, fontSize: 34.0), maxLines: 3, - strutStyle: StrutStyle.disabled, ), ), ); @@ -3740,7 +3739,6 @@ void main() { child: TextField( controller: controller, maxLines: 3, - strutStyle: StrutStyle.disabled, ), ), ), diff --git a/packages/flutter/test/painting/text_painter_test.dart b/packages/flutter/test/painting/text_painter_test.dart index 46f1d43bec..1fadc310dc 100644 --- a/packages/flutter/test/painting/text_painter_test.dart +++ b/packages/flutter/test/painting/text_painter_test.dart @@ -4,6 +4,7 @@ import 'dart:ui' as ui; +import 'package:flutter/foundation.dart'; import 'package:flutter/painting.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -816,4 +817,20 @@ void main() { // issue or similar. See https://github.com/flutter/flutter/issues/43763 // for more info. }, skip: true); + + test('TextPainter caret height and line height', () { + final TextPainter painter = TextPainter() + ..textDirection = TextDirection.ltr + ..strutStyle = const StrutStyle(fontSize: 50.0); + + const String text = 'A'; + painter.text = const TextSpan(text: text, style: TextStyle(height: 1.0)); + painter.layout(); + + final double caretHeight = painter.getFullHeightForCaret( + const ui.TextPosition(offset: 0), + ui.Rect.zero, + ); + expect(caretHeight, 50.0); + }, skip: kIsWeb); } diff --git a/packages/flutter/test/widgets/editable_text_cursor_test.dart b/packages/flutter/test/widgets/editable_text_cursor_test.dart index 7844c4c76f..2e1144a91c 100644 --- a/packages/flutter/test/widgets/editable_text_cursor_test.dart +++ b/packages/flutter/test/widgets/editable_text_cursor_test.dart @@ -658,7 +658,6 @@ void main() { controller: controller, focusNode: focusNode, style: textStyle, - strutStyle: StrutStyle.disabled, ), ), ), diff --git a/packages/flutter/test/widgets/selectable_text_test.dart b/packages/flutter/test/widgets/selectable_text_test.dart index b1c546206a..5c049eed65 100644 --- a/packages/flutter/test/widgets/selectable_text_test.dart +++ b/packages/flutter/test/widgets/selectable_text_test.dart @@ -995,7 +995,6 @@ void main() { dragStartBehavior: DragStartBehavior.down, style: TextStyle(color: Colors.black, fontSize: 34.0), maxLines: 3, - strutStyle: StrutStyle.disabled, ), ), ); @@ -1316,7 +1315,6 @@ void main() { child: SelectableText( text, maxLines: 3, - strutStyle: StrutStyle.disabled, ), ), ),