KeyHandler // Use InputSignalProtocol.

This commit is contained in:
ShikiSuen 2022-09-01 14:08:16 +08:00
parent e6b7f552e3
commit 95abbea039
5 changed files with 26 additions and 37 deletions

View File

@ -392,10 +392,10 @@ public class KeyHandler {
/// 調 /// 調
/// - Parameter input: /// - Parameter input:
/// - Returns: 調 /// - Returns: 調
func isIntonationKey(_ input: InputSignal) -> Bool { func isIntonationKey(_ input: InputSignalProtocol) -> Bool {
var theComposer = composer // var theComposer = composer //
theComposer.clear() // theComposer.clear() //
theComposer.receiveKey(fromCharCode: input.charCode) theComposer.receiveKey(fromString: input.text)
return theComposer.hasToneMarker(withNothingElse: true) return theComposer.hasToneMarker(withNothingElse: true)
} }
@ -417,7 +417,7 @@ public class KeyHandler {
/// ///
/// - Parameter input: /// - Parameter input:
/// - Returns: /// - Returns:
func generatePunctuationNamePrefix(withKeyCondition input: InputSignal) -> String { func generatePunctuationNamePrefix(withKeyCondition input: InputSignalProtocol) -> String {
if mgrPrefs.halfWidthPunctuationEnabled { if mgrPrefs.halfWidthPunctuationEnabled {
return "_half_punctuation_" return "_half_punctuation_"
} }

View File

@ -24,12 +24,10 @@ extension KeyHandler {
/// - Returns: IMK /// - Returns: IMK
func handleCandidate( func handleCandidate(
state: InputStateProtocol, state: InputStateProtocol,
input: InputSignal, input: InputSignalProtocol,
stateCallback: @escaping (InputStateProtocol) -> Void, stateCallback: @escaping (InputStateProtocol) -> Void,
errorCallback: @escaping () -> Void errorCallback: @escaping () -> Void
) -> Bool { ) -> Bool {
let inputText = input.inputText
let charCode: UniChar = input.charCode
guard var ctlCandidateCurrent = delegate?.ctlCandidate() else { guard var ctlCandidateCurrent = delegate?.ctlCandidate() else {
IME.prtDebugIntel("06661F6E") IME.prtDebugIntel("06661F6E")
errorCallback() errorCallback()
@ -275,7 +273,7 @@ extension KeyHandler {
var index: Int = NSNotFound var index: Int = NSNotFound
let match: String = let match: String =
(state is InputState.AssociatedPhrases) ? input.inputTextIgnoringModifiers ?? "" : inputText (state is InputState.AssociatedPhrases) ? input.inputTextIgnoringModifiers ?? "" : input.text
for j in 0..<ctlCandidateCurrent.keyLabels.count { for j in 0..<ctlCandidateCurrent.keyLabels.count {
let label: CandidateKeyLabel = ctlCandidateCurrent.keyLabels[j] let label: CandidateKeyLabel = ctlCandidateCurrent.keyLabels[j]
@ -307,25 +305,23 @@ extension KeyHandler {
let punctuationNamePrefix: String = generatePunctuationNamePrefix(withKeyCondition: input) let punctuationNamePrefix: String = generatePunctuationNamePrefix(withKeyCondition: input)
let parser = currentMandarinParser let parser = currentMandarinParser
let arrCustomPunctuations: [String] = [ let arrCustomPunctuations: [String] = [
punctuationNamePrefix, parser, String(format: "%c", charCode.isPrintableASCII ? CChar(charCode) : inputText), punctuationNamePrefix, parser, input.text,
] ]
let customPunctuation: String = arrCustomPunctuations.joined(separator: "") let customPunctuation: String = arrCustomPunctuations.joined(separator: "")
/// ///
let arrPunctuations: [String] = [ let arrPunctuations: [String] = [
punctuationNamePrefix, String(format: "%c", charCode.isPrintableASCII ? CChar(charCode) : inputText), punctuationNamePrefix, input.text,
] ]
let punctuation: String = arrPunctuations.joined(separator: "") let punctuation: String = arrPunctuations.joined(separator: "")
var shouldAutoSelectCandidate: Bool = var shouldAutoSelectCandidate: Bool =
composer.inputValidityCheck(key: charCode) || currentLM.hasUnigramsFor(key: customPunctuation) composer.inputValidityCheck(key: input.charCode) || currentLM.hasUnigramsFor(key: customPunctuation)
|| currentLM.hasUnigramsFor(key: punctuation) || currentLM.hasUnigramsFor(key: punctuation)
if !shouldAutoSelectCandidate, input.isUpperCaseASCIILetterKey { if !shouldAutoSelectCandidate, input.isUpperCaseASCIILetterKey {
let letter: String! = String( let letter = "_letter_\(input.text)"
format: "%@%c", "_letter_", charCode.isPrintableASCII ? CChar(charCode) : inputText
)
if currentLM.hasUnigramsFor(key: letter) { shouldAutoSelectCandidate = true } if currentLM.hasUnigramsFor(key: letter) { shouldAutoSelectCandidate = true }
} }

View File

@ -18,7 +18,7 @@ extension KeyHandler {
/// - errorCallback: /// - errorCallback:
/// - Returns: IMK /// - Returns: IMK
func handleComposition( func handleComposition(
input: InputSignal, input: InputSignalProtocol,
stateCallback: @escaping (InputStateProtocol) -> Void, stateCallback: @escaping (InputStateProtocol) -> Void,
errorCallback: @escaping () -> Void errorCallback: @escaping () -> Void
) -> Bool? { ) -> Bool? {
@ -42,7 +42,7 @@ extension KeyHandler {
prevReading.0.charComponents.forEach { theComposer.receiveKey(fromPhonabet: $0) } prevReading.0.charComponents.forEach { theComposer.receiveKey(fromPhonabet: $0) }
// 調調 // 調調
let oldIntonation: Tekkon.Phonabet = theComposer.intonation let oldIntonation: Tekkon.Phonabet = theComposer.intonation
theComposer.receiveKey(fromCharCode: input.charCode) theComposer.receiveKey(fromString: input.text)
if theComposer.intonation == oldIntonation, mgrPrefs.specifyIntonationKeyBehavior == 1 { break proc } if theComposer.intonation == oldIntonation, mgrPrefs.specifyIntonationKeyBehavior == 1 { break proc }
theComposer.intonation.clear() theComposer.intonation.clear()
// //
@ -59,7 +59,7 @@ extension KeyHandler {
} }
} }
composer.receiveKey(fromCharCode: input.charCode) composer.receiveKey(fromString: input.text)
keyConsumedByReading = true keyConsumedByReading = true
// 調 updateClientComposingBuffer() return true // 調 updateClientComposingBuffer() return true

View File

@ -24,16 +24,15 @@ extension KeyHandler {
/// - errorCallback: /// - errorCallback:
/// - Returns: IMK /// - Returns: IMK
func handle( func handle(
input: InputSignal, input: InputSignalProtocol,
state: InputStateProtocol, state: InputStateProtocol,
stateCallback: @escaping (InputStateProtocol) -> Void, stateCallback: @escaping (InputStateProtocol) -> Void,
errorCallback: @escaping () -> Void errorCallback: @escaping () -> Void
) -> Bool { ) -> Bool {
// inputTest // inputTest
guard !input.inputText.isEmpty else { return false } guard !input.text.isEmpty else { return false }
let charCode: UniChar = input.charCode let inputText: String = input.text
let inputText: String = input.inputText
var state = state // var state = state //
// Megrez // Megrez
@ -78,7 +77,7 @@ extension KeyHandler {
/// ASCII 使insertText:replacementRange: /// ASCII 使insertText:replacementRange:
/// ASCII /// ASCII
if charCode < 0x80, !charCode.isPrintableASCII { if input.isASCII, !input.charCode.isPrintableASCII {
return false return false
} }
@ -309,7 +308,7 @@ extension KeyHandler {
if input.isMainAreaNumKey, input.isShiftHold, input.isOptionHold, !input.isControlHold, !input.isCommandHold { if input.isMainAreaNumKey, input.isShiftHold, input.isOptionHold, !input.isControlHold, !input.isCommandHold {
// NOTE: macOS 10.11 El Capitan CFStringTransform StringTransform: // NOTE: macOS 10.11 El Capitan CFStringTransform StringTransform:
// https://developer.apple.com/documentation/foundation/stringtransform // https://developer.apple.com/documentation/foundation/stringtransform
guard let stringRAW = input.mapMainAreaNumKey[input.keyCode] else { return false } guard let stringRAW = input.mainAreaNumKeyChar else { return false }
let string = NSMutableString(string: stringRAW) let string = NSMutableString(string: stringRAW)
CFStringTransform(string, nil, kCFStringTransformFullwidthHalfwidth, true) CFStringTransform(string, nil, kCFStringTransformFullwidthHalfwidth, true)
stateCallback( stateCallback(
@ -328,9 +327,7 @@ extension KeyHandler {
let punctuationNamePrefix: String = generatePunctuationNamePrefix(withKeyCondition: input) let punctuationNamePrefix: String = generatePunctuationNamePrefix(withKeyCondition: input)
let parser = currentMandarinParser let parser = currentMandarinParser
let arrCustomPunctuations: [String] = [ let arrCustomPunctuations: [String] = [punctuationNamePrefix, parser, input.text]
punctuationNamePrefix, parser, String(format: "%c", charCode.isPrintableASCII ? CChar(charCode) : inputText),
]
let customPunctuation: String = arrCustomPunctuations.joined(separator: "") let customPunctuation: String = arrCustomPunctuations.joined(separator: "")
if handlePunctuation( if handlePunctuation(
customPunctuation, customPunctuation,
@ -344,9 +341,7 @@ extension KeyHandler {
/// ///
let arrPunctuations: [String] = [ let arrPunctuations: [String] = [punctuationNamePrefix, input.text]
punctuationNamePrefix, String(format: "%c", charCode.isPrintableASCII ? CChar(charCode) : inputText),
]
let punctuation: String = arrPunctuations.joined(separator: "") let punctuation: String = arrPunctuations.joined(separator: "")
if handlePunctuation( if handlePunctuation(
@ -386,9 +381,7 @@ extension KeyHandler {
stateCallback(InputState.Empty()) stateCallback(InputState.Empty())
return true return true
default: // case 0 default: // case 0
let letter: String! = String( let letter = "_letter_\(inputText)"
format: "%@%c", "_letter_", charCode.isPrintableASCII ? CChar(charCode) : inputText
)
if handlePunctuation( if handlePunctuation(
letter, letter,
state: state, state: state,
@ -410,7 +403,7 @@ extension KeyHandler {
/// 便 /// 便
if (state is InputState.NotEmpty) || !composer.isEmpty { if (state is InputState.NotEmpty) || !composer.isEmpty {
IME.prtDebugIntel( IME.prtDebugIntel(
"Blocked data: charCode: \(charCode), keyCode: \(input.keyCode)") "Blocked data: charCode: \(input.charCode), keyCode: \(input.keyCode)")
IME.prtDebugIntel("A9BFF20E") IME.prtDebugIntel("A9BFF20E")
errorCallback() errorCallback()
stateCallback(state) stateCallback(state)

View File

@ -170,7 +170,7 @@ extension KeyHandler {
/// - Returns: ctlInputMethod IMK /// - Returns: ctlInputMethod IMK
func handleMarkingState( func handleMarkingState(
_ state: InputState.Marking, _ state: InputState.Marking,
input: InputSignal, input: InputSignalProtocol,
stateCallback: @escaping (InputStateProtocol) -> Void, stateCallback: @escaping (InputStateProtocol) -> Void,
errorCallback: @escaping () -> Void errorCallback: @escaping () -> Void
) -> Bool { ) -> Bool {
@ -416,7 +416,7 @@ extension KeyHandler {
/// - Returns: ctlInputMethod IMK /// - Returns: ctlInputMethod IMK
func handleBackSpace( func handleBackSpace(
state: InputStateProtocol, state: InputStateProtocol,
input: InputSignal, input: InputSignalProtocol,
stateCallback: @escaping (InputStateProtocol) -> Void, stateCallback: @escaping (InputStateProtocol) -> Void,
errorCallback: @escaping () -> Void errorCallback: @escaping () -> Void
) -> Bool { ) -> Bool {
@ -482,7 +482,7 @@ extension KeyHandler {
/// - Returns: ctlInputMethod IMK /// - Returns: ctlInputMethod IMK
func handleDelete( func handleDelete(
state: InputStateProtocol, state: InputStateProtocol,
input: InputSignal, input: InputSignalProtocol,
stateCallback: @escaping (InputStateProtocol) -> Void, stateCallback: @escaping (InputStateProtocol) -> Void,
errorCallback: @escaping () -> Void errorCallback: @escaping () -> Void
) -> Bool { ) -> Bool {
@ -652,7 +652,7 @@ extension KeyHandler {
/// - Returns: ctlInputMethod IMK /// - Returns: ctlInputMethod IMK
func handleForward( func handleForward(
state: InputStateProtocol, state: InputStateProtocol,
input: InputSignal, input: InputSignalProtocol,
stateCallback: @escaping (InputStateProtocol) -> Void, stateCallback: @escaping (InputStateProtocol) -> Void,
errorCallback: @escaping () -> Void errorCallback: @escaping () -> Void
) -> Bool { ) -> Bool {
@ -726,7 +726,7 @@ extension KeyHandler {
/// - Returns: ctlInputMethod IMK /// - Returns: ctlInputMethod IMK
func handleBackward( func handleBackward(
state: InputStateProtocol, state: InputStateProtocol,
input: InputSignal, input: InputSignalProtocol,
stateCallback: @escaping (InputStateProtocol) -> Void, stateCallback: @escaping (InputStateProtocol) -> Void,
errorCallback: @escaping () -> Void errorCallback: @escaping () -> Void
) -> Bool { ) -> Bool {