hop
Hop: a small native macOS launcher. Clipboard history, find, on-device dictation and your coding agent behind one key.
Brought by drakestapleton
hop / Sources / DictationHUD.swift
| 1 | import AppKit |
| 2 | import Combine |
| 3 | import SwiftUI |
| 4 | |
| 5 | /// A small pill at the bottom of the screen while Hop is listening. It never takes focus or clicks. |
| 6 | @MainActor |
| 7 | final class DictationHUD { |
| 8 | private let panel: NSPanel |
| 9 | private var watcher: AnyCancellable? |
| 10 | |
| 11 | init(_ d: Dictation) { |
| 12 | panel = NSPanel(contentRect: NSRect(x: 0, y: 0, width: 520, height: 64), styleMask: [.borderless, .nonactivatingPanel], |
| 13 | backing: .buffered, defer: false) |
| 14 | panel.isOpaque = false; panel.backgroundColor = .clear; panel.hasShadow = false |
| 15 | panel.level = .statusBar; panel.ignoresMouseEvents = true; panel.hidesOnDeactivate = false |
| 16 | panel.collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary, .stationary] |
| 17 | panel.contentView = NSHostingView(rootView: HUDView(d: d)) |
| 18 | watcher = d.$phase.receive(on: RunLoop.main).sink { [weak self] phase in |
| 19 | guard let self else { return } |
| 20 | if phase == .idle { self.panel.orderOut(nil); return } |
| 21 | if let screen = NSScreen.screens.first(where: { $0.frame.contains(NSEvent.mouseLocation) }) ?? NSScreen.main { |
| 22 | let v = screen.visibleFrame |
| 23 | self.panel.setFrameOrigin(NSPoint(x: v.midX - self.panel.frame.width / 2, y: v.minY + 28)) |
| 24 | } |
| 25 | self.panel.orderFrontRegardless() |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | struct HUDView: View { |
| 31 | @ObservedObject var d: Dictation |
| 32 | private var label: String { |
| 33 | switch d.phase { |
| 34 | case .idle: return "" |
| 35 | case .listening: |
| 36 | let tail = d.partial.count > 54 ? "…" + d.partial.suffix(54) : d.partial |
| 37 | if !tail.isEmpty { return String(tail) } |
| 38 | switch d.mode { |
| 39 | case .hold: return "Listening…" |
| 40 | case .handsFree: return "Listening hands-free. Fn to finish, Esc to cancel" |
| 41 | case .command: return "Say what to do…" |
| 42 | } |
| 43 | case .finishing: return d.partial.isEmpty ? "Writing…" : String(d.partial.suffix(54)) |
| 44 | case .working(let s): return s |
| 45 | case .notice(let s): return s |
| 46 | } |
| 47 | } |
| 48 | private var tint: Color { |
| 49 | switch d.phase { |
| 50 | case .notice: return .orange |
| 51 | case .listening: return d.mode == .command ? .purple : Color(red: 0.39, green: 0.70, blue: 0.93) |
| 52 | default: return .secondary |
| 53 | } |
| 54 | } |
| 55 | var body: some View { |
| 56 | HStack(spacing: 10) { |
| 57 | if d.phase == .listening { |
| 58 | HStack(spacing: 3) { |
| 59 | ForEach(0..<5, id: \.self) { i in |
| 60 | Capsule().fill(tint) |
| 61 | .frame(width: 3, height: 6 + CGFloat(d.level) * [14, 22, 28, 20, 12][i]) |
| 62 | } |
| 63 | }.frame(width: 28, height: 34).animation(.easeOut(duration: 0.08), value: d.level) |
| 64 | } else { |
| 65 | Image(systemName: { if case .notice = d.phase { return "exclamationmark.circle" }; return "ellipsis" }()).foregroundStyle(tint) |
| 66 | } |
| 67 | Text(label).font(.system(size: 13, weight: .medium)).foregroundStyle(.white).lineLimit(1).truncationMode(.head) |
| 68 | } |
| 69 | .padding(.horizontal, 16).frame(height: 40) |
| 70 | .background(Color(red: 0.039, green: 0.039, blue: 0.059).opacity(0.94), in: Capsule()) |
| 71 | .overlay(Capsule().stroke(Color.white.opacity(0.10), lineWidth: 1)) |
| 72 | .frame(maxWidth: .infinity, maxHeight: .infinity) |
| 73 | } |
| 74 | } |