Skip to content

Commit

Permalink
feat(menu): 添加设置应用预设的菜单项,可一键设置正在输入的应用的预设输入模式
Browse files Browse the repository at this point in the history
  • Loading branch information
qwertyyb committed Nov 5, 2023
1 parent 23b7102 commit e24e9c3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Fire.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@
67C9A02D2AB53ED3000B5281 /* CandidatesWindow.swift */,
67C9A01D2AB53ED2000B5281 /* DictManager.swift */,
67C9A0142AB53ED2000B5281 /* en.lproj */,
67C9A01A2AB53ED2000B5281 /* FireMenu.swift */,
67C9A0192AB53ED2000B5281 /* Info.plist */,
67C9A0242AB53ED3000B5281 /* Preferences */,
67C9A01E2AB53ED3000B5281 /* Resources */,
Expand All @@ -196,8 +195,9 @@
67C9A0112AB53EB0000B5281 /* Table */,
67C9A0052AB53E83000B5281 /* fire.pdf */,
67C9A0062AB53E83000B5281 /* Fire.swift */,
67C9A0222AB53ED3000B5281 /* FireInputServer.swift */,
67C9A0072AB53E83000B5281 /* FireInputController.swift */,
67C9A0222AB53ED3000B5281 /* FireInputServer.swift */,
67C9A01A2AB53ED2000B5281 /* FireMenu.swift */,
45B76CA82AEA6042009AFABD /* PunctuationConversion.swift */,
67C9A0022AB53E83000B5281 /* InputSource.swift */,
67C9A0012AB53E83000B5281 /* MainMenu.xib */,
Expand Down
37 changes: 36 additions & 1 deletion Fire/FireMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import Foundation
import AppKit
import Sparkle
import Defaults

extension FireInputController {
/* -- menu actions start -- */
Expand All @@ -29,14 +30,48 @@ extension FireInputController {
NSApp.setActivationPolicy(.accessory)
FirePreferencesController.shared.showPane("用户词库")
}
@objc func setAppicationMode(_ sender: Any!) {
if let menuWrapper = sender as? [String: Any],
let menuItem = menuWrapper["IMKCommandMenuItem"] as? NSMenuItem,
let dict = menuItem.representedObject as? [String: Any],
let bundleID = dict["bundleID"] as? String,
let mode = dict["mode"] as? InputMode {
NSLog("[FireInputController] setApplicationMode, \(bundleID), \(mode)")
var appSettings = Defaults[.appSettings]
appSettings[bundleID] = ApplicationSettingItem(bundleId: bundleID, inputMs: mode == .zhhans ? .zhhans : .enUS)
Defaults[.appSettings] = appSettings
}
}
override func menu() -> NSMenu! {
NSLog("[FireInputController] menu")
let menu = NSMenu()
menu.items = [
NSMenuItem(title: "首选项", action: #selector(showPreferences(_:)), keyEquivalent: ""),
NSMenuItem(title: "用户词库", action: #selector(showUserDictPrefs(_:)), keyEquivalent: ""),
]
if !Defaults[.disableEnMode],
let controller = CandidatesWindow.shared.inputController,
let bundleID = controller.client()?.bundleIdentifier() {
var displayName = bundleID
if let url = NSWorkspace.shared.urlForApplication(withBundleIdentifier: bundleID) {
displayName = FileManager.default.displayName(atPath: url.path)
}
let title = "设置“\(displayName)”的预设为\(Fire.shared.inputMode == .zhhans ? "中文" : "英文")"
let menuItem = NSMenuItem(title: title, action: #selector(setAppicationMode(_:)), keyEquivalent: "")
menuItem.representedObject = [
"bundleID": bundleID,
"mode": Fire.shared.inputMode
]
menu.items.append(contentsOf: [
NSMenuItem.separator(),
menuItem,
])
}
menu.items.append(contentsOf: [
NSMenuItem.separator(),
NSMenuItem(title: "检查更新", action: #selector(checkForUpdates(_:)), keyEquivalent: ""),
NSMenuItem(title: "关于业火输入法", action: #selector(openAbout(_:)), keyEquivalent: "")
]
])
return menu
}
}

0 comments on commit e24e9c3

Please sign in to comment.