SPB Git

Repository Public

Orbit.swift 9.4 KB 207 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : SP Browser9// File             : SPBrowser/Core/Aura/Orbit.swift10// Module           : SPBrowser11// Repository       : https://git.spboucher.ai/sp-browser12// Created          : 2026-11-1913// Last Modified    : 2026-11-1914//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author           : Simon-Pierre Boucher20// Position         : Professor of Finance21// Institution      : Université du Québec en Outaouais (UQO)22// Department       : Département des sciences administratives23// Organization     : SP Boucher24//25// Website          : https://spboucher.ai26// Git              : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General          : info@spboucher.ai33// Professional     : contact@spboucher.ai34// Direct           : simon@spboucher.ai35// Research         : research@spboucher.ai36// Development      : dev@spboucher.ai37// Support          : support@spboucher.ai38// Security/Admin   : admin@spboucher.ai39// Notifications    : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification   : Public66// Distribution     : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License          : None specified (no license file in this repository)80// Maintainer       : Simon-Pierre Boucher81// Technical Contact: dev@spboucher.ai82//83// =============================================================================84// END OF SP BOUCHER SOURCE HEADER85// =============================================================================8687import Foundation8889/// What the user is looking at when Command Orbit opens. Orbit never has commands of its own:90/// it picks from the existing catalog (`CommandID`) the few actions that make sense here and now.91struct OrbitContext: Equatable, Sendable {92    enum Platform: Sendable { case mac, phone, pad }93    var platform: Platform94    var hasPage = false95    var isSearchPage = false96    var isPrivate = false97    var isReader = false98    var aiEnabled = false99    var isSignedIn = false100    var tabCount = 1101    var hasOtherDevices = false102}103104enum OrbitCatalog {105    /// Commands each platform can execute. iOS runs a subset of the catalog (no windows, no split view, no developer tools).106    static func supported(on platform: OrbitContext.Platform) -> Set<CommandID> {107        switch platform {108        case .mac: return Set(CommandID.allCases)109        case .phone, .pad:110            return [.newTab, .newPrivateWindow, .openLocation, .closeTab, .closeOtherTabs, .reopenClosedTab, .duplicateTab, .sharePage, .findInPage,111                    .reload, .stopLoading, .goBack, .goForward, .toggleFocusMode, .toggleReader, .commandPalette, .commandOrbit, .searchTabs,112                    .nextTab, .previousTab, .addBookmark, .showBookmarks, .showHistory, .showDownloads, .openSettings, .addToReadingList,113                    .showReadingList, .showOtherDevices, .toggleFavorite, .showAccount, .syncNow,114                    .showConstellation, .showFlow, .showCanvas, .addToCanvas, .showPortal, .capturePage]115        }116    }117118    /// The ring: at most eight actions, the ones that fit the context, in a stable order.119    static func ring(for context: OrbitContext) -> [CommandID] {120        var out: [CommandID] = [.openLocation]121        if context.hasPage && !context.isSearchPage {122            out += [.toggleReader, .addToCanvas, .sharePage]123            if context.aiEnabled && context.platform == .mac { out.append(.toggleIntelligence) }124            out.append(context.isPrivate ? .addBookmark : .addToReadingList)125            out.append(.capturePage)126        } else if context.hasPage {127            out += [.addToCanvas, .sharePage]128        }129        out.append(context.tabCount > 3 ? .showConstellation : .newTab)130        out.append(.showFlow)131        if !out.contains(.addToCanvas) { out.append(.showCanvas) }132        if context.isSignedIn && context.hasOtherDevices && !context.isPrivate { out.append(.showPortal) }133        if !context.hasPage { out += [.showBookmarks, .showHistory] }134        let allowed = supported(on: context.platform)135        var seen = Set<CommandID>()136        return Array(out.filter { allowed.contains($0) && seen.insert($0).inserted }.prefix(8))137    }138139    /// A short verb for the ring, where the full menu title would not fit.140    static func shortTitle(_ command: CommandID) -> String {141        switch command {142        case .openLocation: return L("Search")143        case .toggleReader: return L("Reader")144        case .addToCanvas: return L("To Canvas")145        case .sharePage: return L("Share")146        case .toggleIntelligence: return L("Summarize")147        case .addToReadingList: return L("Read Later")148        case .addBookmark: return L("Bookmark")149        case .capturePage: return L("Capture")150        case .showConstellation: return L("Constellation")151        case .newTab: return L("New Tab")152        case .showFlow: return L("Flow")153        case .showCanvas: return L("Canvas")154        case .showPortal: return L("Portal")155        case .showBookmarks: return L("Bookmarks")156        case .showHistory: return L("History")157        default: return command.title158        }159    }160161    static func symbol(_ command: CommandID) -> String {162        switch command {163        case .openLocation: return "magnifyingglass"164        case .toggleReader: return "doc.plaintext"165        case .addToCanvas: return "plus.rectangle.on.rectangle"166        case .sharePage: return "square.and.arrow.up"167        case .toggleIntelligence: return "sparkles"168        case .addToReadingList: return "eyeglasses"169        case .addBookmark: return "bookmark"170        case .capturePage: return "camera.viewfinder"171        case .showConstellation: return "sparkle"172        case .newTab: return "plus"173        case .showFlow: return "point.3.connected.trianglepath.dotted"174        case .showCanvas: return "rectangle.3.group"175        case .showPortal: return "laptopcomputer.and.iphone"176        case .showBookmarks: return "book"177        case .showHistory: return "clock"178        case .toggleFocusMode: return "eye"179        case .newPrivateWindow: return "eyeglasses"180        case .reload: return "arrow.clockwise"181        case .closeTab: return "xmark"182        case .findInPage: return "doc.text.magnifyingglass"183        case .openSettings: return "gearshape"184        default: return "command"185        }186    }187188    /// Filters and orders commands for a typed query: prefix matches first, then word matches; recently used break ties.189    static func rank(_ commands: [CommandID], query: String, recent: [CommandID] = []) -> [CommandID] {190        let needle = query.trimmingCharacters(in: .whitespaces).lowercased()191        guard !needle.isEmpty else {192            return commands.sorted { (recent.firstIndex(of: $0) ?? Int.max, $0.title) < (recent.firstIndex(of: $1) ?? Int.max, $1.title) }193        }194        let words = needle.split(separator: " ")195        func score(_ command: CommandID) -> Int? {196            let title = command.title.lowercased(), short = shortTitle(command).lowercased()197            if title.hasPrefix(needle) || short.hasPrefix(needle) { return 0 }198            if title.split(separator: " ").contains(where: { $0.hasPrefix(needle) }) { return 1 }199            if words.allSatisfy({ title.contains($0) || command.category.title.lowercased().contains($0) }) { return 2 }200            return nil201        }202        return commands.compactMap { c in score(c).map { (c, $0) } }203            .sorted { ($0.1, recent.firstIndex(of: $0.0) ?? Int.max, $0.0.title) < ($1.1, recent.firstIndex(of: $1.0) ?? Int.max, $1.0.title) }204            .map(\.0)205    }206}207
Files main
Menu

Theme