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

Commit 0095f6f

Browse files
author
Yuncong Zhang
committed
[1.5.4] Finish TODOs in gesture_detector and text_selection.
1 parent 0d4e94e commit 0095f6f

File tree

7 files changed

+166
-179
lines changed

7 files changed

+166
-179
lines changed

Runtime/gestures/long_press.cs

Lines changed: 56 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -6,168 +6,123 @@ namespace Unity.UIWidgets.gestures {
66

77
public delegate void GestureLongPressUpCallback();
88

9-
public delegate void GestureLongPressDragStartCallback(GestureLongPressDragStartDetails details);
9+
public delegate void GestureLongPressStartCallback(LongPressStartDetails details);
1010

11-
public delegate void GestureLongPressDragUpdateCallback(GestureLongPressDragUpdateDetails details);
11+
public delegate void GestureLongPressMoveUpdateCallback(LongPressMoveUpdateDetails details);
1212

13-
public delegate void GestureLongPressDragUpCallback(GestureLongPressDragUpDetails details);
13+
public delegate void GestureLongPressEndCallback(LongPressEndDetails details);
1414

15-
public class GestureLongPressDragStartDetails {
16-
public GestureLongPressDragStartDetails(
17-
TimeSpan? sourceTimeStamp = null,
15+
public class LongPressStartDetails {
16+
public LongPressStartDetails(
1817
Offset globalPosition = null
1918
) {
20-
this.sourceTimeStamp = sourceTimeStamp;
2119
this.globalPosition = globalPosition ?? Offset.zero;
2220
}
2321

24-
25-
public readonly TimeSpan? sourceTimeStamp;
26-
2722
public readonly Offset globalPosition;
2823
}
2924

30-
public class GestureLongPressDragUpdateDetails {
31-
public GestureLongPressDragUpdateDetails(
32-
TimeSpan? sourceTimeStamp = null,
25+
public class LongPressMoveUpdateDetails {
26+
public LongPressMoveUpdateDetails(
3327
Offset globalPosition = null,
3428
Offset offsetFromOrigin = null
3529
) {
36-
this.sourceTimeStamp = sourceTimeStamp;
3730
this.globalPosition = globalPosition ?? Offset.zero;
3831
this.offsetFromOrigin = offsetFromOrigin ?? Offset.zero;
3932
}
4033

41-
public readonly TimeSpan? sourceTimeStamp;
42-
4334
public readonly Offset globalPosition;
4435

4536
public readonly Offset offsetFromOrigin;
4637
}
4738

48-
public class GestureLongPressDragUpDetails {
49-
public GestureLongPressDragUpDetails(
50-
TimeSpan? sourceTimeStamp = null,
39+
public class LongPressEndDetails {
40+
public LongPressEndDetails(
5141
Offset globalPosition = null
5242
) {
53-
this.sourceTimeStamp = sourceTimeStamp;
5443
this.globalPosition = globalPosition ?? Offset.zero;
5544
}
5645

57-
public readonly TimeSpan? sourceTimeStamp;
58-
5946
public readonly Offset globalPosition;
6047
}
6148

6249

6350
public class LongPressGestureRecognizer : PrimaryPointerGestureRecognizer {
64-
public LongPressGestureRecognizer(object debugOwner = null, PointerDeviceKind? kind = null) :
65-
base(deadline: Constants.kLongPressTimeout, debugOwner: debugOwner, kind: kind) {
66-
}
51+
public LongPressGestureRecognizer(
52+
float? postAcceptSlopTolerance = null,
53+
object debugOwner = null,
54+
PointerDeviceKind? kind = null) : base(
55+
deadline: Constants.kLongPressTimeout,
56+
postAcceptSlopTolerance: postAcceptSlopTolerance,
57+
kind: kind,
58+
debugOwner: debugOwner) { }
6759

6860
bool _longPressAccepted = false;
6961

62+
Offset _longPressOrigin;
63+
7064
public GestureLongPressCallback onLongPress;
7165

66+
public GestureLongPressStartCallback onLongPressStart;
67+
68+
public GestureLongPressMoveUpdateCallback onLongPressMoveUpdate;
69+
7270
public GestureLongPressUpCallback onLongPressUp;
7371

72+
public GestureLongPressEndCallback onLongPressEnd;
73+
7474
protected override void didExceedDeadline() {
7575
this.resolve(GestureDisposition.accepted);
7676
this._longPressAccepted = true;
77-
77+
base.acceptGesture(this.primaryPointer);
7878
if (this.onLongPress != null) {
7979
this.invokeCallback<object>("onLongPress", () => {
8080
this.onLongPress();
8181
return null;
8282
});
8383
}
84-
}
8584

86-
protected override void handlePrimaryPointer(PointerEvent evt) {
87-
if (evt is PointerUpEvent) {
88-
if (this._longPressAccepted && this.onLongPressUp != null) {
89-
this._longPressAccepted = false;
90-
this.invokeCallback<object>("onLongPressUp", () => {
91-
this.onLongPressUp();
85+
if (this.onLongPressStart != null) {
86+
this.invokeCallback<object>("onLongPressStart",
87+
() => {
88+
this.onLongPressStart(new LongPressStartDetails(globalPosition: this._longPressOrigin));
9289
return null;
9390
});
94-
}
95-
else {
96-
this.resolve(GestureDisposition.rejected);
97-
}
98-
}
99-
else if (evt is PointerDownEvent || evt is PointerCancelEvent) {
100-
this._longPressAccepted = false;
101-
}
102-
}
103-
104-
public override string debugDescription {
105-
get { return "long press"; }
106-
}
107-
}
108-
109-
public class LongPressDragGestureRecognizer : PrimaryPointerGestureRecognizer {
110-
public LongPressDragGestureRecognizer(object debugOwner = null) : base(
111-
deadline: Constants.kLongPressTimeout,
112-
postAcceptSlopTolerance: null,
113-
debugOwner: debugOwner
114-
) {
115-
}
116-
117-
bool _longPressAccepted = false;
118-
119-
Offset _longPressOrigin;
120-
121-
TimeSpan? _longPressStartTimestamp;
122-
123-
public GestureLongPressDragStartCallback onLongPressStart;
124-
125-
public GestureLongPressDragUpdateCallback onLongPressDragUpdate;
126-
127-
public GestureLongPressDragUpCallback onLongPressUp;
128-
129-
protected override void didExceedDeadline() {
130-
this.resolve(GestureDisposition.accepted);
131-
this._longPressAccepted = true;
132-
base.acceptGesture(this.primaryPointer);
133-
if (this.onLongPressStart != null) {
134-
this.invokeCallback<object>("onLongPressStart", () => {
135-
this.onLongPressStart(new GestureLongPressDragStartDetails(
136-
sourceTimeStamp: this._longPressStartTimestamp,
137-
globalPosition: this._longPressOrigin
138-
));
139-
return null;
140-
});
14191
}
14292
}
14393

144-
protected override void handlePrimaryPointer(PointerEvent e) {
145-
if (e is PointerUpEvent) {
146-
if (this._longPressAccepted == true && this.onLongPressUp != null) {
147-
this._longPressAccepted = false;
148-
this.invokeCallback<object>("onLongPressUp", () => {
149-
this.onLongPressUp(new GestureLongPressDragUpDetails(
150-
sourceTimeStamp: e.timeStamp,
151-
globalPosition: e.position
152-
));
153-
return null;
154-
});
94+
protected override void handlePrimaryPointer(PointerEvent evt) {
95+
if (evt is PointerUpEvent) {
96+
if (this._longPressAccepted) {
97+
if (this.onLongPressUp != null) {
98+
this.invokeCallback<object>("onLongPressUp", () => {
99+
this.onLongPressUp();
100+
return null;
101+
});
102+
}
103+
104+
if (this.onLongPressEnd != null) {
105+
this.invokeCallback<object>("onLongPressEnd", () => {
106+
this.onLongPressEnd(new LongPressEndDetails(globalPosition: evt.position));
107+
return null;
108+
});
109+
}
110+
111+
this._longPressAccepted = true;
155112
}
156113
else {
157114
this.resolve(GestureDisposition.rejected);
158115
}
159116
}
160-
else if (e is PointerDownEvent) {
117+
else if (evt is PointerDownEvent || evt is PointerCancelEvent) {
161118
this._longPressAccepted = false;
162-
this._longPressStartTimestamp = e.timeStamp;
163-
this._longPressOrigin = e.position;
119+
this._longPressOrigin = evt.position;
164120
}
165-
else if (e is PointerMoveEvent && this._longPressAccepted && this.onLongPressDragUpdate != null) {
166-
this.invokeCallback<object>("onLongPressDrag", () => {
167-
this.onLongPressDragUpdate(new GestureLongPressDragUpdateDetails(
168-
sourceTimeStamp: e.timeStamp,
169-
globalPosition: e.position,
170-
offsetFromOrigin: e.position - this._longPressOrigin
121+
else if (evt is PointerMoveEvent && this._longPressAccepted && this.onLongPressMoveUpdate != null) {
122+
this.invokeCallback<object>("onLongPressMoveUpdate", () => {
123+
this.onLongPressMoveUpdate(new LongPressMoveUpdateDetails(
124+
globalPosition: evt.position,
125+
offsetFromOrigin: evt.position - this._longPressOrigin
171126
));
172127
return null;
173128
});
@@ -177,15 +132,8 @@ protected override void handlePrimaryPointer(PointerEvent e) {
177132
public override void acceptGesture(int pointer) {
178133
}
179134

180-
protected override void didStopTrackingLastPointer(int pointer) {
181-
this._longPressAccepted = false;
182-
this._longPressOrigin = null;
183-
this._longPressStartTimestamp = null;
184-
base.didStopTrackingLastPointer(pointer);
185-
}
186-
187135
public override string debugDescription {
188-
get { return "long press drag"; }
136+
get { return "long press"; }
189137
}
190138
}
191139
}

Runtime/material/text_field.cs

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -366,23 +366,64 @@ void _handleSingleTapCancel() {
366366
this._cancelCurrentSplash();
367367
}
368368

369-
void _handleLongPress() {
370-
if (this.widget.enableInteractiveSelection == true) {
371-
this._renderEditable.handleLongPress();
369+
void _handleSingleLongTapStart(LongPressStartDetails details) {
370+
if (this.widget.selectionEnabled) {
371+
switch (Theme.of(this.context).platform) {
372+
case RuntimePlatform.IPhonePlayer:
373+
this._renderEditable.selectPositionAt(
374+
from: details.globalPosition,
375+
cause: SelectionChangedCause.longPress
376+
);
377+
break;
378+
case RuntimePlatform.Android:
379+
this._renderEditable.selectWord(cause: SelectionChangedCause.longPress);
380+
Feedback.forLongPress(this.context);
381+
break;
382+
}
372383
}
373-
374384
this._confirmCurrentSplash();
375385
}
376386

377-
void _handleDragSelectionStart(DragStartDetails details) {
387+
void _handleSingleLongTapMoveUpdate(LongPressMoveUpdateDetails details) {
388+
if (this.widget.selectionEnabled) {
389+
switch (Theme.of(this.context).platform) {
390+
case RuntimePlatform.IPhonePlayer:
391+
this._renderEditable.selectPositionAt(
392+
from: details.globalPosition,
393+
cause: SelectionChangedCause.longPress
394+
);
395+
break;
396+
case RuntimePlatform.Android:
397+
this._renderEditable.selectWordsInRange(
398+
from: details.globalPosition - details.offsetFromOrigin,
399+
to: details.globalPosition,
400+
cause: SelectionChangedCause.longPress);
401+
Feedback.forLongPress(this.context);
402+
break;
403+
}
404+
}
405+
}
406+
407+
void _handleSingleLongTapEnd(LongPressEndDetails details) {
408+
this._editableTextKey.currentState.showToolbar();
409+
}
410+
411+
void _handleDoubleTapDown(TapDownDetails details) {
412+
if (this.widget.selectionEnabled) {
413+
this._renderEditable.selectWord(cause: SelectionChangedCause.doubleTap);
414+
this._editableTextKey.currentState.showToolbar();
415+
}
416+
}
417+
418+
void _handleMouseDragSelectionStart(DragStartDetails details) {
378419
this._renderEditable.selectPositionAt(
379420
from: details.globalPosition,
380421
cause: SelectionChangedCause.drag);
381422

382423
this._startSplash(details.globalPosition);
383424
}
384425

385-
void _handleDragSelectionUpdate(DragStartDetails startDetails,
426+
void _handleMouseDragSelectionUpdate(DragStartDetails startDetails,
386427
DragUpdateDetails updateDetails) {
387428
this._renderEditable.selectPositionAt(
388429
from: startDetails.globalPosition,
@@ -521,9 +562,12 @@ public override Widget build(BuildContext context) {
521562
// onForcePressStart: forcePressEnabled ? this._handleForcePressStarted : null, // TODO: Remove this when force press is added
522563
onSingleTapUp: this._handleSingleTapUp,
523564
onSingleTapCancel: this._handleSingleTapCancel,
524-
onSingleLongTapStart: this._handleLongPress,
525-
onDragSelectionStart: this._handleDragSelectionStart,
526-
onDragSelectionUpdate: this._handleDragSelectionUpdate,
565+
onSingleLongTapStart: this._handleSingleLongTapStart,
566+
onSingleLongTapMoveUpdate: this._handleSingleLongTapMoveUpdate,
567+
onSingleLongTapEnd: this._handleSingleLongTapEnd,
568+
onDoubleTapDown: this._handleDoubleTapDown,
569+
onDragSelectionStart: this._handleMouseDragSelectionStart,
570+
onDragSelectionUpdate: this._handleMouseDragSelectionUpdate,
527571
behavior: HitTestBehavior.translucent,
528572
child: child
529573
)

Runtime/rendering/editable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,7 @@ public void selectWord(SelectionChangedCause? cause = null) {
11161116
this.selectWordsInRange(from: this._lastTapDownPosition, cause: cause);
11171117
}
11181118

1119-
void selectWordsInRange(Offset from = null, Offset to = null, SelectionChangedCause? cause = null) {
1119+
public void selectWordsInRange(Offset from = null, Offset to = null, SelectionChangedCause? cause = null) {
11201120
D.assert(cause != null);
11211121
D.assert(from != null);
11221122
this._layoutText(this.constraints.maxWidth);

Runtime/widgets/basic.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1977,7 +1977,7 @@ public Listener(
19771977
PointerHoverEventListener onPointerHover = null,
19781978
PointerUpEventListener onPointerUp = null,
19791979
PointerCancelEventListener onPointerCancel = null,
1980-
// TODO: onPointerSignal = null,
1980+
PointerSignalEventListener onPointerSignal = null,
19811981
PointerScrollEventListener onPointerScroll = null,
19821982
PointerDragFromEditorEnterEventListener onPointerDragFromEditorEnter = null,
19831983
PointerDragFromEditorHoverEventListener onPointerDragFromEditorHover = null,
@@ -1990,6 +1990,7 @@ public Listener(
19901990
this.onPointerMove = onPointerMove;
19911991
this.onPointerUp = onPointerUp;
19921992
this.onPointerCancel = onPointerCancel;
1993+
this.onPointerSignal = onPointerSignal;
19931994
this.onPointerHover = onPointerHover;
19941995
this.onPointerExit = onPointerExit;
19951996
this.onPointerEnter = onPointerEnter;
@@ -2010,6 +2011,8 @@ public Listener(
20102011

20112012
public readonly PointerCancelEventListener onPointerCancel;
20122013

2014+
public readonly PointerSignalEventListener onPointerSignal;
2015+
20132016
public readonly PointerHoverEventListener onPointerHover;
20142017

20152018
public readonly PointerEnterEventListener onPointerEnter;
@@ -2034,6 +2037,7 @@ public override RenderObject createRenderObject(BuildContext context) {
20342037
onPointerMove: this.onPointerMove,
20352038
onPointerUp: this.onPointerUp,
20362039
onPointerCancel: this.onPointerCancel,
2040+
onPointerSignal: this.onPointerSignal,
20372041
onPointerEnter: this.onPointerEnter,
20382042
onPointerExit: this.onPointerExit,
20392043
onPointerHover: this.onPointerHover,
@@ -2052,6 +2056,7 @@ public override void updateRenderObject(BuildContext context, RenderObject rende
20522056
renderObject.onPointerMove = this.onPointerMove;
20532057
renderObject.onPointerUp = this.onPointerUp;
20542058
renderObject.onPointerCancel = this.onPointerCancel;
2059+
renderObject.onPointerSignal = this.onPointerSignal;
20552060
renderObject.onPointerEnter = this.onPointerEnter;
20562061
renderObject.onPointerHover = this.onPointerHover;
20572062
renderObject.onPointerExit = this.onPointerExit;
@@ -2085,6 +2090,10 @@ public override void debugFillProperties(DiagnosticPropertiesBuilder properties)
20852090
listeners.Add("cancel");
20862091
}
20872092

2093+
if (this.onPointerSignal != null) {
2094+
listeners.Add("signal");
2095+
}
2096+
20882097
if (this.onPointerEnter != null) {
20892098
listeners.Add("enter");
20902099
}

0 commit comments

Comments
 (0)