KeyHandler // handleInput: Simplify a little.

This commit is contained in:
ShikiSuen 2022-04-18 14:34:56 +08:00
parent a1a948adeb
commit 454743186d
1 changed files with 9 additions and 10 deletions

View File

@ -31,14 +31,13 @@ import Cocoa
@objc extension KeyHandler { @objc extension KeyHandler {
func handle( func handle(
input: keyParser, input: keyParser,
state inState: InputState, state: InputState,
stateCallback: @escaping (InputState) -> Void, stateCallback: @escaping (InputState) -> Void,
errorCallback: @escaping () -> Void errorCallback: @escaping () -> Void
) -> Bool { ) -> Bool {
let charCode: UniChar = input.charCode let charCode: UniChar = input.charCode
var state = inState // Turn this incoming constant into variable. var state = state // Turn this incoming constant into variable.
let inputText: String = input.inputText ?? "" let inputText: String = input.inputText ?? ""
let emptyState = InputState.Empty()
// Ignore the input if its inputText is empty. // Ignore the input if its inputText is empty.
// Reason: such inputs may be functional key combinations. // Reason: such inputs may be functional key combinations.
@ -66,7 +65,7 @@ import Cocoa
} else if input.isCapsLockOn { } else if input.isCapsLockOn {
// Process all possible combination, we hope. // Process all possible combination, we hope.
clear() clear()
stateCallback(emptyState) stateCallback(InputState.Empty())
// When shift is pressed, don't do further processing... // When shift is pressed, don't do further processing...
// ...since it outputs capital letter anyway. // ...since it outputs capital letter anyway.
@ -83,7 +82,7 @@ import Cocoa
// Commit the entire input buffer. // Commit the entire input buffer.
let committingState = InputState.Committing(poppedText: inputText.lowercased()) let committingState = InputState.Committing(poppedText: inputText.lowercased())
stateCallback(committingState) stateCallback(committingState)
stateCallback(emptyState) stateCallback(InputState.Empty())
return true return true
} }
@ -95,10 +94,10 @@ import Cocoa
!input.isUp, !input.isSpace, isPrintable(charCode) !input.isUp, !input.isSpace, isPrintable(charCode)
{ {
clear() clear()
stateCallback(emptyState) stateCallback(InputState.Empty())
let committing = InputState.Committing(poppedText: inputText.lowercased()) let committing = InputState.Committing(poppedText: inputText.lowercased())
stateCallback(committing) stateCallback(committing)
stateCallback(emptyState) stateCallback(InputState.Empty())
return true return true
} }
} }
@ -120,7 +119,7 @@ import Cocoa
if result { if result {
return true return true
} else { } else {
stateCallback(emptyState) stateCallback(InputState.Empty())
} }
} }
@ -203,7 +202,7 @@ import Cocoa
stateCallback(committing) stateCallback(committing)
if !mgrPrefs.associatedPhrasesEnabled { if !mgrPrefs.associatedPhrasesEnabled {
stateCallback(emptyState) stateCallback(InputState.Empty())
} else { } else {
let associatedPhrases = let associatedPhrases =
buildAssociatePhraseState( buildAssociatePhraseState(
@ -213,7 +212,7 @@ import Cocoa
if let associatedPhrases = associatedPhrases { if let associatedPhrases = associatedPhrases {
stateCallback(associatedPhrases) stateCallback(associatedPhrases)
} else { } else {
stateCallback(emptyState) stateCallback(InputState.Empty())
} }
} }
} else { } else {