forked from laishulu/macism
-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathUserPreferences.swift
More file actions
100 lines (89 loc) · 2.8 KB
/
UserPreferences.swift
File metadata and controls
100 lines (89 loc) · 2.8 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import Foundation
class UserPreferences {
static let shared = UserPreferences()
private let defaults = UserDefaults.standard
// 键名常量
private struct Keys {
static let allowedApps = "allowedApps"
static let selectedInputMethod = "selectedInputMethod"
static let selectedEnglishInputMethod = "selectedEnglishInputMethod"
static let useShiftSwitch = "useShiftSwitch"
static let launchAtLogin = "launchAtLogin"
static let useJkSwitch = "useJkSwitch"
}
// Esc 生效的应用
var allowedApps: Set<String> {
get {
let array = defaults.array(forKey: Keys.allowedApps) as? [String] ?? []
return Set(array)
}
set {
defaults.set(Array(newValue), forKey: Keys.allowedApps)
}
}
// 选择的中文输入法
var selectedInputMethod: String? {
get {
defaults.string(forKey: Keys.selectedInputMethod)
}
set {
defaults.set(newValue, forKey: Keys.selectedInputMethod)
}
}
// 选择的英文输入法
var selectedEnglishInputMethod: String {
get {
defaults.string(forKey: Keys.selectedEnglishInputMethod) ?? "com.apple.keylayout.ABC"
}
set {
defaults.set(newValue, forKey: Keys.selectedEnglishInputMethod)
}
}
// 是否使用 shift 切换输入法
var useShiftSwitch: Bool {
get {
defaults.bool(forKey: Keys.useShiftSwitch)
}
set {
defaults.set(newValue, forKey: Keys.useShiftSwitch)
}
}
// 是否使用 jk 组合键切换输入法
var useJkSwitch: Bool {
get {
defaults.bool(forKey: Keys.useJkSwitch)
}
set {
defaults.set(newValue, forKey: Keys.useJkSwitch)
}
}
// 是否开机启动
var launchAtLogin: Bool {
get {
defaults.bool(forKey: Keys.launchAtLogin)
}
set {
defaults.set(newValue, forKey: Keys.launchAtLogin)
}
}
private init() {
// 设置默认值
if defaults.object(forKey: Keys.allowedApps) == nil {
allowedApps = Set([
"com.apple.Terminal",
"com.microsoft.VSCode",
"com.vim.MacVim",
"com.exafunction.windsurf",
"md.obsidian",
"dev.warp.Warp-Stable",
"com.todesktop.230313mzl4w4u92"
])
}
if defaults.object(forKey: Keys.useShiftSwitch) == nil {
useShiftSwitch = true
}
if defaults.object(forKey: Keys.selectedEnglishInputMethod) == nil {
selectedEnglishInputMethod = "com.apple.keylayout.ABC"
}
}
}