import AppKit /// Impostazioni dell'utente (Cmd+,), salvate nelle preferenze del Mac di chi usa l'app. /// Niente qui dentro è legato a un Mac o a una persona: i valori predefiniti valgono per tutti. final class Settings { static let shared = Settings() /// Inviata quando cambia qualcosa: pannelli e finestre si ridisegnano. static let changed = Notification.Name("MacCommander.impostazioniCambiate") private let d = UserDefaults.standard private static let prefix = "imp." struct Defaults { static let fontSize = 13.0 static let editor = "/System/Applications/TextEdit.app" static let terminal = "/System/Applications/Utilities/Terminal.app" } // MARK: Aspetto /// Modalità chiara, scura o come il Mac (Impostazioni di Sistema › Aspetto). enum Mode: Int { case system, light, dark } var mode: Mode { get { Mode(rawValue: d.object(forKey: Self.prefix + "modalita") as? Int ?? Mode.dark.rawValue) ?? .dark } set { d.set(newValue.rawValue, forKey: Self.prefix + "modalita"); applyMode() } } /// Chiede ad AppKit l'aspetto giusto; il resto si aggiorna con la notifica. func applyMode() { switch mode { case .system: NSApp.appearance = nil case .light: NSApp.appearance = NSAppearance(named: .aqua) case .dark: NSApp.appearance = NSAppearance(named: .darkAqua) } NotificationCenter.default.post(name: Self.changed, object: nil) } var isDark: Bool { switch mode { case .light: return false case .dark: return true case .system: return NSApp.effectiveAppearance.bestMatch(from: [.darkAqua, .aqua]) == .darkAqua } } /// I colori dei pannelli, come in Total Commander ([Colors] e [ColorsDark]): una serie per modalità. enum ColorRole: String, CaseIterable { case background = "sfondo", background2 = "sfondo2", text = "testo", hidden = "nascosti" case marked = "selezionati", cursor = "cursore", cursorText = "testoCursore", pathBar = "barra" var title: String { switch self { case .background: return "Sfondo" case .background2: return "Sfondo righe alterne" case .text: return "Testo" case .hidden: return "File nascosti" case .marked: return "File selezionati" case .cursor: return "Cursore" case .cursorText: return "Testo sul cursore pieno" case .pathBar: return "Barra del pannello attivo" } } /// Scura: il Total Commander di Paolo. Chiara: gli stessi rossi su bianco. var defaults: (dark: String, light: String) { switch self { case .background, .background2: return ("#000000", "#FFFFFF") case .text: return ("#FFFFFF", "#000000") case .hidden: return ("#8C8C8C", "#8A8A8A") case .marked: return ("#FF2B21", "#D9150C") case .cursor: return ("#FF2B21", "#D9150C") case .cursorText: return ("#FFFFFF", "#FFFFFF") case .pathBar: return ("#5D789E", "#3F6DB3") } } } func color(_ role: ColorRole, dark: Bool? = nil) -> NSColor { let dk = dark ?? isDark let def = dk ? role.defaults.dark : role.defaults.light return NSColor(hex: d.string(forKey: colorKey(role, dk)) ?? def) ?? NSColor(hex: def)! } func setColor(_ c: NSColor, _ role: ColorRole, dark: Bool) { set(c.hexString, colorKey(role, dark).dropFirst(Self.prefix.count).description) } func resetColors() { for r in ColorRole.allCases { for dk in [true, false] { d.removeObject(forKey: colorKey(r, dk)) } } d.removeObject(forKey: Self.prefix + "cursorePieno") NotificationCenter.default.post(name: Self.changed, object: nil) } private func colorKey(_ r: ColorRole, _ dark: Bool) -> String { Self.prefix + "colori.\(dark ? "scuro" : "chiaro").\(r.rawValue)" } /// Cursore: cornice (come il TC di Paolo) o barra piena (InverseCursor di TC). var cursorFilled: Bool { get { bool("cursorePieno", false) } set { set(newValue, "cursorePieno") } } var fontSize: Double { get { d.object(forKey: Self.prefix + "carattere") as? Double ?? Defaults.fontSize } set { set(newValue, "carattere") } } var showHidden: Bool { get { bool("mostraNascosti", true) } set { set(newValue, "mostraNascosti") } } var tagColors: Bool { get { bool("coloriTag", true) } set { set(newValue, "coloriTag") } } // MARK: Comportamento /// Spazio: seleziona e resta sulla riga (false) o scende alla successiva (true). Ins scende sempre. var spaceMovesDown: Bool { get { bool("spazioScende", false) } set { set(newValue, "spazioScende") } } var quickSearch: Bool { get { bool("ricercaRapida", true) } set { set(newValue, "ricercaRapida") } } var markedFolderSize: Bool { get { bool("spazioCartelleSelezionate", true) } set { set(newValue, "spazioCartelleSelezionate") } } var restorePanels: Bool { get { bool("riapriCartelle", true) } set { set(newValue, "riapriCartelle") } } // MARK: Copia ed eliminazione var keepDates: Bool { get { bool("conservaDate", true) } set { set(newValue, "conservaDate") } } /// F8 sposta nel Cestino (true) o elimina subito (false). Shift+F8 elimina sempre subito. var useTrash: Bool { get { bool("cestino", true) } set { set(newValue, "cestino") } } var confirmDelete: Bool { get { bool("confermaElimina", true) } set { set(newValue, "confermaElimina") } } // MARK: Programmi var editorURL: URL { get { let p = d.string(forKey: Self.prefix + "editor") ?? Defaults.editor return URL(fileURLWithPath: FileManager.default.fileExists(atPath: p) ? p : Defaults.editor) } set { set(newValue.path, "editor") } } /// F2: il terminale che si apre nella cartella del pannello attivo (Terminale, iTerm, Ghostty…). var terminalURL: URL { get { let p = d.string(forKey: Self.prefix + "terminale") ?? Defaults.terminal return URL(fileURLWithPath: FileManager.default.fileExists(atPath: p) ? p : Defaults.terminal) } set { set(newValue.path, "terminale") } } // MARK: Aggiornamenti var checkUpdates: Bool { get { bool("aggiornamentiAvvio", true) } set { set(newValue, "aggiornamentiAvvio") } } /// All'avvio scarica e installa senza chiedere (poi chiede solo se riavviare). var autoInstallUpdates: Bool { get { bool("aggiornamentiDaSolo", true) } set { set(newValue, "aggiornamentiDaSolo") } } /// Numero della versione saltata ("Salta questa versione"). var skippedUpdate: Int { get { d.integer(forKey: Self.prefix + "aggiornamentoSaltato") } set { set(newValue, "aggiornamentoSaltato") } } // MARK: - /// Tutto come al primo avvio (le connessioni FTP e i preferiti restano). func reset() { for k in d.dictionaryRepresentation().keys where k.hasPrefix(Self.prefix) { d.removeObject(forKey: k) } applyMode() } private func bool(_ key: String, _ def: Bool) -> Bool { d.object(forKey: Self.prefix + key) as? Bool ?? def } private func set(_ value: Any, _ key: String) { d.set(value, forKey: Self.prefix + key) NotificationCenter.default.post(name: Self.changed, object: nil) } } extension NSColor { convenience init?(hex: String) { var s = hex.trimmingCharacters(in: .whitespaces) if s.hasPrefix("#") { s.removeFirst() } guard s.count == 6, let v = UInt32(s, radix: 16) else { return nil } self.init(srgbRed: CGFloat((v >> 16) & 0xFF) / 255, green: CGFloat((v >> 8) & 0xFF) / 255, blue: CGFloat(v & 0xFF) / 255, alpha: 1) } var hexString: String { let c = usingColorSpace(.sRGB) ?? self return String(format: "#%02X%02X%02X", Int(round(c.redComponent * 255)), Int(round(c.greenComponent * 255)), Int(round(c.blueComponent * 255))) } }