| 1 | import AppKit |
| 2 | import EquipoKit |
| 3 | import SwiftUI |
| 4 | |
| 5 | struct MacJumpPalette: View { |
| 6 | @Bindable var workspace: MacWorkspace |
| 7 | @Environment(\.dismiss) private var dismiss |
| 8 | @Environment(\.openWindow) private var openWindow |
| 9 | @FocusState private var searchFocused: Bool |
| 10 | @State private var query = "" |
| 11 | @State private var selected: MacDestination? |
| 12 | @State private var selectedAction: MacTaskAction? |
| 13 | @State private var taskContext: MacTaskContext? |
| 14 | |
| 15 | private var results: [MacJumpResult] { workspace.index.results(query) } |
| 16 | private var taskResults: [MacJumpResult] { results.filter { if case .task = $0.target { true } else { false } } } |
| 17 | private var otherResults: [MacJumpResult] { results.filter { if case .task = $0.target { false } else { true } } } |
| 18 | private var context: MacTaskContext? { taskContext ?? workspace.context } |
| 19 | private var actions: [MacTaskAction] { |
| 20 | guard let context else { return [] } |
| 21 | // Task matches keep their actions visible while a title is typed. |
| 22 | return context.actions.filter { !results.isEmpty || MacJumpIndex.score(query, in: $0.title) != nil } |
| 23 | } |
| 24 | private var rowIDs: [String] { taskResults.map { String(describing: $0.id) } + actions.map { "action:" + $0.rawValue } + otherResults.map { String(describing: $0.id) } } |
| 25 | private var selectedID: String? { selectedAction.map { "action:" + $0.rawValue } ?? selected.map { String(describing: $0) } } |
| 26 | |
| 27 | var body: some View { |
| 28 | VStack(alignment: .leading, spacing: 0) { |
| 29 | TextField(EquipoStrings.text("native.mac.search"), text: $query) |
| 30 | .textFieldStyle(.plain).font(.title3).padding(20).focused($searchFocused) |
| 31 | .accessibilityIdentifier("mac-jump-search") |
| 32 | Divider() |
| 33 | ScrollViewReader { proxy in |
| 34 | ScrollView { |
| 35 | LazyVStack(alignment: .leading, spacing: 3) { |
| 36 | ForEach(taskResults) { result in resultRow(result) } |
| 37 | if !actions.isEmpty, let context { |
| 38 | VStack(alignment: .leading, spacing: 4) { |
| 39 | Text(EquipoStrings.text("native.mac.selected_task")).font(.caption).foregroundStyle(.secondary) |
| 40 | Text(context.title).font(.callout.weight(.medium)).lineLimit(2) |
| 41 | }.padding(10) |
| 42 | ForEach(actions) { action in actionRow(action) } |
| 43 | } |
| 44 | ForEach(otherResults) { result in resultRow(result) } |
| 45 | if results.isEmpty && actions.isEmpty { |
| 46 | Text(EquipoStrings.text("native.mac.no_results")).foregroundStyle(.secondary).padding(20) |
| 47 | } |
| 48 | }.padding(10) |
| 49 | }.frame(height: 380) |
| 50 | .onChange(of: selectedID) { _, id in if let id { proxy.scrollTo(id) } } |
| 51 | } |
| 52 | Divider() |
| 53 | HStack { |
| 54 | Button(EquipoStrings.text("chat.card.not_now"), role: .cancel) { dismiss() } |
| 55 | Spacer() |
| 56 | Button(EquipoStrings.text("native.mac.open_window")) { |
| 57 | if let selected { activate(selected, window: true) } |
| 58 | }.disabled(!selectedIsTask) |
| 59 | }.padding(12) |
| 60 | } |
| 61 | .frame(width: 620) |
| 62 | .background(Color(nsColor: .webBackground)) |
| 63 | .defaultFocus($searchFocused, true) |
| 64 | .onAppear { |
| 65 | resetSelection() |
| 66 | #if DEBUG |
| 67 | if ProcessInfo.processInfo.environment["EQUIPO_MAC_POWER_FIXTURE"] .map({ ["palette", "keyboard"].contains($0) }) == true { query = "button" } |
| 68 | #endif |
| 69 | } |
| 70 | .onChange(of: query) { resetSelection() } |
| 71 | .onChange(of: rowIDs) { if selectedID == nil || !rowIDs.contains(selectedID!) { resetSelection() } } |
| 72 | .onKeyPress(.downArrow) { move(1); return .handled } |
| 73 | .onKeyPress(.upArrow) { move(-1); return .handled } |
| 74 | .onKeyPress(keys: [.return], phases: .down) { event in |
| 75 | guard Self.canActivate(firstResponder: NSApp.keyWindow?.firstResponder) else { return .ignored } |
| 76 | if let action = selectedAction { activate(action) } |
| 77 | else if let selected { activate(selected, window: event.modifiers.contains(.command)) } |
| 78 | return .handled |
| 79 | } |
| 80 | .onExitCommand { dismiss() } |
| 81 | .task(id: taskContext?.taskID) { await taskContext?.load() } |
| 82 | .accessibilityElement(children: .contain) |
| 83 | .accessibilityLabel(EquipoStrings.text("native.mac.jump")) |
| 84 | } |
| 85 | static func canActivate(firstResponder: NSResponder?) -> Bool { |
| 86 | (firstResponder as? NSTextInputClient)?.hasMarkedText() != true |
| 87 | } |
| 88 | private func resultRow(_ result: MacJumpResult) -> some View { |
| 89 | Button { activate(result.target, window: false) } label: { |
| 90 | HStack { |
| 91 | Image(systemName: symbol(result.target)) |
| 92 | VStack(alignment: .leading, spacing: 3) { |
| 93 | Text(result.title).lineLimit(2) |
| 94 | if !result.subtitle.isEmpty { Text(result.subtitle).font(.caption).foregroundStyle(.secondary) } |
| 95 | } |
| 96 | Spacer() |
| 97 | if result.needsYou { Image(systemName: "tray").accessibilityLabel(EquipoStrings.text("tab.needs")) } |
| 98 | }.frame(maxWidth: .infinity, alignment: .leading).padding(10) |
| 99 | } |
| 100 | .buttonStyle(.plain) |
| 101 | .background(selected == result.target && selectedAction == nil ? Color.accentColor.opacity(0.14) : .clear, in: .rect(cornerRadius: 7)) |
| 102 | .accessibilityAddTraits(selected == result.target && selectedAction == nil ? .isSelected : []) |
| 103 | .id(String(describing: result.id)) |
| 104 | } |
| 105 | private func actionRow(_ action: MacTaskAction) -> some View { |
| 106 | Button(action.title, role: action == .cancel ? .destructive : nil) { activate(action) } |
| 107 | .buttonStyle(.plain).padding(10).frame(maxWidth: .infinity, alignment: .leading) |
| 108 | .background(selectedAction == action ? Color.accentColor.opacity(0.14) : .clear, in: .rect(cornerRadius: 7)) |
| 109 | .accessibilityAddTraits(selectedAction == action ? .isSelected : []) |
| 110 | .id("action:" + action.rawValue) |
| 111 | } |
| 112 | private var selectedIsTask: Bool { if case .task = selected, selectedAction == nil { true } else { false } } |
| 113 | private func resetSelection() { select(results.first?.target); selectedAction = results.isEmpty ? actions.first : nil } |
| 114 | private func select(_ target: MacDestination?) { |
| 115 | selected = target |
| 116 | if case .task(let id) = target, taskContext?.taskID != id { |
| 117 | taskContext = workspace.context?.taskID == id ? workspace.context : MacTaskContext(session: workspace.session, taskID: id) |
| 118 | } |
| 119 | } |
| 120 | private func move(_ offset: Int) { |
| 121 | let ids = rowIDs |
| 122 | guard !ids.isEmpty else { return } |
| 123 | let previous = selectedID.flatMap { ids.firstIndex(of: $0) } ?? (offset > 0 ? -1 : ids.count) |
| 124 | let index = min(max(previous + offset, 0), ids.count - 1) |
| 125 | let id = ids[index] |
| 126 | if let result = results.first(where: { String(describing: $0.id) == id }) { selectedAction = nil; select(result.target) } |
| 127 | else { selectedAction = actions.first { "action:" + $0.rawValue == id } } |
| 128 | if NSWorkspace.shared.isVoiceOverEnabled { |
| 129 | let label = selectedAction.map { $0.title + "\n" + (context?.title ?? "") } |
| 130 | ?? results.first(where: { $0.target == selected }).map { $0.title + "\n" + $0.subtitle } |
| 131 | if let label, let app = NSApp { |
| 132 | NSAccessibility.post(element: app, notification: .announcementRequested, |
| 133 | userInfo: [.announcement: label, .priority: NSAccessibilityPriorityLevel.medium.rawValue]) |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | private func activate(_ target: MacDestination, window: Bool) { |
| 138 | if window, case .task(let id) = target { |
| 139 | openWindow(id: "task", value: MacTaskWindowID(taskID: id, session: workspace.session)) |
| 140 | } else { workspace.go(target); if workspace.standalone { openWindow(id: "main") } } |
| 141 | dismiss() |
| 142 | } |
| 143 | private func activate(_ action: MacTaskAction) { |
| 144 | guard let context, context.actions.contains(action) else { return } |
| 145 | if workspace.queuePaletteAction(action, context: context) { openWindow(id: "main") } |
| 146 | dismiss() |
| 147 | } |
| 148 | private func symbol(_ target: MacDestination) -> String { |
| 149 | switch target { case .task: "checklist"; case .project: "folder"; case .pane(.conversation): "bubble.left.and.bubble.right"; case .pane(.needs): "tray"; case .pane(.tasks): "checklist"; case .pane(.projects): "folder" } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | struct MacWorkspacePresenter: ViewModifier { |
| 154 | @Bindable var workspace: MacWorkspace |
| 155 | func body(content: Content) -> some View { |
| 156 | content.sheet(isPresented: $workspace.palette, onDismiss: { |
| 157 | let pending = workspace.pendingAction |
| 158 | workspace.pendingAction = nil |
| 159 | if let pending { Task { await workspace.performRoutedAction(pending.action, context: pending.context, navigation: pending.navigation) } } |
| 160 | }) { MacJumpPalette(workspace: workspace) } |
| 161 | .focusedSceneValue(workspace) |
| 162 | } |
| 163 | } |
| 164 | |