vChewing-macOS/Source/Modules/InputHandler_HandleComposit...

278 lines
13 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.
/// InputHandler.HandleInput()
import Shared
import Tekkon
extension InputHandler {
/// InputHandler.HandleInput()
/// - Parameters:
/// - input:
/// - Returns: IMK
func handleComposition(input: InputSignalProtocol) -> Bool? {
prefs.cassetteEnabled ? handleCassetteComposition(input: input) : handlePhonabetComposition(input: input)
}
// MARK: (Handle BPMF Keys)
/// InputHandler.HandleInput()
/// - Parameters:
/// - input:
/// - Returns: IMK
private func handlePhonabetComposition(input: InputSignalProtocol) -> Bool? {
guard let delegate = delegate else { return nil }
var keyConsumedByReading = false
let skipPhoneticHandling =
input.isReservedKey || input.isNumericPadKey || input.isNonLaptopFunctionKey
|| input.isControlHold || input.isOptionHold || input.isShiftHold || input.isCommandHold
// inputValidityCheck() charCode UniChar
// keyConsumedByReading
// composer.receiveKey() String UniChar
if !skipPhoneticHandling && composer.inputValidityCheck(key: input.charCode) {
// macOS 調
//
proc: if [0, 1].contains(prefs.specifyIntonationKeyBehavior), composer.isEmpty, !input.isSpace {
// prevReading 調調
guard let prevReading = previousParsableReading, isIntonationKey(input) else { break proc }
var theComposer = composer
prevReading.0.charComponents.forEach { theComposer.receiveKey(fromPhonabet: $0) }
// 調調
let oldIntonation: Tekkon.Phonabet = theComposer.intonation
theComposer.receiveKey(fromString: input.text)
if theComposer.intonation == oldIntonation, prefs.specifyIntonationKeyBehavior == 1 { break proc }
theComposer.intonation.clear()
//
let temporaryReadingKey = theComposer.getComposition()
if currentLM.hasUnigramsFor(key: theComposer.getComposition()) {
compositor.dropKey(direction: .rear)
walk() // Walk walk
composer = theComposer
// generateStateOfInputting()調 generateStateOfInputting()
} else {
delegate.callError("4B0DD2D4語彙庫內無「\(temporaryReadingKey)」的匹配記錄,放棄覆寫游標身後的內容。")
return true
}
}
composer.receiveKey(fromString: input.text)
keyConsumedByReading = true
// 調 setInlineDisplayWithCursor() return true
// 調
if !composer.hasIntonation() {
delegate.switchState(generateStateOfInputting())
return true
}
}
var composeReading = composer.hasIntonation() && composer.inputValidityCheck(key: input.charCode) //
// Enter Space composer
// |=
composeReading = composeReading || (!composer.isEmpty && (input.isSpace || input.isEnter))
if composeReading {
if input.isSpace, !composer.hasIntonation() {
// 調
// 使 OVMandarin調
composer.receiveKey(fromString: " ")
}
let readingKey = composer.getComposition() //
//
//
if !currentLM.hasUnigramsFor(key: readingKey) {
delegate.callError("B49C0979語彙庫內無「\(readingKey)」的匹配記錄。")
if prefs.keepReadingUponCompositionError {
composer.intonation.clear() // 調
delegate.switchState(generateStateOfInputting())
return true
}
composer.clear()
//
switch compositor.isEmpty {
case false: delegate.switchState(generateStateOfInputting())
case true: delegate.switchState(IMEState.ofAbortion())
}
return true // IMK
}
//
compositor.insertKey(readingKey)
//
walk()
// App
let textToCommit = commitOverflownComposition
//
retrieveUOMSuggestions(apply: true)
//
composer.clear()
// setInlineDisplayWithCursor()
var inputting = generateStateOfInputting()
inputting.textToCommit = textToCommit
delegate.switchState(inputting)
///
if prefs.useSCPCTypingMode {
let candidateState: IMEStateProtocol = generateStateOfCandidates()
switch candidateState.candidates.count {
case 2...: delegate.switchState(candidateState)
case 1:
let firstCandidate = candidateState.candidates.first! //
let reading: String = firstCandidate.0
let text: String = firstCandidate.1
delegate.switchState(IMEState.ofCommitting(textToCommit: text))
if !prefs.associatedPhrasesEnabled {
delegate.switchState(IMEState.ofEmpty())
} else {
let associatedPhrases = generateStateOfAssociates(withPair: .init(key: reading, value: text))
delegate.switchState(associatedPhrases.candidates.isEmpty ? IMEState.ofEmpty() : associatedPhrases)
}
default: break
}
}
// SessionCtl IMK
return true
}
/// 調
if keyConsumedByReading {
// setInlineDisplayWithCursor()
delegate.switchState(generateStateOfInputting())
return true
}
return nil
}
}
// MARK: -
extension InputHandler {
/// InputHandler.HandleInput()
/// - Parameters:
/// - input:
/// - Returns: IMK
private func handleCassetteComposition(input: InputSignalProtocol) -> Bool? {
guard let delegate = delegate else { return nil }
var wildcardKey: String { currentLM.currentCassette.wildcardKey } //
var keyConsumedByStrokes = false
let skipStrokeHandling =
input.isReservedKey || input.isNumericPadKey || input.isNonLaptopFunctionKey
|| input.isControlHold || input.isOptionHold || input.isShiftHold || input.isCommandHold
var isStrokesFull: Bool { calligrapher.count >= currentLM.currentCassette.maxKeyLength }
prehandling: if !skipStrokeHandling && currentLM.currentCassette.allowedKeys.contains(input.text) {
if calligrapher.isEmpty, input.text == wildcardKey {
delegate.callError("3606B9C0")
var newEmptyState = IMEState.ofEmpty()
newEmptyState.tooltip = NSLocalizedString("Wildcard key cannot be the initial key.", comment: "") + "  "
newEmptyState.data.tooltipColorState = .redAlert
delegate.switchState(newEmptyState)
return true
}
if isStrokesFull {
calligrapher = String(calligrapher.dropLast(1))
}
calligrapher.append(input.text)
if input.text == wildcardKey {
break prehandling
}
keyConsumedByStrokes = true
if !isStrokesFull {
delegate.switchState(generateStateOfInputting())
return true
}
}
var compoundStrokes = isStrokesFull || (input.text == wildcardKey && !calligrapher.isEmpty)
// Enter Space calligrapher
// |=
compoundStrokes = compoundStrokes || (!calligrapher.isEmpty && (input.isSpace || input.isEnter))
if compoundStrokes {
//
if !currentLM.hasUnigramsFor(key: calligrapher) {
delegate.callError("B49C0979_Cassette語彙庫內無「\(calligrapher)」的匹配記錄。")
calligrapher.removeAll()
//
switch compositor.isEmpty {
case false: delegate.switchState(generateStateOfInputting())
case true: delegate.switchState(IMEState.ofAbortion())
}
return true // IMK
}
//
compositor.insertKey(calligrapher)
//
walk()
// App
let textToCommit = commitOverflownComposition
//
retrieveUOMSuggestions(apply: true)
//
calligrapher.removeAll()
// setInlineDisplayWithCursor()
var inputting = generateStateOfInputting()
inputting.textToCommit = textToCommit
delegate.switchState(inputting)
///
if prefs.useSCPCTypingMode {
let candidateState: IMEStateProtocol = generateStateOfCandidates()
switch candidateState.candidates.count {
case 2...: delegate.switchState(candidateState)
case 1:
let firstCandidate = candidateState.candidates.first! //
let reading: String = firstCandidate.0
let text: String = firstCandidate.1
delegate.switchState(IMEState.ofCommitting(textToCommit: text))
if !prefs.associatedPhrasesEnabled {
delegate.switchState(IMEState.ofEmpty())
} else {
let associatedPhrases = generateStateOfAssociates(withPair: .init(key: reading, value: text))
delegate.switchState(associatedPhrases.candidates.isEmpty ? IMEState.ofEmpty() : associatedPhrases)
}
default: break
}
}
// SessionCtl IMK
return true
}
/// 調
if keyConsumedByStrokes {
// setInlineDisplayWithCursor()
delegate.switchState(generateStateOfInputting())
return true
}
return nil
}
}