Jotley / app / macOS / Views / RecordingWidgetView.swift
| 1 | import SwiftUI |
| 2 | |
| 3 | @MainActor |
| 4 | final class RecordingWidgetViewModel: ObservableObject { |
| 5 | @Published private(set) var elapsedTime: TimeInterval = 0 |
| 6 | @Published private(set) var audioLevel: Float = 0 |
| 7 | @Published private(set) var isRecording: Bool = false |
| 8 | @Published private(set) var appIcon: NSImage? |
| 9 | |
| 10 | private weak var recorder: ProcessTapRecorder? |
| 11 | private var timer: Timer? |
| 12 | |
| 13 | init(recorder: ProcessTapRecorder) { |
| 14 | self.recorder = recorder |
| 15 | } |
| 16 | |
| 17 | func start() { |
| 18 | stop() |
| 19 | syncFromRecorder() |
| 20 | timer = Timer.scheduledTimer(withTimeInterval: 1.0 / 30.0, repeats: true) { [weak self] _ in |
| 21 | self?.syncFromRecorder() |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | func stop() { |
| 26 | timer?.invalidate() |
| 27 | timer = nil |
| 28 | } |
| 29 | |
| 30 | func stopRecording() { |
| 31 | guard let recorder else { return } |
| 32 | Task { @MainActor in |
| 33 | if let vm = (JotleyAppDelegate.shared?.recordingNotesViewModel ?? JotleyAppDelegate.shared?.activeNotesViewModel) { await vm.stopRecording() } |
| 34 | else { _ = await recorder.stop() } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | private func syncFromRecorder() { |
| 39 | guard let recorder else { return } |
| 40 | elapsedTime = recorder.elapsedTime |
| 41 | audioLevel = recorder.currentAudioLevel |
| 42 | isRecording = recorder.isRecording |
| 43 | appIcon = recorder.icon |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | struct RecordingWidgetView: View { |
| 48 | @ObservedObject var viewModel: RecordingWidgetViewModel |
| 49 | var onWidgetTapped: (() -> Void)? |
| 50 | |
| 51 | var body: some View { |
| 52 | HStack(spacing: 10) { |
| 53 | RecordingIndicator( |
| 54 | appIcon: viewModel.appIcon, |
| 55 | isRecording: viewModel.isRecording, |
| 56 | audioLevel: viewModel.audioLevel, |
| 57 | size: 22 |
| 58 | ) |
| 59 | .frame(width: 22, height: 22) |
| 60 | |
| 61 | Rectangle() |
| 62 | .fill(Color.themeAccent.opacity(0.35)) |
| 63 | .frame(width: 1, height: 18) |
| 64 | |
| 65 | Text(formatTimeInterval(viewModel.elapsedTime)) |
| 66 | .font(.system(size: 13, weight: .medium).monospacedDigit()) |
| 67 | .foregroundColor(Color.themeTextPrimary) |
| 68 | .lineLimit(1) |
| 69 | .frame(width: 50, alignment: .leading) |
| 70 | |
| 71 | Spacer(minLength: 0) |
| 72 | |
| 73 | Button(action: viewModel.stopRecording) { |
| 74 | HStack(spacing: 6) { |
| 75 | Image(systemName: "stop.fill") |
| 76 | .font(.system(size: 12, weight: .medium)) |
| 77 | |
| 78 | Text("Stop") |
| 79 | .font(.system(size: 13, weight: .semibold)) |
| 80 | .lineLimit(1) |
| 81 | .fixedSize(horizontal: true, vertical: false) |
| 82 | } |
| 83 | .foregroundStyle(Color.themePrimaryAccent) |
| 84 | } |
| 85 | .buttonStyle(JotleyGlassButtonStyle()) |
| 86 | .layoutPriority(1) |
| 87 | } |
| 88 | .padding(.horizontal, 12) |
| 89 | .padding(.vertical, 8) |
| 90 | .frame(width: 220, height: 52) |
| 91 | .background( |
| 92 | Capsule(style: .continuous) |
| 93 | .fill(Color.themeContentBackground.opacity(0.92)) |
| 94 | ) |
| 95 | .overlay( |
| 96 | Capsule(style: .continuous) |
| 97 | .stroke(Color.themeAccent.opacity(0.25), lineWidth: 1) |
| 98 | ) |
| 99 | .contentShape(Capsule(style: .continuous)) |
| 100 | .onTapGesture { |
| 101 | onWidgetTapped?() |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | private func formatTimeInterval(_ interval: TimeInterval) -> String { |
| 106 | let totalSeconds = Int(interval) |
| 107 | let minutes = totalSeconds / 60 |
| 108 | let seconds = totalSeconds % 60 |
| 109 | return "\(minutes)m \(seconds)s" |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | struct RecordingWidgetView_Previews: PreviewProvider { |
| 114 | static var previews: some View { |
| 115 | let tap = ProcessTap(muteWhenRunning: true) |
| 116 | let recorder = ProcessTapRecorder() |
| 117 | let viewModel = RecordingWidgetViewModel(recorder: recorder) |
| 118 | return RecordingWidgetView(viewModel: viewModel) |
| 119 | .padding() |
| 120 | .background(Color.gray.opacity(0.2)) |
| 121 | } |
| 122 | } |