-
Notifications
You must be signed in to change notification settings - Fork 30.4k
Expand file tree
/
Copy pathselection.dart
More file actions
922 lines (825 loc) · 31.9 KB
/
selection.dart
File metadata and controls
922 lines (825 loc) · 31.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/// @docImport 'package:flutter/gestures.dart';
/// @docImport 'package:flutter/material.dart';
library;
import 'package:flutter/foundation.dart';
import 'package:vector_math/vector_math_64.dart';
import 'layer.dart';
import 'object.dart';
/// The result after handling a [SelectionEvent].
///
/// [SelectionEvent]s are sent from [SelectionRegistrar] to be handled by
/// [SelectionHandler.dispatchSelectionEvent]. The subclasses of
/// [SelectionHandler] or [Selectable] must return appropriate
/// [SelectionResult]s after handling the events.
///
/// This is used by the [SelectionContainer] to determine how a selection
/// expands across its [Selectable] children.
enum SelectionResult {
/// There is nothing left to select forward in this [Selectable], and further
/// selection should extend to the next [Selectable] in screen order.
///
/// {@template flutter.rendering.selection.SelectionResult.footNote}
/// This is used after subclasses [SelectionHandler] or [Selectable] handled
/// [SelectionEdgeUpdateEvent].
/// {@endtemplate}
next,
/// Selection does not reach this [Selectable] and is located before it in
/// screen order.
///
/// {@macro flutter.rendering.selection.SelectionResult.footNote}
previous,
/// Selection ends in this [Selectable].
///
/// Part of the [Selectable] may or may not be selected, but there is still
/// content to select forward or backward.
///
/// {@macro flutter.rendering.selection.SelectionResult.footNote}
end,
/// The result can't be determined in this frame.
///
/// This is typically used when the subtree is scrolling to reveal more
/// content.
///
/// {@macro flutter.rendering.selection.SelectionResult.footNote}
// See `_SelectableRegionState._triggerSelectionEndEdgeUpdate` for how this
// result affects the selection.
pending,
/// There is no result for the selection event.
///
/// This is used when a selection result is not applicable, e.g.
/// [SelectAllSelectionEvent], [ClearSelectionEvent], and
/// [SelectWordSelectionEvent].
none,
}
/// The abstract interface to handle [SelectionEvent]s.
///
/// This interface is extended by [Selectable] and [SelectionContainerDelegate]
/// and is typically not used directly.
///
/// {@template flutter.rendering.SelectionHandler}
/// This class returns a [SelectionGeometry] as its [value], and is responsible
/// to notify its listener when its selection geometry has changed as the result
/// of receiving selection events.
/// {@endtemplate}
abstract class SelectionHandler implements ValueListenable<SelectionGeometry> {
/// Marks this handler to be responsible for pushing [LeaderLayer]s for the
/// selection handles.
///
/// This handler is responsible for pushing the leader layers with the
/// given layer links if they are not null. It is possible that only one layer
/// is non-null if this handler is only responsible for pushing one layer
/// link.
///
/// The `startHandle` needs to be placed at the visual location of selection
/// start, the `endHandle` needs to be placed at the visual location of selection
/// end. Typically, the visual locations should be the same as
/// [SelectionGeometry.startSelectionPoint] and
/// [SelectionGeometry.endSelectionPoint].
void pushHandleLayers(LayerLink? startHandle, LayerLink? endHandle);
/// Gets the selected content in this object.
///
/// Return `null` if nothing is selected.
SelectedContent? getSelectedContent();
/// Gets the [SelectedContentRange] representing the selected range in this object.
///
/// When nothing is selected, subclasses should return `null`.
SelectedContentRange? getSelection();
/// Handles the [SelectionEvent] sent to this object.
///
/// The subclasses need to update their selections or delegate the
/// [SelectionEvent]s to their subtrees.
///
/// The `event`s are subclasses of [SelectionEvent]. Check
/// [SelectionEvent.type] to determine what kinds of event are dispatched to
/// this handler and handle them accordingly.
///
/// See also:
/// * [SelectionEventType], which contains all of the possible types.
SelectionResult dispatchSelectionEvent(SelectionEvent event);
/// The length of the content in this object.
int get contentLength;
}
/// This class stores the range information of the selection under a [Selectable]
/// or [SelectionHandler].
///
/// The [SelectedContentRange] for a given [Selectable] or [SelectionHandler]
/// can be retrieved by calling [SelectionHandler.getSelection].
@immutable
class SelectedContentRange with Diagnosticable {
/// Creates a [SelectedContentRange] with the given values.
const SelectedContentRange({required this.startOffset, required this.endOffset})
: assert((startOffset >= 0 && endOffset >= 0));
/// The start of the selection relative to the start of the content.
///
/// {@template flutter.rendering.selection.SelectedContentRange.selectionOffsets}
/// For example a [Text] widget's content is in the format of an [TextSpan] tree.
///
/// Take the [Text] widget and [TextSpan] tree below:
///
/// {@tool snippet}
/// ```dart
/// const Text.rich(
/// TextSpan(
/// text: 'Hello world, ',
/// children: <InlineSpan>[
/// WidgetSpan(
/// child: Text('how are you today? '),
/// ),
/// TextSpan(
/// text: 'Good, thanks for asking.',
/// ),
/// ],
/// ),
/// )
/// ```
/// {@end-tool}
///
/// If we select from the beginning of 'world' to the end of the '.'
/// at the end of the [TextSpan] tree, the [SelectedContentRange] from
/// [SelectionHandler.getSelection] will be relative to the text of the
/// [TextSpan] tree, with [WidgetSpan] content being flattened. The [startOffset]
/// will be 6, and [endOffset] will be 56. This takes into account the
/// length of the content in the [WidgetSpan], which is 19, making the overall
/// length of the content 56.
/// {@endtemplate}
final int startOffset;
/// The end of the selection relative to the start of the content.
///
/// {@macro flutter.rendering.selection.SelectedContentRange.selectionOffsets}
final int endOffset;
@override
bool operator ==(Object other) {
if (identical(this, other)) {
return true;
}
if (other.runtimeType != runtimeType) {
return false;
}
return other is SelectedContentRange &&
other.startOffset == startOffset &&
other.endOffset == endOffset;
}
@override
int get hashCode {
return Object.hash(startOffset, endOffset);
}
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(IntProperty('startOffset', startOffset));
properties.add(IntProperty('endOffset', endOffset));
}
}
/// The selected content in a [Selectable] or [SelectionHandler].
// TODO(chunhtai): Add more support for rich content.
// https://github.com/flutter/flutter/issues/104206.
@immutable
class SelectedContent with Diagnosticable {
/// Creates a selected content object.
///
/// Only supports plain text.
const SelectedContent({required this.plainText});
/// The selected content in plain text format.
final String plainText;
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(StringProperty('plainText', plainText));
}
}
/// A mixin that can be selected by users when under a [SelectionArea] widget.
///
/// This object receives selection events and the [value] must reflect the
/// current selection in this [Selectable]. The object must also notify its
/// listener if the [value] ever changes.
///
/// This object is responsible for drawing the selection highlight.
///
/// In order to receive the selection event, the mixer needs to register
/// itself to [SelectionRegistrar]s. Use
/// [SelectionContainer.maybeOf] to get the selection registrar, and
/// mix the [SelectionRegistrant] to subscribe to the [SelectionRegistrar]
/// automatically.
///
/// This mixin is typically mixed by [RenderObject]s. The [RenderObject.paint]
/// methods are responsible to push the [LayerLink]s provided to
/// [pushHandleLayers].
///
/// {@macro flutter.rendering.SelectionHandler}
///
/// See also:
/// * [SelectableRegion], which provides an overview of selection system.
mixin Selectable implements SelectionHandler {
/// {@macro flutter.rendering.RenderObject.getTransformTo}
Matrix4 getTransformTo(RenderObject? ancestor);
/// The size of this [Selectable].
Size get size;
/// A list of [Rect]s that represent the bounding box of this [Selectable]
/// in local coordinates.
List<Rect> get boundingBoxes;
/// Disposes resources held by the mixer.
void dispose();
}
/// A mixin to auto-register the mixer to the [registrar].
///
/// To use this mixin, the mixer needs to set the [registrar] to the
/// [SelectionRegistrar] it wants to register to.
///
/// This mixin only registers the mixer with the [registrar] if the
/// [SelectionGeometry.hasContent] returned by the mixer is true.
mixin SelectionRegistrant on Selectable {
/// The [SelectionRegistrar] the mixer will be or is registered to.
///
/// This [Selectable] only registers the mixer if the
/// [SelectionGeometry.hasContent] returned by the [Selectable] is true.
SelectionRegistrar? get registrar => _registrar;
SelectionRegistrar? _registrar;
set registrar(SelectionRegistrar? value) {
if (value == _registrar) {
return;
}
if (value == null) {
// When registrar goes from non-null to null;
removeListener(_updateSelectionRegistrarSubscription);
} else if (_registrar == null) {
// When registrar goes from null to non-null;
addListener(_updateSelectionRegistrarSubscription);
}
_removeSelectionRegistrarSubscription();
_registrar = value;
_updateSelectionRegistrarSubscription();
}
@override
void dispose() {
_removeSelectionRegistrarSubscription();
super.dispose();
}
bool _subscribedToSelectionRegistrar = false;
void _updateSelectionRegistrarSubscription() {
if (_registrar == null) {
_subscribedToSelectionRegistrar = false;
return;
}
if (_subscribedToSelectionRegistrar && !value.hasContent) {
_registrar!.remove(this);
_subscribedToSelectionRegistrar = false;
} else if (!_subscribedToSelectionRegistrar && value.hasContent) {
_registrar!.add(this);
_subscribedToSelectionRegistrar = true;
}
}
void _removeSelectionRegistrarSubscription() {
if (_subscribedToSelectionRegistrar) {
_registrar!.remove(this);
_subscribedToSelectionRegistrar = false;
}
}
}
/// A utility class that provides useful methods for handling selection events.
abstract final class SelectionUtils {
/// Determines [SelectionResult] purely based on the target rectangle.
///
/// This method returns [SelectionResult.end] if the `point` is inside the
/// `targetRect`. Returns [SelectionResult.previous] if the `point` is
/// considered to be lower than `targetRect` in screen order. Returns
/// [SelectionResult.next] if the point is considered to be higher than
/// `targetRect` in screen order.
static SelectionResult getResultBasedOnRect(Rect targetRect, Offset point) {
if (targetRect.contains(point)) {
return SelectionResult.end;
}
if (point.dy < targetRect.top) {
return SelectionResult.previous;
}
if (point.dy > targetRect.bottom) {
return SelectionResult.next;
}
return point.dx >= targetRect.right ? SelectionResult.next : SelectionResult.previous;
}
/// Adjusts the dragging offset based on the target rect.
///
/// This method moves the offsets to be within the target rect in case they are
/// outside the rect.
///
/// This is used in the case where a drag happens outside of the rectangle
/// of a [Selectable].
///
/// The logic works as the following:
/// 
///
/// For points inside the rect:
/// Their effective locations are unchanged.
///
/// For points in Area 1:
/// Move them to top-left of the rect if text direction is ltr, or top-right
/// if rtl.
///
/// For points in Area 2:
/// Move them to bottom-right of the rect if text direction is ltr, or
/// bottom-left if rtl.
static Offset adjustDragOffset(
Rect targetRect,
Offset point, {
TextDirection direction = TextDirection.ltr,
}) {
if (targetRect.contains(point)) {
return point;
}
if (point.dy <= targetRect.top ||
point.dy <= targetRect.bottom && point.dx <= targetRect.left) {
// Area 1
return direction == TextDirection.ltr ? targetRect.topLeft : targetRect.topRight;
} else {
// Area 2
return direction == TextDirection.ltr ? targetRect.bottomRight : targetRect.bottomLeft;
}
}
}
/// The type of a [SelectionEvent].
///
/// Used by [SelectionEvent.type] to distinguish different types of events.
enum SelectionEventType {
/// An event to update the selection start edge.
///
/// Used by [SelectionEdgeUpdateEvent].
startEdgeUpdate,
/// An event to update the selection end edge.
///
/// Used by [SelectionEdgeUpdateEvent].
endEdgeUpdate,
/// An event to clear the current selection.
///
/// Used by [ClearSelectionEvent].
clear,
/// An event to select all the available content.
///
/// Used by [SelectAllSelectionEvent].
selectAll,
/// An event to select a word at the location
/// [SelectWordSelectionEvent.globalPosition].
///
/// Used by [SelectWordSelectionEvent].
selectWord,
/// An event to select a paragraph at the location
/// [SelectParagraphSelectionEvent.globalPosition].
///
/// Used by [SelectParagraphSelectionEvent].
selectParagraph,
/// An event that extends the selection by a specific [TextGranularity].
granularlyExtendSelection,
/// An event that extends the selection in a specific direction.
directionallyExtendSelection,
}
/// The unit of how selection handles move in text.
///
/// The [GranularlyExtendSelectionEvent] uses this enum to describe how
/// [Selectable] should extend its selection.
enum TextGranularity {
/// Treats each character as an atomic unit when moving the selection handles.
character,
/// Treats word as an atomic unit when moving the selection handles.
word,
/// Treats a paragraph as an atomic unit when moving the selection handles.
paragraph,
/// Treats each line break as an atomic unit when moving the selection handles.
line,
/// Treats the entire document as an atomic unit when moving the selection handles.
document,
}
/// An abstract base class for selection events.
///
/// This should not be directly used. To handle a selection event, it should
/// be downcast to a specific subclass. One can use [type] to look up which
/// subclasses to downcast to.
///
/// See also:
/// * [SelectAllSelectionEvent], for events to select all contents.
/// * [ClearSelectionEvent], for events to clear selections.
/// * [SelectWordSelectionEvent], for events to select words at the locations.
/// * [SelectionEdgeUpdateEvent], for events to update selection edges.
/// * [SelectionEventType], for determining the subclass types.
abstract class SelectionEvent {
const SelectionEvent._(this.type);
/// The type of this selection event.
final SelectionEventType type;
}
/// Selects all selectable contents.
///
/// This event can be sent as the result of keyboard select-all, i.e.
/// ctrl + A, or cmd + A in macOS.
class SelectAllSelectionEvent extends SelectionEvent {
/// Creates a select all selection event.
const SelectAllSelectionEvent() : super._(SelectionEventType.selectAll);
}
/// Clears the selection from the [Selectable] and removes any existing
/// highlight as if there is no selection at all.
class ClearSelectionEvent extends SelectionEvent {
/// Create a clear selection event.
const ClearSelectionEvent() : super._(SelectionEventType.clear);
}
/// Selects the whole word at the location.
///
/// This event can be sent as the result of mobile long press selection.
class SelectWordSelectionEvent extends SelectionEvent {
/// Creates a select word event at the [globalPosition].
const SelectWordSelectionEvent({required this.globalPosition})
: super._(SelectionEventType.selectWord);
/// The position in global coordinates to select word at.
final Offset globalPosition;
}
/// Selects the entire paragraph at the location.
///
/// This event can be sent as the result of a triple click to select.
class SelectParagraphSelectionEvent extends SelectionEvent {
/// Creates a select paragraph event at the [globalPosition].
const SelectParagraphSelectionEvent({required this.globalPosition, this.absorb = false})
: super._(SelectionEventType.selectParagraph);
/// The position in global coordinates to select paragraph at.
final Offset globalPosition;
/// Whether the selectable receiving the event should be absorbed into
/// an encompassing paragraph.
final bool absorb;
}
/// Updates a selection edge.
///
/// An active selection contains two edges, start and end. Use the [type] to
/// determine which edge this event applies to. If the [type] is
/// [SelectionEventType.startEdgeUpdate], the event updates start edge. If the
/// [type] is [SelectionEventType.endEdgeUpdate], the event updates end edge.
///
/// The [globalPosition] contains the new offset of the edge.
///
/// The [granularity] contains the granularity that the selection edge should move by.
/// Only [TextGranularity.character] and [TextGranularity.word] are currently supported.
///
/// This event is dispatched when the framework detects [TapDragStartDetails] in
/// [SelectionArea]'s gesture recognizers for mouse devices, or the selection
/// handles have been dragged to new locations.
class SelectionEdgeUpdateEvent extends SelectionEvent {
/// Creates a selection start edge update event.
///
/// The [globalPosition] contains the location of the selection start edge.
///
/// The [granularity] contains the granularity which the selection edge should move by.
/// This value defaults to [TextGranularity.character].
const SelectionEdgeUpdateEvent.forStart({
required this.globalPosition,
TextGranularity? granularity,
}) : granularity = granularity ?? TextGranularity.character,
super._(SelectionEventType.startEdgeUpdate);
/// Creates a selection end edge update event.
///
/// The [globalPosition] contains the new location of the selection end edge.
///
/// The [granularity] contains the granularity which the selection edge should move by.
/// This value defaults to [TextGranularity.character].
const SelectionEdgeUpdateEvent.forEnd({
required this.globalPosition,
TextGranularity? granularity,
}) : granularity = granularity ?? TextGranularity.character,
super._(SelectionEventType.endEdgeUpdate);
/// The new location of the selection edge.
final Offset globalPosition;
/// The granularity for which the selection moves.
///
/// Only [TextGranularity.character] and [TextGranularity.word] are currently supported.
///
/// Defaults to [TextGranularity.character].
final TextGranularity granularity;
}
/// Extends the start or end of the selection by a given [TextGranularity].
///
/// To handle this event, move the associated selection edge, as dictated by
/// [isEnd], according to the [granularity].
class GranularlyExtendSelectionEvent extends SelectionEvent {
/// Creates a [GranularlyExtendSelectionEvent].
const GranularlyExtendSelectionEvent({
required this.forward,
required this.isEnd,
required this.granularity,
}) : super._(SelectionEventType.granularlyExtendSelection);
/// Whether to extend the selection forward.
final bool forward;
/// Whether this event is updating the end selection edge.
final bool isEnd;
/// The granularity for which the selection extend.
final TextGranularity granularity;
}
/// The direction to extend a selection.
///
/// The [DirectionallyExtendSelectionEvent] uses this enum to describe how
/// [Selectable] should extend their selection.
enum SelectionExtendDirection {
/// Move one edge of the selection vertically to the previous adjacent line.
///
/// For text selection, it should consider both soft and hard linebreak.
///
/// See [DirectionallyExtendSelectionEvent.dx] on how to
/// calculate the horizontal offset.
previousLine,
/// Move one edge of the selection vertically to the next adjacent line.
///
/// For text selection, it should consider both soft and hard linebreak.
///
/// See [DirectionallyExtendSelectionEvent.dx] on how to
/// calculate the horizontal offset.
nextLine,
/// Move the selection edges forward to a certain horizontal offset in the
/// same line.
///
/// If there is no on-going selection, the selection must start with the first
/// line (or equivalence of first line in a non-text selectable) and select
/// toward the horizontal offset in the same line.
///
/// The selectable that receives [DirectionallyExtendSelectionEvent] with this
/// enum must return [SelectionResult.end].
///
/// See [DirectionallyExtendSelectionEvent.dx] on how to
/// calculate the horizontal offset.
forward,
/// Move the selection edges backward to a certain horizontal offset in the
/// same line.
///
/// If there is no on-going selection, the selection must start with the last
/// line (or equivalence of last line in a non-text selectable) and select
/// backward the horizontal offset in the same line.
///
/// The selectable that receives [DirectionallyExtendSelectionEvent] with this
/// enum must return [SelectionResult.end].
///
/// See [DirectionallyExtendSelectionEvent.dx] on how to
/// calculate the horizontal offset.
backward,
}
/// Extends the current selection with respect to a [direction].
///
/// To handle this event, move the associated selection edge, as dictated by
/// [isEnd], according to the [direction].
///
/// The movements are always based on [dx]. The value is in
/// global coordinates and is the horizontal offset the selection edge should
/// move to when moving to across lines.
class DirectionallyExtendSelectionEvent extends SelectionEvent {
/// Creates a [DirectionallyExtendSelectionEvent].
const DirectionallyExtendSelectionEvent({
required this.dx,
required this.isEnd,
required this.direction,
}) : super._(SelectionEventType.directionallyExtendSelection);
/// The horizontal offset the selection should move to.
///
/// The offset is in global coordinates.
final double dx;
/// Whether this event is updating the end selection edge.
final bool isEnd;
/// The directional movement of this event.
///
/// See also:
/// * [SelectionExtendDirection], which explains how to handle each enum.
final SelectionExtendDirection direction;
/// Makes a copy of this object with its property replaced with the new
/// values.
DirectionallyExtendSelectionEvent copyWith({
double? dx,
bool? isEnd,
SelectionExtendDirection? direction,
}) {
return DirectionallyExtendSelectionEvent(
dx: dx ?? this.dx,
isEnd: isEnd ?? this.isEnd,
direction: direction ?? this.direction,
);
}
}
/// A registrar that keeps track of [Selectable]s in the subtree.
///
/// A [Selectable] is only included in the [SelectableRegion] if they are
/// registered with a [SelectionRegistrar]. Once a [Selectable] is registered,
/// it will receive [SelectionEvent]s in
/// [SelectionHandler.dispatchSelectionEvent].
///
/// Use [SelectionContainer.maybeOf] to get the immediate [SelectionRegistrar]
/// in the ancestor chain above the build context.
///
/// See also:
/// * [SelectableRegion], which provides an overview of the selection system.
/// * [SelectionRegistrarScope], which hosts the [SelectionRegistrar] for the
/// subtree.
/// * [SelectionRegistrant], which auto registers the object with the mixin to
/// [SelectionRegistrar].
abstract class SelectionRegistrar {
/// Adds the [selectable] into the registrar.
///
/// A [Selectable] must register with the [SelectionRegistrar] in order to
/// receive selection events.
void add(Selectable selectable);
/// Removes the [selectable] from the registrar.
///
/// A [Selectable] must unregister itself if it is removed from the rendering
/// tree.
void remove(Selectable selectable);
}
/// The status that indicates whether there is a selection and whether the
/// selection is collapsed.
///
/// A collapsed selection means the selection starts and ends at the same
/// location.
enum SelectionStatus {
/// The selection is not collapsed.
///
/// For example if `{}` represent the selection edges:
/// 'ab{cd}', the collapsing status is [uncollapsed].
/// '{abcd}', the collapsing status is [uncollapsed].
uncollapsed,
/// The selection is collapsed.
///
/// For example if `{}` represent the selection edges:
/// 'ab{}cd', the collapsing status is [collapsed].
/// '{}abcd', the collapsing status is [collapsed].
/// 'abcd{}', the collapsing status is [collapsed].
collapsed,
/// No selection.
none,
}
/// The geometry of the current selection.
///
/// This includes details such as the locations of the selection start and end,
/// line height, the rects that encompass the selection, etc. This information
/// is used for drawing selection controls for mobile platforms.
///
/// The positions in geometry are in local coordinates of the [SelectionHandler]
/// or [Selectable].
@immutable
class SelectionGeometry with Diagnosticable {
/// Creates a selection geometry object.
///
/// If any of the [startSelectionPoint] and [endSelectionPoint] is not null,
/// the [status] must not be [SelectionStatus.none].
const SelectionGeometry({
this.startSelectionPoint,
this.endSelectionPoint,
this.selectionRects = const <Rect>[],
required this.status,
required this.hasContent,
}) : assert(
(startSelectionPoint == null && endSelectionPoint == null) ||
status != SelectionStatus.none,
);
/// The geometry information at the selection start.
///
/// This information is used for drawing mobile selection controls. The
/// [SelectionPoint.localPosition] of the selection start is typically at the
/// start of the selection highlight at where the start selection handle
/// should be drawn.
///
/// The [SelectionPoint.handleType] should be [TextSelectionHandleType.left]
/// for forward selection or [TextSelectionHandleType.right] for backward
/// selection in most cases.
///
/// Can be null if the selection start is offstage, for example, when the
/// selection is outside of the viewport or is kept alive by a scrollable.
final SelectionPoint? startSelectionPoint;
/// The geometry information at the selection end.
///
/// This information is used for drawing mobile selection controls. The
/// [SelectionPoint.localPosition] of the selection end is typically at the end
/// of the selection highlight at where the end selection handle should be
/// drawn.
///
/// The [SelectionPoint.handleType] should be [TextSelectionHandleType.right]
/// for forward selection or [TextSelectionHandleType.left] for backward
/// selection in most cases.
///
/// Can be null if the selection end is offstage, for example, when the
/// selection is outside of the viewport or is kept alive by a scrollable.
final SelectionPoint? endSelectionPoint;
/// The status of ongoing selection in the [Selectable] or [SelectionHandler].
final SelectionStatus status;
/// The rects in the local coordinates of the containing [Selectable] that
/// represent the selection if there is any.
final List<Rect> selectionRects;
/// Whether there is any selectable content in the [Selectable] or
/// [SelectionHandler].
final bool hasContent;
/// Whether there is an ongoing selection.
bool get hasSelection => status != SelectionStatus.none;
/// Makes a copy of this object with the given values updated.
SelectionGeometry copyWith({
SelectionPoint? startSelectionPoint,
SelectionPoint? endSelectionPoint,
List<Rect>? selectionRects,
SelectionStatus? status,
bool? hasContent,
}) {
return SelectionGeometry(
startSelectionPoint: startSelectionPoint ?? this.startSelectionPoint,
endSelectionPoint: endSelectionPoint ?? this.endSelectionPoint,
selectionRects: selectionRects ?? this.selectionRects,
status: status ?? this.status,
hasContent: hasContent ?? this.hasContent,
);
}
@override
bool operator ==(Object other) {
if (identical(this, other)) {
return true;
}
if (other.runtimeType != runtimeType) {
return false;
}
return other is SelectionGeometry &&
other.startSelectionPoint == startSelectionPoint &&
other.endSelectionPoint == endSelectionPoint &&
listEquals(other.selectionRects, selectionRects) &&
other.status == status &&
other.hasContent == hasContent;
}
@override
int get hashCode {
return Object.hash(startSelectionPoint, endSelectionPoint, selectionRects, status, hasContent);
}
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(DiagnosticsProperty<SelectionPoint>('startSelectionPoint', startSelectionPoint));
properties.add(DiagnosticsProperty<SelectionPoint>('endSelectionPoint', endSelectionPoint));
properties.add(IterableProperty<Rect>('selectionRects', selectionRects));
properties.add(EnumProperty<SelectionStatus>('status', status));
properties.add(DiagnosticsProperty<bool>('hasContent', hasContent));
}
}
/// The geometry information of a selection point.
@immutable
class SelectionPoint with Diagnosticable {
/// Creates a selection point object.
const SelectionPoint({
required this.localPosition,
required this.lineHeight,
required this.handleType,
});
/// The position of the selection point in the local coordinates of the
/// containing [Selectable].
final Offset localPosition;
/// The line height at the selection point.
final double lineHeight;
/// The selection handle type that should be used at the selection point.
///
/// This is used for building the mobile selection handle.
final TextSelectionHandleType handleType;
@override
bool operator ==(Object other) {
if (identical(this, other)) {
return true;
}
if (other.runtimeType != runtimeType) {
return false;
}
return other is SelectionPoint &&
other.localPosition == localPosition &&
other.lineHeight == lineHeight &&
other.handleType == handleType;
}
@override
int get hashCode {
return Object.hash(localPosition, lineHeight, handleType);
}
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(DiagnosticsProperty<Offset>('localPosition', localPosition));
properties.add(DoubleProperty('lineHeight', lineHeight));
properties.add(EnumProperty<TextSelectionHandleType>('handleType', handleType));
}
}
/// The type of selection handle to be displayed.
///
/// With mixed-direction text, both handles may be the same type. Examples:
///
/// * LTR text: 'the <quick brown> fox':
///
/// The '<' is drawn with the [left] type, the '>' with the [right]
///
/// * RTL text: 'XOF <NWORB KCIUQ> EHT':
///
/// Same as above.
///
/// * mixed text: '<the NWOR<B KCIUQ fox'
///
/// Here 'the QUICK B' is selected, but 'QUICK BROWN' is RTL. Both are drawn
/// with the [left] type.
///
/// See also:
///
/// * [TextDirection], which discusses left-to-right and right-to-left text in
/// more detail.
enum TextSelectionHandleType {
/// The selection handle is to the left of the selection end point.
left,
/// The selection handle is to the right of the selection end point.
right,
/// The start and end of the selection are co-incident at this point.
collapsed,
}