vChewing-macOS/Source/Modules/InputHandler_HandleCandidat...

313 lines
9.2 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).
// ====================
// 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 CandidateWindow
import Shared
// MARK: - § 調 (Handle Candidate State).
extension InputHandler {
///
/// - Parameters:
/// - input:
/// - state:
/// - stateCallback:
/// - errorCallback:
/// - Returns: IMK
func handleCandidate(
state: IMEStateProtocol,
input: InputSignalProtocol,
stateCallback: @escaping (IMEStateProtocol) -> Void,
errorCallback: @escaping (String) -> Void
) -> Bool {
guard var ctlCandidate = delegate?.candidateController() else {
errorCallback("06661F6E")
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
|| prefs.useSCPCTypingMode
|| compositor.isEmpty
{
//
//
// 使 BackSpace
// compositor.isEmpty
stateCallback(IMEState.ofAbortion())
} else {
stateCallback(generateStateOfInputting())
}
if state.type == .ofSymbolTable, let nodePrevious = state.node.previous, !nodePrevious.members.isEmpty {
stateCallback(IMEState.ofSymbolTable(node: nodePrevious))
}
return true
}
// MARK: Enter
if input.isEnter {
if state.type == .ofAssociates, !prefs.alsoConfirmAssociatedCandidatesByEnter {
stateCallback(IMEState.ofAbortion())
return true
}
delegate?.candidateSelectionCalledByInputHandler(at: ctlCandidate.selectedCandidateIndex)
return true
}
// MARK: Tab
if input.isTab {
let updated: Bool =
prefs.specifyShiftTabKeyBehavior
? (input.isShiftHold
? ctlCandidate.showPreviousLine()
: ctlCandidate.showNextLine())
: (input.isShiftHold
? ctlCandidate.highlightPreviousCandidate()
: ctlCandidate.highlightNextCandidate())
if !updated {
errorCallback("9B691919")
}
return true
}
// MARK: Space
if input.isSpace {
let updated: Bool =
prefs.specifyShiftSpaceKeyBehavior
? (input.isShiftHold
? ctlCandidate.highlightNextCandidate()
: ctlCandidate.showNextLine())
: (input.isShiftHold
? ctlCandidate.showNextLine()
: ctlCandidate.highlightNextCandidate())
if !updated {
errorCallback("A11C781F")
}
return true
}
// MARK: PgDn
if input.isPageDown {
let updated: Bool = ctlCandidate.showNextPage()
if !updated {
errorCallback("9B691919")
}
return true
}
// MARK: PgUp
if input.isPageUp {
let updated: Bool = ctlCandidate.showPreviousPage()
if !updated {
errorCallback("9569955D")
}
return true
}
// MARK: Left Arrow
if input.isLeft {
switch ctlCandidate.currentLayout {
case .horizontal:
if !ctlCandidate.highlightPreviousCandidate() {
errorCallback("1145148D")
}
case .vertical:
if !ctlCandidate.showPreviousLine() {
errorCallback("1919810D")
}
@unknown default:
break
}
return true
}
// MARK: Right Arrow
if input.isRight {
switch ctlCandidate.currentLayout {
case .horizontal:
if !ctlCandidate.highlightNextCandidate() {
errorCallback("9B65138D")
}
case .vertical:
if !ctlCandidate.showNextLine() {
errorCallback("9244908D")
}
@unknown default:
break
}
return true
}
// MARK: Up Arrow
if input.isUp {
switch ctlCandidate.currentLayout {
case .horizontal:
if !ctlCandidate.showPreviousLine() {
errorCallback("9B614524")
}
case .vertical:
if !ctlCandidate.highlightPreviousCandidate() {
errorCallback("ASD9908D")
}
@unknown default:
break
}
return true
}
// MARK: Down Arrow
if input.isDown {
switch ctlCandidate.currentLayout {
case .horizontal:
if !ctlCandidate.showNextLine() {
errorCallback("92B990DD")
break
}
case .vertical:
if !ctlCandidate.highlightNextCandidate() {
errorCallback("6B99908D")
}
@unknown default:
break
}
return true
}
// MARK: Home Key
if input.isHome {
if ctlCandidate.selectedCandidateIndex == 0 {
errorCallback("9B6EDE8D")
} else {
ctlCandidate.selectedCandidateIndex = 0
}
return true
}
// MARK: End Key
if state.candidates.isEmpty {
return false
} else { // count > 0!isEmpty滿
if input.isEnd {
if ctlCandidate.selectedCandidateIndex == state.candidates.count - 1 {
errorCallback("9B69AAAD")
} else {
ctlCandidate.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
let selectionKeys = delegate?.selectionKeys ?? PrefMgr.shared.candidateKeys
for j in 0..<selectionKeys.count {
let label = selectionKeys.charComponents[j]
if match.compare(label, options: .caseInsensitive, range: nil, locale: .current) == .orderedSame {
index = j
break
}
}
if index != NSNotFound {
let candidateIndex = ctlCandidate.candidateIndexAtKeyLabelIndex(index)
if candidateIndex != -114_514 {
delegate?.candidateSelectionCalledByInputHandler(at: candidateIndex)
return true
}
}
if state.type == .ofAssociates { return false }
// MARK: (SCPC Mode Processing)
if prefs.useSCPCTypingMode {
///
/// - /
/// -
let punctuationNamePrefix: String = generatePunctuationNamePrefix(withKeyCondition: input)
let parser = currentKeyboardParser
let arrCustomPunctuations: [String] = [
punctuationNamePrefix, parser, input.text,
]
let customPunctuation: String = arrCustomPunctuations.joined()
///
let arrPunctuations: [String] = [
punctuationNamePrefix, input.text,
]
let punctuation: String = arrPunctuations.joined()
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 = ctlCandidate.candidateIndexAtKeyLabelIndex(0)
if candidateIndex != -114_514 {
delegate?.candidateSelectionCalledByInputHandler(at: candidateIndex)
stateCallback(IMEState.ofAbortion())
return handleInput(
event: 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 {
var updated = true
updated = input.isShiftHold ? ctlCandidate.showPreviousLine() : ctlCandidate.showNextLine()
if !updated {
errorCallback("66F3477B")
}
return true
}
errorCallback("172A0F81")
return true
}
}