import Foundation import Observation // Settings holds the user settings. The settings use UserDefaults. The keys // have the prefix "ws." (spec section 7.13). The type is main-actor isolated // and publishes changes with the Observation framework. @MainActor @Observable final class Settings { static let shared = Settings() private enum Key { static let paused = "ws.paused" static let constrainWindows = "ws.constrainWindows" static let excludedBundleIDs = "ws.excludedBundleIDs" static let stripHeight = "ws.stripHeight" } @ObservationIgnored private let defaults: UserDefaults var paused: Bool { didSet { defaults.set(paused, forKey: Key.paused) } } var constrainWindows: Bool { didSet { defaults.set(constrainWindows, forKey: Key.constrainWindows) } } var excludedBundleIDs: [String] { didSet { defaults.set(excludedBundleIDs, forKey: Key.excludedBundleIDs) } } var stripHeight: Double { didSet { defaults.set(stripHeight, forKey: Key.stripHeight) } } init(defaults: UserDefaults = .standard) { self.defaults = defaults defaults.register(defaults: [ Key.paused: false, Key.constrainWindows: true, Key.excludedBundleIDs: [String](), Key.stripHeight: 30.0 ]) paused = defaults.bool(forKey: Key.paused) constrainWindows = defaults.bool(forKey: Key.constrainWindows) excludedBundleIDs = defaults.stringArray(forKey: Key.excludedBundleIDs) ?? [] stripHeight = defaults.double(forKey: Key.stripHeight) } // isExcluded returns true if the application is on the excluded list. func isExcluded(_ bundleID: String?) -> Bool { guard let bundleID = bundleID else { return false } return excludedBundleIDs.contains(bundleID) } func addExcluded(_ bundleID: String) { guard excludedBundleIDs.contains(bundleID) == false else { return } excludedBundleIDs.append(bundleID) } func removeExcluded(_ bundleID: String) { excludedBundleIDs.removeAll { $0 == bundleID } } }