Skip to content

Commit

Permalink
feat(app): 保持应用最后使用的输入模式状态持久化,支持应用重启后仍能获取之前的状态
Browse files Browse the repository at this point in the history
  • Loading branch information
qwertyyb committed Nov 7, 2023
1 parent e24e9c3 commit a46a3d7
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 14 deletions.
4 changes: 4 additions & 0 deletions Fire.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
451A556A2AF9F92D00AF57C1 /* InputModeCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 451A55692AF9F92D00AF57C1 /* InputModeCache.swift */; };
45B76CA92AEA6042009AFABD /* PunctuationConversion.swift in Sources */ = {isa = PBXBuildFile; fileRef = 45B76CA82AEA6042009AFABD /* PunctuationConversion.swift */; };
6753419E2AB54A3A00757F76 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6753419D2AB54A3A00757F76 /* main.cpp */; };
675341A72AB54AEA00757F76 /* sqlite3.c in Sources */ = {isa = PBXBuildFile; fileRef = 675341A52AB54AEA00757F76 /* sqlite3.c */; };
Expand Down Expand Up @@ -68,6 +69,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
451A55692AF9F92D00AF57C1 /* InputModeCache.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InputModeCache.swift; sourceTree = "<group>"; };
45B76CA82AEA6042009AFABD /* PunctuationConversion.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PunctuationConversion.swift; sourceTree = "<group>"; };
6753419B2AB54A3A00757F76 /* TableBuilder */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = TableBuilder; sourceTree = BUILT_PRODUCTS_DIR; };
6753419D2AB54A3A00757F76 /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -207,6 +209,7 @@
67C99FF82AB53E62000B5281 /* Utils */,
67C99FEB2AB53DB2000B5281 /* AppDelegate.swift */,
67C99FF22AB53DB4000B5281 /* Fire.entitlements */,
451A55692AF9F92D00AF57C1 /* InputModeCache.swift */,
);
path = Fire;
sourceTree = "<group>";
Expand Down Expand Up @@ -442,6 +445,7 @@
67C9A0412AB53ED3000B5281 /* ThemePane.swift in Sources */,
67C9A03E2AB53ED3000B5281 /* UserDictPane.swift in Sources */,
67C9A03F2AB53ED3000B5281 /* GeneralPane.swift in Sources */,
451A556A2AF9F92D00AF57C1 /* InputModeCache.swift in Sources */,
67C9A00E2AB53E83000B5281 /* Fire.swift in Sources */,
67C9A0102AB53E83000B5281 /* types.swift in Sources */,
67C99FFF2AB53E62000B5281 /* TipsWindow.swift in Sources */,
Expand Down
10 changes: 5 additions & 5 deletions Fire/FireInputServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import Foundation
import Defaults

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

extension FireInputController {
/**
* 根据当前输入的应用改变输入模式
Expand All @@ -25,7 +23,7 @@ extension FireInputController {
return currentMode != Fire.shared.inputMode
}
// 启用APP缓存设置
if Defaults[.keepAppInputMode], let mode = inputModeCache[identifier] {
if Defaults[.keepAppInputMode], let mode = InputModeCache.shared.get(identifier) {
NSLog("[FireInputController] activeClientInputMode from cache: \(identifier), \(mode)")
Fire.shared.toggleInputMode(mode, showTip: false)
return currentMode != Fire.shared.inputMode
Expand All @@ -34,9 +32,11 @@ extension FireInputController {
}

private func savePreviousClientInputMode() {
if let identifier = CandidatesWindow.shared.inputController?.client()?.bundleIdentifier() {
if Defaults[.keepAppInputMode],
let identifier = CandidatesWindow.shared.inputController?.client()?.bundleIdentifier(),
Defaults[.appSettings][identifier] == nil {
// 缓存当前输入模式
inputModeCache.updateValue(inputMode, forKey: identifier)
InputModeCache.shared.put(identifier, inputMode)
}
}

Expand Down
59 changes: 59 additions & 0 deletions Fire/InputModeCache.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// InputModeCache.swift
// Fire
//
// Created by qwertyyb on 2023/11/7.
//
import Defaults

class InputModeCache {
let capacity = 100
private var cache: [String: InputMode] = [:]
private var keys: [String] = []

private init() {
loadFromUserDefaults()
}

func get(_ key: String) -> InputMode? {
if let value = cache[key] {
updateKeyOrder(key)
return value
}
return nil
}

func put(_ key: String, _ value: InputMode) {
if cache[key] == nil {
if keys.count >= capacity {
let oldestKey = keys.removeFirst()
cache[oldestKey] = nil
}
keys.append(key)
} else {
updateKeyOrder(key)
}
cache[key] = value
saveToUserDefaults()
}

private func updateKeyOrder(_ key: String) {
if let index = keys.firstIndex(of: key) {
keys.remove(at: index)
keys.append(key)
saveToUserDefaults()
}
}

private func saveToUserDefaults() {
Defaults[.keepAppInputMode_keys] = keys
Defaults[.keepAppInputMode_cache] = cache
}

private func loadFromUserDefaults() {
keys = Defaults[.keepAppInputMode_keys]
cache = Defaults[.keepAppInputMode_cache]
}

static let shared = InputModeCache()
}
12 changes: 9 additions & 3 deletions Fire/Preferences/ApplicationPane.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,16 @@ struct ApplicationPane: View {
HStack {
Text("自动切换")
Toggle("保持应用最后使用的输入模式", isOn: $keepAppInputMode)
.padding(.leading, 12)
.padding(.leading, 10)
}
HStack {
Picker("显示提示", selection: $appInputModeTipShowTime) {
Text("仅保留最近使用的\(InputModeCache.shared.capacity)个应用的输入模式")
.font(.footnote)
.padding(.leading, 70)
}
HStack {
Text("显示提示")
Picker("", selection: $appInputModeTipShowTime) {
Text("仅在变化时显示").tag(AppInputModeTipShowTime.onlyChanged)
Text("总是显示").tag(AppInputModeTipShowTime.always)
Text("不显示")
Expand All @@ -130,7 +136,7 @@ struct ApplicationPane: View {
Text("添加")
}
}
.padding(.leading, 12)
.padding(.leading, 8)
}
ScrollView(.vertical) {
if appSettings.count > 0 {
Expand Down
2 changes: 1 addition & 1 deletion Fire/PunctuationConversion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// PunctuationConversion.swift
// Fire
//
// Created by 杨永榜 on 2023/10/26.
// Created by qwertyyb on 2023/10/26.
//

import Foundation
Expand Down
10 changes: 5 additions & 5 deletions Fire/types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ extension Defaults.Keys {

// 应用输入配置
static let keepAppInputMode = Key<Bool>("keepAppInputMode", default: true)
static let keepAppInputMode_keys = Key<[String]>("keepAppInputMode_keys", default: [])
static let keepAppInputMode_cache = Key<[String: InputMode]>("keepAppInputMode_cache", default: [:])

static let appInputModeTipShowTime = Key<AppInputModeTipShowTime>("appInputModeTipShowTime", default: .onlyChanged)
static let appSettings = Key<[String: ApplicationSettingItem]>(
"AppSettings",
default: [:]
)
static let appSettings = Key<[String: ApplicationSettingItem]>("AppSettings", default: [:])
// 标点符号配置
static let punctuationMode = Key<PunctuationMode>("punctuationMode", default: PunctuationMode.zhhans)
static let customPunctuationSettings = Key<[String: String]>("customPunctuationSettings", default: punctuation)
Expand All @@ -145,7 +145,7 @@ extension Defaults.Keys {
// Key Type UserDefaults name Default value
}

enum InputMode: String {
enum InputMode: String, Defaults.Serializable {
case zhhans
case enUS
}
Expand Down

0 comments on commit a46a3d7

Please sign in to comment.