hop
Hop: a small native macOS launcher. Clipboard history, find, on-device dictation and your coding agent behind one key.
Brought by drakestapleton
| 1 | import SwiftUI |
| 2 | import AppKit |
| 3 | import UniformTypeIdentifiers |
| 4 | |
| 5 | struct RootView: View { |
| 6 | @ObservedObject var m: AppModel |
| 7 | @FocusState private var focused: Bool |
| 8 | |
| 9 | var body: some View { |
| 10 | VStack(spacing: 0) { |
| 11 | HStack(spacing: 10) { |
| 12 | // On the one box the spirit sits where the screen's picture is on the others. |
| 13 | if m.tab == .all { SpiritOrb(showing: m.panelShown, coreRadius: 2.5, rings: 2, reach: 0.5).frame(width: 18, height: 18) } |
| 14 | else { Image(systemName: m.tab.icon).foregroundStyle(Spirit.textDim).frame(width: 18) } |
| 15 | TextField(m.placeholder, text: $m.query) |
| 16 | .textFieldStyle(.plain).font(.system(size: 20, weight: .regular)) |
| 17 | .focused($focused) |
| 18 | .onSubmit { m.submit(alt: false) } |
| 19 | .onKeyPress(.upArrow) { m.move(-1); return .handled } |
| 20 | .onKeyPress(.downArrow) { m.move(1); return .handled } |
| 21 | .onKeyPress(.return, phases: .down) { p in |
| 22 | if p.modifiers.contains(.shift) { m.fixIt(); return .handled } // ⇧↩ = fix it, anywhere |
| 23 | if p.modifiers.contains(.command) { m.submit(alt: true); return .handled } |
| 24 | return .ignored |
| 25 | } |
| 26 | .onKeyPress(phases: .down) { p in // Tab / Shift-Tab (which arrives as "backtab") walk the screens |
| 27 | guard p.key == .tab || p.key == KeyEquivalent("\u{19}") else { return .ignored } |
| 28 | m.cycleTab(p.modifiers.contains(.shift) || p.key != .tab ? -1 : 1); return .handled |
| 29 | } |
| 30 | .onKeyPress(.leftArrow) { guard m.query.isEmpty else { return .ignored }; m.cycleTab(-1); return .handled } |
| 31 | .onKeyPress(.rightArrow) { guard m.query.isEmpty else { return .ignored }; m.cycleTab(1); return .handled } |
| 32 | if m.busy { ProgressView().controlSize(.small) } |
| 33 | } |
| 34 | .padding(.horizontal, 16).padding(.vertical, 11) |
| 35 | .background(Spirit.glass(0.6, edge: Spirit.hairlineStrong)) |
| 36 | .padding(.horizontal, 12).padding(.top, 2).padding(.bottom, 8) |
| 37 | // On the one box the agent is a line in the list; the other screens keep the bar. |
| 38 | if !m.query.trimmingCharacters(in: .whitespaces).isEmpty && m.tab != .agent && m.tab != .all && !m.agentRunning { |
| 39 | FixItBar(m: m) |
| 40 | } |
| 41 | ModeBar(m: m) |
| 42 | if !m.notices.isEmpty { NoticeBar(lines: m.notices) } |
| 43 | Group { |
| 44 | switch m.tab { |
| 45 | // Nothing typed: no list, the spirit and at most a few offers. Typing brings the list. |
| 46 | case .all: if m.smartTyped { SmartView(m: m) } else { SpiritHome(m: m) } |
| 47 | case .find: FindView(m: m) |
| 48 | case .clipboard: ClipboardView(m: m, store: m.clipboard) |
| 49 | case .agent: VStack(spacing: 0) { |
| 50 | AgentBar(m: m) |
| 51 | ParkingLotBar(m: m, lot: m.parkingLot) |
| 52 | Divider() |
| 53 | TranscriptView(log: m.agentDetail.shown(m.agentLog), |
| 54 | empty: "Keep the ideas coming. They will be here when you are ready.", |
| 55 | running: m.agentRunning, stop: m.stopAgent, clear: { m.newAgent() }, follow: AgentDetail.allCases.firstIndex(of: m.agentDetail) ?? 0) |
| 56 | } |
| 57 | case .factory: FactoryWorkspaceView(m: m) |
| 58 | case .accounts: AccountsView(m: m) |
| 59 | case .voice: VoiceView(m: m, d: m.dictation) |
| 60 | } |
| 61 | } |
| 62 | .frame(maxWidth: .infinity, maxHeight: .infinity) |
| 63 | } |
| 64 | // The sky fills the whole window, the strip you drag it by included, and gives the window its shape. |
| 65 | .background { |
| 66 | SpiritField(showing: m.panelShown) |
| 67 | .clipShape(Spirit.windowShape) |
| 68 | .overlay(Spirit.windowShape.strokeBorder(Spirit.hairlineStrong, lineWidth: 1)) |
| 69 | .ignoresSafeArea() |
| 70 | } |
| 71 | .mask(Spirit.windowShape.ignoresSafeArea()) |
| 72 | .environment(\.colorScheme, .dark) |
| 73 | .environment(\.spiritShowing, m.panelShown) |
| 74 | .onChange(of: m.boxFocus) { _, _ in focused = true } // a click on a card or a screen button: typing and Enter still go to the box |
| 75 | .onAppear { focused = true } |
| 76 | .onExitCommand { m.hide() } |
| 77 | .background(shortcuts) |
| 78 | } |
| 79 | |
| 80 | /// ⌘1, ⌘2, … still jump to a single screen (Clipboard, Find, Agent, …) for anyone used to them. Nothing needs |
| 81 | /// them: the one box reaches everything by typing. |
| 82 | private var shortcuts: some View { |
| 83 | ZStack { |
| 84 | ForEach(Array(m.tabs.filter { $0 != .all }.enumerated()), id: \.offset) { i, t in |
| 85 | Button("") { m.tab = t; focused = true }.keyboardShortcut(KeyEquivalent(Character("\(i + 1)")), modifiers: .command) |
| 86 | } |
| 87 | Button("") { m.newAgent() }.keyboardShortcut("n", modifiers: .command) |
| 88 | }.opacity(0).allowsHitTesting(false) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /// One quiet line under the box instead of a row of tabs. On the one box it says what typing does; on a single |
| 93 | /// screen it names the screen and offers the way back. Every screen is also in the menu on the right, and typing |
| 94 | /// its name in the box opens it. |
| 95 | struct ModeBar: View { |
| 96 | @ObservedObject var m: AppModel |
| 97 | var body: some View { |
| 98 | HStack(spacing: 8) { |
| 99 | if m.tab == .all { |
| 100 | Text(m.smartTyped ? "↩ does the highlighted line · ↑↓ to choose · ⇧↩ sends it to your agent" |
| 101 | : m.smartShown > 0 ? "↩ does the highlighted card · ↑↓ to choose · or just type" : "Just type.") |
| 102 | .foregroundStyle(Spirit.textQuiet).font(.caption2).lineLimit(1) |
| 103 | } else { |
| 104 | Button { m.tab = .all } label: { Label("Everything", systemImage: "chevron.left") } |
| 105 | .buttonStyle(.plain).foregroundStyle(.secondary) |
| 106 | .help("Back to the one box (Tab also walks the screens)") |
| 107 | Label(m.tab.rawValue, systemImage: m.tab.icon) |
| 108 | .padding(.horizontal, 9).padding(.vertical, 4) |
| 109 | .background(Spirit.accentFill.opacity(0.28), in: RoundedRectangle(cornerRadius: 6)) |
| 110 | } |
| 111 | Spacer() |
| 112 | if m.agentRunning && m.tab != .agent { |
| 113 | Button { m.tab = .agent } label: { Label("Agent working…", systemImage: "wand.and.stars") } |
| 114 | .buttonStyle(.plain).foregroundStyle(.purple) |
| 115 | } |
| 116 | Menu { |
| 117 | ForEach(m.tabs) { t in Button { m.query = ""; m.tab = t } label: { Label(t.rawValue, systemImage: t.icon) } } |
| 118 | } label: { Image(systemName: "square.grid.2x2") } |
| 119 | .menuStyle(.borderlessButton).menuIndicator(.hidden).fixedSize() |
| 120 | .help("All of Hop's screens") |
| 121 | } |
| 122 | .font(.system(size: 12)).padding(.horizontal, 20).padding(.bottom, 8) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // MARK: Find |
| 127 | struct FindView: View { |
| 128 | @ObservedObject var m: AppModel |
| 129 | var body: some View { |
| 130 | VStack(spacing: 0) { |
| 131 | if m.query.isEmpty && m.fileHits.isEmpty { |
| 132 | Hint(lines: [m.config.machines.isEmpty ? "Type part of a file, folder or app name. Searches this Mac." |
| 133 | : "Type part of a file, folder or app name. Searches this Mac and \(m.config.machines.map(\.name).joined(separator: ", ")) together.", |
| 134 | m.config.machines.isEmpty ? "Enter opens · ⌘Enter shows it in Finder" : "Enter opens · ⌘Enter shows it in Finder (or copies the path on another machine)", m.indexStatus]) |
| 135 | } else if m.fileHits.isEmpty { |
| 136 | Hint(lines: ["No matches.", m.remoteNote, m.indexStatus]) |
| 137 | } else { |
| 138 | if m.query.isEmpty { |
| 139 | Text("★ Your most used · Enter opens").font(.caption.weight(.semibold)).foregroundStyle(.secondary) |
| 140 | .frame(maxWidth: .infinity, alignment: .leading).padding(.horizontal, 16).padding(.top, 8) |
| 141 | } |
| 142 | ScrollViewReader { proxy in |
| 143 | List(Array(m.fileHits.enumerated()), id: \.element.id) { i, h in |
| 144 | FileRow(path: h.isFolder ? String(h.path.dropLast()) : h.path, selected: i == m.selection, |
| 145 | machine: h.machine, folder: h.isFolder, |
| 146 | home: m.config.machines.first { $0.name == h.machine }?.homeDir) |
| 147 | .id(i).contentShape(Rectangle()) |
| 148 | .onTapGesture(count: 2) { m.open(h) } |
| 149 | .onTapGesture { m.selection = i } |
| 150 | } |
| 151 | .listStyle(.plain).scrollContentBackground(.hidden) |
| 152 | .onChange(of: m.selection) { _, s in proxy.scrollTo(s) } |
| 153 | } |
| 154 | if !m.remoteNote.isEmpty { |
| 155 | Text(m.remoteNote).font(.caption).foregroundStyle(.secondary).padding(.vertical, 4) |
| 156 | } |
| 157 | CortexHits(m: m, c: m.cortex) |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | struct FileRow: View { |
| 164 | let path: String; let selected: Bool |
| 165 | var machine: String? = nil; var folder = false; var home: String? = nil |
| 166 | private var icon: NSImage { |
| 167 | guard machine != nil else { return NSWorkspace.shared.icon(forFile: path) } |
| 168 | if folder { return NSWorkspace.shared.icon(for: .folder) } |
| 169 | let ext = (path as NSString).pathExtension |
| 170 | return NSWorkspace.shared.icon(for: UTType(filenameExtension: ext) ?? .data) |
| 171 | } |
| 172 | var body: some View { |
| 173 | HStack(spacing: 10) { |
| 174 | Image(nsImage: icon).resizable().frame(width: 22, height: 22) |
| 175 | VStack(alignment: .leading, spacing: 1) { |
| 176 | Text((path as NSString).lastPathComponent).lineLimit(1) |
| 177 | Text((path as NSString).deletingLastPathComponent.replacingOccurrences(of: home ?? NSHomeDirectory(), with: "~")) |
| 178 | .font(.caption).foregroundStyle(.secondary).lineLimit(1).truncationMode(.middle) |
| 179 | } |
| 180 | Spacer() |
| 181 | Text(machine ?? "Mac").font(.caption2.weight(.medium)) |
| 182 | .padding(.horizontal, 6).padding(.vertical, 2) |
| 183 | .background((machine == nil ? Color.secondary : Color.purple).opacity(0.15), in: Capsule()) |
| 184 | } |
| 185 | .padding(.vertical, 3).padding(.horizontal, 6) |
| 186 | .background(selected ? Spirit.accentFill.opacity(0.28) : .clear, in: RoundedRectangle(cornerRadius: 6)) |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // MARK: Clipboard |
| 191 | struct ClipboardView: View { |
| 192 | @ObservedObject var m: AppModel |
| 193 | @ObservedObject var store: ClipboardStore |
| 194 | var body: some View { |
| 195 | let items = m.clipResults |
| 196 | HStack(spacing: 0) { |
| 197 | if let reason = store.lockReason { |
| 198 | VStack(alignment: .leading, spacing: 12) { |
| 199 | Text("Clipboard history is paused").font(.headline) |
| 200 | Text(reason).foregroundStyle(.secondary) |
| 201 | Button("Try again") { store.unlock(allowInteraction: true) } |
| 202 | .keyboardShortcut(.defaultAction).controlSize(.large) |
| 203 | }.padding(18).frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading) |
| 204 | } else if items.isEmpty { |
| 205 | Hint(lines: ["Copied text and images appear here when history is available.", |
| 206 | "Passwords copied from a password manager are left out when the manager marks them as concealed or transient. Other text and images you copy may be saved, so do not rely on Hop to exclude passwords."]) |
| 207 | } else { |
| 208 | ScrollViewReader { proxy in |
| 209 | List(Array(items.enumerated()), id: \.element.id) { i, it in |
| 210 | HStack { |
| 211 | if it.pinned { Image(systemName: "pin.fill").font(.caption).foregroundStyle(.orange) } |
| 212 | Text(it.preview).lineLimit(1) |
| 213 | Spacer() |
| 214 | Text(it.date, style: .relative).font(.caption2).foregroundStyle(.tertiary) |
| 215 | } |
| 216 | .padding(.vertical, 3).padding(.horizontal, 6) |
| 217 | .background(i == m.selection ? Spirit.accentFill.opacity(0.28) : .clear, in: RoundedRectangle(cornerRadius: 6)) |
| 218 | .id(i).contentShape(Rectangle()) |
| 219 | .onTapGesture(count: 2) { m.paste(it) } |
| 220 | .onTapGesture { m.selection = i } |
| 221 | } |
| 222 | .listStyle(.plain).scrollContentBackground(.hidden).frame(width: 330) |
| 223 | .onChange(of: m.selection) { _, s in proxy.scrollTo(s) } |
| 224 | .safeAreaInset(edge: .bottom) { if !AXIsProcessTrusted() { PastePermissionBar() } } |
| 225 | } |
| 226 | Divider() |
| 227 | if items.indices.contains(m.selection) { |
| 228 | let it = items[m.selection] |
| 229 | VStack(alignment: .leading, spacing: 8) { |
| 230 | ScrollView { |
| 231 | if let t = it.text { Text(t).font(.system(.body, design: .monospaced)).textSelection(.enabled).frame(maxWidth: .infinity, alignment: .leading) } |
| 232 | else if let img = store.image(for: it) { Image(nsImage: img).resizable().scaledToFit() } |
| 233 | } |
| 234 | HStack { |
| 235 | Text([it.sourceApp, it.date.formatted(date: .abbreviated, time: .shortened)].compactMap { $0 }.joined(separator: " · ")) |
| 236 | .font(.caption).foregroundStyle(.secondary) |
| 237 | Spacer() |
| 238 | Button(it.pinned ? "Unpin" : "Pin") { store.togglePin(it) } |
| 239 | Button("Delete") { store.delete(it) } |
| 240 | Button("Paste") { m.paste(it) }.keyboardShortcut(.defaultAction) |
| 241 | }.controlSize(.small) |
| 242 | }.padding(12) |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | // MARK: Shared transcript |
| 250 | /// What the UI tests read back: where each message was drawn, and what the reader is being shown. |
| 251 | @MainActor |
| 252 | enum TranscriptProbe { |
| 253 | static var tops: [UUID: CGFloat] = [:] // top edge of each message, in the scrolling page |
| 254 | static var waiting = 0 // messages behind the "new" button |
| 255 | static var digging = false |
| 256 | static var shownTop: UUID? |
| 257 | static var shownTopText = "" |
| 258 | static var frozen = false // the text on show is held, not only the set of messages |
| 259 | static var latestButton: CGRect? // the "new" button, in the window's own coordinates (top left origin) |
| 260 | } |
| 261 | |
| 262 | /// A conversation, newest message on top. Each new message pushes the others down; scrolling down digs into |
| 263 | /// the past. While you dig, nothing is put in above what you are reading (see SpiritOrder.Hold). |
| 264 | struct TranscriptView: View { |
| 265 | let log: [ChatMessage]; let empty: String; let running: Bool; let stop: () -> Void; let clear: (() -> Void)? |
| 266 | var follow = 0 // a change here lets go of the reader's place and shows the newest message (the Agent screen's Show menu) |
| 267 | @State private var hold = SpiritOrder.Hold() |
| 268 | @State private var seen = Seen() |
| 269 | @Environment(\.accessibilityReduceMotion) private var reduceMotion |
| 270 | @Environment(\.spiritShowing) private var showing |
| 271 | private static let latest = "latest", page = "page" |
| 272 | |
| 273 | /// What the page last told this view: where each message is drawn, how far down the reader is, and the |
| 274 | /// conversation as it is now. Kept beside the view, not as view state, so that noting it redraws nothing and a |
| 275 | /// callback that runs later reads today's values, not the ones from when it was made. |
| 276 | private final class Seen { |
| 277 | var frames: [UUID: CGRect] = [:] |
| 278 | var offset: CGFloat = 0 |
| 279 | var log: [ChatMessage] = [] |
| 280 | } |
| 281 | |
| 282 | /// Works out where the reader is (top, inside the newest message, among older ones) and holds or lets go. |
| 283 | private func reconsider() { |
| 284 | let log = seen.log |
| 285 | let newest = hold.shown(log).first.flatMap { seen.frames[$0.id]?.height } ?? 0 // not yet measured counts as "older": the safe side |
| 286 | var next = hold |
| 287 | next.moved(to: SpiritOrder.place(offset: seen.offset, newestHeight: newest), log: log) |
| 288 | if next != hold { hold = next } |
| 289 | } |
| 290 | |
| 291 | /// Lets go of the reader's place and goes to the newest message. The page moves a moment later, once the |
| 292 | /// messages that were waiting have been put in. |
| 293 | private func toTop(_ proxy: ScrollViewProxy, animated: Bool) { |
| 294 | hold = SpiritOrder.Hold() |
| 295 | DispatchQueue.main.async { |
| 296 | withAnimation(animated ? .default : nil) { proxy.scrollTo(Self.latest, anchor: .top) } |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | var body: some View { |
| 301 | let shown = hold.shown(log), waiting = hold.waiting(log) |
| 302 | // Nothing is animated while the window is hidden, with Reduce Motion, or in tests. |
| 303 | let still = SpiritClock(showing: showing, reduceMotion: reduceMotion).still |
| 304 | VStack(spacing: 0) { |
| 305 | if log.isEmpty { Hint(lines: [empty]) } else { |
| 306 | ScrollViewReader { proxy in |
| 307 | ScrollView { |
| 308 | Color.clear.frame(height: 0).id(Self.latest) // the very top of the page |
| 309 | LazyVStack(alignment: .leading, spacing: 12) { |
| 310 | ForEach(Array(shown.enumerated()), id: \.element.id) { depth, msg in |
| 311 | Bubble(msg: msg, depth: depth).id(msg.id) |
| 312 | // Noted under the message's own identity: a message that is being taken away still |
| 313 | // reports once more, and must not be taken for the newest one. |
| 314 | .onGeometryChange(for: CGRect.self) { $0.frame(in: .named(Self.page)) } action: { f in |
| 315 | seen.frames[msg.id] = f |
| 316 | if UITest.active { TranscriptProbe.tops[msg.id] = f.minY } |
| 317 | if msg.id == hold.shown(seen.log).first?.id { reconsider() } // the newest grew: the reader may now be inside it |
| 318 | } |
| 319 | .transition(.move(edge: .top).combined(with: .opacity)) |
| 320 | } |
| 321 | } |
| 322 | .padding(14) |
| 323 | .coordinateSpace(.named(Self.page)) |
| 324 | .animation(still ? nil : .spring(response: 0.36, dampingFraction: 0.85), value: shown.first?.id) |
| 325 | } |
| 326 | .onScrollGeometryChange(for: CGFloat.self) { g in (g.contentOffset.y + g.contentInsets.top).rounded() } action: { _, y in |
| 327 | seen.offset = y |
| 328 | reconsider() |
| 329 | } |
| 330 | .onChange(of: log.count) { _, _ in |
| 331 | if hold.sentSince(log) { toTop(proxy, animated: false) } |
| 332 | } |
| 333 | // The Show menu changed what the same messages say: the reader's place was held on the old |
| 334 | // wording, so it is let go and the newest message is shown, as when the reader sends something. |
| 335 | .onChange(of: follow) { _, _ in toTop(proxy, animated: false) } |
| 336 | .overlay(alignment: .top) { |
| 337 | if hold.digging { |
| 338 | Button { toTop(proxy, animated: !still) } label: { |
| 339 | Label(waiting > 0 ? "\(waiting) new" : "Latest", systemImage: "arrow.up") |
| 340 | .font(.caption.weight(.semibold)).padding(.horizontal, 10).padding(.vertical, 5) |
| 341 | .background(Capsule().fill(Spirit.accentFill)).foregroundStyle(.white) |
| 342 | } |
| 343 | .buttonStyle(.plain).padding(8) |
| 344 | .help("Back to the newest message") |
| 345 | .accessibilityIdentifier("transcript.latest") |
| 346 | .onGeometryChange(for: CGRect.self) { $0.frame(in: .global) } action: { f in |
| 347 | if UITest.active { TranscriptProbe.latestButton = f } |
| 348 | } |
| 349 | .onDisappear { TranscriptProbe.latestButton = nil } |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | if running || (clear != nil && !log.isEmpty) { |
| 355 | HStack { |
| 356 | Spacer() |
| 357 | if running { Button("Stop") { stop() } } |
| 358 | if let clear, !log.isEmpty { Button("Clear") { clear() } } |
| 359 | }.controlSize(.small).padding(8) |
| 360 | } |
| 361 | } |
| 362 | .onChange(of: log, initial: true) { _, now in |
| 363 | seen.log = now |
| 364 | let live = Set(now.map(\.id)) |
| 365 | seen.frames = seen.frames.filter { live.contains($0.key) } |
| 366 | if now.isEmpty { seen.offset = 0 } |
| 367 | hold.settle(now) |
| 368 | } |
| 369 | .onChange(of: TranscriptState(waiting: waiting, digging: hold.digging, frozen: hold.frozen != nil, top: shown.first?.id, text: shown.first?.text ?? ""), initial: true) { _, s in |
| 370 | guard UITest.active else { return } |
| 371 | TranscriptProbe.waiting = s.waiting; TranscriptProbe.digging = s.digging; TranscriptProbe.frozen = s.frozen |
| 372 | TranscriptProbe.shownTop = s.top; TranscriptProbe.shownTopText = s.text |
| 373 | } |
| 374 | } |
| 375 | private struct TranscriptState: Equatable { let waiting: Int; let digging: Bool; let frozen: Bool; let top: UUID?; let text: String } |
| 376 | } |
| 377 | |
| 378 | struct Bubble: View { |
| 379 | let msg: ChatMessage |
| 380 | var depth = 0 // 0 is the newest message; older ones have sunk further |
| 381 | @State private var copied = false |
| 382 | @State private var hovered = false |
| 383 | @Environment(\.accessibilityReduceMotion) private var reduceMotion |
| 384 | @Environment(\.spiritShowing) private var showing |
| 385 | var body: some View { |
| 386 | HStack(alignment: .top) { |
| 387 | if msg.role == "user" { Spacer(minLength: 60) } |
| 388 | VStack(alignment: .leading, spacing: 4) { |
| 389 | if msg.role == "tool" { // a command or its output: exactly as written, in a fixed-width face |
| 390 | Text(verbatim: msg.text).textSelection(.enabled) |
| 391 | .font(.callout.monospaced()).foregroundStyle(Spirit.textDim) |
| 392 | } else { |
| 393 | Text(msg.text.isEmpty ? "…" : (try? AttributedString(markdown: msg.text, options: .init(interpretedSyntax: .inlineOnlyPreservingWhitespace))) ?? AttributedString(msg.text)) |
| 394 | .textSelection(.enabled) |
| 395 | .font(msg.role == "note" ? .callout : .body) |
| 396 | .foregroundStyle(msg.role == "note" ? Spirit.textDim : Color.white) |
| 397 | .tint(Spirit.accent) // links in an answer: the light accent reads on a card, dimmed or not |
| 398 | } |
| 399 | if let meta = msg.meta { |
| 400 | Text(copied ? "Copied" : meta).font(.caption2.weight(.semibold).monospacedDigit()) |
| 401 | .foregroundStyle(meta.hasPrefix("⚡") || meta.hasPrefix("🏠") || meta.hasPrefix("✓") ? Color.green : Spirit.textDim) |
| 402 | .transition(.scale(scale: 0.6).combined(with: .opacity)) |
| 403 | } |
| 404 | } |
| 405 | .padding(.horizontal, 12).padding(.vertical, 8) |
| 406 | // A card on the sky. Your own words are in the action blue; the newest message has the bright edge. |
| 407 | .background { |
| 408 | RoundedRectangle(cornerRadius: 12, style: .continuous) |
| 409 | .fill(msg.role == "user" ? Spirit.accentFill.opacity(0.30) : Spirit.card.opacity(msg.role == "note" || msg.role == "tool" ? 0.35 : 0.6)) |
| 410 | .overlay(RoundedRectangle(cornerRadius: 12, style: .continuous) |
| 411 | .strokeBorder(depth == 0 ? Spirit.accent.opacity(0.5) : Spirit.hairline, lineWidth: 1)) |
| 412 | } |
| 413 | .animation(SpiritClock(showing: showing, reduceMotion: reduceMotion).still ? nil : .spring(response: 0.35, dampingFraction: 0.6), value: msg.meta) |
| 414 | .overlay(alignment: .topTrailing) { |
| 415 | if copied && msg.meta == nil { Text("Copied").font(.caption2.weight(.semibold)).padding(4).foregroundStyle(.green) } |
| 416 | } |
| 417 | .help("Click to copy this whole answer. Drag to highlight just part of it.") |
| 418 | // A plain click copies the whole generation; dragging still highlights part of it. |
| 419 | .simultaneousGesture(TapGesture().onEnded { |
| 420 | guard !msg.text.isEmpty else { return } |
| 421 | NSPasteboard.general.clearContents() |
| 422 | NSPasteboard.general.setString(msg.text, forType: .string) |
| 423 | copied = true |
| 424 | DispatchQueue.main.asyncAfter(deadline: .now() + 1.2) { copied = false } |
| 425 | }) |
| 426 | if msg.role != "user" { Spacer(minLength: 60) } |
| 427 | } |
| 428 | // Older messages sink and dim; the one under the pointer is at full strength. |
| 429 | .opacity(hovered ? 1 : SpiritOrder.fade(depth: depth)) |
| 430 | .onHover { hovered = $0 } |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | |
| 435 | // MARK: Accounts |
| 436 | struct AccountsView: View { |
| 437 | @ObservedObject var m: AppModel |
| 438 | @State private var name = "" |
| 439 | @State private var secret = "" |
| 440 | @State private var kind = "Subscription" |
| 441 | @State private var note = "" |
| 442 | var body: some View { |
| 443 | HStack(alignment: .top, spacing: 0) { |
| 444 | VStack(alignment: .leading, spacing: 8) { |
| 445 | Text("Saved on this Mac").font(.headline) |
| 446 | Text("Stored in your Mac's Keychain, not in Hop's files. If you assign a key to a model, Hop sends it to that model's server to sign in.").font(.caption).foregroundStyle(.secondary) |
| 447 | let rows = m.vault.filter { m.query.isEmpty || $0.account.localizedCaseInsensitiveContains(m.query) || $0.note.localizedCaseInsensitiveContains(m.query) } |
| 448 | List(Array(rows.enumerated()), id: \.element.id) { i, e in |
| 449 | HStack { |
| 450 | VStack(alignment: .leading) { |
| 451 | Text(e.account) |
| 452 | Text([e.kind, e.note].filter { !$0.isEmpty }.joined(separator: " · ")).font(.caption).foregroundStyle(.secondary) |
| 453 | } |
| 454 | Spacer() |
| 455 | Button("Copy") { m.copySecret(e) } |
| 456 | Button(role: .destructive) { Vault.delete(e.account); m.reloadVault() } label: { Image(systemName: "trash") } |
| 457 | } |
| 458 | .controlSize(.small) |
| 459 | .padding(.vertical, 2) |
| 460 | .background(i == m.selection ? Spirit.accentFill.opacity(0.28) : .clear) |
| 461 | } |
| 462 | .listStyle(.plain).scrollContentBackground(.hidden) |
| 463 | GroupBox("Add") { |
| 464 | VStack(spacing: 6) { |
| 465 | HStack { |
| 466 | TextField("Name (e.g. OpenRouter)", text: $name) |
| 467 | Picker("", selection: $kind) { ForEach(["Subscription", "API key", "Login", "Other"], id: \.self) { Text($0) } } |
| 468 | .labelsHidden().frame(width: 120) |
| 469 | } |
| 470 | SecureField("Key, token or password", text: $secret) |
| 471 | TextField("Note (what uses it)", text: $note) |
| 472 | HStack { |
| 473 | Spacer() |
| 474 | Button("Save to Keychain") { |
| 475 | Vault.save(account: name.trimmingCharacters(in: .whitespaces), secret: secret, kind: kind, note: note) |
| 476 | name = ""; secret = ""; note = ""; m.reloadVault() |
| 477 | }.disabled(name.trimmingCharacters(in: .whitespaces).isEmpty || secret.isEmpty) |
| 478 | } |
| 479 | }.textFieldStyle(.roundedBorder).controlSize(.small) |
| 480 | } |
| 481 | } |
| 482 | .padding(14).frame(maxWidth: .infinity) |
| 483 | if m.config.factoryEnabled { |
| 484 | Divider() |
| 485 | VStack(alignment: .leading, spacing: 8) { |
| 486 | Text("Factory sign-ins").font(.headline) |
| 487 | Text("Subscriptions and keys the factory uses. They stay on the factory host; Hop only shows their status.").font(.caption).foregroundStyle(.secondary) |
| 488 | if !m.floor.reachable { Text(m.floor.error ?? "Open the Factory tab to load these.").font(.callout).foregroundStyle(.secondary) } |
| 489 | ScrollView { |
| 490 | VStack(alignment: .leading, spacing: 5) { |
| 491 | ForEach(m.floor.accounts) { a in |
| 492 | HStack { |
| 493 | Circle().fill(a.status == "active" || a.status == "saved" ? .green : .orange).frame(width: 7, height: 7) |
| 494 | Text(a.service).frame(width: 90, alignment: .leading) |
| 495 | Text(a.label).lineLimit(1).foregroundStyle(.secondary) |
| 496 | Spacer() |
| 497 | Text(a.status).font(.caption).foregroundStyle(.tertiary) |
| 498 | }.font(.callout) |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | Button("Add or change factory sign-ins…") { m.openFloor() } |
| 503 | } |
| 504 | .padding(14).frame(width: 330) |
| 505 | } |
| 506 | } |
| 507 | .onAppear { m.reloadVault(); if m.config.factoryEnabled, !m.floor.reachable { m.refreshFactory() } } |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | struct Hint: View { |
| 512 | let lines: [String] |
| 513 | var body: some View { |
| 514 | VStack(spacing: 6) { |
| 515 | ForEach(lines.filter { !$0.isEmpty }, id: \.self) { Text($0).foregroundStyle(.secondary).multilineTextAlignment(.center) } |
| 516 | }.font(.callout).padding(30).frame(maxWidth: .infinity, maxHeight: .infinity) |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | /// Running tally of tokens saved / spent today. Pops when it changes. |
| 521 | struct SavingsBadge: View { |
| 522 | @ObservedObject var s: Savings |
| 523 | @State private var pop = false |
| 524 | var body: some View { |
| 525 | Text(s.headline) |
| 526 | .font(.caption.weight(.semibold).monospacedDigit()) |
| 527 | .padding(.horizontal, 9).padding(.vertical, 3) |
| 528 | .background(Color.green.opacity(pop ? 0.35 : 0.14), in: Capsule()) |
| 529 | .scaleEffect(pop ? 1.08 : 1) |
| 530 | .animation(.spring(response: 0.3, dampingFraction: 0.5), value: pop) |
| 531 | .onChange(of: s.headline) { _, _ in |
| 532 | pop = true |
| 533 | DispatchQueue.main.asyncAfter(deadline: .now() + 0.35) { pop = false } |
| 534 | } |
| 535 | .help("Tokens used today, by where the request went. An instant answer reuses a reply Hop still has in memory, so nothing is sent again. Hop does not know what a model costs.") |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | |
| 540 | /// Something is wrong with a settings file: said plainly, where the user will see it. |
| 541 | struct NoticeBar: View { |
| 542 | let lines: [String] |
| 543 | var body: some View { |
| 544 | VStack(alignment: .leading, spacing: 4) { |
| 545 | ForEach(lines, id: \.self) { l in |
| 546 | Label(l, systemImage: "exclamationmark.triangle").font(.caption).textSelection(.enabled) |
| 547 | .fixedSize(horizontal: false, vertical: true) |
| 548 | } |
| 549 | } |
| 550 | .frame(maxWidth: .infinity, alignment: .leading).padding(.horizontal, 14).padding(.vertical, 6) |
| 551 | .background(Color.orange.opacity(0.18)) |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | /// Shown whenever something is typed: one key sends it to the agent, which answers it or does it. |
| 556 | struct FixItBar: View { |
| 557 | @ObservedObject var m: AppModel |
| 558 | var body: some View { |
| 559 | Button { m.fixIt() } label: { |
| 560 | HStack(spacing: 8) { |
| 561 | Image(systemName: "wand.and.stars").foregroundStyle(.purple) |
| 562 | Text(m.enterGoesToAgent ? "No match. Enter sends it to your agent:" : "Send to your agent:") |
| 563 | .font(.callout.weight(.semibold)) |
| 564 | Text("“\(m.query)”").font(.callout).foregroundStyle(.secondary).lineLimit(1).truncationMode(.tail) |
| 565 | Spacer() |
| 566 | Text(m.enterGoesToAgent ? "↩" : "⇧↩").font(.caption.monospaced().weight(.bold)) |
| 567 | .padding(.horizontal, 6).padding(.vertical, 2) |
| 568 | .background(Color.purple.opacity(0.18), in: RoundedRectangle(cornerRadius: 4)) |
| 569 | } |
| 570 | .padding(.horizontal, 16).padding(.vertical, 6) |
| 571 | .background(Color.purple.opacity(m.enterGoesToAgent ? 0.12 : 0.05)) |
| 572 | .contentShape(Rectangle()) |
| 573 | }.buttonStyle(.plain) |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | |
| 578 | /// Enter copies the item either way; this permission lets Hop also type it into the app you were in. |
| 579 | struct PastePermissionBar: View { |
| 580 | var body: some View { |
| 581 | Button { |
| 582 | _ = AXIsProcessTrustedWithOptions([kAXTrustedCheckOptionPrompt.takeUnretainedValue() as String: true] as CFDictionary) |
| 583 | } label: { |
| 584 | Label("Enter copies but can't paste for you yet. Click to allow, then switch Hop on.", systemImage: "hand.tap") |
| 585 | .font(.caption).frame(maxWidth: .infinity, alignment: .leading) |
| 586 | .padding(.horizontal, 10).padding(.vertical, 6) |
| 587 | .background(Color.orange.opacity(0.18)).contentShape(Rectangle()) |
| 588 | }.buttonStyle(.plain) |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | |
| 593 | /// The line above the conversation. It names no model and no tool to choose: who answers each message is |
| 594 | /// picked in the background, and said in a small line in the conversation. |
| 595 | struct AgentBar: View { |
| 596 | @ObservedObject var m: AppModel |
| 597 | var body: some View { |
| 598 | HStack(spacing: 8) { |
| 599 | if m.strongerLeft && !m.agentRunning { |
| 600 | Button("Stronger") { m.runStronger() } |
| 601 | .help("Not right? Ask the same thing again of a stronger mind. Its answer takes the place of this one.") |
| 602 | .accessibilityIdentifier("agent.stronger") |
| 603 | } |
| 604 | Spacer() |
| 605 | SavingsBadge(s: Savings.shared) |
| 606 | if m.cortex.cfg.enabled { |
| 607 | Label("Cortex", systemImage: "brain").font(.caption).foregroundStyle(.secondary) |
| 608 | .help("What Cortex knows about your message rides along, and what came of it is saved to it in the background.") |
| 609 | } |
| 610 | }.controlSize(.small).padding(.horizontal, 14).padding(.vertical, 7) |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | struct ParkingLotBar: View { |
| 615 | @ObservedObject var m: AppModel |
| 616 | @ObservedObject var lot: ParkingLot |
| 617 | @State private var expanded = false |
| 618 | private struct SortPreview: Identifiable { |
| 619 | let id = UUID() |
| 620 | let text: String |
| 621 | } |
| 622 | @State private var sortPreview: SortPreview? |
| 623 | var body: some View { |
| 624 | VStack(alignment: .leading, spacing: 6) { |
| 625 | Text("Enter parks an idea · ⇧Enter sends when idle").font(.caption).foregroundStyle(.secondary) |
| 626 | if let problem = lot.problem { Text(problem).font(.caption).foregroundStyle(.orange) } |
| 627 | if !lot.ideas.isEmpty { |
| 628 | HStack { |
| 629 | Button("Parking lot (\(lot.ideas.count))") { expanded.toggle() } |
| 630 | .accessibilityIdentifier("agent.parkingLot") |
| 631 | Spacer() |
| 632 | Button("Sort ideas") { |
| 633 | if m.config.reviewParkedIdeasBeforeSorting { |
| 634 | sortPreview = SortPreview(text: lot.sortingPrompt) |
| 635 | } else { m.sortParkedIdeas() } |
| 636 | } |
| 637 | .disabled(m.agentRunning) |
| 638 | .help(m.agentRunning ? "Sorting is available when the agent finishes." : "Sort by feasibility and importance. Keep adding ideas while it works.") |
| 639 | } |
| 640 | if expanded { |
| 641 | ScrollView { |
| 642 | VStack(alignment: .leading, spacing: 8) { |
| 643 | ForEach(lot.ideas.reversed()) { idea in |
| 644 | Text(idea.text).textSelection(.enabled).frame(maxWidth: .infinity, alignment: .leading) |
| 645 | } |
| 646 | } |
| 647 | }.frame(maxHeight: 150) |
| 648 | } |
| 649 | } |
| 650 | }.font(.callout).padding(.horizontal, 14).padding(.vertical, 6) |
| 651 | .sheet(item: $sortPreview) { preview in |
| 652 | VStack(alignment: .leading, spacing: 12) { |
| 653 | Text("Sort your parking lot").font(.title2) |
| 654 | Text("Sending uses your configured agent, which may use an online provider. Your original ideas stay saved; the assessment appears in the conversation.") |
| 655 | if m.learnFromAgent { Text("Learning is on. Requests and results may also be saved to Cortex.").foregroundStyle(.secondary) } |
| 656 | ScrollView { Text(preview.text).textSelection(.enabled).frame(maxWidth: .infinity, alignment: .leading).padding(8) } |
| 657 | .background(Spirit.card.opacity(0.4)) |
| 658 | HStack { |
| 659 | Button("Cancel") { sortPreview = nil }.keyboardShortcut(.cancelAction) |
| 660 | Spacer() |
| 661 | Button("Send for sorting") { |
| 662 | guard !m.agentRunning else { return } |
| 663 | m.sortParkedIdeas(request: preview.text) |
| 664 | sortPreview = nil |
| 665 | m.boxFocus += 1 |
| 666 | } |
| 667 | .disabled(m.agentRunning) |
| 668 | } |
| 669 | }.padding(20).frame(width: 600, height: 500) |
| 670 | } |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | /// What Cortex knows about what you typed in Find. Click one to read it in the conversation. |
| 675 | struct CortexHits: View { |
| 676 | @ObservedObject var m: AppModel |
| 677 | @ObservedObject var c: Cortex |
| 678 | var body: some View { |
| 679 | if !c.hits.isEmpty && !m.query.isEmpty { |
| 680 | VStack(alignment: .leading, spacing: 4) { |
| 681 | Divider() |
| 682 | Text("From Cortex").font(.caption.weight(.semibold)).foregroundStyle(.secondary).padding(.horizontal, 16).padding(.top, 4) |
| 683 | ForEach(c.hits) { h in |
| 684 | Button { |
| 685 | m.agentLog.append(ChatMessage(role: "note", text: h.title + "\n\n" + h.content)) |
| 686 | m.query = ""; m.tab = .agent |
| 687 | } label: { |
| 688 | HStack(spacing: 8) { |
| 689 | Image(systemName: "brain").foregroundStyle(.purple).frame(width: 22) |
| 690 | VStack(alignment: .leading, spacing: 1) { |
| 691 | Text(h.title).lineLimit(1) |
| 692 | Text(h.content.replacingOccurrences(of: "\n", with: " ")).font(.caption).foregroundStyle(.secondary).lineLimit(1) |
| 693 | } |
| 694 | Spacer() |
| 695 | Text(!c.cfg.lessonSpace.isEmpty && h.space == c.cfg.lessonSpace ? "lesson" : "note").font(.caption2.weight(.medium)) |
| 696 | .padding(.horizontal, 6).padding(.vertical, 2).background(Color.purple.opacity(0.15), in: Capsule()) |
| 697 | }.padding(.horizontal, 12).padding(.vertical, 2).contentShape(Rectangle()) |
| 698 | }.buttonStyle(.plain) |
| 699 | } |
| 700 | }.padding(.bottom, 6) |
| 701 | } |
| 702 | } |
| 703 | } |