Touchpad: one finger held, the other slides = drag (move windows, resize, drag icons)

This commit is contained in:
The412Banner committed 2026-09-24 19:10:29 -04:00
1 parent 6e69f252f6
commit 0c06868c5d
1 file changed
+83
@@ -16,6 +16,10 @@ import kotlin.math.abs
* - two fingers up / down ............ scroll wheel
* - hold still, then drag ............ left-button drag
* - double-tap, hold, drag ........... left-button drag as well
* - one finger held, another slides .. left-button drag, as on a laptop's pad: the held finger is
* the button, the other moves the pointer (a window by its
* title bar or edge, a desktop icon). The sliding finger may
* lift and slide again; lifting the held one drops.
*
* Positions handed to the listener are the pointer's, in the view's pixels; the caller keeps them
* inside the picture.
@@ -45,6 +49,16 @@ class TouchpadGestures(
private var twoFingers = false
private var scrollAnchorY = 0f
private var scrolled = false
// Hold-and-slide: the first finger stays as the button, the second moves the pointer.
private var anchorId = -1
private var anchorStartX = 0f
private var anchorStartY = 0f
private var moverId = -1
private var moverStartX = 0f
private var moverStartY = 0f
private var moverLastX = 0f
private var moverLastY = 0f
private var holdDrag = false
private val longPress = Runnable {
held = true
dragging = true
@@ -63,6 +77,7 @@ class TouchpadGestures(
lastX = event.x; lastY = event.y
downAt = event.eventTime
travelled = 0f; held = false; twoFingers = false; scrolled = false; secondWasTap = false
anchorId = event.getPointerId(0); moverId = -1; holdDrag = false
// A tap shortly before this touch: holding now drags (double-tap-and-hold).
if (event.eventTime - lastTapAt < DOUBLE_TAP_MS) {
dragging = true
@@ -75,6 +90,17 @@ class TouchpadGestures(
}
MotionEvent.ACTION_POINTER_DOWN -> {
handler.removeCallbacks(longPress)
val i = event.actionIndex
if (moverId == -1 && event.getPointerId(i) != anchorId) {
moverId = event.getPointerId(i)
moverStartX = event.getX(i); moverStartY = event.getY(i)
moverLastX = moverStartX; moverLastY = moverStartY
event.findPointerIndex(anchorId).takeIf { it >= 0 }?.let {
anchorStartX = event.getX(it); anchorStartY = event.getY(it)
}
}
// Mid-drag, a finger coming back only picks the sliding up again.
if (holdDrag) return true
twoFingers = true
secondDownAt = event.eventTime
secondWasTap = true
@@ -82,6 +108,38 @@ class TouchpadGestures(
return true
}
MotionEvent.ACTION_MOVE -> {
val ai = event.findPointerIndex(anchorId)
val mi = if (moverId == -1) -1 else event.findPointerIndex(moverId)
if (ai >= 0 && mi >= 0) {
val mx = event.getX(mi); val my = event.getY(mi)
// One finger still, the other on the move: that is a drag, not a scroll (which
// moves both). Decided once, before anything has scrolled.
if (!holdDrag && !scrolled) {
val anchorMoved = abs(event.getX(ai) - anchorStartX) + abs(event.getY(ai) - anchorStartY)
val moverMoved = abs(mx - moverStartX) + abs(my - moverStartY)
if (moverMoved > slopPx && anchorMoved < slopPx) {
holdDrag = true
secondWasTap = false
if (!dragging) {
dragging = true
listener.onButton(PointerGestures.BTN_LEFT, true, x, y)
}
}
}
if (holdDrag) {
val dx = mx - moverLastX
val dy = my - moverLastY
moverLastX = mx; moverLastY = my
val speed = abs(dx) + abs(dy)
val gain = sensitivity * (1f + minOf(speed / 40f, 1.5f))
place(x + dx * gain, y + dy * gain)
listener.onMove(x, y)
return true
}
} else if (holdDrag) {
// Only the held finger is down: the drag waits for the next slide.
return true
}
if (twoFingers && event.pointerCount >= 2) {
val mid = (event.getY(0) + event.getY(1)) / 2f
val dy = mid - scrollAnchorY
@@ -109,6 +167,30 @@ class TouchpadGestures(
return true
}
MotionEvent.ACTION_POINTER_UP -> {
val gone = event.getPointerId(event.actionIndex)
if (holdDrag) {
if (gone == anchorId) {
// The held finger lifted: drop. The other finger carries on as the pointer.
listener.onButton(PointerGestures.BTN_LEFT, false, x, y)
dragging = false; holdDrag = false; twoFingers = false
anchorId = moverId; moverId = -1
event.findPointerIndex(anchorId).takeIf { it >= 0 }?.let {
lastX = event.getX(it); lastY = event.getY(it)
}
travelled = slopPx + 1f
} else if (gone == moverId) {
moverId = -1
}
return true
}
if (gone == moverId) moverId = -1
if (gone == anchorId) {
// The first finger left a two-finger gesture: the other one is the pointer now.
anchorId = event.getPointerId(if (event.actionIndex == 0) 1 else 0)
event.findPointerIndex(anchorId).takeIf { it >= 0 }?.let {
lastX = event.getX(it); lastY = event.getY(it)
}
}
// The second finger lifting quickly, with nothing scrolled: a right click.
if (twoFingers && secondWasTap && event.eventTime - secondDownAt < TAP_MS && !scrolled) {
listener.onButton(PointerGestures.BTN_RIGHT, true, x, y)
@@ -136,6 +218,7 @@ class TouchpadGestures(
}
}
dragging = false; held = false; twoFingers = false
holdDrag = false; anchorId = -1; moverId = -1
return true
}
}