vChewing-macOS/Source/Modules/InputHandler_HandleInput.swift

424 lines
16 KiB
Swift
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.
/// 調 IMK 調
/// 調
import LangModelAssembly
import Shared
// MARK: - § 調 (Handle Input with States)
extension InputHandler {
///
/// - Parameters:
/// - input:
/// - state:
/// - stateCallback:
/// - errorCallback:
/// - Returns: IMK
func handleInput(
event input: InputSignalProtocol,
state: IMEStateProtocol,
stateCallback: @escaping (IMEStateProtocol) -> Void,
errorCallback: @escaping (String) -> Void
) -> Bool {
// inputTest
guard !input.text.isEmpty else { return false }
let inputText: String = input.text
var state = state //
// Megrez
if input.isInvalid {
// .Empty(IgnoringPreviousState) .Deactivated
// .Abortion.Empty
if state.type == .ofEmpty || state.type == .ofDeactivated {
return false
}
errorCallback("550BCF7B: InputHandler just refused an invalid input.")
stateCallback(state)
return true
}
//
let isFunctionKey: Bool =
input.isControlHotKey || (input.isCommandHold || input.isOptionHotKey || input.isNonLaptopFunctionKey)
if state.type != .ofAssociates, !state.hasComposition, !state.isCandidateContainer, isFunctionKey {
return false
}
// MARK: Caps Lock processing.
/// Caps Lock
/// Caps Lock
if input.isBackSpace || input.isEnter
|| input.isCursorClockLeft || input.isCursorClockRight
|| input.isCursorForward || input.isCursorBackward
{
// BackSpace
} else if input.isCapsLockOn || state.isASCIIMode {
//
stateCallback(IMEState.ofEmpty())
// Shift
if (input.isUpperCaseASCIILetterKey && state.isASCIIMode)
|| (input.isCapsLockOn && input.isShiftHold)
{
return false
}
/// ASCII 使insertText:replacementRange:
/// ASCII
if input.isASCII, !input.charCode.isPrintableASCII {
return false
}
//
stateCallback(IMEState.ofCommitting(textToCommit: inputText.lowercased()))
stateCallback(IMEState.ofEmpty())
return true
}
// MARK: (Numeric Pad Processing)
// isNumericPadKey KeyCode
// 使 Cocoa flags
//
if input.isNumericPadKey {
if !(state.type == .ofCandidates || state.type == .ofAssociates
|| state.type == .ofSymbolTable)
{
stateCallback(IMEState.ofEmpty())
stateCallback(IMEState.ofCommitting(textToCommit: inputText.lowercased()))
stateCallback(IMEState.ofEmpty())
return true
}
}
// MARK: (Handle Candidates)
if [.ofCandidates, .ofSymbolTable].contains(state.type) {
return handleCandidate(
state: state, input: input, stateCallback: stateCallback, errorCallback: errorCallback
)
}
// MARK: (Handle Associated Phrases)
if state.type == .ofAssociates {
if handleCandidate(
state: state, input: input, stateCallback: stateCallback, errorCallback: errorCallback
) {
return true
} else {
stateCallback(IMEState.ofEmpty())
}
}
// MARK: 便使() (Handle Marking)
if state.type == .ofMarking {
if handleMarkingState(
state, input: input, stateCallback: stateCallback,
errorCallback: errorCallback
) {
return true
}
state = state.convertedToInputting
stateCallback(state)
}
// MARK: (Handle BPMF Keys)
if let compositionHandled = handleComposition(
input: input, stateCallback: stateCallback, errorCallback: errorCallback
) {
return compositionHandled
}
// MARK: (Calling candidate window using Up / Down or PageUp / PageDn.)
if state.hasComposition, composer.isEmpty, !input.isOptionHold,
input.isCursorClockLeft || input.isCursorClockRight || input.isSpace
|| input.isPageDown || input.isPageUp || (input.isTab && prefs.specifyShiftTabKeyBehavior)
{
if input.isSpace {
/// Space
if !prefs.chooseCandidateUsingSpace {
if compositor.cursor >= compositor.length {
let displayedText = state.displayedText
if !displayedText.isEmpty {
stateCallback(IMEState.ofCommitting(textToCommit: displayedText))
}
stateCallback(IMEState.ofCommitting(textToCommit: " "))
stateCallback(IMEState.ofEmpty())
} else if currentLM.hasUnigramsFor(key: " ") {
compositor.insertKey(" ")
walk()
// App
let textToCommit = commitOverflownComposition
var inputting = generateStateOfInputting()
inputting.textToCommit = textToCommit
stateCallback(inputting)
}
return true
} else if input.isShiftHold { // Tab Shift+Command+Space /
return handleInlineCandidateRotation(
state: state, reverseModifier: input.isCommandHold, stateCallback: stateCallback,
errorCallback: errorCallback
)
}
}
let candidateState: IMEStateProtocol = generateStateOfCandidates(state: state)
if candidateState.candidates.isEmpty {
errorCallback("3572F238")
} else {
stateCallback(candidateState)
}
return true
}
// MARK: -
// MARK: Esc
if input.isEsc { return handleEsc(state: state, stateCallback: stateCallback) }
// MARK: Tab
if input.isTab {
return handleInlineCandidateRotation(
state: state, reverseModifier: input.isShiftHold, stateCallback: stateCallback, errorCallback: errorCallback
)
}
// MARK: Cursor backward
if input.isCursorBackward {
return handleBackward(
state: state, input: input, stateCallback: stateCallback, errorCallback: errorCallback
)
}
// MARK: Cursor forward
if input.isCursorForward {
return handleForward(
state: state, input: input, stateCallback: stateCallback, errorCallback: errorCallback
)
}
// MARK: Home
if input.isHome {
return handleHome(state: state, stateCallback: stateCallback, errorCallback: errorCallback)
}
// MARK: End
if input.isEnd {
return handleEnd(state: state, stateCallback: stateCallback, errorCallback: errorCallback)
}
// MARK: Ctrl+PgLf or Shift+PgLf
if (input.isControlHold || input.isShiftHold) && (input.isOptionHold && input.isLeft) {
return handleHome(state: state, stateCallback: stateCallback, errorCallback: errorCallback)
}
// MARK: Ctrl+PgRt or Shift+PgRt
if (input.isControlHold || input.isShiftHold) && (input.isOptionHold && input.isRight) {
return handleEnd(state: state, stateCallback: stateCallback, errorCallback: errorCallback)
}
// MARK: Clock-Left & Clock-Right
if input.isCursorClockLeft || input.isCursorClockRight {
if input.isOptionHold, state.type == .ofInputting {
if input.isCursorClockRight {
return handleInlineCandidateRotation(
state: state, reverseModifier: false, stateCallback: stateCallback, errorCallback: errorCallback
)
}
if input.isCursorClockLeft {
return handleInlineCandidateRotation(
state: state, reverseModifier: true, stateCallback: stateCallback, errorCallback: errorCallback
)
}
}
return handleClockKey(state: state, stateCallback: stateCallback, errorCallback: errorCallback)
}
// MARK: BackSpace
if input.isBackSpace {
return handleBackSpace(state: state, input: input, stateCallback: stateCallback, errorCallback: errorCallback)
}
// MARK: Delete
if input.isDelete {
return handleDelete(state: state, input: input, stateCallback: stateCallback, errorCallback: errorCallback)
}
// MARK: Enter
if input.isEnter {
return (input.isCommandHold && input.isControlHold)
? (input.isOptionHold
? handleCtrlOptionCommandEnter(state: state, stateCallback: stateCallback)
: handleCtrlCommandEnter(state: state, stateCallback: stateCallback))
: handleEnter(state: state, stateCallback: stateCallback)
}
// MARK: -
// MARK: Punctuation list
if input.isSymbolMenuPhysicalKey, !input.isShiftHold, !input.isControlHold, state.type != .ofDeactivated {
if input.isOptionHold {
if currentLM.hasUnigramsFor(key: "_punctuation_list") {
if composer.isEmpty {
compositor.insertKey("_punctuation_list")
walk()
// App
let textToCommit = commitOverflownComposition
var inputting = generateStateOfInputting()
inputting.textToCommit = textToCommit
stateCallback(inputting)
let candidateState = generateStateOfCandidates(state: inputting)
if candidateState.candidates.isEmpty {
errorCallback("B5127D8A")
} else {
stateCallback(candidateState)
}
} else { //
errorCallback("17446655")
}
return true
}
} else {
// commit buffer ESC
// Enter 使 commit buffer
// bool _ =
_ = handleEnter(state: state, stateCallback: stateCallback)
stateCallback(IMEState.ofSymbolTable(node: CandidateNode.root))
return true
}
}
// MARK: / (FW / HW Arabic Numbers Input)
if state.type == .ofEmpty {
if input.isMainAreaNumKey, input.modifierFlags == [.shift, .option] {
guard let stringRAW = input.mainAreaNumKeyChar else { return false }
let newStringFW = stringRAW.applyingTransform(.fullwidthToHalfwidth, reverse: true) ?? stringRAW
let newStringHW = stringRAW.applyingTransform(.fullwidthToHalfwidth, reverse: false) ?? stringRAW
stateCallback(
IMEState.ofCommitting(textToCommit: prefs.halfWidthPunctuationEnabled ? newStringHW : newStringFW)
)
stateCallback(IMEState.ofEmpty())
return true
}
}
// MARK: Punctuation
///
/// - /
/// -
let punctuationNamePrefix: String = generatePunctuationNamePrefix(withKeyCondition: input)
let parser = currentKeyboardParser
let arrCustomPunctuations: [String] = [punctuationNamePrefix, parser, input.text]
let customPunctuation: String = arrCustomPunctuations.joined()
if handlePunctuation(
customPunctuation,
state: state,
stateCallback: stateCallback,
errorCallback: errorCallback
) {
return true
}
///
let arrPunctuations: [String] = [punctuationNamePrefix, input.text]
let punctuation: String = arrPunctuations.joined()
if handlePunctuation(
punctuation,
state: state,
stateCallback: stateCallback,
errorCallback: errorCallback
) {
return true
}
// MARK: / (Full-Width / Half-Width Space)
/// 使
if state.type == .ofEmpty {
if input.isSpace, !input.isOptionHold, !input.isControlHold, !input.isCommandHold {
stateCallback(IMEState.ofCommitting(textToCommit: input.isShiftHold ? " " : " "))
stateCallback(IMEState.ofEmpty())
return true
}
}
// MARK: Shift+ (Shift+Letter Processing)
if input.isUpperCaseASCIILetterKey, !input.isCommandHold, !input.isControlHold {
if input.isShiftHold { // isOptionHold
switch prefs.upperCaseLetterKeyBehavior {
case 1:
stateCallback(IMEState.ofEmpty())
stateCallback(IMEState.ofCommitting(textToCommit: inputText.lowercased()))
stateCallback(IMEState.ofEmpty())
return true
case 2:
stateCallback(IMEState.ofEmpty())
stateCallback(IMEState.ofCommitting(textToCommit: inputText.uppercased()))
stateCallback(IMEState.ofEmpty())
return true
default: // case 0
let letter = "_letter_\(inputText)"
if handlePunctuation(
letter,
state: state,
stateCallback: stateCallback,
errorCallback: errorCallback
) {
return true
}
}
}
}
// MARK: - (Still Nothing)
/// SessionCtl
///
/// F1-F12
/// 便
if state.hasComposition || !composer.isEmpty {
errorCallback(
"Blocked data: charCode: \(input.charCode), keyCode: \(input.keyCode)")
errorCallback("A9BFF20E")
stateCallback(state)
return true
}
return false
}
}