Skip to content

Commit

Permalink
fix: 修复某些场景下(safari地址栏)输入时,从中文切到英文模式下时,原码不会上屏和清除的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
qwertyyb committed Apr 22, 2023
1 parent fe9df00 commit 7c708b3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
6 changes: 2 additions & 4 deletions Fire/CandidatesWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,8 @@ class CandidatesWindow: NSWindow, NSWindowDelegate {
events.forEach { (observer) in NotificationCenter.default.addObserver(
forName: observer.name, object: nil, queue: nil, using: observer.callback
)}
/**
* 1. 由于使用recognizedEvents在一些场景下不能监听到flagChanged事件,比如保存文件场景
* 所以这里需要使用NSEvent.addGlobalMonitorForEvents监听shift键被按下
*/
// 由于使用IMKInputController recognizedEvents在一些场景下不能监听到flagChanged事件,比如保存文件和lanchPad场景
// 所以这里需要使用NSEvent.addGlobalMonitorForEvents监听shift键被按下
NSEvent.addGlobalMonitorForEvents(matching: .flagsChanged) { (event) in
NSLog("[CandidatesWindow] globalMonitorForEvents flagsChanged: \(event)")
if !InputSource.shared.isSelected() {
Expand Down
8 changes: 8 additions & 0 deletions Fire/FireInputController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,14 @@ class FireInputController: IMKInputController {

override func handle(_ event: NSEvent!, client sender: Any!) -> Bool {
NSLog("[FireInputController] handle: \(event.debugDescription)")

// 在activateServer中有把IMKInputController绑定给CandidatesWindow
// 然而在实际运行中发现,在Safari地址栏输入部分原码后,再按shift切到英文输入模式下时,候选窗消失了,但原码没有上屏
// 排查发现,因为shift切换中英文是通过CandidatesWindow调用绑定的inputController方法实现的,而在safari地址栏时,接受键盘输入的inputController
// 和CandidatesWindow绑定的inputController并不是同一个,所以出现了此问题
// 这里猜测之所以会出现不一致,是因为在Safari地址栏输入场景下,会有多个TextInputClient而创建多个inputController, activateServer也会多次执行
// 但是activateServer的调用顺序并不能保证最后调用的就是接受输入事件的TextInputClient对应的inputController, 所以仅是在activateServer中绑定inputController是不行的,需要在此处再绑定一下
CandidatesWindow.shared.inputController = self

let handler = Utils.shared.processHandlers(handlers: [
hotkeyHandler,
Expand Down

0 comments on commit 7c708b3

Please sign in to comment.