Repository Public
1import SwiftUI23struct PaletteItem: Identifiable {4 enum Action { case command(CommandID), tab(UUID), url(String), search(String), workspace(UUID) }5 let id = UUID()6 var symbol: String7 var title: String8 var subtitle: String9 var trailing: String = ""10 var action: Action11}1213/// ⌘K: one field that runs commands, switches tabs and workspaces, and opens bookmarks and history.14/// Prefixes narrow the search: ">" commands, "@" tabs, "#" bookmarks.15struct CommandPaletteView: View {16 @Bindable var state: BrowserWindowState17 let environment: AppEnvironment18 @State private var query = ""19 @State private var selection = 02021 var body: some View {22 let items = results()23 ZStack(alignment: .top) {24 Color.black.opacity(0.24).ignoresSafeArea().onTapGesture(perform: dismiss)25 .accessibilityLabel(L("Close")).accessibilityAddTraits(.isButton)26 .transition(.opacity)27 AuraFloatingPanel(radius: Theme.radiusXL) {28 AuraCommandPalette(query: $query, placeholder: L("Type a command, a tab name or an address…"),29 items: items.enumerated().map { index, item in30 AuraCommandItem(id: "\(index)|\(item.title)|\(item.subtitle)", symbol: item.symbol, title: item.title, subtitle: item.subtitle, trailing: item.trailing)31 },32 selection: $selection, hint: L("↑↓ to move · ↩ to run · > commands · @ tabs · # bookmarks"),33 onRun: { chosen in34 if let index = Int(chosen.id.prefix { $0 != "|" }), items.indices.contains(index) { run(items[index]) }35 }, onDismiss: dismiss)36 }37 .frame(width: 640)38 .padding(.top, 110)39 }40 .onAppear { query = state.palettePrefill }41 .accessibilityAddTraits(.isModal)42 }4344 private func dismiss() {45 AuraMotion.perform(.standard) { state.paletteVisible = false }46 state.window?.makeFirstResponder(state.selectedTab?.webView)47 }4849 private func run(_ item: PaletteItem) {50 dismiss()51 switch item.action {52 case .command(let command): state.perform(command)53 case .tab(let id): if let tab = state.tab(id: id) { state.select(tab) }54 case .url(let text): state.navigate(text, newTab: state.selectedTab?.isBlank == false)55 case .search(let text): state.navigate(text, newTab: state.selectedTab?.isBlank == false)56 case .workspace(let id): state.switchWorkspace(to: id)57 }58 }5960 private func matches(_ text: String, _ needle: String) -> Bool {61 needle.isEmpty || needle.split(separator: " ").allSatisfy { text.localizedCaseInsensitiveContains($0) }62 }6364 private func results() -> [PaletteItem] {65 var text = query.trimmingCharacters(in: .whitespaces)66 var scope: Character?67 if let first = text.first, ">@#".contains(first) { scope = first; text.removeFirst(); text = text.trimmingCharacters(in: .whitespaces) }68 var items: [PaletteItem] = []69 let overrides = environment.settings.shortcutOverrides7071 if scope == nil || scope == "@" {72 for tab in state.tabs where tab.id != state.selectedTabID && matches(tab.displayTitle + " " + (tab.url?.absoluteString ?? ""), text) {73 let workspace = state.workspaces.first { $0.id == tab.workspaceID }?.name ?? ""74 items.append(PaletteItem(symbol: "square.on.square", title: tab.displayTitle, subtitle: tab.url?.host ?? "", trailing: workspace, action: .tab(tab.id)))75 }76 if scope == nil, !text.isEmpty { items = Array(items.prefix(5)) }77 }78 if scope == nil || scope == ">" {79 let commands = CommandID.allCases.filter { $0 != .commandPalette && state.isEnabled($0) && matches($0.title + " " + $0.category.title, text) }80 items += commands.map { PaletteItem(symbol: OrbitCatalog.symbol($0), title: $0.title, subtitle: $0.category.title, trailing: $0.shortcut(overrides: overrides)?.display ?? "", action: .command($0)) }81 for workspace in state.workspaces where workspace.id != state.activeWorkspaceID && matches(workspace.name, text) {82 items.append(PaletteItem(symbol: workspace.symbol, title: L("Switch to %@", workspace.name), subtitle: L("Workspace"), action: .workspace(workspace.id)))83 }84 }85 if (scope == nil && !text.isEmpty) || scope == "#" {86 for bookmark in state.profile.bookmarks.search(text, limit: scope == "#" ? 40 : 5) {87 items.append(PaletteItem(symbol: "bookmark.fill", title: bookmark.title, subtitle: bookmark.url ?? "", action: .url(bookmark.url ?? "")))88 }89 }90 if scope == nil, !text.isEmpty {91 if !state.isPrivate {92 for entry in state.profile.history.suggestions(for: text, limit: 5) {93 items.append(PaletteItem(symbol: "clock", title: entry.title.isEmpty ? entry.url : entry.title, subtitle: entry.url, action: .url(entry.url)))94 }95 }96 let settings = environment.settings97 if let resolved = AddressResolver.resolve(text, engine: settings.activeSearchEngine(), engines: settings.allSearchEngines()) {98 let isSearch = resolved.isSearch99 items.append(PaletteItem(symbol: isSearch ? "magnifyingglass" : "globe", title: text,100 subtitle: isSearch ? L("Search with %@", settings.activeSearchEngine().name) : L("Open address"),101 action: isSearch ? .search(text) : .url(text)))102 }103 }104 return items105 }106}107