Skip to content

Commit 767eaa6

Browse files
Lee Seongjooclaude
andcommitted
HUD subtitle: continuous teleprompter crawl over the whole script
Replace the per-paragraph karaoke scroll with a Star Wars-style crawl: the whole script renders as one column that rises continuously in step with playback, so paragraphs flow into each other with no per-paragraph reset. - AppState: crawlLines (all paragraphs flattened to sentences), crawlFraction (char-weighted overall progress — QueuePlayer.progress is chunk-equal-weighted, the wrong driver), isCurrentLine for the global highlight - Reading line near the top; text fades out through a top gradient mask as it rises; end-clamp so the last sentences settle at the bottom and stay legible - Measure content height via GeometryReader onAppear/onChange (PreferenceKey didn't propagate in the NSHostingView/panel) - Current line distinguished by brightness only (no weight snap/reflow) - Progress bar joins the transport cluster at a fixed width so it doesn't stretch as the panel widens with the subtitle font Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 5f857c8 commit 767eaa6

2 files changed

Lines changed: 107 additions & 32 deletions

File tree

‎Sources/App/AppState.swift‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,48 @@ final class AppState: ObservableObject {
199199
return s.count - 1
200200
}
201201

202+
// MARK: - Continuous crawl (teleprompter over the WHOLE script)
203+
204+
/// One sentence of the playback, flattened across all paragraphs, so the HUD
205+
/// can render the whole script as a single continuously-scrolling column.
206+
struct CrawlLine: Identifiable {
207+
let id: Int // global order (0-based)
208+
let chunk: Int // 0-based paragraph it belongs to
209+
let sentence: Int // sentence index within that paragraph
210+
let text: String
211+
}
212+
213+
/// Every paragraph split into sentences, flattened in reading order.
214+
var crawlLines: [CrawlLine] {
215+
var out: [CrawlLine] = []
216+
var gid = 0
217+
for (ci, chunk) in spokenChunks.enumerated() {
218+
for (si, s) in TextSplitter.sentences(chunk).enumerated() {
219+
out.append(CrawlLine(id: gid, chunk: ci, sentence: si, text: s)); gid += 1
220+
}
221+
}
222+
return out
223+
}
224+
225+
/// The globally-current sentence (chunk, sentence) — for highlighting.
226+
func isCurrentLine(_ line: CrawlLine) -> Bool {
227+
line.chunk == chunkIndex - 1 && line.sentence == currentSentenceIndex
228+
}
229+
230+
/// How far through the WHOLE script playback is, weighted by paragraph length
231+
/// (chars) so the crawl tracks the voice instead of lurching per paragraph
232+
/// (QueuePlayer.progress is chunk-equal-weighted — wrong driver for this).
233+
var crawlFraction: Double {
234+
let lens = spokenChunks.map { Double(max(1, $0.count)) }
235+
let total = lens.reduce(0, +)
236+
guard total > 0 else { return 0 }
237+
let cur = chunkIndex - 1
238+
var read = 0.0
239+
for i in 0..<lens.count where i < cur { read += lens[i] }
240+
if lens.indices.contains(cur) { read += chunkProgress * lens[cur] }
241+
return min(1, read / total)
242+
}
243+
202244
/// Map ElevenLabs per-character start times to one start time per sentence
203245
/// of `text`. Returns [] if the alignment doesn't line up (→ estimate).
204246
private func sentenceStartTimes(text: String, charStarts: [Double]) -> [Double] {

‎Sources/UI/PlayerOverlay.swift‎

Lines changed: 65 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,43 @@ import SwiftUI
66
struct PlayerOverlay: View {
77
@ObservedObject var app: AppState
88

9+
/// Measured natural height of the full subtitle text (for the crawl mapping).
10+
@State private var contentHeight: CGFloat = 0
11+
912
private var tint: Color { app.playbackMode == .tts ? .blue : .purple }
1013

14+
/// Viewport height — a few lines tall so text visibly rises (Star Wars crawl).
15+
private var subtitleViewportHeight: CGFloat { max(160, app.subtitleFontSize * 6.5) }
16+
17+
/// Teleprompter crawl with the reading line near the TOP: at progress 0 the
18+
/// first line sits just below the top (a small pad leaves a band for read
19+
/// text to fade through); as playback advances the text rises and earlier
20+
/// sentences scroll up and off the top. Clamped at the END so the last
21+
/// sentences settle at the bottom and stay on screen (don't scroll off/fade).
22+
private func crawlOffset(progress: Double, contentH: CGFloat, viewportH: CGFloat) -> CGFloat {
23+
let topPad = app.subtitleFontSize * 1.5
24+
let raw = topPad - CGFloat(progress) * contentH
25+
let minOffset = min(topPad, viewportH - contentH) // most-negative: end-of-content rests at the viewport bottom
26+
return max(minOffset, raw)
27+
}
28+
29+
/// Pinned to the viewport's top edge: read text fades out gradually as it
30+
/// rises through the band. No bottom fade — so the last sentences, which
31+
/// settle at the bottom, stay fully legible.
32+
private var crawlFadeMask: LinearGradient {
33+
LinearGradient(stops: [
34+
.init(color: .clear, location: 0.0),
35+
.init(color: .black, location: 0.30),
36+
.init(color: .black, location: 1.0),
37+
], startPoint: .top, endPoint: .bottom)
38+
}
39+
1140
/// Widen the HUD with the subtitle font so a line holds roughly the same
1241
/// number of characters at any size — bigger text → wider panel, fewer
1342
/// wraps. Stays compact (360) when no subtitle is showing; capped so it
1443
/// never runs off a normal screen.
1544
private var hudWidth: CGFloat {
16-
guard app.showSubtitle, !app.currentChunkText.isEmpty else { return 360 }
45+
guard app.showSubtitle, !app.spokenChunks.isEmpty else { return 360 }
1746
return min(max(360, app.subtitleFontSize * 24), 760)
1847
}
1948

@@ -56,41 +85,41 @@ struct PlayerOverlay: View {
5685
}
5786
}
5887

59-
if app.showSubtitle, !app.currentChunkText.isEmpty {
60-
ScrollViewReader { proxy in
61-
ScrollView {
62-
VStack(alignment: .leading, spacing: 4) {
63-
ForEach(Array(app.currentSentences.enumerated()), id: \.offset) { i, s in
64-
let cur = app.currentSentenceIndex
65-
Text(s)
66-
.font(.system(size: app.subtitleFontSize))
67-
.fontWeight(i == cur ? .semibold : .regular)
68-
.foregroundStyle(i == cur ? AnyShapeStyle(.primary) : AnyShapeStyle(.secondary))
69-
.opacity(i == cur ? 1 : max(0.3, 1 - 0.25 * Double(abs(i - cur))))
70-
.padding(.horizontal, i == cur ? 6 : 0)
71-
.padding(.vertical, i == cur ? 3 : 0)
72-
.background(i == cur ? tint.opacity(0.22) : .clear,
73-
in: RoundedRectangle(cornerRadius: 6))
74-
.frame(maxWidth: .infinity, alignment: .leading)
75-
.id(i)
76-
}
77-
}
78-
}
79-
.frame(maxHeight: max(92, app.subtitleFontSize * 5))
80-
.onChange(of: app.currentSentenceIndex) { _, idx in
81-
withAnimation(.easeInOut(duration: 0.25)) { proxy.scrollTo(idx, anchor: .center) }
82-
}
83-
.onChange(of: app.chunkIndex) { _, _ in
84-
proxy.scrollTo(0, anchor: .top) // new paragraph → back to top
88+
if app.showSubtitle, !app.crawlLines.isEmpty {
89+
// The WHOLE script is one column; it scrolls continuously so
90+
// paragraphs flow into each other (no per-paragraph reset).
91+
VStack(alignment: .leading, spacing: 4) {
92+
ForEach(app.crawlLines) { line in
93+
let cur = app.isCurrentLine(line)
94+
Text(line.text)
95+
.font(.system(size: app.subtitleFontSize))
96+
// distinguish the current line by brightness only (no weight
97+
// change — that snaps and reflows, which read as abrupt)
98+
.foregroundStyle(cur ? AnyShapeStyle(.primary) : AnyShapeStyle(.secondary))
99+
.opacity(cur ? 1 : 0.4)
100+
.padding(.horizontal, 6)
101+
.padding(.vertical, 3)
102+
.frame(maxWidth: .infinity, alignment: .leading)
103+
.animation(.easeInOut(duration: 0.5), value: cur) // gently fade the emphasis in/out
85104
}
86105
}
106+
.fixedSize(horizontal: false, vertical: true) // take full natural height (don't compress to the viewport)
107+
.background(GeometryReader { g in // measure that natural height for the crawl mapping
108+
Color.clear
109+
.onAppear { contentHeight = g.size.height }
110+
.onChange(of: g.size.height) { _, h in contentHeight = h }
111+
})
112+
.offset(y: crawlOffset(progress: app.crawlFraction, contentH: contentHeight, viewportH: subtitleViewportHeight))
113+
.animation(.linear(duration: 0.12), value: app.crawlFraction) // smooth between 0.1s ticks
114+
.frame(height: subtitleViewportHeight, alignment: .top) // viewport: fixed window the text scrolls through
115+
.clipped()
116+
.mask(crawlFadeMask) // top-edge fade band (pinned to the viewport, not the moving text)
87117
}
88118

89-
// Progress spans the width; transport stays a fixed-size, centered
90-
// cluster so it doesn't grow or spread out as the subtitle/panel widen.
91-
ProgressView(value: app.progress).progressViewStyle(.linear)
92-
93-
HStack(spacing: 22) {
119+
// Transport + progress are one fixed-size, centered cluster, so the
120+
// progress bar keeps a fixed width and nothing stretches as the
121+
// subtitle/panel widen.
122+
HStack(spacing: 16) {
94123
Button { app.skipPrev() } label: { Image(systemName: "backward.fill") }
95124
.disabled(!app.canSkipPrev)
96125
Button { app.togglePause() } label: {
@@ -101,6 +130,10 @@ struct PlayerOverlay: View {
101130
.help(app.phase == .paused ? "재개" : "일시정지")
102131
Button { app.skipNext() } label: { Image(systemName: "forward.fill") }
103132
.disabled(!app.canSkipNext)
133+
134+
ProgressView(value: app.progress).progressViewStyle(.linear)
135+
.frame(width: 140)
136+
104137
Button { app.stop() } label: { Image(systemName: "stop.fill") }
105138
.foregroundStyle(.secondary).help("중지")
106139
}

0 commit comments

Comments
 (0)