KeyHandler // Use InputSignalProtocol.
This commit is contained in:
parent
e6b7f552e3
commit
95abbea039
|
@ -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_"
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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)即可。
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
Loading…
Reference in New Issue