Jotley / app / macOS / Views / ExpandedTranscriptionView.swift
| 1 | import SwiftUI |
| 2 | import AppKit |
| 3 | import Combine |
| 4 | |
| 5 | /// Window-local controls stay available while the user browses any note. |
| 6 | struct MacCompactTranscriptionBar: View { |
| 7 | @ObservedObject var viewModel: NotesListViewModel |
| 8 | var body: some View { |
| 9 | MacTranscriptionControls(timerText: timerText, isPaused: viewModel.isTranscriptionPaused, |
| 10 | isBusy: viewModel.operationInProgress, recorder: viewModel.recorder, |
| 11 | onPause: { Task { await viewModel.resumeTranscription() } }, |
| 12 | onStop: { Task { await viewModel.stopRecording() } }) |
| 13 | .accessibilityIdentifier("macCompactTranscriptionBar") |
| 14 | } |
| 15 | private var timerText: String { |
| 16 | let seconds = Int(viewModel.recorder.elapsedTime) |
| 17 | return String(format: "%02d:%02d", seconds / 60, seconds % 60) |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | struct MacTranscriptionControls: View { |
| 22 | let timerText: String |
| 23 | let isPaused: Bool |
| 24 | var isBusy = false |
| 25 | var recorder: ProcessTapRecorder? = nil |
| 26 | let onPause: () -> Void |
| 27 | let onStop: () -> Void |
| 28 | var body: some View { |
| 29 | HStack(spacing: 10) { |
| 30 | HStack(spacing: 8) { |
| 31 | if let recorder { |
| 32 | MacCompactTranscriptionIndicator(recorder: recorder, isPaused: isPaused) |
| 33 | .frame(width: 22, height: 24).accessibilityLabel("Live audio waveform") |
| 34 | } else { |
| 35 | RecordingIndicator(appIcon: nil, isRecording: true, audioLevel: 0.1, size: 22, isPaused: isPaused) |
| 36 | .frame(width: 22, height: 24) |
| 37 | } |
| 38 | Text(timerText).font(.system(size: 13, weight: .medium).monospacedDigit()) |
| 39 | .foregroundStyle(Color.themeTextPrimary).fixedSize() |
| 40 | } |
| 41 | .padding(.horizontal, 12) |
| 42 | .frame(height: 36) |
| 43 | .background { JotleyFloatingGlassSurface(shape: Capsule()) } |
| 44 | Button(action: onPause) { |
| 45 | Image(systemName: isPaused ? "play.fill" : "pause.fill") |
| 46 | }.buttonStyle(JotleyGlassButtonStyle(circular: true)).accessibilityLabel(isPaused ? "Continue Transcription" : "Pause Transcription") |
| 47 | .accessibilityIdentifier("macLiveTranscriptionPauseButton") |
| 48 | Button(action: onStop) { |
| 49 | Label { |
| 50 | Text("Stop").modifier(JotleyNoteFooterTextStyle()) |
| 51 | } icon: { |
| 52 | Image(systemName: "stop.fill") |
| 53 | } |
| 54 | .font(.system(size: 13, weight: .semibold)) |
| 55 | }.buttonStyle(JotleyGlassButtonStyle()).accessibilityLabel("Stop Transcription") |
| 56 | .accessibilityIdentifier("macLiveTranscriptionStopButton") |
| 57 | }.disabled(isBusy).fixedSize(horizontal: true, vertical: false) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /// A user scroll suspends following until they return to the transcript's end. |
| 62 | struct MacTranscriptFollowState { |
| 63 | private(set) var isFollowing = true |
| 64 | private(set) var isUserScrolling = false |
| 65 | private(set) var isAtBottom = true |
| 66 | var shouldFollow: Bool { isFollowing && !isUserScrolling } |
| 67 | mutating func scrollPhaseChanged(isUserScrolling: Bool) { |
| 68 | if isUserScrolling { |
| 69 | self.isUserScrolling = true |
| 70 | isFollowing = false |
| 71 | } else if self.isUserScrolling { |
| 72 | self.isUserScrolling = false |
| 73 | isFollowing = isAtBottom |
| 74 | } |
| 75 | } |
| 76 | mutating func bottomChanged(_ atBottom: Bool) { |
| 77 | isAtBottom = atBottom |
| 78 | if !isUserScrolling && atBottom { isFollowing = true } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // Reuse the original three-bar indicator. Meter ticks stay local to this small |
| 83 | // view, rather than invalidating the note list and transcript on every sample. |
| 84 | private struct MacCompactTranscriptionIndicator: View { |
| 85 | let recorder: ProcessTapRecorder |
| 86 | let isPaused: Bool |
| 87 | @State private var level: Float = 0 |
| 88 | @State private var meter = Timer.publish(every: 1.0 / 30.0, on: .main, in: .common).autoconnect() |
| 89 | var body: some View { |
| 90 | RecordingIndicator(appIcon: recorder.icon, isRecording: true, |
| 91 | audioLevel: isPaused ? 0 : level, size: 22, isPaused: isPaused) |
| 92 | .onReceive(meter) { _ in level = recorder.currentAudioLevel } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /// Align the idle and live controls with the sidebar's footer. |
| 97 | struct MacNoteFooterPlacement: ViewModifier { |
| 98 | static let edgeInset: CGFloat = 20 |
| 99 | |
| 100 | func body(content: Content) -> some View { |
| 101 | content.frame(minHeight: 52, alignment: .bottom) |
| 102 | .frame(maxWidth: .infinity, alignment: .center) |
| 103 | .padding([.horizontal, .bottom], Self.edgeInset) |
| 104 | } |
| 105 | } |