import AppKit import EquipoKit import SwiftUI struct MacJumpPalette: View { @Bindable var workspace: MacWorkspace @Environment(\.dismiss) private var dismiss @Environment(\.openWindow) private var openWindow @FocusState private var searchFocused: Bool @State private var query = "" @State private var selected: MacDestination? @State private var selectedAction: MacTaskAction? @State private var taskContext: MacTaskContext? private var results: [MacJumpResult] { workspace.index.results(query) } private var taskResults: [MacJumpResult] { results.filter { if case .task = $0.target { true } else { false } } } private var otherResults: [MacJumpResult] { results.filter { if case .task = $0.target { false } else { true } } } private var context: MacTaskContext? { taskContext ?? workspace.context } private var actions: [MacTaskAction] { guard let context else { return [] } // Task matches keep their actions visible while a title is typed. return context.actions.filter { !results.isEmpty || MacJumpIndex.score(query, in: $0.title) != nil } } private var rowIDs: [String] { taskResults.map { String(describing: $0.id) } + actions.map { "action:" + $0.rawValue } + otherResults.map { String(describing: $0.id) } } private var selectedID: String? { selectedAction.map { "action:" + $0.rawValue } ?? selected.map { String(describing: $0) } } var body: some View { VStack(alignment: .leading, spacing: 0) { TextField(EquipoStrings.text("native.mac.search"), text: $query) .textFieldStyle(.plain).font(.title3).padding(20).focused($searchFocused) .accessibilityIdentifier("mac-jump-search") Divider() ScrollViewReader { proxy in ScrollView { LazyVStack(alignment: .leading, spacing: 3) { ForEach(taskResults) { result in resultRow(result) } if !actions.isEmpty, let context { VStack(alignment: .leading, spacing: 4) { Text(EquipoStrings.text("native.mac.selected_task")).font(.caption).foregroundStyle(.secondary) Text(context.title).font(.callout.weight(.medium)).lineLimit(2) }.padding(10) ForEach(actions) { action in actionRow(action) } } ForEach(otherResults) { result in resultRow(result) } if results.isEmpty && actions.isEmpty { Text(EquipoStrings.text("native.mac.no_results")).foregroundStyle(.secondary).padding(20) } }.padding(10) }.frame(height: 380) .onChange(of: selectedID) { _, id in if let id { proxy.scrollTo(id) } } } Divider() HStack { Button(EquipoStrings.text("chat.card.not_now"), role: .cancel) { dismiss() } Spacer() Button(EquipoStrings.text("native.mac.open_window")) { if let selected { activate(selected, window: true) } }.disabled(!selectedIsTask) }.padding(12) } .frame(width: 620) .background(Color(nsColor: .webBackground)) .defaultFocus($searchFocused, true) .onAppear { resetSelection() #if DEBUG if ProcessInfo.processInfo.environment["EQUIPO_MAC_POWER_FIXTURE"] .map({ ["palette", "keyboard"].contains($0) }) == true { query = "button" } #endif } .onChange(of: query) { resetSelection() } .onChange(of: rowIDs) { if selectedID == nil || !rowIDs.contains(selectedID!) { resetSelection() } } .onKeyPress(.downArrow) { move(1); return .handled } .onKeyPress(.upArrow) { move(-1); return .handled } .onKeyPress(keys: [.return], phases: .down) { event in guard Self.canActivate(firstResponder: NSApp.keyWindow?.firstResponder) else { return .ignored } if let action = selectedAction { activate(action) } else if let selected { activate(selected, window: event.modifiers.contains(.command)) } return .handled } .onExitCommand { dismiss() } .task(id: taskContext?.taskID) { await taskContext?.load() } .accessibilityElement(children: .contain) .accessibilityLabel(EquipoStrings.text("native.mac.jump")) } static func canActivate(firstResponder: NSResponder?) -> Bool { (firstResponder as? NSTextInputClient)?.hasMarkedText() != true } private func resultRow(_ result: MacJumpResult) -> some View { Button { activate(result.target, window: false) } label: { HStack { Image(systemName: symbol(result.target)) VStack(alignment: .leading, spacing: 3) { Text(result.title).lineLimit(2) if !result.subtitle.isEmpty { Text(result.subtitle).font(.caption).foregroundStyle(.secondary) } } Spacer() if result.needsYou { Image(systemName: "tray").accessibilityLabel(EquipoStrings.text("tab.needs")) } }.frame(maxWidth: .infinity, alignment: .leading).padding(10) } .buttonStyle(.plain) .background(selected == result.target && selectedAction == nil ? Color.accentColor.opacity(0.14) : .clear, in: .rect(cornerRadius: 7)) .accessibilityAddTraits(selected == result.target && selectedAction == nil ? .isSelected : []) .id(String(describing: result.id)) } private func actionRow(_ action: MacTaskAction) -> some View { Button(action.title, role: action == .cancel ? .destructive : nil) { activate(action) } .buttonStyle(.plain).padding(10).frame(maxWidth: .infinity, alignment: .leading) .background(selectedAction == action ? Color.accentColor.opacity(0.14) : .clear, in: .rect(cornerRadius: 7)) .accessibilityAddTraits(selectedAction == action ? .isSelected : []) .id("action:" + action.rawValue) } private var selectedIsTask: Bool { if case .task = selected, selectedAction == nil { true } else { false } } private func resetSelection() { select(results.first?.target); selectedAction = results.isEmpty ? actions.first : nil } private func select(_ target: MacDestination?) { selected = target if case .task(let id) = target, taskContext?.taskID != id { taskContext = workspace.context?.taskID == id ? workspace.context : MacTaskContext(session: workspace.session, taskID: id) } } private func move(_ offset: Int) { let ids = rowIDs guard !ids.isEmpty else { return } let previous = selectedID.flatMap { ids.firstIndex(of: $0) } ?? (offset > 0 ? -1 : ids.count) let index = min(max(previous + offset, 0), ids.count - 1) let id = ids[index] if let result = results.first(where: { String(describing: $0.id) == id }) { selectedAction = nil; select(result.target) } else { selectedAction = actions.first { "action:" + $0.rawValue == id } } if NSWorkspace.shared.isVoiceOverEnabled { let label = selectedAction.map { $0.title + "\n" + (context?.title ?? "") } ?? results.first(where: { $0.target == selected }).map { $0.title + "\n" + $0.subtitle } if let label, let app = NSApp { NSAccessibility.post(element: app, notification: .announcementRequested, userInfo: [.announcement: label, .priority: NSAccessibilityPriorityLevel.medium.rawValue]) } } } private func activate(_ target: MacDestination, window: Bool) { if window, case .task(let id) = target { openWindow(id: "task", value: MacTaskWindowID(taskID: id, session: workspace.session)) } else { workspace.go(target); if workspace.standalone { openWindow(id: "main") } } dismiss() } private func activate(_ action: MacTaskAction) { guard let context, context.actions.contains(action) else { return } if workspace.queuePaletteAction(action, context: context) { openWindow(id: "main") } dismiss() } private func symbol(_ target: MacDestination) -> String { 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" } } } struct MacWorkspacePresenter: ViewModifier { @Bindable var workspace: MacWorkspace func body(content: Content) -> some View { content.sheet(isPresented: $workspace.palette, onDismiss: { let pending = workspace.pendingAction workspace.pendingAction = nil if let pending { Task { await workspace.performRoutedAction(pending.action, context: pending.context, navigation: pending.navigation) } } }) { MacJumpPalette(workspace: workspace) } .focusedSceneValue(workspace) } }