Web fills rewound to the start whenever played before their word, so every upcoming word filled at once. iOS ignored the blur filter because React Native draws it only with its SwiftUI filters flag, now turned on at launch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
376 lines
9.4 KiB
TypeScript
376 lines
9.4 KiB
TypeScript
import { memo, useCallback, useEffect, useMemo, useRef, useState } from "react"
|
|
import {
|
|
Pressable,
|
|
ScrollView,
|
|
StyleSheet,
|
|
Text,
|
|
View,
|
|
type ScrollViewInstance,
|
|
} from "react-native"
|
|
import Animated, {
|
|
cancelAnimation,
|
|
Easing,
|
|
useAnimatedStyle,
|
|
useSharedValue,
|
|
withSequence,
|
|
withTiming,
|
|
type SharedValue,
|
|
} from "react-native-reanimated"
|
|
|
|
import {
|
|
usePlaybackActions,
|
|
usePlaybackProgress,
|
|
usePlaybackQueue,
|
|
} from "@/contexts/playback"
|
|
import { useLyricsTime } from "@/hooks/use-lyrics"
|
|
import type { NowPlayingColors } from "@/lib/now-playing-colors"
|
|
import {
|
|
activeLineIndex,
|
|
type LyricLine,
|
|
type LyricWord,
|
|
type Lyrics,
|
|
} from "@otunes/convex/client/lyrics"
|
|
|
|
/** How long a manual scroll keeps the view from following the song. */
|
|
const MANUAL_SCROLL_HOLD_MS = 3000
|
|
/** Where the playing line sits, as a share of the view's height from the top. */
|
|
const ACTIVE_LINE_POSITION = 0.12
|
|
/** Blur radius by distance from the playing line; farther lines share the last. */
|
|
const LINE_BLUR = [0, 1, 2, 3]
|
|
|
|
/**
|
|
* Lyrics in the player's own colors. Synced lines follow playback and seek
|
|
* when tapped; `null` lyrics show a short note instead.
|
|
*/
|
|
export function LyricsView({
|
|
lyrics,
|
|
colors,
|
|
fontSize = 30,
|
|
}: {
|
|
lyrics: Lyrics | null | undefined
|
|
colors: NowPlayingColors
|
|
fontSize?: number
|
|
}) {
|
|
if (lyrics === undefined) return null
|
|
if (lyrics === null) {
|
|
return (
|
|
<View style={styles.empty}>
|
|
<Text style={[styles.emptyText, { color: colors.secondaryForeground }]}>
|
|
No lyrics
|
|
</Text>
|
|
</View>
|
|
)
|
|
}
|
|
return <LyricsLines lyrics={lyrics} colors={colors} fontSize={fontSize} />
|
|
}
|
|
|
|
function LyricsLines({
|
|
lyrics,
|
|
colors,
|
|
fontSize,
|
|
}: {
|
|
lyrics: Lyrics
|
|
colors: NowPlayingColors
|
|
fontSize: number
|
|
}) {
|
|
const { currentTime } = usePlaybackProgress()
|
|
const { isPlaying } = usePlaybackQueue()
|
|
const { seekTo, setIsPlaying } = usePlaybackActions()
|
|
const time = useLyricsTime(lyrics, currentTime, isPlaying)
|
|
const active = activeLineIndex(lyrics, time)
|
|
const synced = lyrics.synced !== "none"
|
|
const scrollRef = useRef<ScrollViewInstance>(null)
|
|
const lineOffsets = useRef<number[]>([])
|
|
const [height, setHeight] = useState(0)
|
|
// While the listener scrolls, lines stay sharp and the view stays put.
|
|
const [browsing, setBrowsing] = useState(false)
|
|
const browseTimer = useRef<ReturnType<typeof setTimeout>>(undefined)
|
|
|
|
useEffect(() => () => clearTimeout(browseTimer.current), [])
|
|
|
|
const scrollToLine = useCallback(
|
|
(index: number) => {
|
|
const y = lineOffsets.current[Math.max(index, 0)]
|
|
if (y === undefined || !synced || height === 0 || browsing) return
|
|
scrollRef.current?.scrollTo({
|
|
y: Math.max(0, y - height * ACTIVE_LINE_POSITION),
|
|
animated: true,
|
|
})
|
|
},
|
|
[height, synced, browsing]
|
|
)
|
|
|
|
useEffect(() => {
|
|
scrollToLine(active)
|
|
}, [active, scrollToLine])
|
|
|
|
const activeRef = useRef(active)
|
|
useEffect(() => {
|
|
activeRef.current = active
|
|
}, [active])
|
|
const handleLineLayout = useCallback(
|
|
(index: number, y: number) => {
|
|
lineOffsets.current[index] = y
|
|
// Opening mid-song: jump once the playing line has been measured.
|
|
if (index === Math.max(activeRef.current, 0)) scrollToLine(index)
|
|
},
|
|
[scrollToLine]
|
|
)
|
|
|
|
const playFrom = useCallback(
|
|
(seconds: number) => {
|
|
seekTo(seconds)
|
|
setIsPlaying(true)
|
|
clearTimeout(browseTimer.current)
|
|
setBrowsing(false)
|
|
},
|
|
[seekTo, setIsPlaying]
|
|
)
|
|
|
|
const releaseScroll = () => {
|
|
clearTimeout(browseTimer.current)
|
|
browseTimer.current = setTimeout(
|
|
() => setBrowsing(false),
|
|
MANUAL_SCROLL_HOLD_MS
|
|
)
|
|
}
|
|
|
|
const lineStyle = useMemo(
|
|
() => ({ fontSize, lineHeight: Math.round(fontSize * 1.18) }),
|
|
[fontSize]
|
|
)
|
|
|
|
return (
|
|
<ScrollView
|
|
ref={scrollRef}
|
|
style={styles.fill}
|
|
contentContainerStyle={{
|
|
paddingTop: height * ACTIVE_LINE_POSITION,
|
|
paddingBottom: height * 0.7,
|
|
}}
|
|
showsVerticalScrollIndicator={false}
|
|
onLayout={(event) => setHeight(event.nativeEvent.layout.height)}
|
|
onScrollBeginDrag={() => {
|
|
clearTimeout(browseTimer.current)
|
|
setBrowsing(true)
|
|
}}
|
|
// Hold from when the scroll settles, not when the finger lifted.
|
|
onScrollEndDrag={releaseScroll}
|
|
onMomentumScrollEnd={releaseScroll}
|
|
>
|
|
{lyrics.lines.map((line, index) =>
|
|
synced ? (
|
|
<SyncedLine
|
|
key={index}
|
|
index={index}
|
|
line={line}
|
|
distance={
|
|
browsing
|
|
? 0
|
|
: Math.min(Math.abs(index - active), LINE_BLUR.length - 1)
|
|
}
|
|
active={index === active}
|
|
time={index === active ? time : null}
|
|
isPlaying={isPlaying}
|
|
colors={colors}
|
|
textStyle={lineStyle}
|
|
onSelect={playFrom}
|
|
onLayoutY={handleLineLayout}
|
|
/>
|
|
) : (
|
|
<Text
|
|
key={index}
|
|
style={[
|
|
styles.plainLine,
|
|
{
|
|
color: colors.foreground,
|
|
fontSize: fontSize * 0.75,
|
|
lineHeight: Math.round(fontSize * 0.75 * 1.25),
|
|
},
|
|
line.text === "" && styles.stanzaGap,
|
|
]}
|
|
>
|
|
{line.text}
|
|
</Text>
|
|
)
|
|
)}
|
|
</ScrollView>
|
|
)
|
|
}
|
|
|
|
/**
|
|
* One timed line, blurred by its `distance` from the playing line. Only the
|
|
* playing line gets the time, so the others skip re-rendering as the song
|
|
* plays.
|
|
*/
|
|
const SyncedLine = memo(function SyncedLine({
|
|
index,
|
|
line,
|
|
distance,
|
|
active,
|
|
time,
|
|
isPlaying,
|
|
colors,
|
|
textStyle,
|
|
onSelect,
|
|
onLayoutY,
|
|
}: {
|
|
index: number
|
|
line: LyricLine
|
|
distance: number
|
|
active: boolean
|
|
time: number | null
|
|
isPlaying: boolean
|
|
colors: NowPlayingColors
|
|
textStyle: { fontSize: number; lineHeight: number }
|
|
onSelect: (seconds: number) => void
|
|
onLayoutY: (index: number, y: number) => void
|
|
}) {
|
|
const blur = LINE_BLUR[distance]
|
|
return (
|
|
<Pressable
|
|
onPress={() => onSelect(line.start ?? 0)}
|
|
onLayout={(event) => onLayoutY(index, event.nativeEvent.layout.y)}
|
|
accessibilityRole="button"
|
|
accessibilityHint="Plays from this line"
|
|
style={[styles.line, blur > 0 && { filter: [{ blur }] }]}
|
|
>
|
|
{active && line.words && time !== null ? (
|
|
<FilledWords
|
|
words={line.words}
|
|
time={time}
|
|
isPlaying={isPlaying}
|
|
colors={colors}
|
|
textStyle={textStyle}
|
|
/>
|
|
) : (
|
|
<Text
|
|
style={[
|
|
styles.lineText,
|
|
textStyle,
|
|
{
|
|
color:
|
|
active && !line.words
|
|
? colors.foreground
|
|
: colors.lyricsInactive,
|
|
},
|
|
]}
|
|
>
|
|
{line.text}
|
|
</Text>
|
|
)}
|
|
</Pressable>
|
|
)
|
|
})
|
|
|
|
/**
|
|
* The playing line's words, each wiping from dim to bright as it is sung.
|
|
* Reanimated runs the wipe; playback updates only re-anchor it.
|
|
*/
|
|
function FilledWords({
|
|
words,
|
|
time,
|
|
isPlaying,
|
|
colors,
|
|
textStyle,
|
|
}: {
|
|
words: LyricWord[]
|
|
time: number
|
|
isPlaying: boolean
|
|
colors: NowPlayingColors
|
|
textStyle: { fontSize: number; lineHeight: number }
|
|
}) {
|
|
const sung = useSharedValue(time)
|
|
const end = words.at(-1)?.end ?? time
|
|
|
|
useEffect(() => {
|
|
cancelAnimation(sung)
|
|
// Jump to the reported time first, so the wipe never runs from a stale one.
|
|
sung.value =
|
|
isPlaying && end > time
|
|
? withSequence(
|
|
withTiming(time, { duration: 0 }),
|
|
withTiming(end, {
|
|
duration: (end - time) * 1000,
|
|
easing: Easing.linear,
|
|
})
|
|
)
|
|
: time
|
|
}, [sung, time, isPlaying, end])
|
|
|
|
return (
|
|
<View style={[styles.words, { columnGap: textStyle.fontSize * 0.25 }]}>
|
|
{words.map((word, index) => (
|
|
<FilledWord
|
|
key={index}
|
|
word={word}
|
|
sung={sung}
|
|
colors={colors}
|
|
textStyle={textStyle}
|
|
/>
|
|
))}
|
|
</View>
|
|
)
|
|
}
|
|
|
|
function FilledWord({
|
|
word,
|
|
sung,
|
|
colors,
|
|
textStyle,
|
|
}: {
|
|
word: LyricWord
|
|
sung: SharedValue<number>
|
|
colors: NowPlayingColors
|
|
textStyle: { fontSize: number; lineHeight: number }
|
|
}) {
|
|
const [width, setWidth] = useState(0)
|
|
const text = word.text.trim()
|
|
const wipe = useAnimatedStyle(() => {
|
|
const progress =
|
|
(sung.value - word.start) / Math.max(word.end - word.start, 0.05)
|
|
return { width: width * Math.min(Math.max(progress, 0), 1) }
|
|
})
|
|
|
|
return (
|
|
<View>
|
|
<Text
|
|
style={[styles.lineText, textStyle, { color: colors.lyricsUnsung }]}
|
|
onLayout={(event) => setWidth(event.nativeEvent.layout.width)}
|
|
>
|
|
{text}
|
|
</Text>
|
|
<Animated.View style={[styles.wipe, wipe]}>
|
|
<Text
|
|
numberOfLines={1}
|
|
style={[
|
|
styles.lineText,
|
|
textStyle,
|
|
{ color: colors.foreground, width },
|
|
]}
|
|
>
|
|
{text}
|
|
</Text>
|
|
</Animated.View>
|
|
</View>
|
|
)
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
fill: { flex: 1 },
|
|
empty: { flex: 1, alignItems: "center", justifyContent: "center" },
|
|
emptyText: { fontSize: 17, fontWeight: "600" },
|
|
line: { paddingVertical: 9 },
|
|
lineText: { fontWeight: "800" },
|
|
words: { flexDirection: "row", flexWrap: "wrap" },
|
|
wipe: {
|
|
position: "absolute",
|
|
top: 0,
|
|
bottom: 0,
|
|
left: 0,
|
|
overflow: "hidden",
|
|
},
|
|
plainLine: { fontWeight: "700", paddingVertical: 2 },
|
|
stanzaGap: { height: 18 },
|
|
})
|