InputHandler // Further simplification, etc.

- Removing certain useless cases of delegate.switchState(IMEState.ofEmpty()) right after ofCommitting().
- Divide handleComposition() for future purposes.
This commit is contained in:
ShikiSuen 2022-10-14 15:15:09 +08:00
parent 4860b52306
commit 19b0b138f2
5 changed files with 34 additions and 83 deletions

View File

@ -320,7 +320,7 @@ public class InputHandler: InputHandlerProtocol {
key: newestSuggestedCandidate.0, value: newestSuggestedCandidate.1.value key: newestSuggestedCandidate.0, value: newestSuggestedCandidate.1.value
) )
vCLog( vCLog(
"UOM: Suggestion retrieved, overriding the node score of the selected candidate: \(suggestedPair.toNGramKey)") "UOM: Suggestion received, overriding the node score of the selected candidate: \(suggestedPair.toNGramKey)")
if !compositor.overrideCandidate(suggestedPair, at: cursorForCandidate, overrideType: overrideBehavior) { if !compositor.overrideCandidate(suggestedPair, at: cursorForCandidate, overrideType: overrideBehavior) {
compositor.overrideCandidateLiteral( compositor.overrideCandidateLiteral(
newestSuggestedCandidate.1.value, at: cursorForCandidate, overrideType: overrideBehavior newestSuggestedCandidate.1.value, at: cursorForCandidate, overrideType: overrideBehavior
@ -336,13 +336,8 @@ public class InputHandler: InputHandlerProtocol {
// MARK: - Extracted methods and functions (Tekkon). // MARK: - Extracted methods and functions (Tekkon).
/// _ /// _
var currentKeyboardParser: String { var currentKeyboardParser: String { currentKeyboardParserType.name + "_" }
currentKeyboardParserType.name + "_" var currentKeyboardParserType: KeyboardParser { .init(rawValue: prefs.keyboardParser) ?? .ofStandard }
}
var currentKeyboardParserType: KeyboardParser {
.init(rawValue: prefs.keyboardParser) ?? .ofStandard
}
/// ///
public func ensureKeyboardParser() { public func ensureKeyboardParser() {
@ -400,9 +395,7 @@ public class InputHandler: InputHandlerProtocol {
/// - Parameter input: /// - Parameter input:
/// - Returns: /// - Returns:
func generatePunctuationNamePrefix(withKeyCondition input: InputSignalProtocol) -> String { func generatePunctuationNamePrefix(withKeyCondition input: InputSignalProtocol) -> String {
if prefs.halfWidthPunctuationEnabled { if prefs.halfWidthPunctuationEnabled { return "_half_punctuation_" }
return "_half_punctuation_"
}
switch (input.isControlHold, input.isOptionHold) { switch (input.isControlHold, input.isOptionHold) {
case (true, true): return "_alt_ctrl_punctuation_" case (true, true): return "_alt_ctrl_punctuation_"
case (true, false): return "_ctrl_punctuation_" case (true, false): return "_ctrl_punctuation_"
@ -430,9 +423,7 @@ extension InputHandler {
compositor.width > compositorWidthLimit, compositor.width > compositorWidthLimit,
let identifier = delegate?.clientBundleIdentifier, let identifier = delegate?.clientBundleIdentifier,
prefs.clientsIMKTextInputIncapable.contains(identifier) prefs.clientsIMKTextInputIncapable.contains(identifier)
else { else { return "" }
return ""
}
// Steam Client Identifier // Steam Client Identifier
var textToCommit = "" var textToCommit = ""
while compositor.width > compositorWidthLimit { while compositor.width > compositorWidthLimit {
@ -447,13 +438,9 @@ extension InputHandler {
} }
let newCursor = max(compositor.cursor - delta, 0) let newCursor = max(compositor.cursor - delta, 0)
compositor.cursor = 0 compositor.cursor = 0
if !node.isReadingMismatched { if !node.isReadingMismatched { consolidateCursorContext(with: node.currentPair) }
consolidateCursorContext(with: node.currentPair)
}
// Bigram // Bigram
for _ in 0..<delta { for _ in 0..<delta { compositor.dropKey(direction: .front) }
compositor.dropKey(direction: .front)
}
compositor.cursor = newCursor compositor.cursor = newCursor
walk() walk()
} }

View File

@ -193,9 +193,7 @@ extension InputHandler {
if input.isSymbolMenuPhysicalKey { if input.isSymbolMenuPhysicalKey {
var updated = true var updated = true
updated = input.isShiftHold ? ctlCandidate.showPreviousLine() : ctlCandidate.showNextLine() updated = input.isShiftHold ? ctlCandidate.showPreviousLine() : ctlCandidate.showNextLine()
if !updated { if !updated { delegate.callError("66F3477B") }
delegate.callError("66F3477B")
}
return true return true
} }

View File

@ -17,6 +17,14 @@ extension InputHandler {
/// - input: /// - input:
/// - Returns: IMK /// - Returns: IMK
func handleComposition(input: InputSignalProtocol) -> Bool? { func handleComposition(input: InputSignalProtocol) -> Bool? {
handlePhonabetComposition(input: input)
}
/// InputHandler.HandleInput()
/// - Parameters:
/// - input:
/// - Returns: IMK
private func handlePhonabetComposition(input: InputSignalProtocol) -> Bool? {
guard let delegate = delegate else { return nil } guard let delegate = delegate else { return nil }
// MARK: (Handle BPMF Keys) // MARK: (Handle BPMF Keys)

View File

@ -68,7 +68,6 @@ extension InputHandler {
// //
delegate.switchState(IMEState.ofCommitting(textToCommit: inputText.lowercased())) delegate.switchState(IMEState.ofCommitting(textToCommit: inputText.lowercased()))
delegate.switchState(IMEState.ofEmpty())
return true return true
} }
@ -79,21 +78,15 @@ extension InputHandler {
// 使 Cocoa flags // 使 Cocoa flags
// //
if input.isNumericPadKey { if input.isNumericPadKey {
if !(state.type == .ofCandidates || state.type == .ofAssociates if ![.ofCandidates, .ofAssociates, .ofSymbolTable].contains(state.type) {
|| state.type == .ofSymbolTable)
{
delegate.switchState(IMEState.ofEmpty())
delegate.switchState(IMEState.ofCommitting(textToCommit: inputText.lowercased())) delegate.switchState(IMEState.ofCommitting(textToCommit: inputText.lowercased()))
delegate.switchState(IMEState.ofEmpty())
return true return true
} }
} }
// MARK: (Handle Candidates) // MARK: (Handle Candidates)
if [.ofCandidates, .ofSymbolTable].contains(state.type) { if [.ofCandidates, .ofSymbolTable].contains(state.type) { return handleCandidate(input: input) }
return handleCandidate(input: input)
}
// MARK: (Handle Associated Phrases) // MARK: (Handle Associated Phrases)
@ -132,7 +125,6 @@ extension InputHandler {
delegate.switchState(IMEState.ofCommitting(textToCommit: displayedText)) delegate.switchState(IMEState.ofCommitting(textToCommit: displayedText))
} }
delegate.switchState(IMEState.ofCommitting(textToCommit: " ")) delegate.switchState(IMEState.ofCommitting(textToCommit: " "))
delegate.switchState(IMEState.ofEmpty())
} else if currentLM.hasUnigramsFor(key: " ") { } else if currentLM.hasUnigramsFor(key: " ") {
compositor.insertKey(" ") compositor.insertKey(" ")
walk() walk()
@ -147,12 +139,9 @@ extension InputHandler {
return handleInlineCandidateRotation(reverseOrder: input.isCommandHold) return handleInlineCandidateRotation(reverseOrder: input.isCommandHold)
} }
} }
//
let candidateState: IMEStateProtocol = generateStateOfCandidates() let candidateState: IMEStateProtocol = generateStateOfCandidates()
if candidateState.candidates.isEmpty { _ = candidateState.candidates.isEmpty ? delegate.callError("3572F238") : delegate.switchState(candidateState)
delegate.callError("3572F238")
} else {
delegate.switchState(candidateState)
}
return true return true
} }
@ -199,12 +188,9 @@ extension InputHandler {
var inputting = generateStateOfInputting() var inputting = generateStateOfInputting()
inputting.textToCommit = textToCommit inputting.textToCommit = textToCommit
delegate.switchState(inputting) delegate.switchState(inputting)
let candidateState = generateStateOfCandidates() //
if candidateState.candidates.isEmpty { let newState = generateStateOfCandidates()
delegate.callError("B5127D8A") _ = newState.candidates.isEmpty ? delegate.callError("B5127D8A") : delegate.switchState(newState)
} else {
delegate.switchState(candidateState)
}
} else { // } else { //
delegate.callError("17446655") delegate.callError("17446655")
} }
@ -212,9 +198,7 @@ extension InputHandler {
} }
} else { } else {
// commit buffer ESC // commit buffer ESC
// Enter 使 commit buffer delegate.switchState(IMEState.ofCommitting(textToCommit: state.displayedText))
// bool _ =
_ = handleEnter()
delegate.switchState(IMEState.ofSymbolTable(node: CandidateNode.root)) delegate.switchState(IMEState.ofSymbolTable(node: CandidateNode.root))
return true return true
} }
@ -224,13 +208,10 @@ extension InputHandler {
if state.type == .ofEmpty { if state.type == .ofEmpty {
if input.isMainAreaNumKey, input.modifierFlags == [.shift, .option] { if input.isMainAreaNumKey, input.modifierFlags == [.shift, .option] {
guard let stringRAW = input.mainAreaNumKeyChar else { return false } guard let strRAW = input.mainAreaNumKeyChar else { return false }
let newStringFW = stringRAW.applyingTransform(.fullwidthToHalfwidth, reverse: true) ?? stringRAW let newString =
let newStringHW = stringRAW.applyingTransform(.fullwidthToHalfwidth, reverse: false) ?? stringRAW strRAW.applyingTransform(.fullwidthToHalfwidth, reverse: !prefs.halfWidthPunctuationEnabled) ?? strRAW
delegate.switchState( delegate.switchState(IMEState.ofCommitting(textToCommit: newString))
IMEState.ofCommitting(textToCommit: prefs.halfWidthPunctuationEnabled ? newStringHW : newStringFW)
)
delegate.switchState(IMEState.ofEmpty())
return true return true
} }
} }
@ -240,23 +221,15 @@ extension InputHandler {
/// ///
/// - / /// - /
/// - /// -
let punctuationNamePrefix: String = generatePunctuationNamePrefix(withKeyCondition: input) let punctuationNamePrefix: String = generatePunctuationNamePrefix(withKeyCondition: input)
let parser = currentKeyboardParser let parser = currentKeyboardParser
let arrCustomPunctuations: [String] = [punctuationNamePrefix, parser, input.text] let arrCustomPunctuations: [String] = [punctuationNamePrefix, parser, input.text]
let customPunctuation: String = arrCustomPunctuations.joined() let customPunctuation: String = arrCustomPunctuations.joined()
if handlePunctuation(customPunctuation) { if handlePunctuation(customPunctuation) { return true }
return true
}
/// ///
let arrPunctuations: [String] = [punctuationNamePrefix, input.text] let arrPunctuations: [String] = [punctuationNamePrefix, input.text]
let punctuation: String = arrPunctuations.joined() let punctuation: String = arrPunctuations.joined()
if handlePunctuation(punctuation) { return true }
if handlePunctuation(punctuation) {
return true
}
// MARK: / (Full-Width / Half-Width Space) // MARK: / (Full-Width / Half-Width Space)
@ -264,7 +237,6 @@ extension InputHandler {
if state.type == .ofEmpty { if state.type == .ofEmpty {
if input.isSpace, !input.isOptionHold, !input.isControlHold, !input.isCommandHold { if input.isSpace, !input.isOptionHold, !input.isControlHold, !input.isCommandHold {
delegate.switchState(IMEState.ofCommitting(textToCommit: input.isShiftHold ? " " : " ")) delegate.switchState(IMEState.ofCommitting(textToCommit: input.isShiftHold ? " " : " "))
delegate.switchState(IMEState.ofEmpty())
return true return true
} }
} }
@ -275,15 +247,10 @@ extension InputHandler {
if input.isShiftHold { // isOptionHold if input.isShiftHold { // isOptionHold
switch prefs.upperCaseLetterKeyBehavior { switch prefs.upperCaseLetterKeyBehavior {
case 1: case 1:
delegate.switchState(IMEState.ofEmpty())
delegate.switchState(IMEState.ofCommitting(textToCommit: inputText.lowercased())) delegate.switchState(IMEState.ofCommitting(textToCommit: inputText.lowercased()))
delegate.switchState(IMEState.ofEmpty())
return true return true
case 2: case 2:
delegate.switchState(IMEState.ofEmpty())
delegate.switchState(IMEState.ofCommitting(textToCommit: inputText.uppercased())) delegate.switchState(IMEState.ofCommitting(textToCommit: inputText.uppercased()))
delegate.switchState(IMEState.ofEmpty())
return true return true
default: // case 0 default: // case 0
let letter = "_letter_\(inputText)" let letter = "_letter_\(inputText)"
@ -301,8 +268,7 @@ extension InputHandler {
/// F1-F12 /// F1-F12
/// 便 /// 便
if state.hasComposition || !composer.isEmpty { if state.hasComposition || !composer.isEmpty {
delegate.callError( delegate.callError("Blocked data: charCode: \(input.charCode), keyCode: \(input.keyCode)")
"Blocked data: charCode: \(input.charCode), keyCode: \(input.keyCode)")
delegate.callError("A9BFF20E") delegate.callError("A9BFF20E")
delegate.switchState(state) delegate.switchState(state)
return true return true

View File

@ -252,7 +252,6 @@ extension InputHandler {
clear() // candidateState clear() // candidateState
if let candidateToCommit: (String, String) = candidateState.candidates.first, !candidateToCommit.1.isEmpty { if let candidateToCommit: (String, String) = candidateState.candidates.first, !candidateToCommit.1.isEmpty {
delegate.switchState(IMEState.ofCommitting(textToCommit: candidateToCommit.1)) delegate.switchState(IMEState.ofCommitting(textToCommit: candidateToCommit.1))
delegate.switchState(IMEState.ofEmpty())
} else { } else {
delegate.switchState(candidateState) delegate.switchState(candidateState)
} }
@ -265,13 +264,12 @@ extension InputHandler {
/// Enter /// Enter
/// - Returns: SessionCtl IMK /// - Returns: SessionCtl IMK
func handleEnter() -> Bool { @discardableResult func handleEnter() -> Bool {
guard let delegate = delegate else { return false } guard let delegate = delegate else { return false }
let state = delegate.state let state = delegate.state
guard state.type == .ofInputting else { return false } guard state.type == .ofInputting else { return false }
delegate.switchState(IMEState.ofCommitting(textToCommit: state.displayedText)) delegate.switchState(IMEState.ofCommitting(textToCommit: state.displayedText))
delegate.switchState(IMEState.ofEmpty())
return true return true
} }
@ -295,7 +293,6 @@ extension InputHandler {
} }
delegate.switchState(IMEState.ofCommitting(textToCommit: displayedText)) delegate.switchState(IMEState.ofCommitting(textToCommit: displayedText))
delegate.switchState(IMEState.ofEmpty())
return true return true
} }
@ -327,7 +324,6 @@ extension InputHandler {
} }
delegate.switchState(IMEState.ofCommitting(textToCommit: composed)) delegate.switchState(IMEState.ofCommitting(textToCommit: composed))
delegate.switchState(IMEState.ofEmpty())
return true return true
} }
@ -432,9 +428,7 @@ extension InputHandler {
guard let delegate = delegate else { return false } guard let delegate = delegate else { return false }
let state = delegate.state let state = delegate.state
guard state.type == .ofInputting else { return false } guard state.type == .ofInputting else { return false }
if !composer.isEmpty { if !composer.isEmpty { delegate.callError("9B6F908D") }
delegate.callError("9B6F908D")
}
delegate.switchState(state) delegate.switchState(state)
return true return true
} }
@ -616,9 +610,7 @@ extension InputHandler {
delegate.switchState(state) delegate.switchState(state)
} }
} else if input.isOptionHold { } else if input.isOptionHold {
if input.isControlHold { if input.isControlHold { return handleHome() }
return handleHome()
}
// //
if !compositor.jumpCursorBySpan(to: .rear) { if !compositor.jumpCursorBySpan(to: .rear) {
delegate.callError("8D50DD9E") delegate.callError("8D50DD9E")