diff --git a/Source/Modules/ControllerModules/KeyHandler_Core.swift b/Source/Modules/ControllerModules/KeyHandler_Core.swift index d2f59cb4..2a15095b 100644 --- a/Source/Modules/ControllerModules/KeyHandler_Core.swift +++ b/Source/Modules/ControllerModules/KeyHandler_Core.swift @@ -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() diff --git a/Source/Modules/ControllerModules/KeyHandler_HandleInput.swift b/Source/Modules/ControllerModules/KeyHandler_HandleInput.swift index 9928654d..96e944f6 100644 --- a/Source/Modules/ControllerModules/KeyHandler_HandleInput.swift +++ b/Source/Modules/ControllerModules/KeyHandler_HandleInput.swift @@ -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: -