Repository navigation
Expand file tree
/
Copy pathPreferences.swift
More file actions
50 lines (42 loc) · 1.71 KB
/
Copy pathPreferences.swift
File metadata and controls
50 lines (42 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import Foundation
/// Central access to app-wide preferences. All UserDefaults keys and their
/// validation rules live here so views, documents, and menus stay in sync.
enum Preferences {
enum Key {
static let dithered = "dithered"
static let speed = "speed"
static let optimizeWithImageOptim = "optimizeWithImageOptim"
}
/// Injectable for tests; production code always uses .standard.
nonisolated(unsafe) static var defaults: UserDefaults = .standard
static let defaultSpeed = QuantizationOptions.defaultSpeed
static let speedRange = 1...10
/// Values a key reads before the user has chosen one.
static func registerDefaults() {
defaults.register(defaults: [Key.optimizeWithImageOptim: true])
}
/// Tri-state: nil means "Automatic" (no explicit user choice).
static var dithering: Bool? {
get { defaults.object(forKey: Key.dithered) as? Bool }
set {
if let value = newValue {
defaults.set(value, forKey: Key.dithered)
} else {
defaults.removeObject(forKey: Key.dithered)
}
}
}
/// Always returns a valid libimagequant speed; out-of-range stored values
/// (including the 0 an unset key reads as) fall back to the default.
static var speed: Int {
get {
let stored = defaults.integer(forKey: Key.speed)
return speedRange.contains(stored) ? stored : defaultSpeed
}
set { defaults.set(newValue, forKey: Key.speed) }
}
static var optimizeWithImageOptim: Bool {
get { defaults.bool(forKey: Key.optimizeWithImageOptim) }
set { defaults.set(newValue, forKey: Key.optimizeWithImageOptim) }
}
}