vChewing-macOS/Source/Modules/ControllerModules/KeyHandler_HandleInput.swift

432 lines
17 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).
// 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.
/// 調 IMK 調
/// 調
import Foundation
// MARK: - § 調 (Handle Input with States)
extension KeyHandler {
///
/// - Parameters:
/// - input:
/// - state:
/// - stateCallback:
/// - errorCallback:
/// - Returns: IMK
func handle(
input: InputSignalProtocol,
state: IMEStateProtocol,
stateCallback: @escaping (IMEStateProtocol) -> Void,
errorCallback: @escaping () -> 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
}
IME.prtDebugIntel("550BCF7B: KeyHandler just refused an invalid input.")
errorCallback()
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
/// Shift Chromium
/// IMK Shift 使
/// Caps Lock
if input.isBackSpace || input.isEnter
|| input.isCursorClockLeft || input.isCursorClockRight
|| input.isCursorForward || input.isCursorBackward
{
// BackSpace
} else if input.isCapsLockOn || input.isASCIIModeInput {
//
stateCallback(IMEState.ofEmpty())
// Shift
if (input.isUpperCaseASCIILetterKey && input.isASCIIModeInput)
|| (input.isCapsLockOn && input.isShiftHold)
{
return false
}
/// ASCII 使insertText:replacementRange:
/// ASCII
if input.isASCII, !input.charCode.isPrintableASCII {
return false
}
// macOS bug
var charToCommit = inputText.lowercased()
if "".contains(charToCommit), input.isSymbolMenuPhysicalKey,
AppleKeyboardConverter.isDynamicBasicKeyboardLayoutEnabled
{
charToCommit = "`"
}
//
stateCallback(IMEState.ofCommitting(textToCommit: charToCommit))
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 && mgrPrefs.specifyShiftTabKeyBehavior)
{
if input.isSpace {
/// Space
if !mgrPrefs.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 = buildInputtingState
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
)
}
}
stateCallback(buildCandidate(state: state))
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 = buildInputtingState
inputting.textToCommit = textToCommit
stateCallback(inputting)
stateCallback(buildCandidate(state: inputting))
} else { //
IME.prtDebugIntel("17446655")
errorCallback()
}
return true
}
} else {
// commit buffer ESC
// Enter 使 commit buffer
// bool _ =
_ = handleEnter(state: state, stateCallback: stateCallback)
stateCallback(IMEState.ofSymbolTable(node: SymbolNode.root))
return true
}
}
// MARK: / (FW / HW Arabic Numbers Input)
if state.type == .ofEmpty {
if input.isMainAreaNumKey, input.isShiftHold, input.isOptionHold, !input.isControlHold, !input.isCommandHold {
// NOTE: macOS 10.11 El Capitan CFStringTransform StringTransform:
// https://developer.apple.com/documentation/foundation/stringtransform
guard let stringRAW = input.mainAreaNumKeyChar else { return false }
let string = NSMutableString(string: stringRAW)
CFStringTransform(string, nil, kCFStringTransformFullwidthHalfwidth, true)
stateCallback(
IMEState.ofCommitting(textToCommit: mgrPrefs.halfWidthPunctuationEnabled ? stringRAW : string as String)
)
stateCallback(IMEState.ofEmpty())
return true
}
}
// MARK: Punctuation
///
/// - /
/// -
let punctuationNamePrefix: String = generatePunctuationNamePrefix(withKeyCondition: input)
let parser = currentMandarinParser
let arrCustomPunctuations: [String] = [punctuationNamePrefix, parser, input.text]
let customPunctuation: String = arrCustomPunctuations.joined(separator: "")
if handlePunctuation(
customPunctuation,
state: state,
usingVerticalTyping: input.isTypingVertical,
stateCallback: stateCallback,
errorCallback: errorCallback
) {
return true
}
///
let arrPunctuations: [String] = [punctuationNamePrefix, input.text]
let punctuation: String = arrPunctuations.joined(separator: "")
if handlePunctuation(
punctuation,
state: state,
usingVerticalTyping: input.isTypingVertical,
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 mgrPrefs.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,
usingVerticalTyping: input.isTypingVertical,
stateCallback: stateCallback,
errorCallback: errorCallback
) {
return true
}
}
}
}
// MARK: - (Still Nothing)
/// ctlInputMethod
///
/// F1-F12
/// 便
if state.hasComposition || !composer.isEmpty {
IME.prtDebugIntel(
"Blocked data: charCode: \(input.charCode), keyCode: \(input.keyCode)")
IME.prtDebugIntel("A9BFF20E")
errorCallback()
stateCallback(state)
return true
}
return false
}
}