vChewing-macOS/Packages/vChewing_MainAssembly/Sources/MainAssembly/InputHandler_TriageInput.swift

219 lines
10 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 CocoaExtension
import IMKUtils
import InputMethodKit
import LangModelAssembly
import Megrez
import Shared
// MARK: - § 調 (Handle Input with States) * Triage
public extension InputHandler {
func triageInput(event input: InputSignalProtocol) -> Bool {
guard let delegate = delegate else { return false }
var state: IMEStateProtocol { delegate.state }
let inputText = input.text
// MARK: - Triage by KeyCode
func triageByKeyCode() -> Bool? {
guard let keyCodeType = KeyCode(rawValue: input.keyCode) else { return nil }
switch keyCodeType {
case .kEscape: return handleEsc()
case .kTab, .kContextMenu: return revolveCandidate(reverseOrder: input.isShiftHold)
case .kUpArrow, .kDownArrow, .kLeftArrow, .kRightArrow:
let rotation: Bool = (input.isOptionHold || input.isShiftHold) && state.type == .ofInputting
handleArrowKey: switch (keyCodeType, delegate.isVerticalTyping) {
case (.kLeftArrow, false), (.kUpArrow, true): return handleBackward(input: input)
case (.kRightArrow, false), (.kDownArrow, true): return handleForward(input: input)
case (.kUpArrow, false), (.kLeftArrow, true):
return rotation ? revolveCandidate(reverseOrder: true) : handleClockKey()
case (.kDownArrow, false), (.kRightArrow, true):
return rotation ? revolveCandidate(reverseOrder: false) : handleClockKey()
default: break handleArrowKey //
}
case .kHome: return handleHome()
case .kEnd: return handleEnd()
case .kBackSpace: return handleBackSpace(input: input)
case .kWindowsDelete: return handleDelete(input: input)
case .kCarriageReturn, .kLineFeed:
let frontNode = compositor.walkedNodes.last
return handleEnter(input: input) {
guard !self.isHaninKeyboardSymbolMode, !self.isCodePointInputMode else { return [] }
guard let frontNode = frontNode else { return [] }
let pair = Megrez.KeyValuePaired(keyArray: frontNode.keyArray, value: frontNode.value)
let associates = self.generateArrayOfAssociates(withPair: pair)
return associates
}
case .kSymbolMenuPhysicalKeyJIS, .kSymbolMenuPhysicalKeyIntl:
let isJIS = keyCodeType == .kSymbolMenuPhysicalKeyJIS
switch input.keyModifierFlags {
case []:
return handlePunctuationList(alternative: false, isJIS: isJIS)
case [.option, .shift]:
return handlePunctuationList(alternative: true, isJIS: isJIS)
case .option:
switch (isCodePointInputMode, isHaninKeyboardSymbolMode) {
case (false, false): return handleCodePointInputToggle()
case (true, false), (false, true):
return handleHaninKeyboardSymbolModeToggle()
default: break
}
return true
default: break
}
case .kSpace:
// Space
//
switch state.type {
case .ofEmpty:
if !input.isOptionHold, !input.isControlHold, !input.isCommandHold {
delegate.switchState(IMEState.ofCommitting(textToCommit: input.isShiftHold ? " " : " "))
return true
}
case .ofInputting:
// Tab Shift+Command+Space /
if input.isShiftHold, !input.isControlHold, !input.isOptionHold {
return revolveCandidate(reverseOrder: input.isCommandHold)
}
if isCodePointInputMode {
delegate.callError("FDD88EDB")
delegate.switchState(IMEState.ofAbortion())
return true
}
if compositor.cursor < compositor.length, compositor.insertKey(" ") {
walk()
// App
let textToCommit = commitOverflownComposition
var inputting = generateStateOfInputting()
inputting.textToCommit = textToCommit
delegate.switchState(inputting)
} else {
let displayedText = state.displayedText
if !displayedText.isEmpty, !isConsideredEmptyForNow {
delegate.switchState(IMEState.ofCommitting(textToCommit: displayedText))
}
delegate.switchState(IMEState.ofCommitting(textToCommit: " "))
}
return true
default: break
}
default: break
}
return nil
}
// MARK: - Triage by States
triageByState: switch state.type {
case .ofDeactivated, .ofAbortion, .ofCommitting: return false
case .ofAssociates, .ofCandidates, .ofSymbolTable:
let result = handleCandidate(input: input)
guard !result, state.type == .ofAssociates else { return true }
delegate.switchState(IMEState.ofEmpty())
return triageInput(event: input)
case .ofMarking:
if handleMarkingState(input: input) { return true }
delegate.switchState(state.convertedToInputting)
return triageInput(event: input)
case .ofEmpty, .ofInputting:
//
guard !(input.isInvalid && state.type == .ofEmpty) else { return false }
//
let isFunctional: Bool = (input.isControlHold && input.beganWithLetter)
|| (input.isCommandHold || input.isOptionHotKey || input.isNonLaptopFunctionKey)
if !state.hasComposition, isFunctional { return false }
// Caps Lock
// Caps Lock
if let capsHandleResult = handleCapsLockAndAlphanumericalMode(input: input) {
return capsHandleResult
}
//
if input.isNumericPadKey {
delegate.switchState(IMEState.ofEmpty())
delegate.switchState(IMEState.ofCommitting(textToCommit: inputText.lowercased()))
return true
}
//
haninSymbolInput: if prefs.classicHaninKeyboardSymbolModeShortcutEnabled {
guard let x = input.inputTextIgnoringModifiers,
"¥\\".contains(x), input.keyModifierFlags.isEmpty
else { break haninSymbolInput }
return handleHaninKeyboardSymbolModeToggle()
}
//
if isHaninKeyboardSymbolMode, [[], .shift].contains(input.keyModifierFlags) {
return handleHaninKeyboardSymbolModeInput(input: input)
} else if let compositionHandled = handleComposition(input: input) {
return compositionHandled
}
//
if callCandidateState(input: input) { return true }
// Ctrl+Command+[]
// Shift+Command+[] Chrome Ctrl
let ctrlCMD: Bool = input.keyModifierFlags == [.control, .command]
let ctrlShiftCMD: Bool = input.keyModifierFlags == [.control, .command, .shift]
revolveCandidateWithBrackets: if ctrlShiftCMD || ctrlCMD {
if state.type != .ofInputting { break revolveCandidateWithBrackets }
// JIS US
switch (input.keyCode, IMEApp.isKeyboardJIS) {
case (30, true), (33, false): return revolveCandidate(reverseOrder: true)
case (42, true), (30, false): return revolveCandidate(reverseOrder: false)
default: break
}
}
// keyCode
if let keyCodeTriaged = triageByKeyCode() { return keyCodeTriaged }
// keysToDirectlyCommit
var cinDirectlyCommit = prefs.cassetteEnabled && !currentLM.keysToDirectlyCommit.isEmpty
cinDirectlyCommit = cinDirectlyCommit && [.ofInputting, .ofEmpty].contains(state.type)
cinDirectlyCommit = cinDirectlyCommit && currentLM.keysToDirectlyCommit.contains(input.text)
guard !cinDirectlyCommit else { break triageByState }
// /
if handleArabicNumeralInputs(input: input) { return true }
//
let queryStrings: [String] = punctuationQueryStrings(input: input)
for queryString in queryStrings {
guard !handlePunctuation(queryString) else { return true }
}
// Shift+
if handleLettersWithShiftHold(input: input) { return true }
}
// Still Nothing
// SessionCtl
//
// F1-F12
// 便
if state.hasComposition || !isComposerOrCalligrapherEmpty {
delegate.callError("Blocked data: charCode: \(input.charCode), keyCode: \(input.keyCode), text: \(input.text)")
delegate.callError("A9BFF20E")
return true
}
return false
}
}