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 bbfcfd7..c8865fb 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 @@ -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 diff --git a/versions.properties b/versions.properties index 6161dc4..34c38f9 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.0 -android.versionCode = 49 +android.versionName = 0.22.1 +android.versionCode = 50 # 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