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()
}
// ObjC 使
func setInputMode(_ value: String) {
// isKindOfClass
// plist
@ -122,7 +121,7 @@ class KeyHandler: NSObject {
let walker = Megrez.Walker(grid: _builder.grid())
// 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
_walkedNodes.removeAll()

View File

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