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 a9e9542..00a1ebf 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,6 +7,7 @@ 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.scrollBy import androidx.compose.foundation.horizontalScroll import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box @@ -27,6 +28,7 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue +import androidx.compose.runtime.withFrameNanos import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.geometry.Offset @@ -41,6 +43,10 @@ import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.rememberTextMeasurer import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp +import kotlinx.coroutines.Job +import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.isActive +import kotlinx.coroutines.launch import cc.tiredsysadmin.android.sizzle.model.Arrangement as ArrModel import cc.tiredsysadmin.android.sizzle.model.LoopRegion import cc.tiredsysadmin.android.sizzle.ui.AppViewModel @@ -230,26 +236,96 @@ fun ArrangementRoll(vm: AppViewModel) { } } // Long-press then drag sweeps out a rectangular selection. - .pointerInput(arr, sig) { + // Dragging into either edge of the viewport scrolls the + // roll under the finger, so a selection can run far past + // what fits on screen. + .pointerInput(arr, sig, scroll) { + val edgeZonePx = EDGE_SCROLL_ZONE.toPx() + val maxSpeedPx = EDGE_SCROLL_MAX_SPEED.toPx() + fun laneAt(off: Offset): Int { val laneH = (size.height - rulerHeightPx) / ArrModel.LANE_COUNT return ((off.y - rulerHeightPx) / laneH).toInt() .coerceIn(0, ArrModel.LANE_COUNT - 1) } - fun beatAt(off: Offset): Int = - (off.x / beatWidthPx).toInt().coerceIn(0, lengthBeats - 1) + // Beat under a VIEWPORT-relative x. The canvas is wider + // than the viewport and slides under the finger while + // edge-scrolling, so a pointer's canvas-local x goes + // stale the instant the view moves — and no new pointer + // event arrives while the finger is held still at the + // rim. scroll.value is the live half of the position. + fun beatAtViewportX(vx: Float): Int = + ((scroll.value + vx) / beatWidthPx).toInt() + .coerceIn(0, lengthBeats - 1) + var dragLane0 = 0; var dragBeat0 = 0 - detectDragGesturesAfterLongPress( - onDragStart = { off -> - dragLane0 = laneAt(off); dragBeat0 = beatAt(off) - vm.setArrangementSelection(dragLane0, dragBeat0, dragLane0, dragBeat0) - }, - onDrag = { change, _ -> - vm.setArrangementSelection( - dragLane0, dragBeat0, laneAt(change.position), beatAt(change.position), - ) - }, - ) + var fingerVx = 0f // viewport-relative x of the finger + var fingerLane = 0 + var lastLane = -1; var lastBeat = -1 + + fun applySelection() { + val beat = beatAtViewportX(fingerVx) + // An edge scroll ticks every frame but only crosses + // a beat boundary now and then; skipping the rest + // keeps it from bumping the revision 60x a second. + if (fingerLane == lastLane && beat == lastBeat) return + lastLane = fingerLane; lastBeat = beat + vm.setArrangementSelection(dragLane0, dragBeat0, fingerLane, beat) + } + + coroutineScope { + var edgeJob: Job? = null + + fun stopEdgeScroll() { edgeJob?.cancel(); edgeJob = null } + + fun startEdgeScroll() { + if (edgeJob?.isActive == true) return + edgeJob = launch { + var last = 0L + while (isActive) { + val now = withFrameNanos { it } + val dt = if (last == 0L) 0f + else (now - last) / 1_000_000_000f + last = now + val w = viewportWidthPx + if (w <= 0 || dt <= 0f) continue + // Speed ramps with how deep into the + // edge zone the finger is: easing in + // creeps, pushing to the rim races. + val push = when { + fingerVx > w - edgeZonePx -> + (fingerVx - (w - edgeZonePx)) / edgeZonePx + fingerVx < edgeZonePx -> + -(edgeZonePx - fingerVx) / edgeZonePx + else -> 0f + }.coerceIn(-1f, 1f) + if (push == 0f) continue + val before = scroll.value + scroll.scrollBy(push * maxSpeedPx * dt) + if (scroll.value != before) applySelection() + } + } + } + + detectDragGesturesAfterLongPress( + onDragStart = { off -> + lastLane = -1; lastBeat = -1 + fingerVx = off.x - scroll.value + fingerLane = laneAt(off) + dragLane0 = fingerLane + dragBeat0 = beatAtViewportX(fingerVx) + applySelection() + startEdgeScroll() + }, + onDrag = { change, _ -> + fingerVx = change.position.x - scroll.value + fingerLane = laneAt(change.position) + applySelection() + }, + onDragEnd = { stopEdgeScroll() }, + onDragCancel = { stopEdgeScroll() }, + ) + } }, ) { // Subscribe the draw phase to edit revisions (live state read, @@ -409,3 +485,17 @@ private fun RegionControl(name: String, region: LoopRegion, onTap: () -> Unit, v RepeatButton("+") { vm.changeRegionRepeats(region, +1) } } } + +/** + * How close to the viewport edge a selection drag has to get before the roll starts + * scrolling under it. A thumb's worth — small enough that an ordinary selection + * inside the visible range never trips it, wide enough to aim at. + */ +private val EDGE_SCROLL_ZONE = 56.dp + +/** + * Edge-scroll speed at the very rim, per second. The roll is 20.dp per beat, so this + * is roughly 15 bars a second in 4/4 at full push — a 16-bar selection sweeps out in + * about a second, and the ramp below full push makes finer work practical. + */ +private val EDGE_SCROLL_MAX_SPEED = 1200.dp diff --git a/versions.properties b/versions.properties index 5bcd712..a9003de 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.25.0 -android.versionCode = 54 +android.versionName = 0.26.0 +android.versionCode = 55 # 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