playback: document wavyslider

To the best of my ability.

It's already largely cleaned up and I understand the vast majority of
it's internals, so I consider this good to ship. The only issues are
the drawing code which is still unfortunately quite mysterious
This commit is contained in:
Alexander Capehart
2026-05-24 21:33:54 -06:00
parent 9c048a001d
commit 3b47395f7f
@@ -70,8 +70,7 @@ constructor(
private var cachedWavelength = -1 private var cachedWavelength = -1
private var cachedTrackLength = -1f private var cachedTrackLength = -1f
private val drawRect = private val drawRect = RectF()
RectF(-trackHeight / 2f, -trackHeight / 2f, trackHeight / 2f, trackHeight / 2f)
private val patchRect = RectF() private val patchRect = RectF()
private val clipRect = RectF() private val clipRect = RectF()
private val roundedRectPath = Path() private val roundedRectPath = Path()
@@ -80,6 +79,7 @@ constructor(
private val endPoint = PointF() private val endPoint = PointF()
private val transparentTrackTint = ColorStateList.valueOf(Color.TRANSPARENT) private val transparentTrackTint = ColorStateList.valueOf(Color.TRANSPARENT)
// Necessary due to us clobbering it when we enable the wavy track
private var tmpTrackTintList: ColorStateList = trackActiveTintList private var tmpTrackTintList: ColorStateList = trackActiveTintList
private var linearActiveTrackSuppressed = false private var linearActiveTrackSuppressed = false
@@ -148,7 +148,7 @@ constructor(
wavePaint = wavePaint =
Paint(Paint.ANTI_ALIAS_FLAG).apply { Paint(Paint.ANTI_ALIAS_FLAG).apply {
style = Paint.Style.STROKE // style = is set later
strokeCap = Paint.Cap.BUTT strokeCap = Paint.Cap.BUTT
strokeWidth = trackHeight.toFloat() strokeWidth = trackHeight.toFloat()
isAntiAlias = true isAntiAlias = true
@@ -160,6 +160,8 @@ constructor(
} }
updateActiveTrackSuppression() updateActiveTrackSuppression()
// Easier logic this way (even if a bit non-standard)
// The funky tangent stuff was just not my thing
if (trackCornerSize < trackHeight) { if (trackCornerSize < trackHeight) {
Timber.w( Timber.w(
"WavySlider can only be fully rounded. Clamping corner size down to trackHeight / 2" "WavySlider can only be fully rounded. Clamping corner size down to trackHeight / 2"
@@ -218,18 +220,24 @@ constructor(
waveEnabled = enabled waveEnabled = enabled
if (enabled) { if (enabled) {
// Enabled, start moving / transition to 1f amplitude
waveSpeed = configuredSpeedPx waveSpeed = configuredSpeedPx
resetPhaseClock() resetPhaseClock()
transitionToAmplitudeFraction(1f) transitionToAmplitudeFraction(1f)
} else if (abs(currentAmplitudeFraction - MIN_VISIBLE_WAVE_FRACTION) < EPSILON) { } else if (abs(currentAmplitudeFraction - MIN_VISIBLE_WAVE_FRACTION) < EPSILON) {
// No amplitude configured, do nothing
waveSpeed = 0 waveSpeed = 0
resetPhaseClock() resetPhaseClock()
} else { } else {
// Off, transition to 0 amplitude and stop
waveSpeed = 0 waveSpeed = 0
transitionToAmplitudeFraction(MIN_VISIBLE_WAVE_FRACTION) { resetPhaseClock() } transitionToAmplitudeFraction(MIN_VISIBLE_WAVE_FRACTION) { resetPhaseClock() }
} }
// Get rid of inactive track if needed
updateActiveTrackSuppression() updateActiveTrackSuppression()
// Start doing the actual moving wave animation
ensurePhaseTickerState() ensurePhaseTickerState()
// Start drawing
markDirty() markDirty()
invalidate() invalidate()
} }
@@ -345,8 +353,8 @@ constructor(
phaseFraction: Float, phaseFraction: Float,
) { ) {
// This logic seems more complex than it should be largely because // This logic seems more complex than it should be largely because
// we have to gracefully collapse the active track as we scroll to // we have to gracefully collapse the active track as we seek
// the minimum // to the end.
val originX = trackSidePadding.toFloat() val originX = trackSidePadding.toFloat()
val trackCenterY = height / 2f val trackCenterY = height / 2f
@@ -359,6 +367,7 @@ constructor(
endPoint.translate(endBlockCenterX + originX, trackCenterY) endPoint.translate(endBlockCenterX + originX, trackCenterY)
if (startBound <= 0f && endBlockCenterX < startBlockCenterX) { if (startBound <= 0f && endBlockCenterX < startBlockCenterX) {
// Too small for caps, need to draw a rounded block instead
drawRoundedBlock( drawRoundedBlock(
canvas = canvas, canvas = canvas,
drawCenter = startPoint, drawCenter = startPoint,
@@ -369,6 +378,9 @@ constructor(
} }
if (startBlockCenterX > endBlockCenterX) { if (startBlockCenterX > endBlockCenterX) {
// Too small for caps, need to draw a rounded block instead
// In this case since start > end we actually need to draw it in
// reverse or it'll break
drawRoundedBlock( drawRoundedBlock(
canvas = canvas, canvas = canvas,
drawCenter = endPoint, drawCenter = endPoint,
@@ -378,50 +390,48 @@ constructor(
return return
} }
// Otherwise draw the wavy stroke
// Need to configure the paint mode for this
wavePaint.style = Paint.Style.STROKE wavePaint.style = Paint.Style.STROKE
// Always fully rounded (easier, albeit not spec compliant)
wavePaint.strokeCap = Paint.Cap.ROUND wavePaint.strokeCap = Paint.Cap.ROUND
if (amplitudeFraction > 0f) { if (amplitudeFraction > 0f) {
// Calculate what we should actually display
// This includes the phase-scroll logic
calculateDisplayedWavePath( calculateDisplayedWavePath(
trackLength = trackLength, trackLength = trackLength,
start = startBlockCenterX / trackLength, start = startBlockCenterX / trackLength,
end = endBlockCenterX / trackLength, end = endBlockCenterX / trackLength,
phaseFraction = phaseFraction, phaseFraction = phaseFraction,
trackCenterY = trackCenterY, baseTranslationX = trackSidePadding.toFloat(),
baseTranslationY = trackCenterY,
) )
// Then draw the displayed wave path
canvas.drawPath(displayedWavePath, wavePaint) canvas.drawPath(displayedWavePath, wavePaint)
} else { } else {
// No wave, simplify to line
canvas.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y, wavePaint) canvas.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y, wavePaint)
} }
} }
private fun calculateDisplayedWavePath(
trackLength: Float,
start: Float,
end: Float,
phaseFraction: Float,
trackCenterY: Float,
) {
calculateDisplayedWavePath(
trackLength = trackLength,
start = start,
end = end,
phaseFraction = phaseFraction,
baseTranslationX = trackSidePadding.toFloat(),
baseTranslationY = trackCenterY,
)
}
private fun drawRoundedBlock( private fun drawRoundedBlock(
canvas: Canvas, canvas: Canvas,
drawCenter: PointF, drawCenter: PointF,
clipCenter: PointF, clipCenter: PointF,
clipRight: Boolean, clipRight: Boolean,
) { ) {
// NOTE: Still need to study this more.
// Have to swap paint to fill for a rounded rect
wavePaint.style = Paint.Style.FILL wavePaint.style = Paint.Style.FILL
canvas.withSave { canvas.withSave {
// Drawing the "block" is weird since we ideally want one that can go arbitrarily
// tiny w/o clipping. This leads to some odd logic.
var clipWidth = trackHeight.toFloat() var clipWidth = trackHeight.toFloat()
val halfHeight = trackHeight / 2f val halfHeight = trackHeight / 2f
// Set up a rect that fills in the side that would otherwise get clobbered
// if we clipped an already rounded sikde
if (clipRight) { if (clipRight) {
val leftEdgeDiff = (clipCenter.x - trackHeight / 2f) - (drawCenter.x - trackHeight) val leftEdgeDiff = (clipCenter.x - trackHeight / 2f) - (drawCenter.x - trackHeight)
if (leftEdgeDiff > 0f) { if (leftEdgeDiff > 0f) {
@@ -448,6 +458,8 @@ constructor(
) )
} }
// Create the mask that applies rounded corners/caps that also adequately shrinks
// when extremely thin
val halfClipWidth = clipWidth / 2f val halfClipWidth = clipWidth / 2f
clipRect.set( clipRect.set(
clipCenter.x - halfClipWidth, clipCenter.x - halfClipWidth,
@@ -456,6 +468,7 @@ constructor(
clipCenter.y + halfHeight, clipCenter.y + halfHeight,
) )
// Create the rounded cap rect
drawRect.set( drawRect.set(
drawCenter.x - halfHeight, drawCenter.x - halfHeight,
drawCenter.y - halfHeight, drawCenter.y - halfHeight,
@@ -463,6 +476,7 @@ constructor(
drawCenter.y + halfHeight, drawCenter.y + halfHeight,
) )
// Then finally draw the three rects to create the final pill shape.
roundedRectPath.reset() roundedRectPath.reset()
roundedRectPath.addRoundRect( roundedRectPath.addRoundRect(
clipRect, clipRect,
@@ -479,6 +493,7 @@ constructor(
private fun applyStartWaveRamp(amplitudeFraction: Float, startRampFraction: Float): Float { private fun applyStartWaveRamp(amplitudeFraction: Float, startRampFraction: Float): Float {
if (amplitudeFraction <= 0f) { if (amplitudeFraction <= 0f) {
// Don't care, nothing to ramp
return 0f return 0f
} }
val easedRamp = applyRampEasing(startRampFraction) val easedRamp = applyRampEasing(startRampFraction)
@@ -512,6 +527,7 @@ constructor(
private fun transitionToAmplitudeFraction(target: Float, onFinished: (() -> Unit)? = null) { private fun transitionToAmplitudeFraction(target: Float, onFinished: (() -> Unit)? = null) {
val clampedTarget = target.coerceIn(MIN_VISIBLE_WAVE_FRACTION, 1f) val clampedTarget = target.coerceIn(MIN_VISIBLE_WAVE_FRACTION, 1f)
if (abs(currentAmplitudeFraction - clampedTarget) < EPSILON) { if (abs(currentAmplitudeFraction - clampedTarget) < EPSILON) {
// Too little work, nothing to do
currentAmplitudeFraction = clampedTarget currentAmplitudeFraction = clampedTarget
updateActiveTrackSuppression() updateActiveTrackSuppression()
ensurePhaseTickerState() ensurePhaseTickerState()
@@ -522,6 +538,9 @@ constructor(
waveTransitionAnimation?.cancel() waveTransitionAnimation?.cancel()
waveTransitionAnimation = null waveTransitionAnimation = null
// Use a standard M3 animation
// Not using Auxio anim utils due to this being outside the Auxio pkg
// TODO: Could move it inside the pkg honestly
val springAnimation = val springAnimation =
SpringAnimation(FloatValueHolder(currentAmplitudeFraction)).apply { SpringAnimation(FloatValueHolder(currentAmplitudeFraction)).apply {
spring = spring =
@@ -631,6 +650,9 @@ constructor(
return return
} }
if (suppress) { if (suppress) {
// We can piggyback off of Slider's existing rendering by just
// making sure the straight track is disabled when we switch to the
// wavy one.
super.setTrackActiveTintList(transparentTrackTint) super.setTrackActiveTintList(transparentTrackTint)
} else { } else {
super.setTrackActiveTintList(tmpTrackTintList) super.setTrackActiveTintList(tmpTrackTintList)
@@ -638,11 +660,6 @@ constructor(
linearActiveTrackSuppressed = suppress linearActiveTrackSuppressed = suppress
} }
private fun applyRampEasing(fraction: Float): Float {
val clamped = fraction.coerceIn(0f, 1f)
return clamped * clamped * (3f - 2f * clamped)
}
private fun markDirty() { private fun markDirty() {
cachedWavelength = -1 cachedWavelength = -1
cachedTrackLength = -1f cachedTrackLength = -1f
@@ -663,6 +680,8 @@ constructor(
} }
private fun invalidateCachedWavePath(trackLength: Float, wavelength: Int) { private fun invalidateCachedWavePath(trackLength: Float, wavelength: Int) {
// NOTE: Going to be honest, this is the part I understand the least.
// Need to study it more.
cachedWavePath.rewind() cachedWavePath.rewind()
adjustedWavelength = 0f adjustedWavelength = 0f
if (trackLength <= 0f || wavelength <= 0) { if (trackLength <= 0f || wavelength <= 0) {
@@ -671,8 +690,12 @@ constructor(
} }
val cycleCount = max(1, (trackLength / wavelength).toInt()) val cycleCount = max(1, (trackLength / wavelength).toInt())
// toInt rounding errors necessitate us to actually indicate what the
// real rendered wavelength is.
adjustedWavelength = trackLength / cycleCount adjustedWavelength = trackLength / cycleCount
for (i in 0..cycleCount) { for (i in 0..cycleCount) {
// To be honest this is math magic but it does draw the two
// curves needed for waves. They aren't distinct up/down poitns.
val cycle = i.toFloat() val cycle = i.toFloat()
cachedWavePath.cubicTo( cachedWavePath.cubicTo(
2 * cycle + WAVE_SMOOTHNESS, 2 * cycle + WAVE_SMOOTHNESS,
@@ -692,6 +715,10 @@ constructor(
) )
} }
// Lay out the actual wave across the seekbar
// Without this phase animations won't work and the track will appear "scrunched"
// Additionally if I understand right this also helps extend the wave enough to
// select parts of it to scroll through.
transform.reset() transform.reset()
transform.setScale(adjustedWavelength / 2f, -2f) transform.setScale(adjustedWavelength / 2f, -2f)
transform.postTranslate(0f, 1f) transform.postTranslate(0f, 1f)
@@ -717,28 +744,35 @@ constructor(
var resultTranslationX = baseTranslationX var resultTranslationX = baseTranslationX
if (adjustedWavelength > EPSILON) { if (adjustedWavelength > EPSILON) {
// Viable wavelength, need to cycle it
val cycleCount = trackLength / adjustedWavelength val cycleCount = trackLength / adjustedWavelength
if (cycleCount > EPSILON) { if (cycleCount > EPSILON) {
// Viable actual wave count, need to convert phase to a wave segment to draw
val phaseFractionInPath = phaseFraction / cycleCount val phaseFractionInPath = phaseFraction / cycleCount
val ratio = cycleCount / (cycleCount + 1f) val ratio = cycleCount / (cycleCount + 1f)
adjustedStart = (adjustedStart + phaseFractionInPath) * ratio adjustedStart = (adjustedStart + phaseFractionInPath) * ratio
adjustedEnd = (adjustedEnd + phaseFractionInPath) * ratio adjustedEnd = (adjustedEnd + phaseFractionInPath) * ratio
} }
// Cycle wave backwards by phase
resultTranslationX -= phaseFraction * adjustedWavelength resultTranslationX -= phaseFraction * adjustedWavelength
} }
adjustedStart = adjustedStart.coerceIn(0f, 1f) adjustedStart = adjustedStart.coerceIn(0f, 1f)
adjustedEnd = adjustedEnd.coerceIn(0f, 1f) adjustedEnd = adjustedEnd.coerceIn(0f, 1f)
if (adjustedEnd <= adjustedStart) { if (adjustedEnd <= adjustedStart) {
// Nothing to draw
return false return false
} }
// Get the wave segment we are cycling through
val startDistance = adjustedStart * pathMeasure.length val startDistance = adjustedStart * pathMeasure.length
val endDistance = adjustedEnd * pathMeasure.length val endDistance = adjustedEnd * pathMeasure.length
pathMeasure.getSegment(startDistance, endDistance, displayedWavePath, true) pathMeasure.getSegment(startDistance, endDistance, displayedWavePath, true)
transform.reset() transform.reset()
transform.setTranslate(resultTranslationX, baseTranslationY) transform.setTranslate(resultTranslationX, baseTranslationY)
// Then actually scale the wave by the current amplitude
// (which may not be full due to anims)
val scaleY = displayedAmplitude val scaleY = displayedAmplitude
if (scaleY > 0f) { if (scaleY > 0f) {
transform.postScale(1f, scaleY, resultTranslationX, baseTranslationY) transform.postScale(1f, scaleY, resultTranslationX, baseTranslationY)
@@ -748,6 +782,11 @@ constructor(
return true return true
} }
private fun applyRampEasing(fraction: Float): Float {
val clamped = fraction.coerceIn(0f, 1f)
return clamped * clamped * (3f - 2f * clamped)
}
private fun PointF.reset() { private fun PointF.reset() {
this.x = 0f this.x = 0f
this.y = 0f this.y = 0f