Keep the tapped cell under the finger on a scrolled pattern grid (v0.22.1)

On a pattern long enough to overflow the viewport, tapping a cell moved the
grid: the follow logic re-centred the viewport on the cursor the moment it
changed, so the row that was just tapped slid up to the middle of the screen,
well away from the fingertip. The hit test itself was correct — the view moved
out from under it.

While not playing the view now holds still and scrolls only far enough to keep
the cursor row on screen, so a tap never moves it and an off-screen cursor
(arrow keys, punch-in advancing a row) still comes back into view. Centring on
the playhead during playback is unchanged, as is the flick-to-detach.

Also clip the Canvas. Compose does not clip a Canvas to its bounds, and the
grid deliberately draws one row past each edge to fill the partially scrolled
rows, so a scrolled grid painted over the toolbar above it.

Verified on an emulator (Pixel, API 34): before the fix a tap near the bottom
of a 64-line pattern jumped the row to the middle; after it the row stays under
the tap, scroll-then-tap lands on the right cell, an off-screen cursor is
pulled back to the nearest edge, and the playhead still centres during
playback.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Reactorcoremeltdown
2026-11-03 17:09:39 +01:00
co-authored by Claude Opus 5
parent fdee7853e5
commit 1815566b6c
2 changed files with 35 additions and 17 deletions
@@ -16,6 +16,7 @@ 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.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clipToBounds
import androidx.compose.ui.geometry.Offset import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
@@ -86,29 +87,41 @@ fun PatternGrid(vm: AppViewModel, modifier: Modifier = Modifier) {
val visibleRows = (heightPx / rowPx).toInt().coerceAtLeast(1) val visibleRows = (heightPx / rowPx).toInt().coerceAtLeast(1)
val maxScrollPx = (pattern.length * rowPx - heightPx).coerceAtLeast(0f) val maxScrollPx = (pattern.length * rowPx - heightPx).coerceAtLeast(0f)
// Vertical scroll model. By default the view FOLLOWS — it auto-centres on the // Vertical scroll model. While PLAYING the view auto-centres on the playhead;
// playhead while playing, else on the cursor. A finger flick detaches it into // a finger flick detaches it into free inertial scrolling, and switching to
// free inertial scrolling; switching to another block re-attaches. Both states // another block re-attaches (both states are keyed on `pattern`).
// are keyed on `pattern`, so selecting a different block re-inits them (== the //
// "resume following on block switch" behaviour). // 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) }
// Pixel scroll offset the view auto-follows (top of viewport, 0 == line 0). // Pixel offset that centres the playhead (top of viewport, 0 == line 0).
fun autoScrollPx(): Float { fun playheadScrollPx(line: Int): Float =
(line * rowPx + rowPx / 2f - heightPx / 2f).coerceIn(0f, maxScrollPx)
// The offset actually shown. Pure: it only reads state, never writes it, so
// it is safe to call from the draw phase and from the gesture handlers.
fun effScrollPx(): Float {
val t = transportState.value val t = transportState.value
val focusLine = if (t.isPlaying) t.currentLine else vm.cursorLine if (t.isPlaying && following.value) return playheadScrollPx(t.currentLine)
return (focusLine * rowPx + rowPx / 2f - heightPx / 2f).coerceIn(0f, maxScrollPx) // 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)
} }
// The offset actually shown: the followed one, or the free one once detached.
fun effScrollPx(): Float =
if (following.value) autoScrollPx() else scrollPx.value.coerceIn(0f, maxScrollPx)
// Fling/drag scrolling. The first delta seeds the free position from wherever // Fling/drag scrolling. The first delta seeds the free position from wherever
// the followed view currently sits, then detaches following. // the view currently sits, then detaches the playhead follow.
val scrollState = remember(pattern, rowPx, heightPx, maxScrollPx) { val scrollState = remember(pattern, rowPx, heightPx, maxScrollPx) {
ScrollableState { delta -> ScrollableState { delta ->
val old = if (following.value) autoScrollPx() else scrollPx.value val old = effScrollPx()
following.value = false following.value = false
val next = (old - delta).coerceIn(0f, maxScrollPx) val next = (old - delta).coerceIn(0f, maxScrollPx)
scrollPx.value = next scrollPx.value = next
@@ -133,7 +146,12 @@ fun PatternGrid(vm: AppViewModel, modifier: Modifier = Modifier) {
Canvas( Canvas(
Modifier Modifier
.fillMaxSize() .fillMaxSize()
// Inertial vertical scrolling (fling), detaching the auto-follow. // Rows at the top and bottom of a scrolled viewport are drawn
// partially outside it; Compose does not clip a Canvas by default, so
// without this they paint over the toolbar above and the arrangement
// divider below.
.clipToBounds()
// 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) {
// Tap moves the cursor to the cell/column. Note entry is done with // Tap moves the cursor to the cell/column. Note entry is done with
+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.0 android.versionName = 0.22.1
android.versionCode = 49 android.versionCode = 50
# 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