Repository navigation
Expand file tree
/
Copy pathPlayerOverlay.swift
More file actions
177 lines (162 loc) · 9.34 KB
/
Copy pathPlayerOverlay.swift
File metadata and controls
177 lines (162 loc) · 9.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import SwiftUI
private struct OverlayControlBoundsKey: PreferenceKey {
static let defaultValue: [Anchor<CGRect>] = []
static func reduce(value: inout [Anchor<CGRect>], nextValue: () -> [Anchor<CGRect>]) {
value.append(contentsOf: nextValue())
}
}
/// Compact floating transport shown while synthesizing / playing. Shows WHAT is
/// playing (TTS vs 해설) rather than the script text, with standard paragraph
/// navigation.
struct PlayerOverlay: View {
@ObservedObject var app: AppState
/// The 10 Hz playback values (progress / crawl position) — observed here ONLY,
/// so ticking doesn't re-render the whole app. crawlFraction on `app` reads
/// telemetry.chunkProgress, so observing telemetry keeps the crawl live.
@ObservedObject var telemetry: PlaybackTelemetry
/// Measured natural height of the full subtitle column (for the crawl mapping).
@State private var contentHeight: CGFloat = 0
private var tint: Color { app.playbackMode == .tts ? .blue : .purple }
/// Viewport height — a few lines tall so context shows above/below the
/// current sentence.
private var subtitleViewportHeight: CGFloat { max(160, app.subtitleFontSize * 6.5) }
/// Where the currently-read position sits in the viewport (0.5 = center).
private let readingAnchor: CGFloat = 0.5
/// Continuous crawl: keep the point at `progress` through the script at the
/// reading line, so the text rises smoothly in step with playback and the
/// current sentence stays centered.
private func crawlOffset(progress: Double, contentH: CGFloat, viewportH: CGFloat) -> CGFloat {
CrawlLayout.crawlOffset(progress: progress, contentHeight: contentH,
viewportHeight: viewportH, readingAnchor: readingAnchor)
}
/// Centered reading line → fade both ends: upcoming text fades in at the
/// bottom, read text fades out at the top; the middle (current sentence) is sharp.
private var crawlFadeMask: LinearGradient {
LinearGradient(stops: [
.init(color: .clear, location: 0.0),
.init(color: .black, location: 0.30),
.init(color: .black, location: 0.70),
.init(color: .clear, location: 1.0),
], startPoint: .top, endPoint: .bottom)
}
/// The subtitle area shows only once playback has actually started — never
/// during 해설/대본 generation or synthesis — so a previous run's subtitle
/// can't linger and the new one appears fresh as playback begins.
private var showsSubtitleArea: Bool {
app.showSubtitle && (app.phase == .playing || app.phase == .paused) && !app.crawlLines.isEmpty
}
/// Widen the HUD with the subtitle font so a line holds roughly the same
/// number of characters at any size — bigger text → wider panel, fewer
/// wraps. Stays compact (360) when no subtitle is showing; capped so it
/// never runs off a normal screen.
private var hudWidth: CGFloat {
guard showsSubtitleArea else { return 360 }
return min(max(360, app.subtitleFontSize * 24), 760)
}
var body: some View {
VStack(alignment: .leading, spacing: 8) {
HStack(spacing: 6) {
Label(app.playbackMode.label, systemImage: app.playbackMode.icon)
.font(.caption.weight(.semibold))
.padding(.horizontal, 8).padding(.vertical, 3)
.background(tint.opacity(0.18), in: Capsule())
.foregroundStyle(tint)
if app.chunkCount > 1 {
Text("문단 \(app.chunkIndex)/\(app.chunkCount)")
.font(.caption).foregroundStyle(.secondary)
}
Spacer()
if app.phase == .synthesizing {
ProgressView().controlSize(.small)
.anchorPreference(key: OverlayControlBoundsKey.self, value: .bounds) { [$0] }
Text("합성 중…").font(.caption).foregroundStyle(.secondary)
} else {
HStack(spacing: 10) {
Button { app.toggleSubtitle() } label: {
Image(systemName: app.showSubtitle ? "captions.bubble.fill" : "captions.bubble")
}
.help(app.showSubtitle ? "자막 끄기" : "자막 켜기")
if app.showSubtitle {
HStack(spacing: 2) {
Button { app.adjustSubtitleFont(by: -2) } label: { Image(systemName: "minus") }
.disabled(app.subtitleFontSize <= AppState.subtitleFontRange.lowerBound)
.help("자막 작게")
Image(systemName: "textformat.size").foregroundStyle(.secondary)
Button { app.adjustSubtitleFont(by: 2) } label: { Image(systemName: "plus") }
.disabled(app.subtitleFontSize >= AppState.subtitleFontRange.upperBound)
.help("자막 크게")
}
}
}
.buttonStyle(.plain)
.font(.caption.weight(.semibold))
.anchorPreference(key: OverlayControlBoundsKey.self, value: .bounds) { [$0] }
}
}
if showsSubtitleArea {
// The WHOLE script is one column that scrolls continuously in step
// with playback (paragraphs flow into each other), keeping the
// current sentence centered.
let currentLineID = app.currentCrawlLineID // compute once per frame, not per line
VStack(alignment: .leading, spacing: 4) {
ForEach(app.crawlLines) { line in
let cur = line.id == currentLineID
Text(line.text)
.font(.system(size: app.subtitleFontSize))
// distinguish the current line by brightness only (no weight
// change — that snaps and reflows, which read as abrupt)
.foregroundStyle(cur ? AnyShapeStyle(.primary) : AnyShapeStyle(.secondary))
.opacity(cur ? 1 : 0.4)
.padding(.horizontal, 6)
.padding(.vertical, 3)
.frame(maxWidth: .infinity, alignment: .leading)
.animation(.easeInOut(duration: 0.5), value: cur) // gently fade the emphasis in/out
}
}
.fixedSize(horizontal: false, vertical: true) // full natural height (don't compress to the viewport)
.background(GeometryReader { g in // measure that height for the crawl mapping
Color.clear
.onAppear { contentHeight = g.size.height }
.onChange(of: g.size.height) { _, h in contentHeight = h }
})
.offset(y: crawlOffset(progress: app.crawlFraction, contentH: contentHeight, viewportH: subtitleViewportHeight))
.animation(.linear(duration: 0.12), value: app.crawlFraction) // smooth between 0.1s ticks
.frame(height: subtitleViewportHeight, alignment: .top) // fixed viewport the text scrolls through
.clipped()
.mask(crawlFadeMask)
}
// Transport + progress are one fixed-size, centered cluster, so the
// progress bar keeps a fixed width and nothing stretches as the
// subtitle/panel widen.
HStack(spacing: 16) {
Button { app.skipPrev() } label: { Image(systemName: "backward.fill") }
.disabled(!app.canSkipPrev)
Button { app.togglePause() } label: {
Image(systemName: app.phase == .paused ? "play.fill" : "pause.fill")
.font(.system(size: 18, weight: .semibold)).frame(width: 22)
}
.disabled(app.phase == .synthesizing)
.help(app.phase == .paused ? "재개" : "일시정지")
Button { app.skipNext() } label: { Image(systemName: "forward.fill") }
.disabled(!app.canSkipNext)
ProgressView(value: telemetry.progress).progressViewStyle(.linear)
.frame(width: 140)
Button { app.stop() } label: { Image(systemName: "stop.fill") }
.foregroundStyle(.secondary).help("중지")
}
.buttonStyle(.plain)
.font(.system(size: 14, weight: .semibold))
.anchorPreference(key: OverlayControlBoundsKey.self, value: .bounds) { [$0] }
.frame(maxWidth: .infinity) // keep whitespace outside the controls draggable
}
.padding(.horizontal, 16).padding(.vertical, 12)
.frame(width: hudWidth)
.background(.ultraThinMaterial, in: RoundedRectangle(cornerRadius: 14))
.overlay(RoundedRectangle(cornerRadius: 14).strokeBorder(.white.opacity(0.08)))
.overlayPreferenceValue(OverlayControlBoundsKey.self) { anchors in
GeometryReader { geometry in
OverlayDragHandle(excludedRects: anchors.map { geometry[$0] })
}
}
}
}