Voxley / app / Sources / Voxley / RecorderPanel.swift
| 1 | import AppKit |
| 2 | import SwiftUI |
| 3 | |
| 4 | @MainActor |
| 5 | final class RecorderPanel { |
| 6 | private var panel: NonactivatingRecorder? |
| 7 | private let size = NSSize(width: 386, height: 141) |
| 8 | |
| 9 | func connect(_ controller: DictationController) { |
| 10 | withObservationTracking { |
| 11 | if controller.phase != .idle { |
| 12 | show(controller) |
| 13 | } else { |
| 14 | panel?.orderOut(nil) |
| 15 | } |
| 16 | } onChange: { [weak self, weak controller] in |
| 17 | Task { @MainActor in |
| 18 | if let controller { self?.connect(controller) } |
| 19 | } |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | private func show(_ controller: DictationController) { |
| 24 | let panel = panel ?? makePanel(controller) |
| 25 | guard !panel.isVisible else { return } |
| 26 | let anchor = controller.recordingWindow |
| 27 | let referencePoint = anchor.map { NSPoint(x: $0.midX, y: $0.midY) } ?? NSEvent.mouseLocation |
| 28 | guard let screen = NSScreen.screens.first(where: { $0.frame.contains(referencePoint) }) ?? NSScreen.main |
| 29 | else { return } |
| 30 | |
| 31 | let visible = screen.visibleFrame |
| 32 | let width = min(size.width, visible.width - 24) |
| 33 | let left = visible.midX - width / 2 |
| 34 | let bottom = visible.minY + 24 |
| 35 | panel.setFrame(NSRect(x: left, y: bottom, width: width, height: size.height), display: true) |
| 36 | panel.orderFrontRegardless() |
| 37 | } |
| 38 | |
| 39 | private func makePanel(_ controller: DictationController) -> NonactivatingRecorder { |
| 40 | let panel = NonactivatingRecorder( |
| 41 | contentRect: NSRect(origin: .zero, size: size), |
| 42 | styleMask: [.borderless, .nonactivatingPanel], |
| 43 | backing: .buffered, |
| 44 | defer: false |
| 45 | ) |
| 46 | panel.title = "Voxley Recorder" |
| 47 | panel.level = .floating |
| 48 | panel.collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary, .ignoresCycle] |
| 49 | panel.backgroundColor = .clear |
| 50 | panel.isOpaque = false |
| 51 | panel.hasShadow = true |
| 52 | panel.hidesOnDeactivate = false |
| 53 | panel.isExcludedFromWindowsMenu = true |
| 54 | panel.contentView = RecorderHostingView(rootView: RecorderView(controller: controller)) |
| 55 | self.panel = panel |
| 56 | return panel |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | private final class NonactivatingRecorder: NSPanel { |
| 61 | override var canBecomeKey: Bool { false } |
| 62 | override var canBecomeMain: Bool { false } |
| 63 | } |
| 64 | |
| 65 | private final class RecorderHostingView: NSHostingView<RecorderView> { |
| 66 | override func acceptsFirstMouse(for event: NSEvent?) -> Bool { true } |
| 67 | } |
| 68 | |
| 69 | private struct RecorderView: View { |
| 70 | let controller: DictationController |
| 71 | private var recording: Bool { controller.phase == .recording } |
| 72 | private var refining: Bool { controller.captureKind == .refine } |
| 73 | |
| 74 | var body: some View { |
| 75 | VStack(alignment: .leading, spacing: 7) { |
| 76 | RecorderTranscript(text: controller.liveText, status: controller.statusTitle) |
| 77 | |
| 78 | HStack(spacing: 10) { |
| 79 | if recording { |
| 80 | Image(systemName: refining ? "sparkles" : "mic.fill") |
| 81 | .foregroundStyle(refining ? Color.voxleyAccent : .white) |
| 82 | .accessibilityLabel(refining ? "Recording for Refine" : "Microphone recording") |
| 83 | RecorderWave(frame: controller.waveform) |
| 84 | .frame(maxWidth: 220) |
| 85 | .frame(height: 60) |
| 86 | .mask { |
| 87 | LinearGradient(stops: [ |
| 88 | .init(color: .clear, location: 0), |
| 89 | .init(color: .black, location: 0.5), |
| 90 | .init(color: .clear, location: 1), |
| 91 | ], startPoint: .leading, endPoint: .trailing) |
| 92 | } |
| 93 | .padding(.horizontal, 8) |
| 94 | .frame(maxWidth: .infinity) |
| 95 | .allowsHitTesting(false) |
| 96 | .accessibilityHidden(true) |
| 97 | Button { |
| 98 | controller.stopRecording() |
| 99 | } label: { |
| 100 | Image(systemName: "stop.fill") |
| 101 | .frame(width: 18, height: 16) |
| 102 | } |
| 103 | .buttonStyle(.bordered) |
| 104 | .controlSize(.small) |
| 105 | .accessibilityLabel("Stop recording") |
| 106 | .help(refining ? "Stop recording and open Refine" : "Stop recording and paste the transcript") |
| 107 | } else { |
| 108 | ProgressView() |
| 109 | .controlSize(.small) |
| 110 | .accessibilityHidden(true) |
| 111 | Spacer() |
| 112 | } |
| 113 | } |
| 114 | .frame(height: 62) |
| 115 | } |
| 116 | .padding(.horizontal, 14) |
| 117 | .frame(maxWidth: .infinity, maxHeight: .infinity) |
| 118 | .background { |
| 119 | RoundedRectangle(cornerRadius: 17) |
| 120 | .fill(.black) |
| 121 | } |
| 122 | .overlay { |
| 123 | RoundedRectangle(cornerRadius: 17) |
| 124 | .strokeBorder(refining ? AnyShapeStyle(Color.voxleyAccent.opacity(0.7)) : AnyShapeStyle(.quaternary), |
| 125 | lineWidth: 1) |
| 126 | .allowsHitTesting(false) |
| 127 | } |
| 128 | .padding(2) |
| 129 | .environment(\.colorScheme, .dark) |
| 130 | } |
| 131 | } |