SPB Git

Repository Public

AppSettings.swift 5.3 KB 90 lines swift

1import Foundation2import SwiftUI3import SheetCore4import SheetUI56/// User preferences (UserDefaults). Nothing here leaves the Mac.7@MainActor @Observable8final class AppSettings {9    static let shared = AppSettings()10    enum Separators: String, CaseIterable { case automatic, comma, semicolon }11    enum Appearance: String, CaseIterable { case system, light, dark }1213    var separators: Separators { didSet { UserDefaults.standard.set(separators.rawValue, forKey: "separators") } }14    var appearance: Appearance { didSet { UserDefaults.standard.set(appearance.rawValue, forKey: "appearance"); applyAppearance() } }15    var showWelcomeAtLaunch: Bool { didSet { UserDefaults.standard.set(showWelcomeAtLaunch, forKey: "showWelcome") } }16    var backgroundCalcThreshold: Int { didSet { UserDefaults.standard.set(backgroundCalcThreshold, forKey: "calcThreshold") } }17    /// Zoom of the grid in new windows, in percent (75–200): the app's text-size setting for the sheet.18    var defaultZoomPercent: Int { didSet { UserDefaults.standard.set(defaultZoomPercent, forKey: "defaultZoom") } }19    static let zoomRange = 75...2002021    private init() {22        let d = UserDefaults.standard23        separators = Separators(rawValue: d.string(forKey: "separators") ?? "") ?? .automatic24        appearance = Appearance(rawValue: d.string(forKey: "appearance") ?? "") ?? .system25        showWelcomeAtLaunch = d.object(forKey: "showWelcome") as? Bool ?? true26        backgroundCalcThreshold = d.object(forKey: "calcThreshold") as? Int ?? 20_00027        defaultZoomPercent = min(max(d.object(forKey: "defaultZoom") as? Int ?? 100, Self.zoomRange.lowerBound), Self.zoomRange.upperBound)28    }2930    /// Locale used to read what the user types (decimal and argument separators). Files always store the canonical form.31    var locale: Locale {32        switch separators {33        case .automatic: return .current34        case .comma: return Locale(identifier: "en_US")35        case .semicolon: return Locale(identifier: "fr_CA")36        }37    }3839    func applyAppearance() {40        switch appearance {41        case .system: NSApp.appearance = nil42        case .light: NSApp.appearance = NSAppearance(named: .aqua)43        case .dark: NSApp.appearance = NSAppearance(named: .darkAqua)44        }45    }46}4748struct SettingsView: View {49    @Bindable var settings = AppSettings.shared5051    var body: some View {52        Form {53            Section(NSLocalizedString("Formulas", comment: "")) {54                Picker(NSLocalizedString("Separators", comment: ""), selection: $settings.separators) {55                    Text(NSLocalizedString("Follow system region", comment: "")).tag(AppSettings.Separators.automatic)56                    Text(NSLocalizedString("Period and comma: =SUM(1.5, 2)", comment: "")).tag(AppSettings.Separators.comma)57                    Text(NSLocalizedString("Comma and semicolon: =SUM(1,5; 2)", comment: "")).tag(AppSettings.Separators.semicolon)58                }59                Text(NSLocalizedString("Applies to new windows. Workbooks always store formulas in a locale-independent form.", comment: ""))60                    .font(.caption).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true)61                Stepper(value: $settings.backgroundCalcThreshold, in: 1_000...1_000_000, step: 5_000) {62                    Text(String(format: NSLocalizedString("Calculate in the background above %d pending formulas", comment: ""), settings.backgroundCalcThreshold))63                }64            }65            Section(NSLocalizedString("Appearance", comment: "")) {66                Picker(NSLocalizedString("Theme", comment: ""), selection: $settings.appearance) {67                    Text(NSLocalizedString("System", comment: "")).tag(AppSettings.Appearance.system)68                    Text(NSLocalizedString("Light", comment: "")).tag(AppSettings.Appearance.light)69                    Text(NSLocalizedString("Dark", comment: "")).tag(AppSettings.Appearance.dark)70                }71                Toggle(NSLocalizedString("Show the template chooser at launch", comment: ""), isOn: $settings.showWelcomeAtLaunch)72            }73            Section(NSLocalizedString("Accessibility", comment: "")) {74                Stepper(value: $settings.defaultZoomPercent, in: AppSettings.zoomRange, step: 5) {75                    Text(String(format: NSLocalizedString("Default zoom of the grid: %d%%", comment: ""), settings.defaultZoomPercent))76                }77                .accessibilityValue(String(format: NSLocalizedString("%d%%", comment: ""), settings.defaultZoomPercent))78                Text(NSLocalizedString("Sets the size of cell text and headers in new windows. ORIX Sheet also follows the system settings Reduce Motion, Increase Contrast and Differentiate Without Colour.", comment: ""))79                    .font(.caption).foregroundStyle(.secondary)80            }81            Section(NSLocalizedString("Privacy", comment: "")) {82                Label(NSLocalizedString("ORIX Sheet has no account and no analytics. Calculation, files, Python and AI assistance all run on this Mac. The only network use is installing a Python package, and only when you ask for it in Python Studio.", comment: ""), systemImage: "lock.shield")83                    .font(.callout)84            }85        }86        .formStyle(.grouped)87        .frame(width: 560, height: 660)88    }89}90
Files main
Menu

Theme