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
177 lines (150 loc) · 5.82 KB
/
UserPreferences.swift
File metadata and controls
177 lines (150 loc) · 5.82 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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"
static let customShortcutsVersion = "customShortcutsVersion"
}
// 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)
// 同步到新的快捷键系统
CustomShortcutPreferences.shared.setEnabled(.singleShift, enabled: newValue)
}
}
// 是否使用 jk 组合键切换输入法(兼容旧版本)
var useJkSwitch: Bool {
get {
defaults.bool(forKey: Keys.useJkSwitch)
}
set {
defaults.set(newValue, forKey: Keys.useJkSwitch)
// 同步到新的快捷键系统
CustomShortcutPreferences.shared.setEnabled(.jkSequence, enabled: newValue)
}
}
// 是否开机启动
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.selectedEnglishInputMethod) == nil {
selectedEnglishInputMethod = "com.apple.keylayout.ABC"
}
// 初始化快捷键系统
initializeCustomShortcuts()
}
// 初始化自定义快捷键系统
private func initializeCustomShortcuts() {
let currentVersion = defaults.integer(forKey: Keys.customShortcutsVersion)
// 如果是新安装或首次运行,迁移旧设置
if currentVersion < 1 {
migrateLegacySettings()
defaults.set(1, forKey: Keys.customShortcutsVersion)
}
}
// 迁移旧的快捷键设置到新系统
private func migrateLegacySettings() {
let customPrefs = CustomShortcutPreferences.shared
// 检查是否已经有旧的设置需要迁移
let hasShiftSetting = defaults.object(forKey: Keys.useShiftSwitch) != nil
let hasJkSetting = defaults.object(forKey: "useJkSwitch") != nil
if hasShiftSetting || hasJkSetting {
// 迁移现有设置
customPrefs.migrateLegacySettings()
} else {
// 新安装用户:设置默认值(只启用 Shift 切换)
customPrefs.setEnabled(.singleShift, enabled: true)
// 同时设置旧版本的默认值
defaults.set(true, forKey: Keys.useShiftSwitch)
}
}
// MARK: - 新的快捷键管理方法
/// 获取所有启用的快捷键
func getEnabledCustomShortcuts() -> Set<CustomShortcutType> {
return CustomShortcutPreferences.shared.getAllEnabledShortcuts()
}
/// 设置启用的快捷键
func setEnabledCustomShortcuts(_ types: Set<CustomShortcutType>) {
CustomShortcutPreferences.shared.setEnabledShortcuts(types)
// 刷新快捷键管理器的缓存
CustomShortcutManager.shared.refreshCache()
// 同步到旧的设置(向后兼容)
defaults.set(types.contains(.singleShift), forKey: Keys.useShiftSwitch)
defaults.set(types.contains(.jkSequence), forKey: Keys.useJkSwitch)
}
/// 检查特定快捷键是否启用
func isCustomShortcutEnabled(_ type: CustomShortcutType) -> Bool {
return CustomShortcutPreferences.shared.isEnabled(type)
}
/// 设置特定快捷键的启用状态
func setCustomShortcutEnabled(_ type: CustomShortcutType, enabled: Bool) {
CustomShortcutPreferences.shared.setEnabled(type, enabled: enabled)
// 刷新快捷键管理器的缓存
CustomShortcutManager.shared.refreshCache()
// 同步到旧的设置(向后兼容)
if type == .singleShift {
defaults.set(enabled, forKey: Keys.useShiftSwitch)
} else if type == .jkSequence {
defaults.set(enabled, forKey: Keys.useJkSwitch)
}
}
}