Skip to content

Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.

Lay out lyrics in the song's own direction (#92)

Leloniob4d0917094e8
diff --git a/app/src/main/java/dev/lelonio/square/ui/player/LyricsView.kt b/app/src/main/java/dev/lelonio/square/ui/player/LyricsView.kt
index 254a2f2..e2e6477 100644
--- a/app/src/main/java/dev/lelonio/square/ui/player/LyricsView.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/player/LyricsView.kt
@@ -78,6 +78,7 @@ import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.Dp
+import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex
@@ -106,6 +107,35 @@ data class LyricVisualSettings(
val LocalLyricVisualSettings = compositionLocalOf { LyricVisualSettings() }
+/**
+ * The direction the song is written in, whatever the app is read in.
+ *
+ * The page used to take the app's: with the phone in Arabic, an English song
+ * was laid out right to left, its sung line's words set down in reverse by
+ * the row that places them and every question mark at the wrong end (issue
+ * #92). The words decide instead, by counting the letters that have a
+ * direction of their own; [fallback] for a sheet that has none, such as one
+ * of only numbers.
+ */
+fun lyricsDirection(lines: List<LyricLine>, fallback: LayoutDirection): LayoutDirection {
+ var ltr = 0
+ var rtl = 0
+ lines.forEach { line ->
+ line.text.forEach { c ->
+ when (Character.getDirectionality(c)) {
+ Character.DIRECTIONALITY_LEFT_TO_RIGHT -> ltr++
+ Character.DIRECTIONALITY_RIGHT_TO_LEFT,
+ Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC -> rtl++
+ }
+ }
+ }
+ return when {
+ ltr > rtl -> LayoutDirection.Ltr
+ rtl > ltr -> LayoutDirection.Rtl
+ else -> fallback
+ }
+}
+
/**
* Synced lyrics view ported directly from Spicy Lyrics (spicy-lyrics / LyricsAnimator.ts).
*/
diff --git a/app/src/main/java/dev/lelonio/square/ui/player/PlayerScreen.kt b/app/src/main/java/dev/lelonio/square/ui/player/PlayerScreen.kt
index 775ff42..32df6de 100644
--- a/app/src/main/java/dev/lelonio/square/ui/player/PlayerScreen.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/player/PlayerScreen.kt
@@ -56,6 +56,7 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
+import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.animation.animateColorAsState
import androidx.compose.foundation.Canvas
import androidx.compose.runtime.DisposableEffect
@@ -2867,6 +2868,13 @@ private fun LyricsStage(
}
}
+ // The song's own direction for the words, the app's for everything around
+ // them; see lyricsDirection.
+ val appDirection = LocalLayoutDirection.current
+ val songDirection = remember(lyrics, appDirection) {
+ lyricsDirection(lyrics?.lines.orEmpty(), appDirection)
+ }
+
Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
when {
optionsOpen && query != null -> LyricsOptionsPanel(
@@ -2890,18 +2898,23 @@ private fun LyricsStage(
color = GlassInkDim,
)
- style == dev.lelonio.square.data.PreferencesStore.LyricsStyle.CLASSIC -> ClassicLyricsView(
- lyrics = shown ?: lyrics,
- positionMs = positionMs,
- showTranslation = translated,
- onSeek = onSeek,
- modifier = Modifier.fillMaxSize(),
- )
+ style == dev.lelonio.square.data.PreferencesStore.LyricsStyle.CLASSIC -> CompositionLocalProvider(
+ LocalLayoutDirection provides songDirection,
+ ) {
+ ClassicLyricsView(
+ lyrics = shown ?: lyrics,
+ positionMs = positionMs,
+ showTranslation = translated,
+ onSeek = onSeek,
+ modifier = Modifier.fillMaxSize(),
+ )
+ }
else -> CompositionLocalProvider(
LocalLyricVisualSettings provides LocalLyricVisualSettings.current.let {
if (alone) it.copy(inactiveBlur = 0f) else it
},
+ LocalLayoutDirection provides songDirection,
) {
LyricsView(
lyrics = shown ?: lyrics,