Separate the flick from the tap on the tracker and arrangement canvases (v0.23.0)

Both canvases detected their tap with detectTapGestures, which is the wrong
tool next to a scroll container for two reasons:

  - it CONSUMES the initial down, so the scrollable/horizontalScroll wrapped
    around the canvas had to fight for the gesture. A quick flick frequently
    did nothing, or landed as a tap and moved the cursor; only a slow,
    deliberate drag scrolled reliably.
  - it knows nothing about an in-flight fling, so tapping to halt a flick — the
    reflex every scrolling surface trains — selected a cell under a view that
    was still moving.

Replaced with detectScrollAwareTaps, shared by both canvases: it leaves the
down unconsumed so the scroll container sees the whole gesture, drops the tap
as soon as the finger travels past touch slop or the container claims a drag,
and treats a touch landing on a moving view as "stop here" rather than
"select".

Also fixes a regression from v0.22.1: keeping the cursor row on screen was
written as a standing clamp inside the scroll accessor, which made it a
constraint on the viewport rather than a reaction to the cursor moving. With
the cursor on line 0 it pinned the grid to the top and no amount of dragging
could scroll it. It is now a snapshotFlow on the cursor line that nudges the
offset only when the cursor actually moves off screen, so free scrolling is
unconstrained and a tapped cell still never moves.

Verified on an emulator (Pixel, API 34): a fast flick on the pattern grid now
flings across the whole 64-line pattern where it previously did not move at
all; a slow drag tracks 1:1 with no overshoot; a tap lands on the exact cell
and leaves the view still; the arrangement roll flings from bar 1 to bar 31 and
taps the right lane/bar. The tap-stops-a-fling path is not covered — adb cannot
inject a tap inside the fling window.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Reactorcoremeltdown
2026-11-03 20:06:14 +01:00
co-authored by Claude Opus 5
parent 1815566b6c
commit 208de84012
4 changed files with 107 additions and 28 deletions
@@ -7,7 +7,6 @@ import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.clickable import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.detectDragGesturesAfterLongPress import androidx.compose.foundation.gestures.detectDragGesturesAfterLongPress
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.horizontalScroll import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
@@ -217,11 +216,12 @@ fun ArrangementRoll(vm: AppViewModel) {
Modifier Modifier
.width(beatWidth * lengthBeats) .width(beatWidth * lengthBeats)
.fillMaxHeight() .fillMaxHeight()
// Tap selects a single cell (no toggle). The ruler is // Tap selects a single cell (no toggle); a flick scrolls
// display-only, so ruler taps are ignored. // the roll and never selects. The ruler is display-only,
.pointerInput(arr, sig) { // so ruler taps are ignored.
detectTapGestures { off -> .pointerInput(arr, sig, scroll) {
if (off.y < rulerHeightPx) return@detectTapGestures detectScrollAwareTaps(scroll) { off ->
if (off.y < rulerHeightPx) return@detectScrollAwareTaps
val laneH = (size.height - rulerHeightPx) / ArrModel.LANE_COUNT val laneH = (size.height - rulerHeightPx) / ArrModel.LANE_COUNT
val lane = ((off.y - rulerHeightPx) / laneH).toInt() val lane = ((off.y - rulerHeightPx) / laneH).toInt()
.coerceIn(0, ArrModel.LANE_COUNT - 1) .coerceIn(0, ArrModel.LANE_COUNT - 1)
@@ -7,14 +7,15 @@ import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.Orientation import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.ScrollableState import androidx.compose.foundation.gestures.ScrollableState
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.gestures.scrollable import androidx.compose.foundation.gestures.scrollable
import androidx.compose.foundation.layout.BoxWithConstraints import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clipToBounds import androidx.compose.ui.draw.clipToBounds
import androidx.compose.ui.geometry.Offset import androidx.compose.ui.geometry.Offset
@@ -89,13 +90,8 @@ fun PatternGrid(vm: AppViewModel, modifier: Modifier = Modifier) {
// Vertical scroll model. While PLAYING the view auto-centres on the playhead; // Vertical scroll model. While PLAYING the view auto-centres on the playhead;
// a finger flick detaches it into free inertial scrolling, and switching to // a finger flick detaches it into free inertial scrolling, and switching to
// another block re-attaches (both states are keyed on `pattern`). // another block re-attaches (both states are keyed on `pattern`). While NOT
// // playing `scrollPx` is simply where the finger left the view.
// While NOT playing the view holds still and only ever scrolls far enough to
// keep the cursor row on screen. It deliberately does NOT re-centre on the
// cursor: the cursor moves because the user just tapped a cell, and centring
// would yank that cell out from under their finger — on a pattern long enough
// to overflow, the tapped row visibly jumps to the middle of the viewport.
val following = remember(pattern) { mutableStateOf(true) } val following = remember(pattern) { mutableStateOf(true) }
val scrollPx = remember(pattern) { mutableStateOf(0f) } val scrollPx = remember(pattern) { mutableStateOf(0f) }
@@ -108,13 +104,28 @@ fun PatternGrid(vm: AppViewModel, modifier: Modifier = Modifier) {
fun effScrollPx(): Float { fun effScrollPx(): Float {
val t = transportState.value val t = transportState.value
if (t.isPlaying && following.value) return playheadScrollPx(t.currentLine) if (t.isPlaying && following.value) return playheadScrollPx(t.currentLine)
// Minimal scroll-into-view around the cursor row: `lo` is the offset that return scrollPx.value.coerceIn(0f, maxScrollPx)
// puts the cursor on the last visible line, `hi` the one that puts it on }
// the first. Anything between leaves the view exactly where it is.
val top = vm.cursorLine * rowPx // Scroll the cursor back into view when it MOVES off screen (arrow keys,
val lo = (top + rowPx - heightPx).coerceIn(0f, maxScrollPx) // punch-in advancing a row). Deliberately minimal and deliberately reactive:
val hi = top.coerceIn(lo, maxScrollPx) //
return scrollPx.value.coerceIn(lo, hi) // - minimal, because re-centring would yank a cell out from under the finger
// that just tapped it — on a pattern long enough to overflow, the tapped
// row visibly jumped to the middle of the viewport;
// - reactive rather than a clamp inside effScrollPx(), because a clamp is a
// standing constraint on the viewport. With the cursor on line 0 it pinned
// the view to the top and the grid could not be scrolled at all.
//
// snapshotFlow keeps this out of composition: a cursor move invalidates the
// draw phase alone, as a playhead tick does.
LaunchedEffect(pattern, rowPx, heightPx, maxScrollPx) {
snapshotFlow { vm.cursorLine }.collect { line ->
val top = line * rowPx
val lo = (top + rowPx - heightPx).coerceIn(0f, maxScrollPx)
val hi = top.coerceIn(lo, maxScrollPx)
scrollPx.value = effScrollPx().coerceIn(lo, hi)
}
} }
// Fling/drag scrolling. The first delta seeds the free position from wherever // Fling/drag scrolling. The first delta seeds the free position from wherever
@@ -153,11 +164,11 @@ fun PatternGrid(vm: AppViewModel, modifier: Modifier = Modifier) {
.clipToBounds() .clipToBounds()
// Inertial vertical scrolling (fling), detaching the playhead follow. // Inertial vertical scrolling (fling), detaching the playhead follow.
.scrollable(orientation = Orientation.Vertical, state = scrollState) .scrollable(orientation = Orientation.Vertical, state = scrollState)
.pointerInput(pattern, widthPx, heightPx) { .pointerInput(pattern, widthPx, heightPx, scrollState) {
// Tap moves the cursor to the cell/column. Note entry is done with // Tap moves the cursor to the cell/column; a flick scrolls and
// the punch-in keyboard or external controllers — there is no // never selects. Note entry is done with the punch-in keyboard or
// long-press cell editor. // external controllers — there is no long-press cell editor.
detectTapGestures { off -> detectScrollAwareTaps(scrollState) { off ->
hitTest(off.x, off.y)?.let { (t, col, line) -> vm.focusCell(t, line, col) } hitTest(off.x, off.y)?.let { (t, col, line) -> vm.focusCell(t, line, col) }
} }
}, },
@@ -0,0 +1,68 @@
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
package cc.tiredsysadmin.android.sizzle.ui.tracker
import androidx.compose.foundation.gestures.ScrollableState
import androidx.compose.foundation.gestures.awaitEachGesture
import androidx.compose.foundation.gestures.awaitFirstDown
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.input.pointer.PointerInputScope
import androidx.compose.ui.input.pointer.changedToUp
import androidx.compose.ui.input.pointer.positionChange
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
/**
* Tap-to-select for a canvas that also scrolls, keeping the tap strictly separate
* from the flick that scrolls it.
*
* `detectTapGestures` is the obvious tool and the wrong one here, for two reasons:
*
* - it **consumes the initial down**, which starves the scroll container wrapped
* around the canvas of the gesture. A quick flick then had to fight for the
* pointer, so it often did nothing at all, or landed as a tap and moved the
* cursor instead of scrolling. Only a slow, deliberate drag scrolled reliably.
* - it knows nothing about an **in-flight fling**, so tapping to halt a flick —
* the reflex every scrolling surface trains — selected a cell under a view that
* was still moving, which is never what was aimed at.
*
* This detector leaves the down unconsumed so the scroll container still sees the
* whole gesture; abandons the tap as soon as the finger travels past touch slop or
* the container consumes a drag; and treats a touch that lands on a moving view as
* "stop here" rather than "select".
*
* @param scrollState the scroll container this canvas lives in, consulted for an
* in-flight fling and preempted to stop one.
* @param onTap called with the canvas-local position of a tap that was not a scroll.
*/
suspend fun PointerInputScope.detectScrollAwareTaps(
scrollState: ScrollableState,
onTap: (Offset) -> Unit,
) = coroutineScope {
awaitEachGesture {
val down = awaitFirstDown(requireUnconsumed = false)
// A touch that lands while the view is still flinging means "stop", not
// "select". An empty scroll block preempts the fling's mutation and ends
// immediately, leaving the state free for the drag that may follow.
val haltsFling = scrollState.isScrollInProgress
if (haltsFling) launch { scrollState.scroll { } }
// Total travel, not net displacement: a wobbly hold that wanders past slop
// is a scroll attempt (or a shaky finger), not a cell the user aimed at.
var travel = 0f
var isTap = true
while (true) {
val event = awaitPointerEvent()
val change = event.changes.firstOrNull { it.id == down.id } ?: break
travel += change.positionChange().getDistance()
// `isConsumed` == the scroll container has claimed this as a drag.
if (travel > viewConfiguration.touchSlop || change.isConsumed) isTap = false
if (change.changedToUp()) {
if (isTap && !haltsFling) onTap(change.position)
break
}
if (!change.pressed) break
}
}
}
+2 -2
View File
@@ -8,8 +8,8 @@
# or polish. android.versionCode is monotonic and bumps by 1 on every release, # or polish. android.versionCode is monotonic and bumps by 1 on every release,
# because F-Droid and the Android package manager order installs by it alone. # because F-Droid and the Android package manager order installs by it alone.
android.versionName = 0.22.1 android.versionName = 0.23.0
android.versionCode = 50 android.versionCode = 51
# No desktop builds exist yet. These lines are the agreed slots so a port starts # No desktop builds exist yet. These lines are the agreed slots so a port starts
# with a version instead of inventing one; `-` means "not built on this # with a version instead of inventing one; `-` means "not built on this