diff --git a/Fire.xcodeproj/project.pbxproj b/Fire.xcodeproj/project.pbxproj index 87fbf58..c7ad82e 100644 --- a/Fire.xcodeproj/project.pbxproj +++ b/Fire.xcodeproj/project.pbxproj @@ -32,7 +32,6 @@ 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 */; }; @@ -108,7 +107,6 @@ 45D996A9253C5232001460A8 /* GeneralPane.swift */ = {isa = PBXFileReference; indentWidth = 4; lastKnownFileType = sourcecode.swift; path = GeneralPane.swift; sourceTree = ""; }; 45DB6EC627E5B8FE00A39925 /* ThemeConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThemeConfig.swift; sourceTree = ""; }; 45DB6EC827E609CB00A39925 /* ThemePane.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThemePane.swift; sourceTree = ""; }; - 45DCE62126A31F140009FED1 /* ApplicationSettingCache.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ApplicationSettingCache.swift; sourceTree = ""; }; 45E85F732573BE1800F71024 /* wb_98_table.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = wb_98_table.txt; sourceTree = ""; }; 45EBB54D283A073000A56CBA /* Statistics.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Statistics.swift; sourceTree = ""; }; 45EBB54F283A311C00A56CBA /* StatisticsPane.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatisticsPane.swift; sourceTree = ""; }; @@ -277,7 +275,6 @@ 673C417125468FFA00F462A3 /* ModifierKeyUpChecker.swift */, 673C4177254697E400F462A3 /* TipsWindow.swift */, 677A0873254BD47D000B58D4 /* ToastWindow.swift */, - 45DCE62126A31F140009FED1 /* ApplicationSettingCache.swift */, 45EBB54D283A073000A56CBA /* Statistics.swift */, ); path = Utils; @@ -495,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 */, diff --git a/Fire.xcodeproj/xcshareddata/xcschemes/Fire.xcscheme b/Fire.xcodeproj/xcshareddata/xcschemes/Fire.xcscheme index bf5ee62..ef46fb5 100644 --- a/Fire.xcodeproj/xcshareddata/xcschemes/Fire.xcscheme +++ b/Fire.xcodeproj/xcshareddata/xcschemes/Fire.xcscheme @@ -45,7 +45,7 @@ ActionType = "Xcode.IDEStandardExecutionActionsCore.ExecutionActionType.ShellScriptAction"> + scriptText = "# Type a script or drag a script file from your workspace to insert its path. pkill -9 Fire "> diff --git a/Fire/Fire.swift b/Fire/Fire.swift index 1ce4045..5848a41 100644 --- a/Fire/Fire.swift +++ b/Fire/Fire.swift @@ -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? { diff --git a/Fire/FireInputServer.swift b/Fire/FireInputServer.swift index 181edf0..36ab293 100644 --- a/Fire/FireInputServer.swift +++ b/Fire/FireInputServer.swift @@ -9,24 +9,23 @@ import Foundation import Defaults -var previousClientIdentifier: String = "" +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 - } - if !Defaults[.keepAppInputMode] { return } - // 启用APP缓存设置 - if let appSetting = Fire.shared.appSettingCache.get(bundleIdentifier: identifier), + 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) } @@ -34,14 +33,8 @@ extension FireInputController { private func savePreviousClientInputMode() { if previousClientIdentifier.count > 0 { - // 缓存当前输入模式 - Fire.shared.appSettingCache.add( - bundleIdentifier: previousClientIdentifier, - setting: ApplicationSettingItem( - bundleId: previousClientIdentifier, - inputMs: InputModeSetting(rawValue: inputMode.rawValue)! - ) - ) + // 缓存当前输入模式 + inputModeCache.updateValue(inputMode, forKey: previousClientIdentifier) } } @@ -64,14 +57,15 @@ extension FireInputController { } /** - * 由于使用recognizedEvents在一些场景下不能监听到flagChanged事件,比如保存文件场景 - * 所以这里需要使用NSEvent.addGlobalMonitorForEvents监听shift键被按下 + * 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 } diff --git a/Fire/Utils/ApplicationSettingCache.swift b/Fire/Utils/ApplicationSettingCache.swift deleted file mode 100644 index 43ccf32..0000000 --- a/Fire/Utils/ApplicationSettingCache.swift +++ /dev/null @@ -1,38 +0,0 @@ -// -// ApplicationSettingCache.swift -// Fire -// -// Created by 虚幻 on 2021/7/17. -// Copyright © 2021 qwertyyb. All rights reserved. -// - -import Foundation - -class ApplicationSettingCache { - - private var cache: [String: ApplicationSettingItem] = [:] - - private var maxCount: Int = 50 - - private func handleRemove() { - if cache.count <= maxCount { return } - guard let oldest = cache.sorted(by: { a, b in - a.value.createdTimestamp < b.value.createdTimestamp - }).first else { return } - cache.removeValue(forKey: oldest.key) - } - - func add(bundleIdentifier: String, setting: ApplicationSettingItem) { - cache[bundleIdentifier] = setting - } - func get(bundleIdentifier: String) -> ApplicationSettingItem? { - guard let setting = cache[bundleIdentifier] else { return nil } - cache.removeValue(forKey: bundleIdentifier) - add( - bundleIdentifier: bundleIdentifier, - setting: ApplicationSettingItem(bundleId: setting.bundleIdentifier, inputMs: setting.inputModeSetting) - ) - handleRemove() - return setting - } -}