Repository Public License
1import Foundation2import LunarDesign3import NavigatorCore4import SwiftUI5import WebRuntime67// MARK: - Shortcut89/// A default keyboard shortcut of a command, independent of SwiftUI so that it can be compared, listed in Help10/// and checked for collisions.11public struct CommandShortcut: Hashable, Sendable, CustomStringConvertible {12 public enum Key: Hashable, Sendable {13 case character(Character)14 case tab, `return`, escape, delete, space, upArrow, downArrow, leftArrow, rightArrow15 }1617 public struct Modifiers: OptionSet, Hashable, Sendable {18 public let rawValue: Int19 public init(rawValue: Int) { self.rawValue = rawValue }20 public static let command = Modifiers(rawValue: 1)21 public static let shift = Modifiers(rawValue: 2)22 public static let option = Modifiers(rawValue: 4)23 public static let control = Modifiers(rawValue: 8)24 }2526 public var key: Key27 public var modifiers: Modifiers2829 public init(_ key: Key, modifiers: Modifiers = .command) {30 // Letters are stored in lower case: "T" with ⌘ and "t" with ⌘ are the same shortcut.31 if case .character(let character) = key {32 self.key = .character(Character(character.lowercased()))33 } else {34 self.key = key35 }36 self.modifiers = modifiers37 }3839 public init(_ character: Character, modifiers: Modifiers = .command) {40 self.init(.character(character), modifiers: modifiers)41 }4243 public var keyEquivalent: KeyEquivalent {44 switch key {45 case .character(let character): KeyEquivalent(character)46 case .tab: .tab47 case .return: .return48 case .escape: .escape49 case .delete: .delete50 case .space: .space51 case .upArrow: .upArrow52 case .downArrow: .downArrow53 case .leftArrow: .leftArrow54 case .rightArrow: .rightArrow55 }56 }5758 public var eventModifiers: EventModifiers {59 var result: EventModifiers = []60 if modifiers.contains(.command) { result.insert(.command) }61 if modifiers.contains(.shift) { result.insert(.shift) }62 if modifiers.contains(.option) { result.insert(.option) }63 if modifiers.contains(.control) { result.insert(.control) }64 return result65 }6667 public var keyboardShortcut: KeyboardShortcut { KeyboardShortcut(keyEquivalent, modifiers: eventModifiers) }6869 /// The key caps in the order macOS menus use: ⌃ ⌥ ⇧ ⌘ then the key.70 public var keyCaps: [String] {71 var caps: [String] = []72 if modifiers.contains(.control) { caps.append("⌃") }73 if modifiers.contains(.option) { caps.append("⌥") }74 if modifiers.contains(.shift) { caps.append("⇧") }75 if modifiers.contains(.command) { caps.append("⌘") }76 switch key {77 case .character(let character): caps.append(String(character).uppercased())78 case .tab: caps.append("⇥")79 case .return: caps.append("↩")80 case .escape: caps.append("⎋")81 case .delete: caps.append("⌫")82 case .space: caps.append("␣")83 case .upArrow: caps.append("↑")84 case .downArrow: caps.append("↓")85 case .leftArrow: caps.append("←")86 case .rightArrow: caps.append("→")87 }88 return caps89 }9091 /// "⇧⌘T".92 public var description: String { keyCaps.joined() }93}9495// MARK: - Category, placement9697/// Heading under which a command is listed in the command palette and in Help.98public struct CommandCategory: Identifiable, Hashable, Sendable {99 public let id: String100 public let title: LocalizedText101 public let order: Int102103 public init(id: String, title: LocalizedText, order: Int) { self.id = id; self.title = title; self.order = order }104105 public static let tabs = CommandCategory(id: "tabs", title: T("Tabs"), order: 10)106 public static let navigation = CommandCategory(id: "navigation", title: T("Navigation"), order: 20)107 public static let page = CommandCategory(id: "page", title: T("Page"), order: 30)108 public static let view = CommandCategory(id: "view", title: T("View"), order: 40)109 public static let bookmarks = CommandCategory(id: "bookmarks", title: T("Bookmarks"), order: 50)110 public static let history = CommandCategory(id: "history", title: T("History"), order: 60)111 public static let windows = CommandCategory(id: "windows", title: T("Windows"), order: 70)112 public static let tools = CommandCategory(id: "tools", title: T("Tools"), order: 80)113 public static let privacy = CommandCategory(id: "privacy", title: T("Privacy"), order: 90)114 public static let app = CommandCategory(id: "app", title: T("ORIX Navigator"), order: 1_000)115}116117/// Menus of the menu bar (Mac) and of the hardware-keyboard command list (iPad).118public enum MenuBarMenu: String, Sendable, CaseIterable {119 /// The application menu, next to "Settings…" (Mac only).120 case app121 case file, edit, view, history, bookmarks122 /// Feature commands that fit nowhere else. The menu exists only when a command asks for it.123 case tools124 case window, help125}126127/// Where a command shows up besides the palette. `group` separates runs of items with a divider (lower groups128/// first); `order` sorts inside a group. The shell uses multiples of 10 for both, leaving room in between.129public enum CommandPlacement: Hashable, Sendable {130 case menuBar(MenuBarMenu, group: Int, order: Int)131 /// The "…" menu of the toolbar capsule (Mac, iPad) and of the phone dock.132 case windowMenu(group: Int, order: Int)133 /// The context menu of a tab (sidebar row, strip chip, pinned tile). The context's `tab` is the tab clicked.134 case tabContextMenu(group: Int, order: Int)135 /// The context menu of the page (Mac: every element; iOS: links). Appended after WebKit's own items.136 case pageContextMenu(order: Int)137138 var sortKey: (Int, Int) {139 switch self {140 case .menuBar(_, let group, let order), .windowMenu(let group, let order), .tabContextMenu(let group, let order):141 (group, order)142 case .pageContextMenu(let order): (0, order)143 }144 }145}146147/// What triggered a command.148public enum CommandSource: String, Sendable {149 case menuBar, windowMenu, contextMenu, toolbar, sidebar, palette, help, launchArgument, feature150}151152// MARK: - Context153154/// Everything a command may act on. Built by the shell for the focused window; `tab` is the selected tab unless155/// the command was invoked on a specific one (tab context menu).156@MainActor157public struct CommandContext {158 public let environment: AppEnvironment159 public let store: BrowserStore160 /// `nil` when no browser window has focus (Mac with every window closed).161 public let window: WindowModel?162 public let tab: TabModel?163 public var source: CommandSource164 /// The command is shown in the compact-width layout (iPhone, narrow iPad window).165 public var isCompactWidth: Bool166167 public init(environment: AppEnvironment, store: BrowserStore, window: WindowModel?, tab: TabModel? = nil,168 source: CommandSource = .feature, isCompactWidth: Bool = false) {169 self.environment = environment170 self.store = store171 self.window = window172 self.tab = tab ?? window?.selectedTab173 self.source = source174 self.isCompactWidth = isCompactWidth175 }176177 public var registry: FeatureRegistry { store.registry }178 public var services: FeatureServices { store.services }179 public var settings: AppSettings { store.settings }180 public var engine: WebEngine? { tab?.engine }181 /// Address of the target tab (`nil` for the new-tab page).182 public var url: URL? { tab?.engine.url }183 /// The target tab shows something other than the new-tab page.184 public var hasPage: Bool { tab.map { !$0.isBlank } ?? false }185 /// The target tab shows an http(s) page.186 public var isWebPage: Bool {187 guard let scheme = url?.scheme?.lowercased() else { return false }188 return scheme == "http" || scheme == "https"189 }190 public var isEphemeralWindow: Bool { window?.isEphemeral ?? false }191192 /// The same context, aimed at another tab of the window.193 public func targeting(_ tab: TabModel?, source: CommandSource? = nil) -> CommandContext {194 CommandContext(environment: environment, store: store, window: window, tab: tab, source: source ?? self.source,195 isCompactWidth: isCompactWidth)196 }197}198199// MARK: - Command200201/// One action of the browser. Menus, keyboard shortcuts, context menus, the command palette, Help and the202/// `-ORIXRun` launch argument all run commands from the registry: an action exists once.203public struct NavigatorCommand: Identifiable {204 /// Stable, dotted, lower-case: `tab.new`, `orbit.open`. Never shown to the user; never reused.205 public let id: String206 /// Name in the palette, in Help and (unless `dynamicTitle` answers) in menus.207 public var title: LocalizedText208 public var category: CommandCategory209 public var icon: LunarIcon?210 public var shortcut: CommandShortcut?211 /// Extra search terms for the palette, in the app's language.212 public var keywords: [String]213 public var placements: [CommandPlacement]214 /// Menus drop the item instead of disabling it when the command is unavailable.215 public var hidesWhenUnavailable: Bool216 /// Title for the current state ("Unpin Tab", "Hide Sidebar"). `nil` keeps `title`.217 public var dynamicTitle: (@MainActor (CommandContext) -> LocalizedText?)?218 public var isAvailable: @MainActor (CommandContext) -> Bool219 /// Non-nil for commands with an on/off state: menus show a check mark.220 public var isChecked: (@MainActor (CommandContext) -> Bool)?221 public var perform: @MainActor (CommandContext) async -> Void222223 public init(id: String, title: LocalizedText, category: CommandCategory, icon: LunarIcon? = nil,224 shortcut: CommandShortcut? = nil, keywords: [String] = [], placements: [CommandPlacement] = [],225 hidesWhenUnavailable: Bool = false,226 dynamicTitle: (@MainActor (CommandContext) -> LocalizedText?)? = nil,227 isAvailable: @escaping @MainActor (CommandContext) -> Bool = { _ in true },228 isChecked: (@MainActor (CommandContext) -> Bool)? = nil,229 perform: @escaping @MainActor (CommandContext) async -> Void) {230 self.id = id; self.title = title; self.category = category; self.icon = icon; self.shortcut = shortcut231 self.keywords = keywords; self.placements = placements; self.hidesWhenUnavailable = hidesWhenUnavailable232 self.dynamicTitle = dynamicTitle; self.isAvailable = isAvailable; self.isChecked = isChecked233 self.perform = perform234 }235236 /// The title to show for the current state.237 @MainActor public func displayTitle(in context: CommandContext) -> String {238 (dynamicTitle?(context) ?? title).string239 }240241 func placement(where matches: (CommandPlacement) -> Bool) -> CommandPlacement? { placements.first(where: matches) }242}243