forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLiveTranscriptView.swift
More file actions
492 lines (447 loc) · 16.3 KB
/
Copy pathLiveTranscriptView.swift
File metadata and controls
492 lines (447 loc) · 16.3 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
import OmiTheme
import SwiftUI
/// Self-contained panel that observes LiveTranscriptMonitor internally,
/// so the parent view does NOT need to observe transcript changes.
struct LiveTranscriptPanel: View {
@ObservedObject private var monitor = LiveTranscriptMonitor.shared
var speakerNames: [Int: String] = [:]
var onSpeakerTapped: ((SpeakerSegment) -> Void)? = nil
private var displaySegments: [SpeakerSegment] {
if !monitor.segments.isEmpty { return monitor.segments }
return monitor.savedSegments
}
var body: some View {
if displaySegments.isEmpty {
VStack(spacing: OmiSpacing.lg) {
Image(systemName: "waveform")
.scaledFont(size: 48)
.foregroundColor(Ink.secondary)
.opacity(0.5)
Text("Live Transcript")
.scaledFont(size: OmiType.subheading, weight: .medium)
.foregroundColor(Ink.secondary)
Text("Start speaking and your transcript will appear here")
.scaledFont(size: OmiType.body)
.foregroundColor(Ink.secondary)
.multilineTextAlignment(.center)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.padding(OmiSpacing.section)
} else {
LiveTranscriptView(
segments: displaySegments,
speakerNames: speakerNames,
onSpeakerTapped: onSpeakerTapped
)
}
}
}
/// Self-contained audio level waveforms that observe AudioLevelMonitor internally,
/// so the parent view does NOT need to observe audio level changes.
struct RecordingBarAudioLevels: View {
@ObservedObject private var monitor = AudioLevelMonitor.shared
var body: some View {
HStack(spacing: OmiSpacing.lg) {
HStack(spacing: OmiSpacing.xs) {
Image(systemName: "mic.fill")
.scaledFont(size: OmiType.caption)
.foregroundColor(Ink.secondary)
AudioLevelWaveformView(
level: monitor.microphoneLevel,
barCount: 8,
isActive: true
)
}
HStack(spacing: OmiSpacing.xs) {
Image(systemName: "speaker.wave.2.fill")
.scaledFont(size: OmiType.caption)
.foregroundColor(Ink.secondary)
AudioLevelWaveformView(
level: monitor.systemLevel,
barCount: 8,
isActive: true
)
}
}
.fixedSize() // Prevent constraint invalidations from propagating to parent NSHostingView
}
}
/// Self-contained recording duration text that observes RecordingTimer internally.
struct RecordingBarDuration: View {
@ObservedObject private var timer = RecordingTimer.shared
var body: some View {
Text(timer.formattedDuration)
.scaledFont(size: OmiType.body, weight: .medium, design: .monospaced)
.foregroundColor(Ink.secondary)
}
}
/// Small view for the recording bar that observes LiveTranscriptMonitor
/// without forcing the parent to re-render.
struct RecordingBarTranscriptText: View {
@ObservedObject private var monitor = LiveTranscriptMonitor.shared
var body: some View {
if let latestText = monitor.latestText, !monitor.isEmpty {
Text(latestText)
.scaledFont(size: OmiType.body)
.foregroundColor(Ink.secondary)
.lineLimit(1)
.truncationMode(.head)
.frame(maxWidth: 260, alignment: .leading)
} else {
Text("Listening")
.scaledFont(size: OmiType.body, weight: .medium)
.foregroundColor(Ink.primary)
}
}
}
/// Live transcript card shown at the top of Conversations while recording —
/// updates in real time as speech is transcribed. Observes the monitor directly
/// so the surrounding page doesn't re-render on every segment.
///
/// Clicking anywhere on the card invokes `onExpand`, letting the parent present
/// a full-screen view of the live transcript.
struct ConversationsLiveTranscript: View {
@ObservedObject private var monitor = LiveTranscriptMonitor.shared
/// Invoked when the user clicks the card. When non-nil, the card shows an
/// expand affordance and the whole surface becomes tappable.
var onExpand: (() -> Void)? = nil
@State private var isHovered = false
var body: some View {
VStack(alignment: .leading, spacing: OmiSpacing.sm) {
HStack(spacing: OmiSpacing.xs) {
Circle().fill(Ink.errorRed).frame(width: 7, height: 7)
Text("Live").scaledFont(size: OmiType.caption, weight: .semibold)
.foregroundColor(Ink.secondary)
Spacer()
if onExpand != nil {
Image(systemName: "arrow.up.left.and.arrow.down.right")
.scaledFont(size: OmiType.caption)
.foregroundColor(isHovered ? Ink.primary : Ink.secondary)
}
}
if monitor.isEmpty {
Text("Listening…").scaledFont(size: OmiType.body).foregroundColor(Ink.secondary)
.padding(.vertical, OmiSpacing.sm)
} else {
LiveTranscriptView(segments: monitor.segments)
.frame(maxHeight: 220)
// Let clicks fall through to the card's expand tap rather than being
// captured by the inner scroll / text selection.
.allowsHitTesting(false)
}
}
.padding(OmiSpacing.lg)
.background(
RoundedRectangle(cornerRadius: OmiChrome.cardRadius, style: .continuous)
.fill(Ink.rowFill)
.overlay(
RoundedRectangle(cornerRadius: OmiChrome.cardRadius, style: .continuous)
.stroke(
Ink.separator.opacity(onExpand != nil && isHovered ? 0.55 : 0.3), lineWidth: 1))
)
.contentShape(RoundedRectangle(cornerRadius: OmiChrome.cardRadius, style: .continuous))
.modifier(LiveTranscriptExpandTap(onExpand: onExpand, isHovered: $isHovered))
}
}
/// Replaces the Live card between "capture stopped" and "row in the list".
/// Same slot, same shape, same last line of transcript — the capture the
/// user was watching is what is being saved.
struct ConversationsSavingCaptureCard: View {
@ObservedObject private var monitor = LiveTranscriptMonitor.shared
@State private var pulse = false
private var lastLine: String? {
monitor.savedSegments.last?.text ?? monitor.latestText
}
var body: some View {
VStack(alignment: .leading, spacing: OmiSpacing.sm) {
HStack(spacing: OmiSpacing.xs) {
Circle()
.fill(Ink.accent)
.frame(width: 7, height: 7)
.opacity(pulse ? 0.4 : 1.0)
.animation(.easeInOut(duration: 0.9).repeatForever(autoreverses: true), value: pulse)
Text("Saving").scaledFont(size: OmiType.caption, weight: .semibold)
.foregroundColor(Ink.secondary)
Spacer()
}
if let lastLine, !lastLine.isEmpty {
Text(lastLine)
.scaledFont(size: OmiType.body)
.foregroundColor(Ink.secondary)
.lineLimit(2)
.padding(.vertical, OmiSpacing.sm)
} else {
Text("Adding to your conversations…").scaledFont(size: OmiType.body).foregroundColor(Ink.secondary)
.padding(.vertical, OmiSpacing.sm)
}
}
.padding(OmiSpacing.lg)
.background(
RoundedRectangle(cornerRadius: OmiChrome.cardRadius, style: .continuous)
.fill(Ink.rowFill)
.overlay(
RoundedRectangle(cornerRadius: OmiChrome.cardRadius, style: .continuous)
.stroke(Ink.separator.opacity(0.3), lineWidth: 1))
)
.onAppear { pulse = true }
.accessibilityIdentifier("conversations-saving-capture")
}
}
/// Adds the click-to-expand behavior only when an `onExpand` handler is present,
/// so the card stays inert (no pointer cursor, no tap) when expansion is
/// unavailable.
private struct LiveTranscriptExpandTap: ViewModifier {
let onExpand: (() -> Void)?
@Binding var isHovered: Bool
/// Tracks whether we currently hold a pushed cursor so push/pop stay balanced
/// and the pointing-hand cursor is always popped when the card stops being
/// hovered — SwiftUI doesn't deliver an `onHover(false)` when the view leaves
/// the hierarchy. Two exit paths are handled explicitly: `onDisappear`
/// (capture pauses and the `if appState.isLiveCapturing` card is removed) and
/// the tap handler (expanding replaces the page, which also unmounts the card;
/// popping here keeps the cursor from lingering for a frame).
@State private var didPushCursor = false
private func setHovered(_ hovering: Bool) {
isHovered = hovering
if hovering, !didPushCursor {
NSCursor.pointingHand.push()
didPushCursor = true
} else if !hovering, didPushCursor {
NSCursor.pop()
didPushCursor = false
}
}
func body(content: Content) -> some View {
if let onExpand {
content
.onTapGesture {
// Pop the pointing-hand before the page swaps away the card.
setHovered(false)
onExpand()
}
.onHover { setHovered($0) }
.onDisappear { setHovered(false) }
.help("Expand live transcript")
} else {
content
}
}
}
/// Full-panel live transcript, presented in place of the Conversations list when
/// the user expands the compact live card. Observes the monitor directly so it
/// keeps updating live. Hosted on the same glass panel — paints no ground of its own.
struct ConversationsLiveTranscriptFullScreen: View {
@ObservedObject private var monitor = LiveTranscriptMonitor.shared
var onCollapse: () -> Void
var body: some View {
VStack(alignment: .leading, spacing: 0) {
HStack(spacing: OmiSpacing.sm) {
Circle().fill(Ink.errorRed).frame(width: 8, height: 8)
Text("Live Transcript")
.scaledFont(size: OmiType.subheading, weight: .semibold)
.foregroundColor(Ink.primary)
Spacer()
Button(action: onCollapse) {
Image(systemName: "arrow.down.right.and.arrow.up.left")
.scaledFont(size: OmiType.body)
.foregroundColor(Ink.secondary)
.padding(OmiSpacing.sm)
.glassChip()
}
.buttonStyle(.plain)
.keyboardShortcut(.escape, modifiers: [])
.help("Collapse")
}
.padding(.horizontal, OmiSpacing.xxl)
.padding(.top, OmiSpacing.lg)
.padding(.bottom, OmiSpacing.md)
Divider().overlay(Ink.separator.opacity(0.3))
if monitor.isEmpty {
VStack(spacing: OmiSpacing.md) {
Image(systemName: "waveform")
.scaledFont(size: 48)
.foregroundColor(Ink.secondary)
.opacity(0.5)
Text("Listening…")
.scaledFont(size: OmiType.body)
.foregroundColor(Ink.secondary)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
} else {
LiveTranscriptView(segments: monitor.segments)
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
/// Live transcript view showing speaker segments during recording
struct LiveTranscriptView: View {
let segments: [SpeakerSegment]
var speakerNames: [Int: String] = [:]
var onSpeakerTapped: ((SpeakerSegment) -> Void)? = nil
/// Format timestamp as MM:SS
private func formatTime(_ seconds: Double) -> String {
let totalSeconds = Int(seconds)
let minutes = totalSeconds / 60
let secs = totalSeconds % 60
return String(format: "%d:%02d", minutes, secs)
}
/// A lightweight fingerprint of the segments to detect any content change
private var scrollTrigger: String {
guard let last = segments.last else { return "" }
return "\(segments.count)-\(last.id)-\(last.text.count)"
}
var body: some View {
ScrollViewReader { proxy in
ScrollView {
VStack(alignment: .leading, spacing: OmiSpacing.md) {
ForEach(segments) { segment in
LiveSegmentView(
segment: segment,
formatTime: formatTime,
personName: speakerNames[segment.speaker],
onSpeakerTapped: !segment.isUser ? { onSpeakerTapped?(segment) } : nil
)
}
// Stable bottom anchor that never changes ID
Color.clear
.frame(height: 1)
.id("transcript-bottom")
}
.padding(OmiSpacing.lg)
}
.defaultScrollAnchor(.bottom)
.onChange(of: scrollTrigger) { _, _ in
proxy.scrollTo("transcript-bottom", anchor: .bottom)
}
}
}
}
/// Individual segment view for live transcript
private struct LiveSegmentView: View {
let segment: SpeakerSegment
let formatTime: (Double) -> String
var personName: String? = nil
var onSpeakerTapped: (() -> Void)? = nil
@State private var isHovered = false
private var isUser: Bool {
segment.isUser
}
private var speakerLabel: String {
if isUser { return "You" }
if let name = personName { return name }
return "Speaker \(segment.speaker)"
}
private var bubbleColor: Color {
isUser ? PageGlass.speakerTints[0] : Ink.rowFillHover
}
var body: some View {
HStack(alignment: .top, spacing: 0) {
if !isUser {
// Other speakers - left aligned
segmentContent
Spacer(minLength: 60)
} else {
// User - right aligned
Spacer(minLength: 60)
segmentContent
}
}
}
private var segmentContent: some View {
VStack(alignment: isUser ? .trailing : .leading, spacing: OmiSpacing.xxs) {
// Speaker label and timestamp
HStack(spacing: OmiSpacing.sm) {
if !isUser {
speakerAvatar
}
if !isUser, let onTap = onSpeakerTapped {
Button(action: onTap) {
HStack(spacing: OmiSpacing.xxs) {
Text(speakerLabel)
.scaledFont(size: OmiType.caption, weight: personName != nil ? .semibold : .medium)
.foregroundColor(personName != nil ? Ink.primary : Ink.secondary)
if personName == nil {
Image(systemName: "pencil")
.scaledFont(size: OmiType.micro)
.foregroundColor(Ink.secondary)
.opacity(isHovered ? 1 : 0)
}
}
}
.buttonStyle(.plain)
.onHover { hovering in
isHovered = hovering
if hovering {
NSCursor.pointingHand.push()
} else {
NSCursor.pop()
}
}
} else {
Text(speakerLabel)
.scaledFont(size: OmiType.caption, weight: .medium)
.foregroundColor(Ink.secondary)
}
Text(formatTime(segment.start))
.scaledFont(size: OmiType.caption)
.foregroundColor(Ink.secondary)
if isUser {
speakerAvatar
}
}
// Message bubble
Text(segment.text)
.scaledFont(size: OmiType.body)
.foregroundColor(Ink.primary)
.textSelection(.enabled)
.padding(.horizontal, OmiSpacing.md)
.padding(.vertical, OmiSpacing.sm)
.background(
RoundedRectangle(cornerRadius: OmiChrome.controlRadius)
.fill(bubbleColor)
)
// Translations from backend
if !segment.translations.isEmpty {
ForEach(segment.translations, id: \.lang) { translation in
Text(translation.text)
.scaledFont(size: OmiType.body)
.foregroundColor(Ink.secondary)
.italic()
.textSelection(.enabled)
.padding(.horizontal, OmiSpacing.md)
.padding(.vertical, OmiSpacing.xs)
.background(
RoundedRectangle(cornerRadius: OmiChrome.chipRadius)
.fill(bubbleColor.opacity(0.5))
)
}
}
}
}
private var speakerAvatar: some View {
Circle()
.fill(isUser ? Ink.primary : Ink.rowFillHover)
.frame(width: 24, height: 24)
.overlay(
Text(isUser ? "Y" : (personName?.prefix(1).uppercased() ?? String(segment.speaker)))
.scaledFont(size: OmiType.caption, weight: .medium)
.foregroundColor(isUser ? Ink.surface : Ink.primary)
)
}
}
#if canImport(PreviewsMacros)
#Preview {
LiveTranscriptView(segments: [
SpeakerSegment(speaker: 0, text: "Hello, how are you doing today?", start: 0.0, end: 2.5),
SpeakerSegment(speaker: 1, text: "I'm doing great, thanks for asking!", start: 3.0, end: 5.5),
SpeakerSegment(
speaker: 0, text: "That's wonderful to hear. Let me tell you about what we're working on.", start: 6.0,
end: 10.0),
SpeakerSegment(speaker: 1, text: "Sure, I'd love to hear more about it.", start: 10.5, end: 12.0),
])
.frame(width: 400, height: 300)
.background(Ink.rowFill)
}
#endif