-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from qwertyyb/feature/shift-key-up
feat: 调短shift按键的检测时间,降低误触发概率;一键设置正在输入应用的预设输入模式
- Loading branch information
Showing
9 changed files
with
132 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters