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

View File

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

View File

@ -17,6 +17,14 @@ extension InputHandler {
/// - input:
/// - Returns: IMK
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 }
// MARK: (Handle BPMF Keys)

View File

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

View File

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