Skip to content

Commit

Permalink
Merge pull request #79 from qwertyyb/fix/last-inputmode
Browse files Browse the repository at this point in the history
fix: 修复保持应用最后输入模式在某些场景下不正常的问题
  • Loading branch information
qwertyyb committed Jul 16, 2022
2 parents cf8f199 + 00f97e7 commit c698e6e
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 119 deletions.
8 changes: 4 additions & 4 deletions Fire.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
45D996AA253C5232001460A8 /* GeneralPane.swift in Sources */ = {isa = PBXBuildFile; fileRef = 45D996A9253C5232001460A8 /* GeneralPane.swift */; };
45DB6EC727E5B8FE00A39925 /* ThemeConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 45DB6EC627E5B8FE00A39925 /* ThemeConfig.swift */; };
45DB6EC927E609CB00A39925 /* ThemePane.swift in Sources */ = {isa = PBXBuildFile; fileRef = 45DB6EC827E609CB00A39925 /* ThemePane.swift */; };
45DCE62226A31F140009FED1 /* ApplicationSettingCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 45DCE62126A31F140009FED1 /* ApplicationSettingCache.swift */; };
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 @@ -107,11 +107,11 @@
45D996A9253C5232001460A8 /* GeneralPane.swift */ = {isa = PBXFileReference; indentWidth = 4; lastKnownFileType = sourcecode.swift; path = GeneralPane.swift; sourceTree = "<group>"; };
45DB6EC627E5B8FE00A39925 /* ThemeConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThemeConfig.swift; sourceTree = "<group>"; };
45DB6EC827E609CB00A39925 /* ThemePane.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThemePane.swift; sourceTree = "<group>"; };
45DCE62126A31F140009FED1 /* ApplicationSettingCache.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ApplicationSettingCache.swift; sourceTree = "<group>"; };
45E85F732573BE1800F71024 /* wb_98_table.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = wb_98_table.txt; sourceTree = "<group>"; };
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 +197,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 @@ -274,7 +275,6 @@
673C417125468FFA00F462A3 /* ModifierKeyUpChecker.swift */,
673C4177254697E400F462A3 /* TipsWindow.swift */,
677A0873254BD47D000B58D4 /* ToastWindow.swift */,
45DCE62126A31F140009FED1 /* ApplicationSettingCache.swift */,
45EBB54D283A073000A56CBA /* Statistics.swift */,
);
path = Utils;
Expand Down Expand Up @@ -475,6 +475,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 All @@ -491,7 +492,6 @@
45577EB4254576720064325B /* FirePreferencesController.swift in Sources */,
450B7D9A26A2847D00808A4D /* ApplicationPane.swift in Sources */,
4500AC68287036CB006F3FCC /* DictManager.swift in Sources */,
45DCE62226A31F140009FED1 /* ApplicationSettingCache.swift in Sources */,
459DE990232EB26600A3ACD1 /* CandidatesView.swift in Sources */,
45DB6EC727E5B8FE00A39925 /* ThemeConfig.swift in Sources */,
453377E52849E76A0064E4F2 /* StatusBar.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion Fire.xcodeproj/xcshareddata/xcschemes/Fire.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
ActionType = "Xcode.IDEStandardExecutionActionsCore.ExecutionActionType.ShellScriptAction">
<ActionContent
title = "Run Script"
scriptText = "# Type a script or drag a script file from your workspace to insert its path.&#10;pkill Fire&#10;">
scriptText = "# Type a script or drag a script file from your workspace to insert its path.&#10;pkill -9 Fire&#10;">
</ActionContent>
</ExecutionAction>
</PreActions>
Expand Down
2 changes: 0 additions & 2 deletions Fire/Fire.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ class Fire: NSObject {
static let candidateInserted = Notification.Name("Fire.candidateInserted")
static let inputModeChanged = Notification.Name("Fire.inputModeChanged")

let appSettingCache = ApplicationSettingCache()

var inputMode: InputMode = .zhhans

func transformPunctution(_ origin: String)-> String? {
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()
}
}
93 changes: 93 additions & 0 deletions Fire/FireInputServer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//
// FireInputServer.swift
// Fire
//
// Created by marchyang on 2022/7/13.
// Copyright © 2022 qwertyyb. All rights reserved.
//

import Foundation
import Defaults

private var previousClientIdentifier: String = ""
private var inputModeCache: [String: InputMode] = [:]

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
}
// 启用APP缓存设置
if Defaults[.keepAppInputMode], let mode = inputModeCache[identifier] {
print("[FireInputController] activeClientInputMode from cache: \(identifier), \(mode)")
Fire.shared.toggleInputMode(mode)
}
}

private func savePreviousClientInputMode() {
if previousClientIdentifier.count > 0 {
// 缓存当前输入模式
inputModeCache.updateValue(inputMode, forKey: previousClientIdentifier)
}
}

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()
}

/**
* 1. 由于使用recognizedEvents在一些场景下不能监听到flagChanged事件,比如保存文件场景
* 所以这里需要使用NSEvent.addGlobalMonitorForEvents监听shift键被按下
* 2. 当client变化时,deactiveServer 和 activeServer 的执行是不固定的,有可能 activeServer 先执行,所以需要在activeServer中执行清理逻辑
*/
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")")
}
}
38 changes: 0 additions & 38 deletions Fire/Utils/ApplicationSettingCache.swift

This file was deleted.

5 changes: 5 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
![](https://github.com/qwertyyb/FireWubi/workflows/CI/badge.svg)
![swift](https://img.shields.io/badge/swift-green.svg)
[![codebeat badge](https://codebeat.co/badges/f15efd85-e85d-4f72-87ab-f204abd17412)](https://codebeat.co/projects/github-com-qwertyyb-fire-master)
![GitHub release (latest by date including pre-releases)](https://img.shields.io/github/v/release/qwertyyb/Fire?include_prereleases)

## 介绍

Expand Down Expand Up @@ -32,6 +33,10 @@
7. 候选窗口显示输入编码
8. 应用输入模式切换
9. 支持手动调整候选词顺序(`control` + `数字`)
10. 支持用户词库
11. 支持自定义符号
12. 支持自定义主题
13. 支持数据统计


## 安装
Expand Down

0 comments on commit c698e6e

Please sign in to comment.