Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 从其它输入法切换至业火输入法时,设置输入模式为中文 #135

Merged
merged 3 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Fire/Fire.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ class Fire: NSObject {
override init() {
super.init()
_ = InputSource.shared.onSelectChanged { selected in
StatusBar.shared.refresh()
NSLog("[Fire] onSelectChanged: \(selected)")
if selected {
self.toastCurrentMode()
// 如果从其他输入法切换至当前输入法,则把当前输入法的输入模式设置为中文
// 此处有一个点需要关注:此回调和 activateServer 的调用顺序没有明确的结论,所以这里设置的状态有可能会被 activateServer 中的 activeCurrentClientInputMode 重写,这不太符合预期。目前在实际的使用过程中发现是 activateServer 先调用,暂时符合预期,需要观察一下实际使用过程中的情况
self.toggleInputMode(.zhhans)
}
StatusBar.shared.refresh()
}
}

Expand Down
16 changes: 15 additions & 1 deletion Fire/InputSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ enum InputSourceUsage {
class InputSource {
let installLocation = "/Library/Input Methods/Fire.app"
let kSourceID = Bundle.main.bundleIdentifier!
var selected: Bool? = nil

func registerInputSource() {
if !isEnabled() {
Expand Down Expand Up @@ -104,13 +105,26 @@ class InputSource {
}

func onSelectChanged(callback: @escaping (Bool) -> Void) -> NSObjectProtocol {
NSLog("[InputSource] onSelectChanged")
let observer = DistributedNotificationCenter.default()
.addObserver(
forName: .init(String(kTISNotifySelectedKeyboardInputSourceChanged)),
object: nil,
queue: nil,
using: { _ in
callback(self.isSelected())
// 这个回调发现两个问题
// 1. 在当前输入法是 ABC 英文输入法时,在应用启动后第一次切换到当前输入法时,此回调不会调用,此问题暂时无法处理
// 2. 在此回调中直接获取当前输入法是否被选择,可能不准确(状态尚未更新),需要 asyncAfter 0.1s 后再获取状态
// 3. 此事件有可能会被重复调用,比如切换到搜狗输入法时,所以事件需要过滤一下
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
let selected = self.isSelected()
NSLog("[InputSource] onSelectChanged callback: \(String(describing: self.selected)), \(selected)")
// 此事件会重复触发,此处判断需要过滤一下
if (selected != self.selected) {
self.selected = selected
callback(self.selected!)
}
}
}
)
return observer
Expand Down
2 changes: 1 addition & 1 deletion Fire/StatusBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class StatusBar {
}

private func refreshVisibleStatus() {
NSLog("StatusBar.refreshVisibleStatus: \(InputSource.shared.isSelected())")
NSLog("[StatusBar] refreshVisibleStatus: \(InputSource.shared.isSelected())")
statusItem.isVisible = Defaults[.showInputModeStatus] && InputSource.shared.isSelected()
}

Expand Down
Loading