Edge-scroll the arrangement roll while dragging a selection (v0.26.0)
A long-press selection drag could only reach what was already on screen — about four bars — so selecting a long span meant selecting, scrolling, and selecting again. Dragging into either edge of the roll's viewport now scrolls it under the finger, and the selection keeps extending while it moves. Speed ramps with how deep into the 56.dp edge zone the finger is, so easing in creeps for fine work and pushing to the rim covers roughly 15 bars a second. Both edges, not just the right one: scrolling out is not much use without scrolling back, and a drag that overshoots has to be able to return. The subtlety is that a pointer's canvas-local x goes stale the moment the view moves beneath it, and no new pointer event arrives at all while the finger is held still at the rim — which is exactly the case that has to keep working. The drag therefore tracks a VIEWPORT-relative x and recomputes the beat against the live scroll.value each frame, rather than trusting the last reported position. The per-frame loop only runs between onDragStart and onDragEnd/onDragCancel, and it applies a selection update only when the beat under the finger actually changes, so an edge scroll does not bump the revision sixty times a second. Verified on an emulator with synthesised motionevents (adb input swipe cannot hold still long enough to trigger a long press): a right-edge drag carried the view from bar 1 to bar 34 with the selection following; a left-edge drag carried it back to bar 12; the scroll stops dead on finger lift, with no drift four seconds later; and an ordinary drag inside the viewport still selects a single bar without scrolling at all. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
c2aedcc95c
commit
68e9ed8b41
@@ -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
|
||||
|
||||
+2
-2
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user