KeyHandler // Refactoring a little.

This commit is contained in:
ShikiSuen 2022-05-13 12:32:25 +08:00
parent b64e48239e
commit f31373a104
2 changed files with 26 additions and 31 deletions

View File

@ -84,7 +84,6 @@ class KeyHandler: NSObject {
_walkedNodes.removeAll() _walkedNodes.removeAll()
} }
// ObjC 使
func setInputMode(_ value: String) { func setInputMode(_ value: String) {
// isKindOfClass // isKindOfClass
// plist // plist
@ -122,7 +121,7 @@ class KeyHandler: NSObject {
let walker = Megrez.Walker(grid: _builder.grid()) let walker = Megrez.Walker(grid: _builder.grid())
// the reverse walk traces the grid from the end // the reverse walk traces the grid from the end
let walked: [Megrez.NodeAnchor] = walker.reverseWalk(at: _builder.grid().width(), nodesLimit: 3, balanced: true) let walked = walker.reverseWalk(at: _builder.grid().width(), nodesLimit: 3, balanced: true)
// then we use ".reversed()" to reverse the nodes so that we get the forward-walked nodes // then we use ".reversed()" to reverse the nodes so that we get the forward-walked nodes
_walkedNodes.removeAll() _walkedNodes.removeAll()

View File

@ -37,12 +37,10 @@ extension KeyHandler {
) -> Bool { ) -> Bool {
let charCode: UniChar = input.charCode let charCode: UniChar = input.charCode
var state = state // Turn this incoming constant into variable. var state = state // Turn this incoming constant into variable.
let inputText: String = input.inputText ?? ""
// 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.
guard let inputText: String = input.inputText, !inputText.isEmpty else {
if inputText.isEmpty {
return false return false
} }
@ -238,36 +236,34 @@ extension KeyHandler {
// MARK: Calling candidate window using Space or Down or PageUp / PageDn. // MARK: Calling candidate window using Space or Down or PageUp / PageDn.
if let currentState = state as? InputState.NotEmpty { if let currentState = state as? InputState.NotEmpty, _composer.isEmpty,
if _composer.isEmpty, input.isExtraChooseCandidateKey || input.isExtraChooseCandidateKeyReverse || input.isSpace
input.isExtraChooseCandidateKey || input.isExtraChooseCandidateKeyReverse || input.isSpace || input.isPageDown || input.isPageUp || input.isTab
|| input.isPageDown || input.isPageUp || input.isTab || (input.useVerticalMode && (input.isVerticalModeOnlyChooseCandidateKey))
|| (input.useVerticalMode && (input.isVerticalModeOnlyChooseCandidateKey)) {
{ if input.isSpace {
if input.isSpace { // If the Space key is NOT set to be a selection key
// If the Space key is NOT set to be a selection key if input.isShiftHold || !mgrPrefs.chooseCandidateUsingSpace {
if input.isShiftHold || !mgrPrefs.chooseCandidateUsingSpace { if getBuilderCursorIndex() >= getBuilderLength() {
if getBuilderCursorIndex() >= getBuilderLength() { let composingBuffer = currentState.composingBuffer
let composingBuffer = currentState.composingBuffer if !composingBuffer.isEmpty {
if (composingBuffer.count) != 0 { stateCallback(InputState.Committing(poppedText: composingBuffer))
stateCallback(InputState.Committing(poppedText: composingBuffer))
}
clear()
stateCallback(InputState.Committing(poppedText: " "))
stateCallback(InputState.Empty())
} else if ifLangModelHasUnigrams(forKey: " ") {
insertReadingToBuilderAtCursor(reading: " ")
let poppedText = popOverflowComposingTextAndWalk()
let inputting = buildInputtingState()
inputting.poppedText = poppedText
stateCallback(inputting)
} }
return true clear()
stateCallback(InputState.Committing(poppedText: " "))
stateCallback(InputState.Empty())
} else if ifLangModelHasUnigrams(forKey: " ") {
insertReadingToBuilderAtCursor(reading: " ")
let poppedText = popOverflowComposingTextAndWalk()
let inputting = buildInputtingState()
inputting.poppedText = poppedText
stateCallback(inputting)
} }
return true
} }
stateCallback(buildCandidate(state: currentState, useVerticalMode: input.useVerticalMode))
return true
} }
stateCallback(buildCandidate(state: currentState, useVerticalMode: input.useVerticalMode))
return true
} }
// MARK: - // MARK: -