Skip to content

Commit

Permalink
Changing TextPainter.getOffsetForCaret implementation to remove the…
Browse files Browse the repository at this point in the history
… logarithmic search (#143281)

The behavior largely remains the same, except:

1. The EOT cursor `(textLength, downstream)` for text ending in the opposite writing direction as the paragraph is now placed at the visual end of the last line. 
  For example, in a LTR paragraph, the EOT cursor for `aA` (lowercase for LTR and uppercase for RTL) is placed to the right of the line: `aA|` (it was `a|A` before). 
  This matches the behavior of most applications that do logical order arrow key navigation instead of visual order navigation. 
  And it makes the navigation order consistent for `aA\naA`:
```
  |aA    =>  aA|  => aA|  => aA  => aA   => aA 
   aA        aA      aA     |aA     aA|     aA|     
   (1)       (2)     (3)    (4)    (5)      (6)
```
This is indeed still pretty confusing as (2) and (3), as well as (5) and (6) are hard to distinguish (when the I beam has a large width they are actually visually distinguishable -- they use the same anchor but one gets painted to the left and the other to the right. I noticed that emacs does the same). 
But logical order navigation will always be confusing in bidi text, in one way or another.

Interestingly there are 3 different behaviors I've observed in chrome:
- the chrome download dialog (which I think uses GTK text widgets but not sure which version) gives me 2 cursors when navigating bidi text, and 
- its HTML fields only show one, and presumably they place the I beam at the **trailing edge** of the character (which makes more sense for backspacing I guess). 
- On the other hand, its (new) omnibar seems to use visual order arrow navigation

Side note: we may need to update the "tap to place the caret here" logic to handle the case where the tap lands outside of the text and the text ends in the opposite writing direction. 

2. Removed the logarithmic search. The same could be done using the characters package but when glyphInfo tells you about the baseline location in the future we probably don't need the `getBoxesForRange` call. This should fix flutter/flutter#123424.

## Internal Tests

This is going to change the image output of some internal golden tests. I'm planning to merge flutter/flutter#143281 before this to avoid updating the same golden files twice for invalid selections.
  • Loading branch information
LongCatIsLooong authored Feb 20, 2024
1 parent 3ae71f5 commit 3538e4c
Show file tree
Hide file tree
Showing 7 changed files with 902 additions and 805 deletions.
32 changes: 16 additions & 16 deletions examples/api/test/widgets/text_magnifier/text_magnifier.0_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ void main() {
const Duration durationBetweenActions = Duration(milliseconds: 20);
const String defaultText = 'I am a magnifier, fear me!';

Future<void> showMagnifier(WidgetTester tester, String characterToTapOn) async {
final Offset tapOffset = _textOffsetToPosition(tester, defaultText.indexOf(characterToTapOn));
Future<void> showMagnifier(WidgetTester tester, int textOffset) async {
assert(textOffset >= 0);
final Offset tapOffset = _textOffsetToPosition(tester, textOffset);

// Double tap 'Magnifier' word to show the selection handles.
final TestGesture testGesture = await tester.startGesture(tapOffset);
Expand All @@ -59,11 +60,11 @@ void main() {
await testGesture.up();
await tester.pumpAndSettle();

final TextSelection selection = tester
.firstWidget<TextField>(find.byType(TextField))
.controller!
.selection;
final TextEditingController controller = tester
.firstWidget<TextField>(find.byType(TextField))
.controller!;

final TextSelection selection = controller.selection;
final RenderEditable renderEditable = _findRenderEditable(tester);
final List<TextSelectionPoint> endpoints = _globalize(
renderEditable.getEndpointsForSelection(selection),
Expand All @@ -86,7 +87,7 @@ void main() {
testWidgets('should show custom magnifier on drag', (WidgetTester tester) async {
await tester.pumpWidget(const example.TextMagnifierExampleApp(text: defaultText));

await showMagnifier(tester, 'e');
await showMagnifier(tester, defaultText.indexOf('e'));
expect(find.byType(example.CustomMagnifier), findsOneWidget);

await expectLater(
Expand All @@ -96,16 +97,15 @@ void main() {
}, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS, TargetPlatform.android }));


for (final TextDirection textDirection in TextDirection.values) {
testWidgets('should show custom magnifier in $textDirection', (WidgetTester tester) async {
final String text = textDirection == TextDirection.rtl ? 'أثارت زر' : defaultText;
final String textToTapOn = textDirection == TextDirection.rtl ? 'ت' : 'e';
testWidgets('should show custom magnifier in RTL', (WidgetTester tester) async {
const String text = 'أثارت زر';
const String textToTapOn = 'ت';

await tester.pumpWidget(example.TextMagnifierExampleApp(textDirection: textDirection, text: text));
await tester.pumpWidget(const example.TextMagnifierExampleApp(textDirection: TextDirection.rtl, text: text));

await showMagnifier(tester, textToTapOn);
await showMagnifier(tester, text.indexOf(textToTapOn));

expect(find.byType(example.CustomMagnifier), findsOneWidget);
});

expect(find.byType(example.CustomMagnifier), findsOneWidget);
});
}
}
Loading

0 comments on commit 3538e4c

Please sign in to comment.