SPB Git

Repository Public

AppSettings.swift 5.7 KB 155 lines swift

1import Foundation23enum TabLayout: String, Codable, CaseIterable, Sendable { case horizontal, vertical }4enum AppearanceMode: String, Codable, CaseIterable, Sendable { case system, light, dark }5enum NewTabContent: String, Codable, CaseIterable, Sendable { case startPage, homepage, blank }67struct StartPageSettings: Codable, Equatable, Sendable {8    var showGreeting = true9    var showSearch = true10    var showFavorites = true11    var showTopSites = true12    var showRecentlyClosed = false13    var showPrivacyNote = true14    // Added in 2.015    var showSyncedFavorites = true16    var showRecentlyVisited = true17    var showRecentSearches = true18    var showBookmarks = false19    var showOtherDevices = true20    var showReadingList = true21    var showPrivacyDashboard = false22    var background: StartPageBackground = .aurora23    // Added in 3.0: the ambient theme replaces `background` (kept so 2.0 files still decode and can be migrated).24    var ambient: AmbientTheme = .aurora25    var showWorkspaces = true26    var showRecentSessions = true27}2829enum StartPageBackground: String, Codable, CaseIterable, Sendable {30    case aurora, plain, dusk, forest, graphite31}3233/// Every user preference. New fields must have a default: `JSONFile.decodeMerging` fills in34/// keys that are missing from files written by older versions.35struct AppSettings: Codable, Equatable, Sendable {36    static let currentSchema = 437    var schemaVersion = AppSettings.currentSchema3839    // General40    var homepage = ""41    var newTabContent: NewTabContent = .startPage42    var restoreSessionOnLaunch = true43    var confirmBeforeClosingManyTabs = true4445    // Appearance46    var appearance: AppearanceMode = .system47    var tabLayout: TabLayout = .horizontal48    // AURA (3.0)49    /// Preview a link when the pointer rests on it (⌥-hover and the shortcut always work).50    var peekOnHover = false51    /// Thin progress line while reading, in Focus Mode and in the reader.52    var focusTrailEnabled = true53    /// Hide the browser's own distractions (toasts, badges, loading indicators) while in Focus Mode.54    var focusHidesDistractions = true55    /// Decorative motion (ambient backgrounds, shimmer). Reduce Motion and Low Power always win.56    var ambientMotionEnabled = true57    /// Record how pages lead to one another (AURA Flow). Never recorded in private browsing.58    var flowEnabled = true59    var sidebarVisible = true60    var showFullURL = false61    var startPage = StartPageSettings()6263    // Search64    var searchEngineID = SearchEngine.fallback.id65    var customSearchEngines: [SearchEngine] = []66    var suggestFromHistory = true67    var suggestFromBookmarks = true6869    // Account & sync70    /// When off, nothing is sent to or fetched from SP Sync, even while signed in.71    var syncEnabled = true7273    // Tabs74    /// Minutes of inactivity after which a background tab is unloaded; 0 disables suspension.75    var suspendTabsAfterMinutes = 307677    // Downloads78    var askWhereToSaveDownloads = false7980    // Privacy & security81    var contentBlockerEnabled = true82    var blockTrackers = true83    var blockAds = true84    /// Hosts on which the content blocker is turned off.85    var blockerDisabledHosts: [String] = []86    var blockPopups = true87    var upgradeToHTTPS = true88    var warnOnDeceptiveURLs = true89    var customUserAgent = ""90    /// Local, opt-in diagnostics log. Nothing is ever uploaded.91    var diagnosticsEnabled = false9293    // Automation94    var userScriptsEnabled = false9596    // Keyboard: command id → key combination overriding the default.97    var shortcutOverrides: [String: KeyCombo] = [:]9899    // Intelligence100    var aiEnabled = false101    var aiProviders: [AIProviderConfig] = []102    var aiActiveProviderID: String?103    var aiPreferLocal = true104    var aiMaxPageCharacters = 24_000105    var aiKeepHistory = true106107    func allSearchEngines() -> [SearchEngine] { SearchEngine.builtIn + customSearchEngines.filter(\.isValid) }108109    func activeSearchEngine() -> SearchEngine {110        allSearchEngines().first { $0.id == searchEngineID } ?? SearchEngine.fallback111    }112113    func isBlockerDisabled(forHost host: String) -> Bool {114        let h = host.lowercased()115        return blockerDisabledHosts.contains { h == $0 || h.hasSuffix("." + $0) }116    }117}118119/// Loads and saves `AppSettings`, migrating older files forward.120final class SettingsStore: Sendable {121    private let store: JSONFileStore<AppSettings>122123    init(url: URL?) {124        store = JSONFileStore(url: url, defaults: AppSettings())125        if store.value.schemaVersion < AppSettings.currentSchema {126            store.update(Self.migrate)127        }128    }129130    var settings: AppSettings {131        get { store.value }132        set { store.value = newValue }133    }134135    var recoveredFromCorruption: Bool { store.recoveredFromCorruption }136137    static func migrate(_ settings: inout AppSettings) {138        if settings.schemaVersion < 2 {139            // v1 stored hosts with a leading "www."; v2 matches sub-domains itself.140            settings.blockerDisabledHosts = Array(Set(settings.blockerDisabledHosts.map {141                $0.lowercased().hasPrefix("www.") ? String($0.lowercased().dropFirst(4)) : $0.lowercased()142            })).sorted()143        }144        if settings.schemaVersion < 3 {145            // 2.0: SP Search replaces DuckDuckGo as the default. A deliberate other choice (Google, a custom engine…) is kept.146            if settings.searchEngineID == "duckduckgo" { settings.searchEngineID = SearchEngine.spSearch.id }147        }148        if settings.schemaVersion < 4 {149            // 3.0: the five static backgrounds become ambient themes; the user's choice is carried over.150            settings.startPage.ambient = AmbientTheme(legacy: settings.startPage.background)151        }152        settings.schemaVersion = AppSettings.currentSchema153    }154}155
Files main
Menu

Theme