Skip to content

Commit 77c9580

Browse files
committed
הגבל נעילת קצה בחירה לפסקת RTL מזוהה
1 parent 3e10e1f commit 77c9580

2 files changed

Lines changed: 243 additions & 9 deletions

File tree

‎lib/widgets/text/rtl_selection_shortcuts.dart‎

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:flutter/foundation.dart';
2+
import 'package:flutter/rendering.dart';
23
import 'package:flutter/services.dart';
34
import 'package:flutter/widgets.dart';
45

@@ -382,14 +383,8 @@ class _LockSelectionEdgeIntent extends Intent {
382383
final bool forward;
383384
}
384385

385-
/// נועל את הקצה הנכון בבחירה של שורה אחת ב-SelectableRegion, ומשחרר את המקש
386-
/// לברירת המחדל של Flutter (#6048).
387-
///
388-
/// Flutter מזיז את הקצה שמגדיל את הבחירה: המאוחר בטקסט קדימה, המוקדם אחורה.
389-
/// ברב-שורתי הוא קובע אותו לפי Y, אבל בשורה אחת לפי X בהנחת LTR, ולכן ב-RTL
390-
/// הוא מזיז את הקצה ההפוך והבחירה מהעכבר הולכת לאיבוד. צעד תו פנימה וחזרה
391-
/// מקבע את הקצה הנכון (כמו ה-priming של [_PhysicalExtendSelectionAction]),
392-
/// ו-Flutter ממשיך איתו. בשדה קלט ובבחירה רב-שורתית לא נוגעים בדבר.
386+
/// נועל קצה בחירה RTL באותה פסקה: Flutter מכריע בין קצוות באותה שורה לפי X כ-LTR.
387+
/// בשאר המקרים משאיר את הטיפול ל-Flutter.
393388
class _LockSelectionEdgeAction extends Action<_LockSelectionEdgeIntent> {
394389
@override
395390
Object? invoke(_LockSelectionEdgeIntent intent) {
@@ -403,7 +398,8 @@ class _LockSelectionEdgeAction extends Action<_LockSelectionEdgeIntent> {
403398
final points = state == null ? null : _selectionEndpoints(state);
404399
if (points == null ||
405400
points.length < 2 ||
406-
(points.first.point.dy - points.last.point.dy).abs() > 1.0) {
401+
(points.first.point.dy - points.last.point.dy).abs() > 1.0 ||
402+
!_hasRtlSelectionEdges(state!, points)) {
407403
return null;
408404
}
409405
rtlSelectionPriming = true;
@@ -423,6 +419,47 @@ class _LockSelectionEdgeAction extends Action<_LockSelectionEdgeIntent> {
423419
return null;
424420
}
425421

422+
bool _hasRtlSelectionEdges(
423+
SelectableRegionState state,
424+
List<TextSelectionPoint> points,
425+
) {
426+
final root = state.context.findRenderObject();
427+
if (root is! RenderBox) return false;
428+
RenderParagraph? selectedParagraph;
429+
for (var i = 0; i < 2; i++) {
430+
final point = points[i].point;
431+
final other = points[1 - i].point;
432+
final height = i == 0 ? state.startGlyphHeight : state.endGlyphHeight;
433+
final inside = point + Offset((other.dx - point.dx).sign, -height / 2);
434+
final hit = BoxHitTestResult();
435+
root.hitTest(hit, position: inside);
436+
final paragraphs = hit.path
437+
.map((entry) => entry.target)
438+
.whereType<RenderParagraph>();
439+
if (paragraphs.isEmpty) return false;
440+
final paragraph = paragraphs.first;
441+
if (selectedParagraph != null &&
442+
!identical(selectedParagraph, paragraph)) {
443+
return false;
444+
}
445+
selectedParagraph = paragraph;
446+
final local = paragraph.globalToLocal(root.localToGlobal(inside));
447+
final word = paragraph.getWordBoundary(
448+
paragraph.getPositionForOffset(local),
449+
);
450+
final boxes = paragraph
451+
.getBoxesForSelection(
452+
TextSelection(baseOffset: word.start, extentOffset: word.end),
453+
)
454+
.where((box) => box.toRect().contains(local));
455+
if (boxes.isEmpty ||
456+
boxes.any((box) => box.direction != TextDirection.rtl)) {
457+
return false;
458+
}
459+
}
460+
return true;
461+
}
462+
426463
@override
427464
KeyEventResult toKeyEventResult(
428465
_LockSelectionEdgeIntent intent,

‎test/widgets/text/rtl_selection_shortcuts_test.dart‎

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import 'package:flutter/gestures.dart';
22
import 'package:flutter/material.dart';
3+
import 'package:flutter/rendering.dart';
34
import 'package:flutter/services.dart';
45
import 'package:flutter_test/flutter_test.dart';
56
import 'package:otzaria/widgets/text/rtl_selection_shortcuts.dart';
7+
import 'package:otzaria/widgets/selection/item_selection_container.dart';
8+
import 'package:otzaria/widgets/selection/paragraph_selection_container.dart';
69

710
/// בודק את [RtlSelectionShortcuts]: Shift+חץ פועל תמיד על קצה ה-focus (לעולם
811
/// לא משחית את ה-anchor/תחילת המחרוזת), ובכיוון הנכון — בשורה אחת (דרך
@@ -526,6 +529,200 @@ void main() {
526529
});
527530
});
528531

532+
group('כיוון הטקסט בשורה אחת בתוך אזור RTL', () {
533+
for (final selection in const [
534+
('אנגלית', 'abcde fghij klmno', 0, 4),
535+
('אנגלית בגרירה הפוכה', 'abcde fghij klmno', 4, 0),
536+
('אנגלית בגבולות הפסקה', 'abcde fghij klmno', 0, 17),
537+
('אנגלית בתוך עברית', 'אבגדה abcde fghij וזחטי', 6, 10),
538+
('אנגלית בתוך עברית בגרירה הפוכה', 'אבגדה abcde fghij וזחטי', 10, 6),
539+
('קצוות בכיוונים שונים', 'אבגדה abcde fghij וזחטי', 2, 10),
540+
('קצוות בכיוונים שונים בגרירה הפוכה', 'אבגדה abcde fghij וזחטי', 10, 2),
541+
('עברית סביב אנגלית', 'אבגדה abcde fghij וזחטי', 2, 20),
542+
('עברית סביב אנגלית בגרירה הפוכה', 'אבגדה abcde fghij וזחטי', 20, 2),
543+
('עברית', 'אבגדה והוזח טיכלמ', 0, 4),
544+
('עברית בגרירה הפוכה', 'אבגדה והוזח טיכלמ', 4, 0),
545+
]) {
546+
testWidgets(selection.$1, (tester) async {
547+
Future<({String before, String after})> extend({
548+
required bool corrected,
549+
}) async {
550+
String? selected;
551+
final region = SelectableRegion(
552+
selectionControls: emptyTextSelectionControls,
553+
onSelectionChanged: (value) {
554+
if (!rtlSelectionPriming) selected = value?.plainText;
555+
},
556+
child: Column(
557+
mainAxisSize: MainAxisSize.min,
558+
children: [
559+
Text(
560+
selection.$2,
561+
textDirection: selection.$2.startsWith('abcde')
562+
? TextDirection.ltr
563+
: null,
564+
style: const TextStyle(fontSize: 24),
565+
),
566+
const Text(
567+
'שורה שניה abcde fghij',
568+
style: TextStyle(fontSize: 24),
569+
),
570+
],
571+
),
572+
);
573+
await tester.pumpWidget(
574+
MaterialApp(
575+
home: Scaffold(
576+
body: Directionality(
577+
textDirection: TextDirection.rtl,
578+
child: Center(
579+
child: SizedBox(
580+
width: 600,
581+
child: corrected
582+
? RtlSelectionShortcuts(child: region)
583+
: region,
584+
),
585+
),
586+
),
587+
),
588+
),
589+
);
590+
await tester.pumpAndSettle();
591+
final paragraph = tester.renderObject<RenderParagraph>(
592+
find.descendant(
593+
of: find.text(selection.$2),
594+
matching: find.byType(RichText),
595+
),
596+
);
597+
Offset caret(int offset) {
598+
final atEnd = offset == selection.$2.length;
599+
final box = paragraph
600+
.getBoxesForSelection(
601+
TextSelection(
602+
baseOffset: atEnd ? offset - 1 : offset,
603+
extentOffset: atEnd ? offset : offset + 1,
604+
),
605+
)
606+
.first;
607+
return paragraph.localToGlobal(
608+
Offset(
609+
(atEnd ? box.end : box.start) +
610+
(atEnd ? -1 : 1) *
611+
(box.direction == TextDirection.ltr ? 1 : -1),
612+
(box.top + box.bottom) / 2,
613+
),
614+
);
615+
}
616+
617+
final gesture = await tester.startGesture(
618+
caret(selection.$3),
619+
kind: PointerDeviceKind.mouse,
620+
);
621+
addTearDown(gesture.removePointer);
622+
await tester.pump();
623+
await gesture.moveTo(caret(selection.$4));
624+
await tester.pump();
625+
await gesture.up();
626+
await tester.pumpAndSettle();
627+
final before = selected;
628+
expect(before, isNotEmpty);
629+
await shiftPress(tester, LogicalKeyboardKey.arrowDown);
630+
expect(selected, isNotNull);
631+
final result = (before: before!, after: selected!);
632+
await tester.pumpWidget(const SizedBox.shrink());
633+
return result;
634+
}
635+
636+
if (selection.$1.startsWith('עברית')) {
637+
final result = await extend(corrected: true);
638+
expect(result.after, contains(result.before));
639+
} else {
640+
final baseline = await extend(corrected: false);
641+
final corrected = await extend(corrected: true);
642+
expect(corrected.before, baseline.before);
643+
expect(corrected.after, baseline.after);
644+
}
645+
});
646+
}
647+
});
648+
649+
for (final reversed in [false, true]) {
650+
testWidgets('אותה שורה בין containers נשארת כפי שהייתה, הפוכה=$reversed', (
651+
tester,
652+
) async {
653+
String? baselineBefore;
654+
String? baselineAfter;
655+
for (final corrected in [false, true]) {
656+
String? selected;
657+
final region = SelectableRegion(
658+
selectionControls: emptyTextSelectionControls,
659+
onSelectionChanged: (value) {
660+
if (!rtlSelectionPriming) selected = value?.plainText;
661+
},
662+
child: Column(
663+
mainAxisSize: MainAxisSize.min,
664+
children: [
665+
ItemSelectionContainer(
666+
child: Row(
667+
mainAxisSize: MainAxisSize.min,
668+
children: const [
669+
ParagraphSelectionContainer(
670+
child: Text('אבגדה', style: TextStyle(fontSize: 24)),
671+
),
672+
ParagraphSelectionContainer(
673+
child: Text('וזחטי', style: TextStyle(fontSize: 24)),
674+
),
675+
],
676+
),
677+
),
678+
const Text('קלמנס עפרשת', style: TextStyle(fontSize: 24)),
679+
],
680+
),
681+
);
682+
await tester.pumpWidget(
683+
MaterialApp(
684+
home: Scaffold(
685+
body: Directionality(
686+
textDirection: TextDirection.rtl,
687+
child: Center(
688+
child: corrected
689+
? RtlSelectionShortcuts(child: region)
690+
: region,
691+
),
692+
),
693+
),
694+
),
695+
);
696+
await tester.pumpAndSettle();
697+
final first = tester.getRect(find.text('אבגדה'));
698+
final last = tester.getRect(find.text('וזחטי'));
699+
final from = Offset(first.right - 24, first.center.dy);
700+
final to = Offset(last.left + 24, last.center.dy);
701+
final gesture = await tester.startGesture(
702+
reversed ? to : from,
703+
kind: PointerDeviceKind.mouse,
704+
);
705+
addTearDown(gesture.removePointer);
706+
await tester.pump();
707+
await gesture.moveTo(reversed ? from : to);
708+
await tester.pump();
709+
await gesture.up();
710+
await tester.pumpAndSettle();
711+
final before = selected;
712+
expect(before, isNotEmpty);
713+
await shiftPress(tester, LogicalKeyboardKey.arrowDown);
714+
if (corrected) {
715+
expect(before, baselineBefore);
716+
expect(selected, baselineAfter);
717+
} else {
718+
baselineBefore = before;
719+
baselineAfter = selected;
720+
}
721+
await tester.pumpWidget(const SizedBox.shrink());
722+
}
723+
});
724+
}
725+
529726
testWidgets('LTR: ה-widget שקוף ואינו מוסיף Shortcuts', (tester) async {
530727
await tester.pumpWidget(
531728
const Directionality(

0 commit comments

Comments
 (0)