Unverified Commit cd6279c1 authored by LongCatIsLooong's avatar LongCatIsLooong Committed by GitHub

fix _getPixelPerfectCursorOffset logic when infinite (#66845)

parent 9efdcf61
...@@ -1999,15 +1999,17 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin { ...@@ -1999,15 +1999,17 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
offset.applyContentDimensions(0.0, _maxScrollExtent); offset.applyContentDimensions(0.0, _maxScrollExtent);
} }
/// Computes the offset to apply to the given [caretRect] so it perfectly
/// snaps to physical pixels.
Offset _getPixelPerfectCursorOffset(Rect caretRect) { Offset _getPixelPerfectCursorOffset(Rect caretRect) {
final Offset caretPosition = localToGlobal(caretRect.topLeft); final Offset caretPosition = localToGlobal(caretRect.topLeft);
final double pixelMultiple = 1.0 / _devicePixelRatio; final double pixelMultiple = 1.0 / _devicePixelRatio;
final double pixelPerfectOffsetX = caretPosition.dx.isFinite final double pixelPerfectOffsetX = caretPosition.dx.isFinite
? (caretPosition.dx / pixelMultiple).round() * pixelMultiple - caretPosition.dx ? (caretPosition.dx / pixelMultiple).round() * pixelMultiple - caretPosition.dx
: caretPosition.dx; : 0;
final double pixelPerfectOffsetY = caretPosition.dy.isFinite final double pixelPerfectOffsetY = caretPosition.dy.isFinite
? (caretPosition.dy / pixelMultiple).round() * pixelMultiple - caretPosition.dy ? (caretPosition.dy / pixelMultiple).round() * pixelMultiple - caretPosition.dy
: caretPosition.dy; : 0;
return Offset(pixelPerfectOffsetX, pixelPerfectOffsetY); return Offset(pixelPerfectOffsetX, pixelPerfectOffsetY);
} }
......
...@@ -3427,17 +3427,22 @@ void main() { ...@@ -3427,17 +3427,22 @@ void main() {
}); });
testWidgets( testWidgets(
'does not throw when sending infinite Rect', 'zero matrix paint transform',
(WidgetTester tester) async { (WidgetTester tester) async {
controller.value = TextEditingValue(text: 'a' * 100, composing: const TextRange(start: 0, end: 10)); controller.value = TextEditingValue(text: 'a' * 100, composing: const TextRange(start: 0, end: 10));
// Use a FittedBox with an zero-sized child to set the paint transform
// to the zero matrix.
await tester.pumpWidget(FittedBox(child: SizedBox.fromSize(size: Size.zero, child: builder()))); await tester.pumpWidget(FittedBox(child: SizedBox.fromSize(size: Size.zero, child: builder())));
await tester.showKeyboard(find.byType(EditableText)); await tester.showKeyboard(find.byType(EditableText));
expect(tester.testTextInput.log, contains(matchesMethodCall('TextInput.setMarkedTextRect', args: <String, dynamic> { expect(tester.testTextInput.log, contains(matchesMethodCall(
'width': -1, 'TextInput.setMarkedTextRect',
'height': -1, args: allOf(
'x': 0, containsPair('width', isNotNaN),
'y': 0, containsPair('height', isNotNaN),
}))); containsPair('x', isNotNaN),
containsPair('y', isNotNaN),
),
)));
}); });
}); });
...@@ -4695,7 +4700,7 @@ void main() { ...@@ -4695,7 +4700,7 @@ void main() {
final EditableTextState state = tester.state<EditableTextState>(find.byType(EditableText)); final EditableTextState state = tester.state<EditableTextState>(find.byType(EditableText));
final Rect rect = state.renderEditable.getLocalRectForCaret(const TextPosition(offset: 0)); final Rect rect = state.renderEditable.getLocalRectForCaret(const TextPosition(offset: 0));
expect(rect.isFinite, false); expect(rect.isFinite, true);
expect(tester.takeException(), isNull); expect(tester.takeException(), isNull);
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment