InputHandler // Implement (BOOL)handleCandidateInput:ignoringModifiers:.

This commit is contained in:
ShikiSuen 2024-01-18 13:18:19 +08:00
parent 71dc1afc48
commit 5e1208bc5e
2 changed files with 12 additions and 12 deletions

View File

@ -18,14 +18,17 @@ import Shared
extension InputHandler { extension InputHandler {
/// ///
/// - Parameter input: /// - Parameters:
/// - input:
/// - ignoringModifiers:
/// - Returns: IMK /// - Returns: IMK
func handleCandidate(input: InputSignalProtocol) -> Bool { func handleCandidate(input: InputSignalProtocol, ignoringModifiers: Bool = false) -> Bool {
guard let delegate = delegate else { return false } guard let delegate = delegate else { return false }
guard var ctlCandidate = delegate.candidateController() else { return false } guard var ctlCandidate = delegate.candidateController() else { return false }
let state = delegate.state let state = delegate.state
guard state.isCandidateContainer else { return false } // isEmpty guard state.isCandidateContainer else { return false } // isEmpty
guard ctlCandidate.visible else { return false } guard ctlCandidate.visible else { return false }
let inputText = ignoringModifiers ? (input.inputTextIgnoringModifiers ?? input.text) : input.text
// MARK: 使 // MARK: 使
@ -184,7 +187,7 @@ extension InputHandler {
let cassetteShift = currentLM.areCassetteCandidateKeysShiftHeld let cassetteShift = currentLM.areCassetteCandidateKeysShiftHeld
shaltShiftHold = shaltShiftHold || cassetteShift shaltShiftHold = shaltShiftHold || cassetteShift
} }
let matched: String = shaltShiftHold ? input.inputTextIgnoringModifiers ?? "" : input.text let matched: String = shaltShiftHold ? input.inputTextIgnoringModifiers ?? "" : inputText
checkSelectionKey: for keyPair in delegate.selectionKeys.enumerated() { checkSelectionKey: for keyPair in delegate.selectionKeys.enumerated() {
guard matched.lowercased() == keyPair.element.lowercased() else { continue } guard matched.lowercased() == keyPair.element.lowercased() else { continue }
index = Int(keyPair.offset) index = Int(keyPair.offset)
@ -210,27 +213,27 @@ extension InputHandler {
let punctuationNamePrefix: String = generatePunctuationNamePrefix(withKeyCondition: input) let punctuationNamePrefix: String = generatePunctuationNamePrefix(withKeyCondition: input)
let parser = currentKeyboardParser let parser = currentKeyboardParser
let arrCustomPunctuations: [String] = [ let arrCustomPunctuations: [String] = [
punctuationNamePrefix, parser, input.text, punctuationNamePrefix, parser, inputText,
] ]
let customPunctuation: String = arrCustomPunctuations.joined() let customPunctuation: String = arrCustomPunctuations.joined()
/// ///
let arrPunctuations: [String] = [ let arrPunctuations: [String] = [
punctuationNamePrefix, input.text, punctuationNamePrefix, inputText,
] ]
let punctuation: String = arrPunctuations.joined() let punctuation: String = arrPunctuations.joined()
let isInputValid: Bool = let isInputValid: Bool =
prefs.cassetteEnabled prefs.cassetteEnabled
? currentLM.isThisCassetteKeyAllowed(key: input.text) : composer.inputValidityCheck(key: input.charCode) ? currentLM.isThisCassetteKeyAllowed(key: inputText) : composer.inputValidityCheck(key: input.charCode)
var shouldAutoSelectCandidate: Bool = var shouldAutoSelectCandidate: Bool =
isInputValid || currentLM.hasUnigramsFor(keyArray: [customPunctuation]) isInputValid || currentLM.hasUnigramsFor(keyArray: [customPunctuation])
|| currentLM.hasUnigramsFor(keyArray: [punctuation]) || currentLM.hasUnigramsFor(keyArray: [punctuation])
if !shouldAutoSelectCandidate, input.isUpperCaseASCIILetterKey { if !shouldAutoSelectCandidate, input.isUpperCaseASCIILetterKey {
let letter = "_letter_\(input.text)" let letter = "_letter_\(inputText)"
if currentLM.hasUnigramsFor(keyArray: [letter]) { shouldAutoSelectCandidate = true } if currentLM.hasUnigramsFor(keyArray: [letter]) { shouldAutoSelectCandidate = true }
} }

View File

@ -30,12 +30,9 @@ extension InputHandler {
// `%symboldef` // `%symboldef`
if handleCassetteSymbolTable(input: input) { if handleCassetteSymbolTable(input: input) {
return true return true
} else if hasQuickCandidates, input.text != currentLM.cassetteWildcardKey, } else if hasQuickCandidates, input.text != currentLM.cassetteWildcardKey {
let itim = input.inputTextIgnoringModifiers,
let newEv = (input as? NSEvent)?.reinitiate(characters: itim)
{
// `%quick` `%symboldef` // `%quick` `%symboldef`
guard !(handleQuickCandidate && handleCandidate(input: newEv)) else { return true } guard !(handleQuickCandidate && handleCandidate(input: input, ignoringModifiers: true)) else { return true }
} else { } else {
// `%quick` // `%quick`
guard !(hasQuickCandidates && handleQuickCandidate && handleCandidate(input: input)) else { return true } guard !(hasQuickCandidates && handleQuickCandidate && handleCandidate(input: input)) else { return true }