Skip to content

Commit

Permalink
fix: 修复保持最后输入模式未按预期切换的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
qwertyyb committed Jul 13, 2022
1 parent cf8f199 commit 9a27e0e
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 74 deletions.
4 changes: 4 additions & 0 deletions Fire.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
45E85F742573BE1800F71024 /* wb_98_table.txt in Resources */ = {isa = PBXBuildFile; fileRef = 45E85F732573BE1800F71024 /* wb_98_table.txt */; };
45EBB54E283A073000A56CBA /* Statistics.swift in Sources */ = {isa = PBXBuildFile; fileRef = 45EBB54D283A073000A56CBA /* Statistics.swift */; };
45EBB550283A311C00A56CBA /* StatisticsPane.swift in Sources */ = {isa = PBXBuildFile; fileRef = 45EBB54F283A311C00A56CBA /* StatisticsPane.swift */; };
672C1987287EDAF8002B2540 /* FireInputServer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 672C1986287EDAF8002B2540 /* FireInputServer.swift */; };
673A400C253D9FE70003901E /* InputSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 673A400B253D9FE70003901E /* InputSource.swift */; };
673C416925468A4800F462A3 /* FireMenu.swift in Sources */ = {isa = PBXBuildFile; fileRef = 673C416825468A4800F462A3 /* FireMenu.swift */; };
673C417225468FFA00F462A3 /* ModifierKeyUpChecker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 673C417125468FFA00F462A3 /* ModifierKeyUpChecker.swift */; };
Expand Down Expand Up @@ -112,6 +113,7 @@
45EBB54D283A073000A56CBA /* Statistics.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Statistics.swift; sourceTree = "<group>"; };
45EBB54F283A311C00A56CBA /* StatisticsPane.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatisticsPane.swift; sourceTree = "<group>"; };
63CA87FF28AC32324A6D50A5 /* Pods-TableBuilder.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-TableBuilder.release.xcconfig"; path = "Target Support Files/Pods-TableBuilder/Pods-TableBuilder.release.xcconfig"; sourceTree = "<group>"; };
672C1986287EDAF8002B2540 /* FireInputServer.swift */ = {isa = PBXFileReference; indentWidth = 4; lastKnownFileType = sourcecode.swift; path = FireInputServer.swift; sourceTree = "<group>"; };
673A400B253D9FE70003901E /* InputSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InputSource.swift; sourceTree = "<group>"; };
673C416825468A4800F462A3 /* FireMenu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FireMenu.swift; sourceTree = "<group>"; };
673C417125468FFA00F462A3 /* ModifierKeyUpChecker.swift */ = {isa = PBXFileReference; indentWidth = 4; lastKnownFileType = sourcecode.swift; path = ModifierKeyUpChecker.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -197,6 +199,7 @@
451E605C232E3A3C007B0463 /* MainMenu.xib */,
451E604F232E227C007B0463 /* Fire.entitlements */,
451E6055232E24A5007B0463 /* FireInputController.swift */,
672C1986287EDAF8002B2540 /* FireInputServer.swift */,
673C416825468A4800F462A3 /* FireMenu.swift */,
451E605E232E400B007B0463 /* Fire.swift */,
45587F2C24E8FBFB005F291B /* Utils.swift */,
Expand Down Expand Up @@ -475,6 +478,7 @@
4500AC64286F2B42006F3FCC /* UserDictPane.swift in Sources */,
45577EA1254552110064325B /* ThesaurusPane.swift in Sources */,
451E6056232E24A5007B0463 /* FireInputController.swift in Sources */,
672C1987287EDAF8002B2540 /* FireInputServer.swift in Sources */,
451E605F232E400B007B0463 /* Fire.swift in Sources */,
4500AC622869F8CC006F3FCC /* PunctutionPane.swift in Sources */,
45587F2D24E8FBFB005F291B /* Utils.swift in Sources */,
Expand Down
76 changes: 2 additions & 74 deletions Fire/FireInputController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ typealias NotificationObserver = (name: Notification.Name, callback: (_ notifica
class FireInputController: IMKInputController {
private var _candidates: [Candidate] = []
private var _hasNext: Bool = false
private var inputMode: InputMode {
internal var inputMode: InputMode {
get { Fire.shared.inputMode }
set(value) { Fire.shared.inputMode = value }
}

private var temp: (
internal var temp: (
observerList: [NSObjectProtocol],
monitorList: [Any?]
) = (
Expand Down Expand Up @@ -362,76 +362,4 @@ class FireInputController: IMKInputController {
})
]
}

/**
* 根据当前输入的应用改变输入模式
*/
private func activeClientInputMode() {
guard let identifier = client()?.bundleIdentifier() else { return }
if let appSetting = Defaults[.appSettings][identifier],
let mode = InputMode(rawValue: appSetting.inputModeSetting.rawValue) {
print("[FireInputController] activeClientInputMode from setting : \(identifier), \(mode)")
Fire.shared.toggleInputMode(mode)
return
}
if !Defaults[.keepAppInputMode] { return }
// 启用APP缓存设置
if let appSetting = Fire.shared.appSettingCache.get(bundleIdentifier: identifier),
let mode = InputMode(rawValue: appSetting.inputModeSetting.rawValue) {
print("[FireInputController] activeClientInputMode from cache: \(identifier), \(mode)")
Fire.shared.toggleInputMode(mode)
}
}

/**
* 由于使用recognizedEvents在一些场景下不能监听到flagChanged事件,比如保存文件场景
* 所以这里需要使用NSEvent.addGlobalMonitorForEvents监听shift键被按下
*/
override func activateServer(_ sender: Any!) {
NSLog("[FireInputController] activate server: \(client()?.bundleIdentifier() ?? sender.debugDescription)")

// 监听candidateView点击,翻页事件
notificationList().forEach { (observer) in temp.observerList.append(NotificationCenter.default.addObserver(
forName: observer.name, object: nil, queue: nil, using: observer.callback
))}
if Defaults[.disableEnMode] {
Fire.shared.toggleInputMode(.zhhans)
return
}

activeClientInputMode()
temp.monitorList.append(NSEvent.addGlobalMonitorForEvents(matching: .flagsChanged) { (event) in
if !InputSource.shared.isSelected() {
return self.clearEventListener()
}
_ = self.handle(event, client: self.client())
})
}
func clearEventListener() {
temp.monitorList.forEach { (monitor) in
if let m = monitor {
NSEvent.removeMonitor(m)
}
}
temp.observerList.forEach { (observer) in
NotificationCenter.default.removeObserver(observer)
}
temp.monitorList = []
temp.observerList = []
}
override func deactivateServer(_ sender: Any!) {
NSLog("[FireInputController] deactivate server: \(client()?.bundleIdentifier() ?? "no client deactivate")")
if let identifier = client()?.bundleIdentifier(), !Defaults[.disableEnMode] {
// 缓存当前输入模式
Fire.shared.appSettingCache.add(
bundleIdentifier: identifier,
setting: ApplicationSettingItem(
bundleId: identifier,
inputMs: InputModeSetting(rawValue: inputMode.rawValue)!
)
)
}
clean()
clearEventListener()
}
}
99 changes: 99 additions & 0 deletions Fire/FireInputServer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//
// FireInputServer.swift
// Fire
//
// Created by marchyang on 2022/7/13.
// Copyright © 2022 qwertyyb. All rights reserved.
//

import Foundation
import Defaults

var previousClientIdentifier: String = ""

extension FireInputController {
/**
* 根据当前输入的应用改变输入模式
*/
private func activeCurrentClientInputMode() {
guard let identifier = client()?.bundleIdentifier() else { return }
if let appSetting = Defaults[.appSettings][identifier],
let mode = InputMode(rawValue: appSetting.inputModeSetting.rawValue) {
print("[FireInputController] activeClientInputMode from setting : \(identifier), \(mode)")
Fire.shared.toggleInputMode(mode)
return
}
if !Defaults[.keepAppInputMode] { return }
// 启用APP缓存设置
if let appSetting = Fire.shared.appSettingCache.get(bundleIdentifier: identifier),
let mode = InputMode(rawValue: appSetting.inputModeSetting.rawValue) {
print("[FireInputController] activeClientInputMode from cache: \(identifier), \(mode)")
Fire.shared.toggleInputMode(mode)
}
}

private func savePreviousClientInputMode() {
if previousClientIdentifier.count > 0 {
// 缓存当前输入模式
Fire.shared.appSettingCache.add(
bundleIdentifier: previousClientIdentifier,
setting: ApplicationSettingItem(
bundleId: previousClientIdentifier,
inputMs: InputModeSetting(rawValue: inputMode.rawValue)!
)
)
}
}

func clearEventListener() {
temp.monitorList.forEach { (monitor) in
if let m = monitor {
NSEvent.removeMonitor(m)
}
}
temp.observerList.forEach { (observer) in
NotificationCenter.default.removeObserver(observer)
}
temp.monitorList = []
temp.observerList = []
}
func previousClientHandler() {
clean()
clearEventListener()
savePreviousClientInputMode()
}

/**
* 由于使用recognizedEvents在一些场景下不能监听到flagChanged事件,比如保存文件场景
* 所以这里需要使用NSEvent.addGlobalMonitorForEvents监听shift键被按下
*/
override func activateServer(_ sender: Any!) {
NSLog("[FireInputController] activate server: \(client()?.bundleIdentifier() ?? sender.debugDescription)")

previousClientHandler()

if let identifier = client()?.bundleIdentifier() {
previousClientIdentifier = identifier
}

// 监听candidateView点击,翻页事件
notificationList().forEach { (observer) in temp.observerList.append(NotificationCenter.default.addObserver(
forName: observer.name, object: nil, queue: nil, using: observer.callback
))}
if Defaults[.disableEnMode] {
Fire.shared.toggleInputMode(.zhhans)
return
}

activeCurrentClientInputMode()
temp.monitorList.append(NSEvent.addGlobalMonitorForEvents(matching: .flagsChanged) { (event) in
if !InputSource.shared.isSelected() {
return self.clearEventListener()
}
_ = self.handle(event, client: self.client())
})
}
override func deactivateServer(_ sender: Any!) {
NSLog("[FireInputController] deactivate server: \(client()?.bundleIdentifier() ?? "no client deactivate")")
}
}

0 comments on commit 9a27e0e

Please sign in to comment.