Skip to content

Native Android client for Spotify, built with Jetpack Compose

A tap on a word-synced lyric line plays from the word tapped, not the start of the line (Immersive and Classic); between two words it plays the one before, the first word still plays from the line

kota-zo624953db77b8
diff --git a/base/app/src/main/java/com/music/spotui/ui/immersive/lyrics/ImmersiveLyricsPanel.kt b/base/app/src/main/java/com/music/spotui/ui/immersive/lyrics/ImmersiveLyricsPanel.kt
index e79657fc..03e0a65a 100644
--- a/base/app/src/main/java/com/music/spotui/ui/immersive/lyrics/ImmersiveLyricsPanel.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/immersive/lyrics/ImmersiveLyricsPanel.kt
@@ -108,6 +108,8 @@ import com.music.spotui.ui.look.SweptLine
import com.music.spotui.ui.look.SweptLyricLine
import com.music.spotui.ui.look.UNSUNG_ALPHA
import com.music.spotui.ui.look.rememberReduceMotion
+import com.music.spotui.ui.look.lastPress
+import com.music.spotui.ui.look.tapTime
import com.music.spotui.ui.look.toSwept
import com.music.spotui.ui.viewmodel.LyricsViewModel
import kotlinx.coroutines.delay
@@ -548,6 +550,10 @@ private fun PanelRow(
val staggerDelay = behind.coerceIn(0, STAGGER_STEPS) * STAGGER_FRACTION * run.durationMs
val interaction = remember { MutableInteractionSource() }
val pressed by interaction.collectIsPressedAsState()
+ // Where the finger went down and how wide the row was: a tap plays from the word it hit.
+ var pressAt by remember { mutableStateOf(Offset.Zero) }
+ var pressWidth by remember { mutableStateOf(0) }
+ val density = LocalDensity.current
val scale by animateFloatAsState(
targetValue = when {
pressed -> PRESSED_SCALE
@@ -609,10 +615,17 @@ private fun PanelRow(
}
} else Modifier)
.secondaryClick(onHold)
+ .lastPress { at, size -> pressAt = at; pressWidth = size.width }
.combinedClickable(
enabled = synced || onHold != null, interactionSource = interaction, indication = null,
onLongClick = onHold,
- onClick = { if (synced) onSeek(row.timeMs) },
+ onClick = {
+ if (synced) {
+ val room = with(density) { GLOW_ROOM.roundToPx() }
+ val laid = measureNoteLine(noteMeasurer, row.text, lineStyle, pressWidth, room)
+ onSeek(tapTime(row.swept, laid, pressAt - Offset(room.toFloat(), room.toFloat())))
+ }
+ },
)
CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Ltr) {
diff --git a/base/app/src/main/java/com/music/spotui/ui/look/LyricTap.kt b/base/app/src/main/java/com/music/spotui/ui/look/LyricTap.kt
new file mode 100644
index 00000000..f104bc74
--- /dev/null
+++ b/base/app/src/main/java/com/music/spotui/ui/look/LyricTap.kt
@@ -0,0 +1,30 @@
+package com.music.spotui.ui.look
+
+import androidx.compose.foundation.gestures.awaitEachGesture
+import androidx.compose.foundation.gestures.awaitFirstDown
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.geometry.Offset
+import androidx.compose.ui.input.pointer.PointerEventPass
+import androidx.compose.ui.input.pointer.pointerInput
+import androidx.compose.ui.text.TextLayoutResult
+import androidx.compose.ui.unit.IntSize
+
+/**
+ * Where the finger last went down on this element, in its own coordinates, and how big the
+ * element was then. Seen before the click handlers below it and never consumed, so a tap on a
+ * lyric line can be turned into the word it landed on.
+ */
+fun Modifier.lastPress(onDown: (Offset, IntSize) -> Unit): Modifier = pointerInput(onDown) {
+ awaitEachGesture {
+ val down = awaitFirstDown(requireUnconsumed = false, pass = PointerEventPass.Initial)
+ onDown(down.position, size)
+ }
+}
+
+/** When to play from for a tap at [at] (relative to the text's own top left) on [line] laid out as [layout]. */
+fun tapTime(line: SweptLine, layout: TextLayoutResult, at: Offset): Long {
+ if (!line.isWordSynced) return line.timeMs
+ val drawn = layout.layoutInput.text.text
+ val laid = if (drawn == line.text) line else line.copy(text = drawn)
+ return laid.startAtChar(layout.getOffsetForPosition(at))
+}
diff --git a/base/app/src/main/java/com/music/spotui/ui/look/SweptLyricModel.kt b/base/app/src/main/java/com/music/spotui/ui/look/SweptLyricModel.kt
index eeb58203..d80385bd 100644
--- a/base/app/src/main/java/com/music/spotui/ui/look/SweptLyricModel.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/look/SweptLyricModel.kt
@@ -93,6 +93,16 @@ data class SweptLine(
}
}
+ /**
+ * Where a tap at character [offset] of [text] plays from: the start of the word under it,
+ * or of the word before when it lands between two. The first word, and a line without
+ * word timings, play from where the line starts.
+ */
+ fun startAtChar(offset: Int): Long {
+ val index = wordSpans.indexOfLast { it.first <= offset }
+ return if (index <= 0) minOf(timeMs, words.firstOrNull()?.startMs ?: timeMs) else words[index].startMs
+ }
+
/**
* Where each separately timed piece of the line sits in [text]: a word's
* syllables where the source split it, the word itself where it did not.
diff --git a/base/app/src/main/java/com/music/spotui/ui/screens/LyricsScreen.kt b/base/app/src/main/java/com/music/spotui/ui/screens/LyricsScreen.kt
index 16c075d3..744aa30a 100644
--- a/base/app/src/main/java/com/music/spotui/ui/screens/LyricsScreen.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/screens/LyricsScreen.kt
@@ -1,6 +1,9 @@
package com.music.spotui.ui.screens
import com.music.spotui.ui.components.secondaryClick
+import com.music.spotui.ui.look.lastPress
+import com.music.spotui.ui.look.tapTime
+import com.music.spotui.ui.look.toSwept
import com.music.spotui.ui.components.pressable
import androidx.compose.ui.res.stringResource
@@ -263,7 +266,8 @@ private fun LyricLineText(
fontSize: TextUnit,
position: () -> Long,
translation: String?,
- onTap: (() -> Unit)?,
+ /** Gets the time to play from: the start of the word tapped on a word-synced line. */
+ onTap: ((Long) -> Unit)?,
modifier: Modifier = Modifier,
annotated: Boolean = false,
onHold: (() -> Unit)? = null,
@@ -273,18 +277,22 @@ private fun LyricLineText(
else -> palette.highlight
}
val color by animateColorAsState(target, tween(220), label = "lyricColor")
+ // Explained by Genius: a light dotted line under each row of the text.
+ var layout by remember { mutableStateOf<androidx.compose.ui.text.TextLayoutResult?>(null) }
+ var pressAt by remember { mutableStateOf(androidx.compose.ui.geometry.Offset.Zero) }
+ val swept = remember(line) { line.toSwept() }
val lineModifier = modifier
.fillMaxWidth()
.then(
- if (onTap != null || onHold != null) Modifier.secondaryClick(onHold).combinedClickable(
- interactionSource = remember { MutableInteractionSource() },
- indication = null,
- onLongClick = onHold,
- onClick = { onTap?.invoke() },
- ) else Modifier
+ if (onTap != null || onHold != null) Modifier.secondaryClick(onHold)
+ .lastPress { at, _ -> pressAt = at }
+ .combinedClickable(
+ interactionSource = remember { MutableInteractionSource() },
+ indication = null,
+ onLongClick = onHold,
+ onClick = { onTap?.invoke(layout?.let { tapTime(swept, it, pressAt) } ?: line.timeMs) },
+ ) else Modifier
)
- // Explained by Genius: a light dotted line under each row of the text.
- var layout by remember { mutableStateOf<androidx.compose.ui.text.TextLayoutResult?>(null) }
val underline = if (!annotated) Modifier else Modifier.drawBehind {
val t = layout ?: return@drawBehind
val dot = 2.dp.toPx()
@@ -538,7 +546,7 @@ fun LyricsScreen(
fontSize = 24.sp,
position = position,
translation = if (state.showTranslation) state.translations?.getOrNull(i) else null,
- onTap = if (l.synced) ({ jumpTo(line.timeMs) }) else null,
+ onTap = if (l.synced) ({ jumpTo(it) }) else null,
annotated = i in notes,
onHold = notes[i]?.let { n -> {
haptics.performHapticFeedback(androidx.compose.ui.hapticfeedback.HapticFeedbackType.LongPress)
diff --git a/base/app/src/test/java/com/music/spotui/ui/look/SweptLineTapTest.kt b/base/app/src/test/java/com/music/spotui/ui/look/SweptLineTapTest.kt
new file mode 100644
index 00000000..1f772eb9
--- /dev/null
+++ b/base/app/src/test/java/com/music/spotui/ui/look/SweptLineTapTest.kt
@@ -0,0 +1,43 @@
+package com.music.spotui.ui.look
+
+import org.junit.Assert.assertEquals
+import org.junit.Test
+
+class SweptLineTapTest {
+ // The first word starts a little after the line's own stamp.
+ private val line = SweptLine(
+ timeMs = 10_000,
+ text = "fuck love, fuck love",
+ words = listOf(
+ SweptWord(10_200, 10_500, "fuck"),
+ SweptWord(10_500, 11_000, "love,"),
+ SweptWord(11_400, 11_700, "fuck"),
+ SweptWord(11_700, 12_300, "love"),
+ ),
+ )
+
+ @Test
+ fun firstWordPlaysFromTheLine() {
+ assertEquals(10_000, line.startAtChar(0))
+ assertEquals(10_000, line.startAtChar(3))
+ }
+
+ @Test
+ fun laterWordPlaysFromItsOwnStart() {
+ assertEquals(10_500, line.startAtChar(5))
+ assertEquals(10_500, line.startAtChar(8))
+ // The repeated word lines up with its own occurrence, not the first one.
+ assertEquals(11_400, line.startAtChar(12))
+ assertEquals(11_700, line.startAtChar(17))
+ }
+
+ @Test
+ fun betweenTwoWordsPlaysTheOneBefore() {
+ assertEquals(10_500, line.startAtChar(10)) // the space after "love,"
+ }
+
+ @Test
+ fun lineWithoutWordTimingsPlaysFromItsStart() {
+ assertEquals(4_000, SweptLine(4_000, "no words here").startAtChar(9))
+ }
+}