Skip to content

Commit

Permalink
feat: 从其它输入法切换至此输入法时,把输入模式设置为中文输入
Browse files Browse the repository at this point in the history
  • Loading branch information
qwertyyb committed Sep 8, 2024
1 parent 7f0c7cc commit 748c6b0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"originHash" : "4e641473ec14b7a6f951dbca895c408f01acd79320722c6f58d680da383b809e",
"pins" : [
{
"identity" : "defaults",
Expand Down Expand Up @@ -46,5 +47,5 @@
}
}
],
"version" : 2
"version" : 3
}
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
17 changes: 16 additions & 1 deletion Fire/InputSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import Carbon
import AppKit
import Sauce

enum InputSourceUsage {
case enable
Expand All @@ -17,6 +18,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 +106,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

0 comments on commit 748c6b0

Please sign in to comment.