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

Commit a6995b2

Browse files
author
Yuncong Zhang
authored
Merge pull request #246 from UnityTech/longpressup
Longpressup
2 parents d147abc + 11d8dda commit a6995b2

File tree

3 files changed

+35
-21
lines changed

3 files changed

+35
-21
lines changed

Runtime/gestures/long_press.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,16 @@ public LongPressGestureRecognizer(object debugOwner = null, PointerDeviceKind? k
6565
base(deadline: Constants.kLongPressTimeout, debugOwner: debugOwner, kind: kind) {
6666
}
6767

68+
bool _longPressAccepted = false;
69+
6870
public GestureLongPressCallback onLongPress;
6971

72+
public GestureLongPressUpCallback onLongPressUp;
73+
7074
protected override void didExceedDeadline() {
7175
this.resolve(GestureDisposition.accepted);
76+
this._longPressAccepted = true;
77+
7278
if (this.onLongPress != null) {
7379
this.invokeCallback<object>("onLongPress", () => {
7480
this.onLongPress();
@@ -79,7 +85,19 @@ protected override void didExceedDeadline() {
7985

8086
protected override void handlePrimaryPointer(PointerEvent evt) {
8187
if (evt is PointerUpEvent) {
82-
this.resolve(GestureDisposition.rejected);
88+
if (this._longPressAccepted && this.onLongPressUp != null) {
89+
this._longPressAccepted = false;
90+
this.invokeCallback<object>("onLongPressUp", () => {
91+
this.onLongPressUp();
92+
return null;
93+
});
94+
}
95+
else {
96+
this.resolve(GestureDisposition.rejected);
97+
}
98+
}
99+
else if (evt is PointerDownEvent || evt is PointerCancelEvent) {
100+
this._longPressAccepted = false;
83101
}
84102
}
85103

@@ -101,7 +119,7 @@ public LongPressDragGestureRecognizer(object debugOwner = null) : base(
101119
Offset _longPressOrigin;
102120

103121
TimeSpan? _longPressStartTimestamp;
104-
122+
105123
public GestureLongPressDragStartCallback onLongPressStart;
106124

107125
public GestureLongPressDragUpdateCallback onLongPressDragUpdate;

Runtime/widgets/gesture_detector.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ public GestureDetector(
113113
throw new UIWidgetsError(
114114
"Incorrect GestureDetector arguments.\n" +
115115
"Having both a pan gesture recognizer and a scale gesture recognizer is redundant; scale is a superset of pan. Just use the scale gesture recognizer."
116-
);
116+
);
117117
}
118-
118+
119119
string recognizer = havePan ? "pan" : "scale";
120120
if (haveVerticalDrag && haveHorizontalDrag) {
121121
throw new UIWidgetsError(
@@ -125,7 +125,7 @@ public GestureDetector(
125125
);
126126
}
127127
}
128-
128+
129129
if (haveLongPress && haveLongPressDrag) {
130130
throw new UIWidgetsError(
131131
"Incorrect GestureDetector arguments.\n" +
@@ -235,7 +235,10 @@ public override Widget build(BuildContext context) {
235235
gestures[typeof(LongPressGestureRecognizer)] =
236236
new GestureRecognizerFactoryWithHandlers<LongPressGestureRecognizer>(
237237
() => new LongPressGestureRecognizer(debugOwner: this),
238-
instance => { instance.onLongPress = this.onLongPress; }
238+
instance => {
239+
instance.onLongPress = this.onLongPress;
240+
instance.onLongPressUp = this.onLongPressUp;
241+
}
239242
);
240243
}
241244

Samples/UIWidgetSample/LongPressSample.cs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1-
using Unity.UIWidgets.animation;
2-
using Unity.UIWidgets.engine;
31
using Unity.UIWidgets.material;
42
using Unity.UIWidgets.widgets;
53
using UnityEngine;
64

75
namespace UIWidgetsSample {
8-
public class LongPressSample: UIWidgetsSamplePanel {
9-
10-
protected override Widget createWidget() {
6+
public class LongPressSample : UIWidgetsSamplePanel {
7+
protected override Widget createWidget() {
118
return new WidgetsApp(
129
home: new LongPressSampleWidget(),
1310
pageRouteBuilder: this.pageRouteBuilder);
1411
}
1512
}
1613

17-
public class LongPressSampleWidget: StatefulWidget {
14+
public class LongPressSampleWidget : StatefulWidget {
1815
public override State createState() {
1916
return new _LongPressSampleWidgetState();
2017
}
@@ -23,15 +20,11 @@ public override State createState() {
2320
class _LongPressSampleWidgetState : State<LongPressSampleWidget> {
2421
public override Widget build(BuildContext context) {
2522
return new GestureDetector(
26-
onLongPressDragStart: (value) => {
27-
Debug.Log($"Long Press Drag Start: {value}");
28-
},
29-
onLongPressDragUpdate: (value) => {
30-
Debug.Log($"Long Press Drag Update: {value}");
31-
},
32-
onLongPressDragUp: (value) => {
33-
Debug.Log($"Long Press Drag Up: {value}");
34-
},
23+
onLongPressDragStart: (value) => { Debug.Log($"Long Press Drag Start: {value}"); },
24+
onLongPressDragUpdate: (value) => { Debug.Log($"Long Press Drag Update: {value}"); },
25+
onLongPressDragUp: (value) => { Debug.Log($"Long Press Drag Up: {value}"); },
26+
onLongPressUp: () => { Debug.Log($"Long Press Up"); },
27+
onLongPress: () => { Debug.Log($"Long Press"); },
3528
child: new Center(
3629
child: new Container(
3730
width: 200,

0 commit comments

Comments
 (0)