forked from laishulu/macism
-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathStatusBarManager.swift
More file actions
309 lines (257 loc) · 12.2 KB
/
StatusBarManager.swift
File metadata and controls
309 lines (257 loc) · 12.2 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import Cocoa
class StatusBarManager {
let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
private var menu: NSMenu?
private var appSelectionWindowController: AppSelectionWindowController?
weak var appDelegate: AppDelegate?
func setupStatusBarItem() {
if let button = statusItem.button {
updateStatusBarIcon()
createAndShowMenu()
button.isEnabled = true
} else {
print("错误:无法创建状态栏按钮")
}
}
func updateStatusBarIcon() {
guard let button = statusItem.button else {
print("Status item button not found")
return
}
let enabledShortcuts = KeyboardManager.shared.getEnabledCustomShortcuts()
if enabledShortcuts.isEmpty {
button.image = NSImage(systemSymbolName: "keyboard", accessibilityDescription: "MacVimSwitch")
} else {
button.image = NSImage(systemSymbolName: "keyboard.badge.ellipsis", accessibilityDescription: "MacVimSwitch (\(enabledShortcuts.count) shortcuts enabled)")
}
button.isEnabled = true
}
func createAndShowMenu() {
print("开始创建菜单...")
let newMenu = NSMenu()
let homepageItem = NSMenuItem(title: "使用说明", action: #selector(openHomepage), keyEquivalent: "")
homepageItem.target = self
newMenu.addItem(homepageItem)
newMenu.addItem(NSMenuItem.separator())
print("添加输入法菜单...")
// 添加输入法选择子菜单
let inputMethodMenu = NSMenu()
let inputMethodItem = NSMenuItem(title: "选择中文输入法", action: nil, keyEquivalent: "")
inputMethodItem.submenu = inputMethodMenu
// 获取所有CJKV输入法并添加到子菜单
if let inputMethods = InputMethodManager.shared.getAvailableCJKVInputMethods() {
print("当前保存的中文输入法: \(KeyboardManager.shared.lastInputSource ?? "nil")")
print("UserPreferences中的中文输入法: \(UserPreferences.shared.selectedInputMethod ?? "nil")")
for (sourceId, name) in inputMethods {
print("添加CJKV输入法菜单项: \(name) (\(sourceId))")
let item = NSMenuItem(
title: name,
action: #selector(selectCJKVInputMethod(_:)),
keyEquivalent: ""
)
item.target = self
item.representedObject = sourceId
// 检查是否是当前选中的输入法
if sourceId == KeyboardManager.shared.lastInputSource {
print("设置中文输入法选中状态: \(name) (\(sourceId))")
item.state = .on
}
inputMethodMenu.addItem(item)
}
}
newMenu.addItem(inputMethodItem)
// 添加英文输入法选择子菜单
let englishInputMethodMenu = NSMenu()
let englishInputMethodItem = NSMenuItem(title: "选择英文输入法", action: nil, keyEquivalent: "")
englishInputMethodItem.submenu = englishInputMethodMenu
// 获取所有英文输入法并添加到子菜单
if let englishInputMethods = InputMethodManager.shared.getAvailableEnglishInputMethods() {
print("当前保存的英文输入法: \(KeyboardManager.shared.englishInputSource)")
for (sourceId, name) in englishInputMethods {
print("添加英文输入法菜单项: \(name) (\(sourceId))")
let item = NSMenuItem(
title: name,
action: #selector(selectEnglishInputMethod(_:)),
keyEquivalent: ""
)
item.target = self
item.representedObject = sourceId
// 检查是否是当前选中的输入法
if sourceId == KeyboardManager.shared.englishInputSource {
print("设置英文输入法选中状态: \(name) (\(sourceId))")
item.state = .on
}
englishInputMethodMenu.addItem(item)
}
}
newMenu.addItem(englishInputMethodItem)
newMenu.addItem(NSMenuItem.separator())
// 添加应用列表子菜单
if let delegate = appDelegate {
let appsMenu = NSMenu()
let appsMenuItem = NSMenuItem(title: "Esc生效的应用", action: nil, keyEquivalent: "")
appsMenuItem.submenu = appsMenu
let manageAppsItem = NSMenuItem(title: "批量管理应用...", action: #selector(openAppSelectionWindow), keyEquivalent: "")
manageAppsItem.target = self
appsMenu.addItem(manageAppsItem)
appsMenu.addItem(NSMenuItem.separator())
// 添加所有应用到子菜单
for app in delegate.systemApps {
let item = NSMenuItem(title: app.name, action: #selector(AppDelegate.toggleApp(_:)), keyEquivalent: "")
item.state = delegate.allowedApps.contains(app.bundleId) ? .on : .off
item.representedObject = app.bundleId
item.target = delegate
appsMenu.addItem(item)
}
// 添加刷新应用列表选项
appsMenu.addItem(NSMenuItem.separator())
let refreshItem = NSMenuItem(title: "刷新应用列表", action: #selector(AppDelegate.refreshAppList), keyEquivalent: "r")
refreshItem.target = delegate
appsMenu.addItem(refreshItem)
newMenu.addItem(appsMenuItem)
newMenu.addItem(NSMenuItem.separator())
}
print("添加简化的自定义快捷键菜单...")
// 添加自定义快捷键子菜单 - 修复版本
let shortcutsMenu = NSMenu()
let shortcutsMenuItem = NSMenuItem(title: "快捷键设置", action: nil, keyEquivalent: "")
shortcutsMenuItem.submenu = shortcutsMenu
// 添加基础的快捷键选项(暂时不获取启用状态)
let basicShortcuts: [(CustomShortcutType, String)] = [
(.commandSpace, "Command+Space → ESC"),
(.capsLock, "CapsLock → ESC"),
(.ctrlOpenBracket, "Ctrl+[ → ESC"),
(.jkSequence, "JK 序列 → ESC"),
(.singleShift, "单击 Shift → ESC")
]
for (shortcutType, displayName) in basicShortcuts {
let item = NSMenuItem(
title: displayName,
action: #selector(toggleCustomShortcut(_:)),
keyEquivalent: ""
)
item.representedObject = shortcutType
item.target = self
// 获取真实的启用状态
let isEnabled = UserPreferences.shared.isCustomShortcutEnabled(shortcutType)
item.state = isEnabled ? .on : .off
shortcutsMenu.addItem(item)
print("添加快捷键菜单项: \(displayName) - 状态: \(isEnabled ? "启用" : "禁用")")
}
// 添加分隔线和重置选项
shortcutsMenu.addItem(NSMenuItem.separator())
let resetShortcutsItem = NSMenuItem(
title: "重置为默认设置",
action: #selector(resetShortcutsToDefault),
keyEquivalent: ""
)
resetShortcutsItem.target = self
shortcutsMenu.addItem(resetShortcutsItem)
newMenu.addItem(shortcutsMenuItem)
newMenu.addItem(NSMenuItem.separator())
// 添加开机启动选项
let launchAtLoginItem = NSMenuItem(
title: "开机启动",
action: #selector(toggleLaunchAtLogin),
keyEquivalent: ""
)
launchAtLoginItem.target = self
launchAtLoginItem.state = UserPreferences.shared.launchAtLogin ? .on : .off
newMenu.addItem(launchAtLoginItem)
newMenu.addItem(NSMenuItem.separator())
// 添加检查更新选项
let checkUpdateItem = NSMenuItem(title: "检查更新", action: #selector(checkForUpdates), keyEquivalent: "")
checkUpdateItem.target = self
newMenu.addItem(checkUpdateItem)
newMenu.addItem(NSMenuItem.separator())
// 添加退出选项
let quitItem = NSMenuItem(title: "退出", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q")
quitItem.target = NSApp
newMenu.addItem(quitItem)
print("设置状态栏菜单...")
statusItem.menu = newMenu
self.menu = newMenu
print("菜单创建完成!")
}
@objc private func openHomepage() {
if let url = URL(string: "https://github.com/Jackiexiao/macvimswitch") {
NSWorkspace.shared.open(url)
}
}
@objc private func openAppSelectionWindow() {
guard let delegate = appDelegate else { return }
if appSelectionWindowController == nil {
appSelectionWindowController = AppSelectionWindowController(appDelegate: delegate)
} else {
appSelectionWindowController?.reloadApps()
}
appSelectionWindowController?.showWindow(nil)
appSelectionWindowController?.window?.makeKeyAndOrderFront(nil)
NSApp.activate(ignoringOtherApps: true)
}
@objc private func toggleCustomShortcut(_ sender: NSMenuItem) {
guard let shortcutType = sender.representedObject as? CustomShortcutType else {
print("无法获取快捷键类型")
return
}
print("切换快捷键: \(shortcutType.displayName)")
// 获取当前状态
let isEnabled = UserPreferences.shared.isCustomShortcutEnabled(shortcutType)
let newState = !isEnabled
// 设置新状态
UserPreferences.shared.setCustomShortcutEnabled(shortcutType, enabled: newState)
// 更新UI
sender.state = newState ? .on : .off
updateStatusBarIcon()
createAndShowMenu()
print("快捷键 \(shortcutType.displayName) 已\(newState ? "启用" : "禁用")")
}
@objc private func resetShortcutsToDefault() {
let alert = NSAlert()
alert.messageText = "重置快捷键设置"
alert.informativeText = "确定要重置所有快捷键设置为默认值吗?\n\n默认启用:单击 Shift → ESC"
alert.alertStyle = .warning
alert.addButton(withTitle: "确定")
alert.addButton(withTitle: "取消")
if alert.runModal() == .alertFirstButtonReturn {
// 重置为默认设置(只启用 Shift)
UserPreferences.shared.setEnabledCustomShortcuts([.singleShift])
updateStatusBarIcon()
createAndShowMenu()
print("快捷键设置已重置为默认值")
}
}
@objc private func selectCJKVInputMethod(_ sender: NSMenuItem) {
guard let sourceId = sender.representedObject as? String else { return }
print("[StatusBarManager] 选择CJKV输入法: \(sourceId)")
KeyboardManager.shared.setLastInputSource(sourceId)
createAndShowMenu() // 重新创建菜单以更新选中状态
}
@objc private func selectEnglishInputMethod(_ sender: NSMenuItem) {
guard let sourceId = sender.representedObject as? String else { return }
print("[StatusBarManager] 选择英文输入法: \(sourceId)")
KeyboardManager.shared.englishInputSource = sourceId
createAndShowMenu() // 重新创建菜单以更新选中状态
}
@objc private func toggleLaunchAtLogin() {
if LaunchManager.shared.toggleLaunchAtLogin() {
// 操作成功,重新创建菜单以更新状态
createAndShowMenu()
} else {
// 操作失败,显示错误提示
let alert = NSAlert()
alert.messageText = "设置失败"
alert.informativeText = "无法修改开机启动设置,请检查系统权限。"
alert.alertStyle = .warning
alert.addButton(withTitle: "确定")
alert.runModal()
}
}
@objc private func checkForUpdates() {
UpdateManager.shared.checkForUpdates(silent: false)
}
@objc private func quitApp() {
KeyboardManager.shared.disableEventTap()
NSApplication.shared.terminate(self)
}
}