Repository Public
1import Foundation23/// A keyboard shortcut stored independently of AppKit so it can be persisted and tested.4struct KeyCombo: Codable, Equatable, Hashable, Sendable {5 /// A single lowercase character, or a named key: "return", "tab", "left", "right", "up", "down", "delete", "escape", "space".6 var key: String7 var command = false8 var shift = false9 var option = false10 var control = false1112 init(_ key: String, command: Bool = true, shift: Bool = false, option: Bool = false, control: Bool = false) {13 self.key = key.lowercased()14 self.command = command15 self.shift = shift16 self.option = option17 self.control = control18 }1920 var display: String {21 var out = ""22 if control { out += "⌃" }23 if option { out += "⌥" }24 if shift { out += "⇧" }25 if command { out += "⌘" }26 let names = ["return": "↩", "tab": "⇥", "left": "←", "right": "→", "up": "↑", "down": "↓",27 "delete": "⌫", "escape": "⎋", "space": "␣"]28 return out + (names[key] ?? key.uppercased())29 }3031 /// A shortcut needs ⌘ or ⌃ (or be a function-like named key with a modifier) to avoid stealing typing.32 var isUsable: Bool { !key.isEmpty && (command || control) }33}3435enum CommandCategory: String, CaseIterable, Sendable {36 case file, edit, view, navigation, tabs, bookmarks, tools, window3738 var title: String {39 switch self {40 case .file: return L("File")41 case .edit: return L("Edit")42 case .view: return L("View")43 case .navigation: return L("History")44 case .tabs: return L("Tabs")45 case .bookmarks: return L("Bookmarks")46 case .tools: return L("Tools")47 case .window: return L("Window")48 }49 }50}5152/// Stable identifiers of every action the browser can perform. They are the public automation surface:53/// menus, the command palette, keyboard shortcuts and App Intents all dispatch through these ids.54enum CommandID: String, CaseIterable, Sendable {55 case newTab, newWindow, newPrivateWindow, openFile, openLocation, closeTab, closeWindow, closeOtherTabs56 case reopenClosedTab, duplicateTab, pinTab, moveTabToNewWindow, savePageAsPDF, capturePage, printPage, sharePage57 case findInPage, findNext, findPrevious58 case reload, hardReload, stopLoading, goBack, goForward, goHome59 case zoomIn, zoomOut, zoomReset, toggleSidebar, toggleTabLayout, toggleFocusMode, toggleReader, toggleFullScreen60 case commandPalette, searchTabs, nextTab, previousTab, toggleSplitView, swapSplit61 case suspendTab, newTabGroup, nextWorkspace, previousWorkspace62 case addBookmark, showBookmarks, showHistory, showDownloads, showNotes, showSessions63 case importBookmarks, exportBookmarks, clearHistory, saveSession64 case pageInfo, viewSource, pageMetadata, toggleIntelligence, toggleBlockerForSite, clearSiteData65 case openSettings, changeUserAgent66 case addToReadingList, showReadingList, showOtherDevices, toggleFavorite, showAccount, syncNow67 // Added in 3.0 (AURA)68 case commandOrbit, showConstellation, showFlow, showCanvas, addToCanvas, showPortal, peekLink6970 var title: String {71 switch self {72 case .newTab: return L("New Tab")73 case .newWindow: return L("New Window")74 case .newPrivateWindow: return L("New Private Window")75 case .openFile: return L("Open File…")76 case .openLocation: return L("Open Location…")77 case .closeTab: return L("Close Tab")78 case .closeWindow: return L("Close Window")79 case .closeOtherTabs: return L("Close Other Tabs")80 case .reopenClosedTab: return L("Reopen Closed Tab")81 case .duplicateTab: return L("Duplicate Tab")82 case .pinTab: return L("Pin or Unpin Tab")83 case .moveTabToNewWindow: return L("Move Tab to New Window")84 case .savePageAsPDF: return L("Save Page as PDF…")85 case .capturePage: return L("Capture Page as Image…")86 case .printPage: return L("Print…")87 case .sharePage: return L("Share…")88 case .findInPage: return L("Find in Page…")89 case .findNext: return L("Find Next")90 case .findPrevious: return L("Find Previous")91 case .reload: return L("Reload Page")92 case .hardReload: return L("Reload Ignoring Cache")93 case .stopLoading: return L("Stop Loading")94 case .goBack: return L("Back")95 case .goForward: return L("Forward")96 case .goHome: return L("Home")97 case .zoomIn: return L("Zoom In")98 case .zoomOut: return L("Zoom Out")99 case .zoomReset: return L("Actual Size")100 case .toggleSidebar: return L("Show or Hide Sidebar")101 case .toggleTabLayout: return L("Switch Between Horizontal and Vertical Tabs")102 case .toggleFocusMode: return L("Focus Mode")103 case .toggleReader: return L("Reader View")104 case .toggleFullScreen: return L("Enter or Exit Full Screen")105 case .commandPalette: return L("Command Palette…")106 case .searchTabs: return L("Search Tabs…")107 case .nextTab: return L("Next Tab")108 case .previousTab: return L("Previous Tab")109 case .toggleSplitView: return L("Split View")110 case .swapSplit: return L("Swap Split Panes")111 case .suspendTab: return L("Suspend Background Tabs")112 case .newTabGroup: return L("New Tab Group from Current Tab")113 case .nextWorkspace: return L("Next Workspace")114 case .previousWorkspace: return L("Previous Workspace")115 case .addBookmark: return L("Bookmark This Page…")116 case .showBookmarks: return L("Show Bookmarks")117 case .showHistory: return L("Show History")118 case .showDownloads: return L("Show Downloads")119 case .showNotes: return L("Show Page Notes")120 case .showSessions: return L("Show Sessions")121 case .importBookmarks: return L("Import Bookmarks…")122 case .exportBookmarks: return L("Export Bookmarks…")123 case .clearHistory: return L("Clear History…")124 case .saveSession: return L("Save Current Session…")125 case .pageInfo: return L("Page Information")126 case .viewSource: return L("View Page Source")127 case .pageMetadata: return L("Page Metadata, Links and Resources")128 case .toggleIntelligence: return L("SP Intelligence")129 case .toggleBlockerForSite: return L("Turn Content Blocker On or Off for This Site")130 case .clearSiteData: return L("Clear Data for This Site…")131 case .openSettings: return L("Settings…")132 case .changeUserAgent: return L("User Agent…")133 case .addToReadingList: return L("Add to Reading List")134 case .showReadingList: return L("Show Reading List")135 case .showOtherDevices: return L("Show Tabs from Other Devices")136 case .toggleFavorite: return L("Add to or Remove from Favorites")137 case .showAccount: return L("Account and Synchronization…")138 case .syncNow: return L("Synchronize Now")139 case .commandOrbit: return L("Command Orbit…")140 case .showConstellation: return L("Tab Constellation")141 case .showFlow: return L("Show Flow")142 case .showCanvas: return L("Show Canvas")143 case .addToCanvas: return L("Add Page to Canvas")144 case .showPortal: return L("Portal: Other Devices")145 case .peekLink: return L("Peek at Hovered Link")146 }147 }148149 var category: CommandCategory {150 switch self {151 case .newTab, .newWindow, .newPrivateWindow, .openFile, .openLocation, .closeTab, .closeWindow,152 .savePageAsPDF, .capturePage, .printPage, .sharePage, .openSettings, .showAccount: return .file153 case .findInPage, .findNext, .findPrevious: return .edit154 case .reload, .hardReload, .stopLoading, .zoomIn, .zoomOut, .zoomReset, .toggleSidebar, .toggleTabLayout,155 .toggleFocusMode, .toggleReader, .toggleFullScreen, .commandPalette, .toggleSplitView, .swapSplit, .commandOrbit, .showFlow, .peekLink: return .view156 case .goBack, .goForward, .goHome, .showHistory, .clearHistory, .reopenClosedTab: return .navigation157 case .closeOtherTabs, .duplicateTab, .pinTab, .moveTabToNewWindow, .searchTabs, .nextTab, .previousTab,158 .suspendTab, .newTabGroup, .nextWorkspace, .previousWorkspace, .showOtherDevices, .showConstellation, .showPortal: return .tabs159 case .addBookmark, .showBookmarks, .importBookmarks, .exportBookmarks, .addToReadingList, .showReadingList, .toggleFavorite: return .bookmarks160 case .showDownloads, .showNotes, .showSessions, .saveSession, .pageInfo, .viewSource, .pageMetadata,161 .toggleIntelligence, .toggleBlockerForSite, .clearSiteData, .changeUserAgent, .syncNow, .showCanvas, .addToCanvas: return .tools162 }163 }164165 var defaultShortcut: KeyCombo? {166 switch self {167 case .newTab: return KeyCombo("t")168 case .newWindow: return KeyCombo("n")169 case .newPrivateWindow: return KeyCombo("n", shift: true)170 case .openFile: return KeyCombo("o")171 case .openLocation: return KeyCombo("l")172 case .closeTab: return KeyCombo("w")173 case .closeWindow: return KeyCombo("w", shift: true)174 case .closeOtherTabs: return KeyCombo("w", option: true)175 case .reopenClosedTab: return KeyCombo("t", shift: true)176 case .duplicateTab: return KeyCombo("d", shift: true)177 case .printPage: return KeyCombo("p")178 case .findInPage: return KeyCombo("f")179 case .findNext: return KeyCombo("g")180 case .findPrevious: return KeyCombo("g", shift: true)181 case .reload: return KeyCombo("r")182 case .hardReload: return KeyCombo("r", shift: true)183 case .stopLoading: return KeyCombo(".")184 case .goBack: return KeyCombo("[")185 case .goForward: return KeyCombo("]")186 case .goHome: return KeyCombo("h", shift: true)187 case .zoomIn: return KeyCombo("=")188 case .zoomOut: return KeyCombo("-")189 case .zoomReset: return KeyCombo("0")190 case .toggleSidebar: return KeyCombo("s", control: true)191 case .toggleFocusMode: return KeyCombo("f", shift: true)192 case .toggleReader: return KeyCombo("r", shift: false, option: true)193 case .toggleFullScreen: return KeyCombo("f", control: true)194 case .commandPalette: return KeyCombo("k")195 case .searchTabs: return KeyCombo("a", shift: true)196 case .nextTab: return KeyCombo("]", shift: true)197 case .previousTab: return KeyCombo("[", shift: true)198 case .toggleSplitView: return KeyCombo("\\")199 case .addBookmark: return KeyCombo("d")200 case .showBookmarks: return KeyCombo("b", option: true)201 case .showHistory: return KeyCombo("y")202 case .addToReadingList: return KeyCombo("d", option: true)203 case .showReadingList: return KeyCombo("l", control: true)204 case .showDownloads: return KeyCombo("l", option: true)205 case .pageInfo: return KeyCombo("i")206 case .viewSource: return KeyCombo("u", option: true)207 case .toggleIntelligence: return KeyCombo("i", shift: true)208 case .openSettings: return KeyCombo(",")209 case .nextWorkspace: return KeyCombo("right", option: true, control: true)210 case .previousWorkspace: return KeyCombo("left", option: true, control: true)211 case .commandOrbit: return KeyCombo("j")212 case .showConstellation: return KeyCombo("e", shift: true)213 case .showFlow: return KeyCombo("y", shift: true)214 case .showCanvas: return KeyCombo("b", shift: true)215 case .addToCanvas: return KeyCombo("b", control: true)216 case .showPortal: return KeyCombo("p", shift: true)217 case .peekLink: return KeyCombo("p", option: true)218 default: return nil219 }220 }221222 /// The effective shortcut after applying the user's overrides. An override with an empty key removes the shortcut.223 func shortcut(overrides: [String: KeyCombo]) -> KeyCombo? {224 if let custom = overrides[rawValue] { return custom.key.isEmpty ? nil : custom }225 return defaultShortcut226 }227228 /// Commands whose effective shortcut collides with another command's.229 static func conflicts(overrides: [String: KeyCombo]) -> [KeyCombo: [CommandID]] {230 var map: [KeyCombo: [CommandID]] = [:]231 for command in allCases {232 if let combo = command.shortcut(overrides: overrides) { map[combo, default: []].append(command) }233 }234 return map.filter { $0.value.count > 1 }235 }236}237238/// A user script injected into matching pages. Disabled globally by default (see `AppSettings.userScriptsEnabled`).239struct UserScript: Codable, Equatable, Identifiable, Sendable {240 enum RunAt: String, Codable, CaseIterable, Sendable { case documentStart, documentEnd }241 var id: UUID = UUID()242 var name: String243 var source: String244 /// Host patterns: "example.com" (that host and its sub-domains) or "*.example.com" (sub-domains only).245 /// A script with no pattern never runs — there is deliberately no "all sites" wildcard.246 var hosts: [String]247 var runAt: RunAt = .documentEnd248 var mainFrameOnly = true249 var enabled = false250251 func applies(to host: String?) -> Bool {252 guard enabled, let host = host?.lowercased(), !host.isEmpty else { return false }253 return hosts.contains { HostPattern.matches($0, host: host) }254 }255}256257enum HostPattern {258 static func isValid(_ pattern: String) -> Bool {259 var p = pattern.lowercased()260 if p.hasPrefix("*.") { p.removeFirst(2) }261 if p == "localhost" { return true }262 return AddressResolver.classify(host: p) != .notAHost263 }264265 static func matches(_ pattern: String, host: String) -> Bool {266 let p = pattern.lowercased().trimmingCharacters(in: .whitespaces), h = host.lowercased()267 guard isValid(p) else { return false }268 if p.hasPrefix("*.") { return h.hasSuffix(String(p.dropFirst(1))) }269 return h == p || h.hasSuffix("." + p)270 }271}272