Skip to content

Commit 49b4b64

Browse files
committed
fix rage touch masked by scheduled long press handler
1 parent bb986cf commit 49b4b64

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

uipanel/KeyModifier.swift

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ struct KeyModifier: ViewModifier {
2727
let threshold: CGFloat = 30
2828
let stepSize: CGFloat = 15
2929

30+
@State private var touchId = 0
3031
@State private var isPressed = false
31-
@State private var didTriggerLongPress = false
32-
@State private var didMoveFarEnough = false
3332
@State private var startLocation: CGPoint?
3433
@State private var lastLocation: CGFloat?
34+
@State private var didTriggerLongPress = false
35+
@State private var didMoveFarEnough = false
3536
@State private var slideActivated = false
3637

3738
let x: CGFloat
@@ -77,24 +78,29 @@ struct KeyModifier: ViewModifier {
7778
let bubbleWidth = width - columnGap
7879
let bubbleHeight = height - rowGap
7980

80-
if startLocation == nil {
81+
if !isPressed { // touch start
82+
touchId = (touchId + 1) & 0xFFFF
83+
let currentTouchId = touchId
84+
isPressed = true
8185
startLocation = value.startLocation
8286
lastLocation = value.startLocation.x
83-
isPressed = true
84-
didTriggerLongPress = false
85-
didMoveFarEnough = false
87+
8688
virtualKeyboardView.setBubble(
8789
bubbleX, bubbleY, bubbleWidth, bubbleHeight, background, shadow, bubbleLabel)
8890

8991
// Schedule long press that can be interrupted by move.
9092
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
93+
if touchId != currentTouchId {
94+
// Called from a previous touch.
95+
return
96+
}
9197
if isPressed && !didTriggerLongPress && !didMoveFarEnough {
9298
didTriggerLongPress = true
9399
clearBubble()
94100
action.onLongPress?()
95101
}
96102
}
97-
} else {
103+
} else { // touch move
98104
let dx = value.location.x - (startLocation?.x ?? 0)
99105
let dy = value.location.y - (startLocation?.y ?? 0)
100106

@@ -132,11 +138,13 @@ struct KeyModifier: ViewModifier {
132138
}
133139
}
134140
.onEnded { value in
135-
isPressed = false
136141
clearBubble()
137142
defer {
143+
isPressed = false
138144
startLocation = nil
139145
lastLocation = nil
146+
didTriggerLongPress = false
147+
didMoveFarEnough = false
140148
slideActivated = false
141149
}
142150

0 commit comments

Comments
 (0)