Skip to content
This repository was archived by the owner on Apr 29, 2021. It is now read-only.

Commit 7dd5b30

Browse files
authored
Merge pull request #123 from UnityTech/textselection
Textselection
2 parents 64cecde + 23a98e0 commit 7dd5b30

File tree

12 files changed

+997
-48
lines changed

12 files changed

+997
-48
lines changed

Runtime/gestures/long_press.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
using Unity.UIWidgets.ui;
2+
13
namespace Unity.UIWidgets.gestures {
24
public delegate void GestureLongPressCallback();
35

46
public class LongPressGestureRecognizer : PrimaryPointerGestureRecognizer {
5-
public LongPressGestureRecognizer(object debugOwner = null) :
6-
base(deadline: Constants.kLongPressTimeout, debugOwner: debugOwner) {
7+
public LongPressGestureRecognizer(object debugOwner = null, PointerDeviceKind? kind = null) :
8+
base(deadline: Constants.kLongPressTimeout, debugOwner: debugOwner, kind: kind) {
79
}
810

911
public GestureLongPressCallback onLongPress;

Runtime/gestures/monodrag.cs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@ enum _DragState {
1616
public delegate void GestureDragCancelCallback();
1717

1818
public abstract class DragGestureRecognizer : OneSequenceGestureRecognizer {
19-
public DragGestureRecognizer(object debugOwner = null)
20-
: base(debugOwner: debugOwner) {
19+
public DragGestureRecognizer(
20+
object debugOwner = null,
21+
PointerDeviceKind? kind = null,
22+
DragStartBehavior dragStartBehavior = DragStartBehavior.start)
23+
: base(debugOwner: debugOwner, kind: kind) {
24+
this.dragStartBehavior = dragStartBehavior;
2125
}
2226

27+
public DragStartBehavior dragStartBehavior;
28+
2329
public GestureDragDownCallback onDown;
2430

2531
public GestureDragStartCallback onStart;
@@ -65,7 +71,7 @@ public override void addScrollPointer(PointerScrollEvent evt) {
6571
}
6672
}
6773

68-
public override void addPointer(PointerDownEvent evt) {
74+
public override void addAllowedPointer(PointerDownEvent evt) {
6975
this.startTrackingPointer(evt.pointer);
7076
this._velocityTrackers[evt.pointer] = new VelocityTracker();
7177
if (this._state == _DragState.ready) {
@@ -146,6 +152,20 @@ public override void acceptGesture(int pointer) {
146152
this._state = _DragState.accepted;
147153
Offset delta = this._pendingDragOffset;
148154
var timestamp = this._lastPendingEventTimestamp;
155+
156+
Offset updateDelta = null;
157+
switch (this.dragStartBehavior) {
158+
case DragStartBehavior.start:
159+
this._initialPosition = this._initialPosition + delta;
160+
updateDelta = Offset.zero;
161+
break;
162+
case DragStartBehavior.down:
163+
updateDelta = this._getDeltaForDetails(delta);
164+
break;
165+
}
166+
167+
D.assert(updateDelta != null);
168+
149169
this._pendingDragOffset = Offset.zero;
150170
this._lastPendingEventTimestamp = default(TimeSpan);
151171
if (this.onStart != null) {
@@ -158,13 +178,13 @@ public override void acceptGesture(int pointer) {
158178
});
159179
}
160180

161-
if (delta != Offset.zero && this.onUpdate != null) {
181+
if (updateDelta != Offset.zero && this.onUpdate != null) {
162182
this.invokeCallback<object>("onUpdate", () => {
163183
this.onUpdate(new DragUpdateDetails(
164184
sourceTimeStamp: timestamp,
165-
delta: this._getDeltaForDetails(delta),
166-
primaryDelta: this._getPrimaryValueFromOffset(delta),
167-
globalPosition: this._initialPosition
185+
delta: updateDelta,
186+
primaryDelta: this._getPrimaryValueFromOffset(updateDelta),
187+
globalPosition: this._initialPosition + updateDelta
168188
));
169189
return null;
170190
});
@@ -247,8 +267,8 @@ public override void dispose() {
247267
}
248268

249269
public class VerticalDragGestureRecognizer : DragGestureRecognizer {
250-
public VerticalDragGestureRecognizer(object debugOwner = null)
251-
: base(debugOwner: debugOwner) {
270+
public VerticalDragGestureRecognizer(object debugOwner = null, PointerDeviceKind? kind = null)
271+
: base(debugOwner: debugOwner, kind: kind) {
252272
}
253273

254274
protected override bool _isFlingGesture(VelocityEstimate estimate) {
@@ -275,8 +295,8 @@ public override string debugDescription {
275295
}
276296

277297
public class HorizontalDragGestureRecognizer : DragGestureRecognizer {
278-
public HorizontalDragGestureRecognizer(object debugOwner = null)
279-
: base(debugOwner: debugOwner) {
298+
public HorizontalDragGestureRecognizer(object debugOwner = null, PointerDeviceKind? kind = null)
299+
: base(debugOwner: debugOwner, kind: kind) {
280300
}
281301

282302
protected override bool _isFlingGesture(VelocityEstimate estimate) {
@@ -303,8 +323,8 @@ public override string debugDescription {
303323
}
304324

305325
public class PanGestureRecognizer : DragGestureRecognizer {
306-
public PanGestureRecognizer(object debugOwner = null)
307-
: base(debugOwner: debugOwner) {
326+
public PanGestureRecognizer(object debugOwner = null, PointerDeviceKind? kind = null)
327+
: base(debugOwner: debugOwner, kind: kind) {
308328
}
309329

310330
protected override bool _isFlingGesture(VelocityEstimate estimate) {

Runtime/gestures/multidrag.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,14 @@ public virtual void dispose() {
140140

141141
public abstract class MultiDragGestureRecognizer<T> : GestureRecognizer where T : MultiDragPointerState {
142142
protected MultiDragGestureRecognizer(
143-
object debugOwner) : base(debugOwner: debugOwner) {
143+
object debugOwner, PointerDeviceKind? kind = null) : base(debugOwner: debugOwner, kind: kind) {
144144
}
145145

146146
public GestureMultiDragStartCallback onStart;
147147

148148
Dictionary<int, T> _pointers = new Dictionary<int, T>();
149149

150-
public override void addPointer(PointerDownEvent pEvent) {
150+
public override void addAllowedPointer(PointerDownEvent pEvent) {
151151
D.assert(this._pointers != null);
152152
D.assert(pEvent.position != null);
153153
D.assert(!this._pointers.ContainsKey(pEvent.pointer));
@@ -264,7 +264,8 @@ public override void accepted(GestureMultiDragStartCallback starter) {
264264

265265

266266
public class ImmediateMultiDragGestureRecognizer : MultiDragGestureRecognizer<_ImmediatePointerState> {
267-
public ImmediateMultiDragGestureRecognizer(object debugOwner) : base(debugOwner: debugOwner) {
267+
public ImmediateMultiDragGestureRecognizer(object debugOwner, PointerDeviceKind? kind = null) : base(
268+
debugOwner: debugOwner, kind: kind) {
268269
}
269270

270271
public override _ImmediatePointerState createNewPointerState(PointerDownEvent pEvent) {
@@ -293,7 +294,8 @@ public override void accepted(GestureMultiDragStartCallback starter) {
293294
}
294295

295296
public class HorizontalMultiDragGestureRecognizer : MultiDragGestureRecognizer<_HorizontalPointerState> {
296-
public HorizontalMultiDragGestureRecognizer(object debugOwner) : base(debugOwner: debugOwner) {
297+
public HorizontalMultiDragGestureRecognizer(object debugOwner, PointerDeviceKind? kind = null) : base(
298+
debugOwner: debugOwner, kind: kind) {
297299
}
298300

299301
public override _HorizontalPointerState createNewPointerState(PointerDownEvent pEvent) {
@@ -324,7 +326,8 @@ public override void accepted(GestureMultiDragStartCallback starter) {
324326

325327

326328
public class VerticalMultiDragGestureRecognizer : MultiDragGestureRecognizer<_VerticalPointerState> {
327-
public VerticalMultiDragGestureRecognizer(object debugOwner) : base(debugOwner: debugOwner) {
329+
public VerticalMultiDragGestureRecognizer(object debugOwner, PointerDeviceKind? kind = null) : base(
330+
debugOwner: debugOwner, kind: kind) {
328331
}
329332

330333
public override _VerticalPointerState createNewPointerState(PointerDownEvent pEvent) {
@@ -402,7 +405,8 @@ public override void dispose() {
402405
public class DelayedMultiDragGestureRecognizer : MultiDragGestureRecognizer<_DelayedPointerState> {
403406
public DelayedMultiDragGestureRecognizer(
404407
TimeSpan? delay = null,
405-
object debugOwner = null) : base(debugOwner: debugOwner) {
408+
object debugOwner = null,
409+
PointerDeviceKind? kind = null) : base(debugOwner: debugOwner, kind: kind) {
406410
if (delay == null) {
407411
delay = Constants.kLongPressTimeout;
408412
}

Runtime/gestures/multitap.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ public bool isWithinTolerance(PointerEvent evt, float tolerance) {
5353

5454

5555
public class DoubleTapGestureRecognizer : GestureRecognizer {
56-
public DoubleTapGestureRecognizer(object debugOwner = null)
57-
: base(debugOwner: debugOwner) {
56+
public DoubleTapGestureRecognizer(object debugOwner = null, PointerDeviceKind? kind = null)
57+
: base(debugOwner: debugOwner, kind: kind) {
5858
}
5959

6060
public GestureDoubleTapCallback onDoubleTap;
@@ -63,7 +63,7 @@ public DoubleTapGestureRecognizer(object debugOwner = null)
6363
_TapTracker _firstTap;
6464
readonly Dictionary<int, _TapTracker> _trackers = new Dictionary<int, _TapTracker>();
6565

66-
public override void addPointer(PointerDownEvent evt) {
66+
public override void addAllowedPointer(PointerDownEvent evt) {
6767
if (this._firstTap != null &&
6868
!this._firstTap.isWithinTolerance(evt, Constants.kDoubleTapSlop)) {
6969
return;

Runtime/gestures/recognizer.cs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,38 @@
88
namespace Unity.UIWidgets.gestures {
99
public delegate T RecognizerCallback<T>();
1010

11+
public enum DragStartBehavior {
12+
down,
13+
start
14+
}
15+
1116
public abstract class GestureRecognizer : DiagnosticableTree, GestureArenaMember {
12-
protected GestureRecognizer(object debugOwner = null) {
17+
protected GestureRecognizer(object debugOwner = null, PointerDeviceKind? kind = null) {
1318
this.debugOwner = debugOwner;
19+
this._kind = kind;
1420
}
1521

1622
public readonly object debugOwner;
1723

18-
public abstract void addPointer(PointerDownEvent evt);
24+
readonly PointerDeviceKind? _kind;
25+
26+
public void addPointer(PointerDownEvent evt) {
27+
if (this.isPointerAllowed(evt)) {
28+
this.addAllowedPointer(evt);
29+
}
30+
else {
31+
this.handleNonAllowedPointer(evt);
32+
}
33+
}
34+
35+
public abstract void addAllowedPointer(PointerDownEvent evt);
36+
37+
protected virtual void handleNonAllowedPointer(PointerDownEvent evt) {
38+
}
39+
40+
protected bool isPointerAllowed(PointerDownEvent evt) {
41+
return this._kind == null || this._kind == evt.kind;
42+
}
1943

2044
public virtual void addScrollPointer(PointerScrollEvent evt) {
2145
}
@@ -72,7 +96,8 @@ public override void debugFillProperties(DiagnosticPropertiesBuilder properties)
7296
}
7397

7498
public abstract class OneSequenceGestureRecognizer : GestureRecognizer {
75-
protected OneSequenceGestureRecognizer(object debugOwner = null) : base(debugOwner) {
99+
protected OneSequenceGestureRecognizer(object debugOwner = null, PointerDeviceKind? kind = null) : base(
100+
debugOwner, kind) {
76101
}
77102

78103
readonly Dictionary<int, GestureArenaEntry> _entries = new Dictionary<int, GestureArenaEntry>();
@@ -175,8 +200,9 @@ public enum GestureRecognizerState {
175200
public abstract class PrimaryPointerGestureRecognizer : OneSequenceGestureRecognizer {
176201
protected PrimaryPointerGestureRecognizer(
177202
TimeSpan? deadline = null,
178-
object debugOwner = null
179-
) : base(debugOwner: debugOwner) {
203+
object debugOwner = null,
204+
PointerDeviceKind? kind = null
205+
) : base(debugOwner: debugOwner, kind: kind) {
180206
this.deadline = deadline;
181207
}
182208

@@ -190,7 +216,7 @@ protected PrimaryPointerGestureRecognizer(
190216

191217
Timer _timer;
192218

193-
public override void addPointer(PointerDownEvent evt) {
219+
public override void addAllowedPointer(PointerDownEvent evt) {
194220
this.startTrackingPointer(evt.pointer);
195221
if (this.state == GestureRecognizerState.ready) {
196222
this.state = GestureRecognizerState.possible;

Runtime/material/text_field.cs

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,12 @@ void _handleSelectionChanged(TextSelection selection, SelectionChangedCause caus
249249
}
250250
}
251251

252-
InteractiveInkFeature _createInkFeature(TapDownDetails details) {
252+
InteractiveInkFeature _createInkFeature(Offset globalPosition) {
253253
MaterialInkController inkController = Material.of(this.context);
254254
BuildContext editableContext = this._editableTextKey.currentContext;
255255
RenderBox referenceBox =
256256
(RenderBox) (InputDecorator.containerOf(editableContext) ?? editableContext.findRenderObject());
257-
Offset position = referenceBox.globalToLocal(details.globalPosition);
257+
Offset position = referenceBox.globalToLocal(globalPosition);
258258
Color color = Theme.of(this.context).splashColor;
259259

260260
InteractiveInkFeature splash = null;
@@ -290,10 +290,10 @@ RenderEditable _renderEditable {
290290

291291
void _handleTapDown(TapDownDetails details) {
292292
this._renderEditable.handleTapDown(details);
293-
this._startSplash(details);
293+
this._startSplash(details.globalPosition);
294294
}
295295

296-
void _handleTap() {
296+
void _handleSingleTapUp(TapUpDetails details) {
297297
if (this.widget.enableInteractiveSelection) {
298298
this._renderEditable.handleTap();
299299
}
@@ -305,7 +305,7 @@ void _handleTap() {
305305
}
306306
}
307307

308-
void _handleTapCancel() {
308+
void _handleSingleTapCancel() {
309309
this._cancelCurrentSplash();
310310
}
311311

@@ -317,12 +317,29 @@ void _handleLongPress() {
317317
this._confirmCurrentSplash();
318318
}
319319

320-
void _startSplash(TapDownDetails details) {
320+
void _handleDragSelectionStart(DragStartDetails details) {
321+
this._renderEditable.selectPositionAt(
322+
from: details.globalPosition,
323+
cause: SelectionChangedCause.drag);
324+
325+
this._startSplash(details.globalPosition);
326+
}
327+
328+
void _handleDragSelectionUpdate(DragStartDetails startDetails,
329+
DragUpdateDetails updateDetails) {
330+
this._renderEditable.selectPositionAt(
331+
from: startDetails.globalPosition,
332+
to: updateDetails.globalPosition,
333+
cause: SelectionChangedCause.drag);
334+
}
335+
336+
337+
void _startSplash(Offset globalPosition) {
321338
if (this._effectiveFocusNode.hasFocus) {
322339
return;
323340
}
324341

325-
InteractiveInkFeature splash = this._createInkFeature(details);
342+
InteractiveInkFeature splash = this._createInkFeature(globalPosition);
326343
this._splashes = this._splashes ?? new HashSet<InteractiveInkFeature>();
327344
this._splashes.Add(splash);
328345
this._currentSplash = splash;
@@ -426,12 +443,14 @@ public override Widget build(BuildContext context) {
426443

427444
return new IgnorePointer(
428445
ignoring: !(this.widget.enabled ?? this.widget.decoration?.enabled ?? true),
429-
child: new GestureDetector(
430-
behavior: HitTestBehavior.translucent,
446+
child: new TextSelectionGestureDetector(
431447
onTapDown: this._handleTapDown,
432-
onTap: this._handleTap,
433-
onTapCancel: this._handleTapCancel,
434-
onLongPress: this._handleLongPress,
448+
onSingleTapUp: this._handleSingleTapUp,
449+
onSingleTapCancel: this._handleSingleTapCancel,
450+
onSingleLongTapStart: this._handleLongPress,
451+
onDragSelectionStart: this._handleDragSelectionStart,
452+
onDragSelectionUpdate: this._handleDragSelectionUpdate,
453+
behavior: HitTestBehavior.translucent,
435454
child: child
436455
)
437456
);

0 commit comments

Comments
 (0)