vChewing-macOS/Source/Modules/ControllerModules/KeyHandler_HandleCompositio...

165 lines
7.9 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).
// 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.
/// KeyHandler.HandleInput()
extension KeyHandler {
/// KeyHandler.HandleInput()
/// - Parameters:
/// - input:
/// - stateCallback:
/// - errorCallback:
/// - Returns: IMK
func handleComposition(
input: InputSignalProtocol,
stateCallback: @escaping (IMEStateProtocol) -> Void,
errorCallback: @escaping () -> Void
) -> Bool? {
// MARK: (Handle BPMF Keys)
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(mgrPrefs.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, mgrPrefs.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
// buildInputtingState調 buildInputtingState
} else {
IME.prtDebugIntel("4B0DD2D4語彙庫內無「\(temporaryReadingKey)」的匹配記錄,放棄覆寫游標身後的內容。")
errorCallback()
return true
}
}
composer.receiveKey(fromString: input.text)
keyConsumedByReading = true
// 調 updateClientdisplayedText() return true
// 調
if !composer.hasToneMarker() {
stateCallback(buildInputtingState)
return true
}
}
var composeReading = composer.hasToneMarker() && composer.inputValidityCheck(key: input.charCode) //
// Enter Space _composer
// |=
composeReading = composeReading || (!composer.isEmpty && (input.isSpace || input.isEnter))
if composeReading {
if input.isSpace, !composer.hasToneMarker() {
// 調
// 使 OVMandarin調
composer.receiveKey(fromString: " ")
}
let readingKey = composer.getComposition() //
//
//
if !currentLM.hasUnigramsFor(key: readingKey) {
IME.prtDebugIntel("B49C0979語彙庫內無「\(readingKey)」的匹配記錄。")
errorCallback()
if mgrPrefs.keepReadingUponCompositionError {
composer.intonation.clear() // 調
stateCallback(buildInputtingState)
return true
}
composer.clear()
//
switch compositor.isEmpty {
case false: stateCallback(buildInputtingState)
case true:
stateCallback(IMEState.ofAbortion())
}
return true // IMK
}
//
compositor.insertKey(readingKey)
//
walk()
// App
let textToCommit = commitOverflownComposition
//
fetchSuggestionsFromUOM(apply: true)
//
composer.clear()
// updateClientdisplayedText()
var inputting = buildInputtingState
inputting.textToCommit = textToCommit
stateCallback(inputting)
///
if mgrPrefs.useSCPCTypingMode {
let candidateState: IMEState = buildCandidate(
state: inputting,
isTypingVertical: input.isTypingVertical
)
if candidateState.candidates.count == 1, let firstCandidate = candidateState.candidates.first {
let reading: String = firstCandidate.0
let text: String = firstCandidate.1
stateCallback(IMEState.ofCommitting(textToCommit: text))
if !mgrPrefs.associatedPhrasesEnabled {
stateCallback(IMEState.ofEmpty())
} else {
let associatedPhrases =
buildAssociatePhraseState(
withPair: .init(key: reading, value: text)
)
stateCallback(associatedPhrases.candidates.isEmpty ? IMEState.ofEmpty() : associatedPhrases)
}
} else {
stateCallback(candidateState)
}
}
// ctlInputMethod IMK
return true
}
/// 調
if keyConsumedByReading {
// updateClientdisplayedText()
stateCallback(buildInputtingState)
return true
}
return nil
}
}