vChewing-macOS/Source/Modules/ControllerModules/KeyHandler_HandleCandidate....

330 lines
10 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// (c) 2021 and onwards The vChewing Project (MIT-NTL License).
// Refactored from the ObjCpp-version of this class by:
// (c) 2011 and onwards The OpenVanilla Project (MIT License).
// ====================
// This code is released under the MIT license (SPDX-License-Identifier: MIT)
// ... with NTL restriction stating that:
// No trademark license is granted to use the trade names, trademarks, service
// marks, or product names of Contributor, except as required to fulfill notice
// requirements defined in MIT License.
/// 調
import Foundation
// MARK: - § 調 (Handle Candidate State).
extension KeyHandler {
///
/// - Parameters:
/// - input:
/// - state:
/// - stateCallback:
/// - errorCallback:
/// - Returns: IMK
func handleCandidate(
state: IMEStateProtocol,
input: InputSignalProtocol,
stateCallback: @escaping (IMEStateProtocol) -> Void,
errorCallback: @escaping () -> Void
) -> Bool {
guard var ctlCandidateCurrent = delegate?.ctlCandidate() else {
IME.prtDebugIntel("06661F6E")
errorCallback()
return true
}
// MARK: (Cancel Candidate)
let cancelCandidateKey =
input.isBackSpace || input.isEsc || input.isDelete
|| ((input.isCursorBackward || input.isCursorForward) && input.isShiftHold)
if cancelCandidateKey {
if state.type == .ofAssociates
|| mgrPrefs.useSCPCTypingMode
|| compositor.isEmpty
{
//
//
// 使 BackSpace
// compositor.isEmpty
stateCallback(IMEState.ofAbortion())
} else {
stateCallback(buildInputtingState)
}
if state.type == .ofSymbolTable, let nodePrevious = state.node.previous, let _ = nodePrevious.children {
stateCallback(IMEState.ofSymbolTable(node: nodePrevious))
}
return true
}
// MARK: Enter
if input.isEnter {
if state.type == .ofAssociates, !mgrPrefs.alsoConfirmAssociatedCandidatesByEnter {
stateCallback(IMEState.ofAbortion())
return true
}
delegate?.keyHandler(
self,
didSelectCandidateAt: ctlCandidateCurrent.selectedCandidateIndex,
ctlCandidate: ctlCandidateCurrent
)
return true
}
// MARK: Tab
if input.isTab {
let updated: Bool =
mgrPrefs.specifyShiftTabKeyBehavior
? (input.isShiftHold
? ctlCandidateCurrent.showPreviousPage()
: ctlCandidateCurrent.showNextPage())
: (input.isShiftHold
? ctlCandidateCurrent.highlightPreviousCandidate()
: ctlCandidateCurrent.highlightNextCandidate())
if !updated {
IME.prtDebugIntel("9B691919")
errorCallback()
}
return true
}
// MARK: Space
if input.isSpace {
let updated: Bool =
mgrPrefs.specifyShiftSpaceKeyBehavior
? (input.isShiftHold
? ctlCandidateCurrent.highlightNextCandidate()
: ctlCandidateCurrent.showNextPage())
: (input.isShiftHold
? ctlCandidateCurrent.showNextPage()
: ctlCandidateCurrent.highlightNextCandidate())
if !updated {
IME.prtDebugIntel("A11C781F")
errorCallback()
}
return true
}
// MARK: PgDn
if input.isPageDown {
let updated: Bool = ctlCandidateCurrent.showNextPage()
if !updated {
IME.prtDebugIntel("9B691919")
errorCallback()
}
return true
}
// MARK: PgUp
if input.isPageUp {
let updated: Bool = ctlCandidateCurrent.showPreviousPage()
if !updated {
IME.prtDebugIntel("9569955D")
errorCallback()
}
return true
}
// MARK: Left Arrow
if input.isLeft {
switch ctlCandidateCurrent.currentLayout {
case .horizontal:
if !ctlCandidateCurrent.highlightPreviousCandidate() {
IME.prtDebugIntel("1145148D")
errorCallback()
}
case .vertical:
if !ctlCandidateCurrent.showPreviousPage() {
IME.prtDebugIntel("1919810D")
errorCallback()
}
}
return true
}
// MARK: Right Arrow
if input.isRight {
switch ctlCandidateCurrent.currentLayout {
case .horizontal:
if !ctlCandidateCurrent.highlightNextCandidate() {
IME.prtDebugIntel("9B65138D")
errorCallback()
}
case .vertical:
if !ctlCandidateCurrent.showNextPage() {
IME.prtDebugIntel("9244908D")
errorCallback()
}
}
return true
}
// MARK: Up Arrow
if input.isUp {
switch ctlCandidateCurrent.currentLayout {
case .horizontal:
if !ctlCandidateCurrent.showPreviousPage() {
IME.prtDebugIntel("9B614524")
errorCallback()
}
case .vertical:
if !ctlCandidateCurrent.highlightPreviousCandidate() {
IME.prtDebugIntel("ASD9908D")
errorCallback()
}
}
return true
}
// MARK: Down Arrow
if input.isDown {
switch ctlCandidateCurrent.currentLayout {
case .horizontal:
if !ctlCandidateCurrent.showNextPage() {
IME.prtDebugIntel("92B990DD")
errorCallback()
}
case .vertical:
if !ctlCandidateCurrent.highlightNextCandidate() {
IME.prtDebugIntel("6B99908D")
errorCallback()
}
}
return true
}
// MARK: Home Key
if input.isHome {
if ctlCandidateCurrent.selectedCandidateIndex == 0 {
IME.prtDebugIntel("9B6EDE8D")
errorCallback()
} else {
ctlCandidateCurrent.selectedCandidateIndex = 0
}
return true
}
// MARK: End Key
if state.candidates.isEmpty {
return false
} else { // count > 0!isEmpty滿
if input.isEnd {
if ctlCandidateCurrent.selectedCandidateIndex == state.candidates.count - 1 {
IME.prtDebugIntel("9B69AAAD")
errorCallback()
} else {
ctlCandidateCurrent.selectedCandidateIndex = state.candidates.count - 1
}
return true
}
}
// MARK: (Associated Phrases)
if state.type == .ofAssociates {
if !input.isShiftHold { return false }
}
var index: Int = NSNotFound
let match: String =
(state.type == .ofAssociates) ? input.inputTextIgnoringModifiers ?? "" : input.text
for j in 0..<ctlCandidateCurrent.keyLabels.count {
let label: CandidateKeyLabel = ctlCandidateCurrent.keyLabels[j]
if match.compare(label.key, options: .caseInsensitive, range: nil, locale: .current) == .orderedSame {
index = j
break
}
}
if index != NSNotFound {
let candidateIndex = ctlCandidateCurrent.candidateIndexAtKeyLabelIndex(index)
if candidateIndex != Int.max {
delegate?.keyHandler(
self, didSelectCandidateAt: candidateIndex, ctlCandidate: ctlCandidateCurrent
)
return true
}
}
if state.type == .ofAssociates { return false }
// MARK: (SCPC Mode Processing)
if mgrPrefs.useSCPCTypingMode {
///
/// - /
/// -
let punctuationNamePrefix: String = generatePunctuationNamePrefix(withKeyCondition: input)
let parser = currentMandarinParser
let arrCustomPunctuations: [String] = [
punctuationNamePrefix, parser, input.text,
]
let customPunctuation: String = arrCustomPunctuations.joined(separator: "")
///
let arrPunctuations: [String] = [
punctuationNamePrefix, input.text,
]
let punctuation: String = arrPunctuations.joined(separator: "")
var shouldAutoSelectCandidate: Bool =
composer.inputValidityCheck(key: input.charCode) || currentLM.hasUnigramsFor(key: customPunctuation)
|| currentLM.hasUnigramsFor(key: punctuation)
if !shouldAutoSelectCandidate, input.isUpperCaseASCIILetterKey {
let letter = "_letter_\(input.text)"
if currentLM.hasUnigramsFor(key: letter) { shouldAutoSelectCandidate = true }
}
if shouldAutoSelectCandidate {
let candidateIndex = ctlCandidateCurrent.candidateIndexAtKeyLabelIndex(0)
if candidateIndex != Int.max {
delegate?.keyHandler(
self,
didSelectCandidateAt: candidateIndex,
ctlCandidate: ctlCandidateCurrent
)
stateCallback(IMEState.ofAbortion())
return handle(
input: input, state: IMEState.ofEmpty(), stateCallback: stateCallback, errorCallback: errorCallback
)
}
return true
}
}
// MARK: - Flipping pages by using symbol menu keys (when they are not occupied).
if input.isSymbolMenuPhysicalKey {
let updated: Bool =
input.isShiftHold ? ctlCandidateCurrent.showPreviousPage() : ctlCandidateCurrent.showNextPage()
if !updated {
IME.prtDebugIntel("66F3477B")
errorCallback()
}
return true
}
IME.prtDebugIntel("172A0F81")
errorCallback()
return true
}
}