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:
co-authored by
Claude Opus 5
parent
fdee7853e5
commit
1815566b6c
@@ -16,6 +16,7 @@ import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clipToBounds
|
||||
import androidx.compose.ui.geometry.Offset
|
||||
import androidx.compose.ui.geometry.Size
|
||||
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 maxScrollPx = (pattern.length * rowPx - heightPx).coerceAtLeast(0f)
|
||||
|
||||
// Vertical scroll model. By default the view FOLLOWS — it auto-centres on the
|
||||
// playhead while playing, else on the cursor. A finger flick detaches it into
|
||||
// free inertial scrolling; switching to another block re-attaches. Both states
|
||||
// are keyed on `pattern`, so selecting a different block re-inits them (== the
|
||||
// "resume following on block switch" behaviour).
|
||||
// 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.
|
||||
val following = remember(pattern) { mutableStateOf(true) }
|
||||
val scrollPx = remember(pattern) { mutableStateOf(0f) }
|
||||
|
||||
// Pixel scroll offset the view auto-follows (top of viewport, 0 == line 0).
|
||||
fun autoScrollPx(): Float {
|
||||
// Pixel offset that centres the playhead (top of viewport, 0 == line 0).
|
||||
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 focusLine = if (t.isPlaying) t.currentLine else vm.cursorLine
|
||||
return (focusLine * rowPx + rowPx / 2f - heightPx / 2f).coerceIn(0f, maxScrollPx)
|
||||
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)
|
||||
}
|
||||
// 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
|
||||
// the followed view currently sits, then detaches following.
|
||||
// the view currently sits, then detaches the playhead follow.
|
||||
val scrollState = remember(pattern, rowPx, heightPx, maxScrollPx) {
|
||||
ScrollableState { delta ->
|
||||
val old = if (following.value) autoScrollPx() else scrollPx.value
|
||||
val old = effScrollPx()
|
||||
following.value = false
|
||||
val next = (old - delta).coerceIn(0f, maxScrollPx)
|
||||
scrollPx.value = next
|
||||
@@ -133,7 +146,12 @@ fun PatternGrid(vm: AppViewModel, modifier: Modifier = Modifier) {
|
||||
Canvas(
|
||||
Modifier
|
||||
.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)
|
||||
.pointerInput(pattern, widthPx, heightPx) {
|
||||
// Tap moves the cursor to the cell/column. Note entry is done with
|
||||
|
||||
Reference in New Issue
Block a user