diff --git a/app/src/main/java/cc/tiredsysadmin/android/sizzle/ui/tracker/ArrangementRoll.kt b/app/src/main/java/cc/tiredsysadmin/android/sizzle/ui/tracker/ArrangementRoll.kt index d5d4692..a9e9542 100644 --- a/app/src/main/java/cc/tiredsysadmin/android/sizzle/ui/tracker/ArrangementRoll.kt +++ b/app/src/main/java/cc/tiredsysadmin/android/sizzle/ui/tracker/ArrangementRoll.kt @@ -7,7 +7,6 @@ import androidx.compose.foundation.Canvas import androidx.compose.foundation.background import androidx.compose.foundation.clickable import androidx.compose.foundation.gestures.detectDragGesturesAfterLongPress -import androidx.compose.foundation.gestures.detectTapGestures import androidx.compose.foundation.horizontalScroll import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box @@ -217,11 +216,12 @@ fun ArrangementRoll(vm: AppViewModel) { Modifier .width(beatWidth * lengthBeats) .fillMaxHeight() - // Tap selects a single cell (no toggle). The ruler is - // display-only, so ruler taps are ignored. - .pointerInput(arr, sig) { - detectTapGestures { off -> - if (off.y < rulerHeightPx) return@detectTapGestures + // Tap selects a single cell (no toggle); a flick scrolls + // the roll and never selects. The ruler is display-only, + // so ruler taps are ignored. + .pointerInput(arr, sig, scroll) { + detectScrollAwareTaps(scroll) { off -> + if (off.y < rulerHeightPx) return@detectScrollAwareTaps val laneH = (size.height - rulerHeightPx) / ArrModel.LANE_COUNT val lane = ((off.y - rulerHeightPx) / laneH).toInt() .coerceIn(0, ArrModel.LANE_COUNT - 1) diff --git a/app/src/main/java/cc/tiredsysadmin/android/sizzle/ui/tracker/PatternGrid.kt b/app/src/main/java/cc/tiredsysadmin/android/sizzle/ui/tracker/PatternGrid.kt index c8865fb..b03755f 100644 --- a/app/src/main/java/cc/tiredsysadmin/android/sizzle/ui/tracker/PatternGrid.kt +++ b/app/src/main/java/cc/tiredsysadmin/android/sizzle/ui/tracker/PatternGrid.kt @@ -7,14 +7,15 @@ import androidx.compose.foundation.Canvas import androidx.compose.foundation.background import androidx.compose.foundation.gestures.Orientation import androidx.compose.foundation.gestures.ScrollableState -import androidx.compose.foundation.gestures.detectTapGestures import androidx.compose.foundation.gestures.scrollable import androidx.compose.foundation.layout.BoxWithConstraints import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.runtime.snapshotFlow import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clipToBounds 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; // a finger flick detaches it into free inertial scrolling, and switching to - // another block re-attaches (both states are keyed on `pattern`). - // - // 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. + // another block re-attaches (both states are keyed on `pattern`). While NOT + // playing `scrollPx` is simply where the finger left the view. val following = remember(pattern) { mutableStateOf(true) } val scrollPx = remember(pattern) { mutableStateOf(0f) } @@ -108,13 +104,28 @@ fun PatternGrid(vm: AppViewModel, modifier: Modifier = Modifier) { fun effScrollPx(): Float { val t = transportState.value if (t.isPlaying && following.value) return playheadScrollPx(t.currentLine) - // Minimal scroll-into-view around the cursor row: `lo` is the offset that - // 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 - val lo = (top + rowPx - heightPx).coerceIn(0f, maxScrollPx) - val hi = top.coerceIn(lo, maxScrollPx) - return scrollPx.value.coerceIn(lo, hi) + return scrollPx.value.coerceIn(0f, maxScrollPx) + } + + // Scroll the cursor back into view when it MOVES off screen (arrow keys, + // punch-in advancing a row). Deliberately minimal and deliberately reactive: + // + // - 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 @@ -153,11 +164,11 @@ fun PatternGrid(vm: AppViewModel, modifier: Modifier = Modifier) { .clipToBounds() // Inertial vertical scrolling (fling), detaching the playhead follow. .scrollable(orientation = Orientation.Vertical, state = scrollState) - .pointerInput(pattern, widthPx, heightPx) { - // Tap moves the cursor to the cell/column. Note entry is done with - // the punch-in keyboard or external controllers — there is no - // long-press cell editor. - detectTapGestures { off -> + .pointerInput(pattern, widthPx, heightPx, scrollState) { + // Tap moves the cursor to the cell/column; a flick scrolls and + // never selects. Note entry is done with the punch-in keyboard or + // external controllers — there is no long-press cell editor. + detectScrollAwareTaps(scrollState) { off -> hitTest(off.x, off.y)?.let { (t, col, line) -> vm.focusCell(t, line, col) } } }, diff --git a/app/src/main/java/cc/tiredsysadmin/android/sizzle/ui/tracker/ScrollAwareTap.kt b/app/src/main/java/cc/tiredsysadmin/android/sizzle/ui/tracker/ScrollAwareTap.kt new file mode 100644 index 0000000..6215538 --- /dev/null +++ b/app/src/main/java/cc/tiredsysadmin/android/sizzle/ui/tracker/ScrollAwareTap.kt @@ -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 + } + } +} diff --git a/versions.properties b/versions.properties index 34c38f9..c0f6b04 100644 --- a/versions.properties +++ b/versions.properties @@ -8,8 +8,8 @@ # 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. -android.versionName = 0.22.1 -android.versionCode = 50 +android.versionName = 0.23.0 +android.versionCode = 51 # 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