Skip to content

Commit ca50726

Browse files
Lee Seongjooclaude
andcommitted
HUD subtitles (per-paragraph) with per-mode toggle; bottom-anchored panel
- Show the paragraph being read as a subtitle in the HUD, toggleable separately for TTS and 해설 (설정 → 음성 합성 → HUD 자막; persisted). AppState tracks the spoken paragraphs (spokenChunks) and exposes currentChunkText / showSubtitle. - Make the HUD panel resize to its content and stay anchored by its bottom edge: OverlayWindow re-lays-out on paragraph/mode/phase changes (setContentSize + bottom-pinned origin) so the subtitle growing no longer pushes the HUD off the bottom of the screen. Long-paragraph handling (auto-scroll vs fixed-height scroll vs sentence split) is still open — subtitle currently truncates at 4 lines. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 3a13fd8 commit ca50726

4 files changed

Lines changed: 79 additions & 24 deletions

File tree

‎Sources/App/AppState.swift‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@ final class AppState: ObservableObject {
155155
}
156156
@Published var playbackMode: PlaybackMode = .tts
157157

158+
// HUD subtitles: show the paragraph being read (toggle per mode).
159+
@Published var subtitleTTS = true
160+
@Published var subtitleExplain = true
161+
@Published var spokenChunks: [String] = [] // the paragraph texts of the current playback
162+
var showSubtitle: Bool { playbackMode == .tts ? subtitleTTS : subtitleExplain }
163+
var currentChunkText: String {
164+
let i = chunkIndex - 1
165+
return spokenChunks.indices.contains(i) ? spokenChunks[i] : ""
166+
}
167+
158168
@Published var currentText = "" // text being spoken (overlay label)
159169
@Published var inputText = "" // original (top panel / source text)
160170
@Published var scriptText = "" // TTS-friendly script actually sent to the engine (bottom panel)
@@ -272,6 +282,7 @@ final class AppState: ObservableObject {
272282

273283
task?.cancel(); player.stop(); stopTimer()
274284
currentText = t // overlay label (the spoken text); do NOT touch inputText
285+
spokenChunks = chunks // per-paragraph subtitles
275286
progress = 0
276287
player.start(expected: chunks.count)
277288
chunkCount = chunks.count; chunkIndex = 0
@@ -869,7 +880,7 @@ final class AppState: ObservableObject {
869880
playbackMode = .tts
870881
guard let url = cache.fileURL(forKey: e.id) else { statusText = "오디오 파일 없음"; return }
871882
cache.touch(e.id); historyRevision += 1
872-
inputText = e.text; currentText = e.text
883+
inputText = e.text; currentText = e.text; spokenChunks = [e.text]
873884
chunkCount = 1; chunkIndex = 1; progress = 0
874885
player.start(expected: 1)
875886
player.enqueue(url)
@@ -968,6 +979,7 @@ final class AppState: ObservableObject {
968979
"voiceId": voiceId, "voiceName": voiceName, "modelId": modelId,
969980
"backend": backendKind.rawValue, "useCache": useCache, "localBaseURL": localBaseURL,
970981
"maxChunkChars": maxChunkChars,
982+
"subtitleTTS": subtitleTTS, "subtitleExplain": subtitleExplain,
971983
"normalize": normalizeEnabled, "ollamaModel": ollamaModel, "ollamaURL": ollamaURL,
972984
"normalizeProvider": normalizeProvider.rawValue, "geminiModel": geminiModel,
973985
"explainProvider": explainProvider.rawValue, "scriptProvider": scriptProvider.rawValue,
@@ -1003,6 +1015,8 @@ final class AppState: ObservableObject {
10031015
useCache = o["useCache"] as? Bool ?? useCache
10041016
localBaseURL = o["localBaseURL"] as? String ?? localBaseURL
10051017
maxChunkChars = o["maxChunkChars"] as? Int ?? maxChunkChars
1018+
subtitleTTS = o["subtitleTTS"] as? Bool ?? subtitleTTS
1019+
subtitleExplain = o["subtitleExplain"] as? Bool ?? subtitleExplain
10061020
normalizeEnabled = o["normalize"] as? Bool ?? normalizeEnabled
10071021
ollamaModel = o["ollamaModel"] as? String ?? ollamaModel
10081022
ollamaURL = o["ollamaURL"] as? String ?? ollamaURL

‎Sources/UI/OverlayWindow.swift‎

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import AppKit
22
import SwiftUI
3+
import Combine
34

45
/// A panel that never becomes key or main, so showing it cannot pull keyboard
56
/// focus away from the app the user is working in. Its buttons still receive
@@ -11,41 +12,64 @@ final class NonKeyPanel: NSPanel {
1112

1213
/// A borderless, floating, non-activating HUD panel that hosts PlayerOverlay.
1314
/// Appears over whatever app the user is in while audio is synthesizing/playing,
14-
/// WITHOUT stealing focus from the source window.
15+
/// WITHOUT stealing focus. Re-sizes to its content (the subtitle grows/shrinks
16+
/// per paragraph) and stays anchored by its BOTTOM edge so it never drifts off
17+
/// the bottom of the screen.
1518
@MainActor
1619
final class OverlayWindow {
1720
private var panel: NSPanel?
21+
private var host: NSHostingView<PlayerOverlay>?
1822
private let app: AppState
23+
private var cancellables = Set<AnyCancellable>()
24+
25+
private let bottomInset: CGFloat = 60
1926

2027
init(app: AppState) { self.app = app }
2128

2229
func show() {
23-
if panel == nil {
24-
let p = NonKeyPanel(contentRect: NSRect(x: 0, y: 0, width: 340, height: 80),
25-
styleMask: [.nonactivatingPanel, .borderless],
26-
backing: .buffered, defer: false)
27-
p.level = .floating
28-
p.isFloatingPanel = true
29-
p.hidesOnDeactivate = false
30-
p.isOpaque = false
31-
p.backgroundColor = .clear
32-
p.hasShadow = true
33-
p.isMovableByWindowBackground = true
34-
p.collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary, .stationary]
35-
let host = NSHostingView(rootView: PlayerOverlay(app: app))
36-
p.contentView = host
37-
p.setContentSize(host.fittingSize)
38-
panel = p
39-
}
40-
if let p = panel { position(p); p.orderFrontRegardless() }
30+
ensurePanel()
31+
relayout()
32+
panel?.orderFrontRegardless()
4133
}
4234

4335
func hide() { panel?.orderOut(nil) }
4436

45-
private func position(_ p: NSPanel) {
46-
guard let screen = NSScreen.main else { return }
37+
private func ensurePanel() {
38+
guard panel == nil else { return }
39+
let h = NSHostingView(rootView: PlayerOverlay(app: app))
40+
let p = NonKeyPanel(contentRect: NSRect(x: 0, y: 0, width: 360, height: 80),
41+
styleMask: [.nonactivatingPanel, .borderless],
42+
backing: .buffered, defer: false)
43+
p.level = .floating
44+
p.isFloatingPanel = true
45+
p.hidesOnDeactivate = false
46+
p.isOpaque = false
47+
p.backgroundColor = .clear
48+
p.hasShadow = true
49+
p.isMovableByWindowBackground = true
50+
p.collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary, .stationary]
51+
p.contentView = h
52+
host = h
53+
panel = p
54+
55+
// Content height changes per paragraph (subtitle) / mode — re-anchor then.
56+
app.$chunkIndex
57+
.merge(with: app.$phase.map { _ in 0 }, app.$playbackMode.map { _ in 0 })
58+
.sink { [weak self] _ in
59+
DispatchQueue.main.async { self?.relayout() } // after SwiftUI re-lays-out
60+
}
61+
.store(in: &cancellables)
62+
}
63+
64+
/// Size the panel to its current content and pin the bottom edge above the
65+
/// screen's bottom (grows upward, never downward off-screen).
66+
private func relayout() {
67+
guard let p = panel, let h = host, p.isVisible || p.contentView === h else { return }
68+
let size = h.fittingSize
69+
guard size.width > 0, size.height > 0 else { return }
70+
guard let screen = NSScreen.main else { p.setContentSize(size); return }
4771
let f = screen.visibleFrame
48-
let size = p.frame.size
49-
p.setFrameOrigin(NSPoint(x: f.midX - size.width / 2, y: f.minY + 48))
72+
p.setContentSize(size)
73+
p.setFrameOrigin(NSPoint(x: f.midX - size.width / 2, y: f.minY + bottomInset))
5074
}
5175
}

‎Sources/UI/PlayerOverlay.swift‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ struct PlayerOverlay: View {
2727
}
2828
}
2929

30+
if app.showSubtitle, !app.currentChunkText.isEmpty {
31+
Text(app.currentChunkText)
32+
.font(.callout)
33+
.lineLimit(4)
34+
.fixedSize(horizontal: false, vertical: true)
35+
.frame(maxWidth: .infinity, alignment: .leading)
36+
}
37+
3038
HStack(spacing: 16) {
3139
Button { app.skipPrev() } label: { Image(systemName: "backward.fill") }
3240
.disabled(!app.canSkipPrev)

‎Sources/UI/SettingsView.swift‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ struct SettingsView: View {
3939
.onChange(of: app.ollamaModel) { _, _ in app.saveSettings() }
4040
.onChange(of: app.ollamaURL) { _, _ in app.saveSettings() }
4141
.onChange(of: app.maxChunkChars) { _, _ in app.saveSettings() }
42+
.onChange(of: app.subtitleTTS) { _, _ in app.saveSettings() }
43+
.onChange(of: app.subtitleExplain) { _, _ in app.saveSettings() }
4244
.onChange(of: app.voiceId) { _, newID in
4345
if let v = app.voices.first(where: { $0.id == newID }) { app.voiceName = v.name }
4446
app.saveSettings()
@@ -129,6 +131,13 @@ struct SettingsView: View {
129131
.font(.caption).foregroundStyle(.secondary)
130132
}
131133

134+
Section("HUD 자막") {
135+
Toggle("TTS 재생 시 자막 표시", isOn: $app.subtitleTTS)
136+
Toggle("해설 재생 시 자막 표시", isOn: $app.subtitleExplain)
137+
Text("재생 중 떠 있는 HUD에 현재 읽는 문단을 자막으로 보여줍니다.")
138+
.font(.caption).foregroundStyle(.secondary)
139+
}
140+
132141
Section("전역 단축키 (선택 → 읽기/해설)") {
133142
LabeledContent("읽어주기", value: "⌃⌥⌘R")
134143
LabeledContent("코드 해설", value: "⌃⌥⌘E")

0 commit comments

Comments
 (0)