SessionCtl // Refactor candidatePairSelectionConfirmed().

This commit is contained in:
ShikiSuen 2023-03-09 17:57:26 +08:00
parent 625897a744
commit 22ead33ba9
1 changed files with 18 additions and 33 deletions

View File

@ -112,19 +112,15 @@ extension SessionCtl: CtlCandidateDelegate {
public func candidatePairSelectionConfirmed(at index: Int) {
guard let inputHandler = inputHandler else { return }
if state.type == .ofSymbolTable, (0 ..< state.node.members.count).contains(index) {
switch state.type {
case .ofSymbolTable where (0 ..< state.node.members.count).contains(index):
let node = state.node.members[index]
if !node.members.isEmpty {
switchState(IMEState.ofEmpty()) //
switchState(IMEState.ofSymbolTable(node: node))
} else {
switchState(IMEState.ofCommitting(textToCommit: node.name))
switchState(IMEState.ofEmpty())
}
return
}
if [.ofCandidates, .ofSymbolTable].contains(state.type) {
case .ofCandidates where (0 ..< state.candidates.count).contains(index):
let selectedValue = state.candidates[index]
if state.type == .ofCandidates {
inputHandler.consolidateNode(
@ -132,45 +128,34 @@ extension SessionCtl: CtlCandidateDelegate {
preConsolidate: PrefMgr.shared.consolidateContextOnCandidateSelection
)
}
let inputting = inputHandler.generateStateOfInputting()
var result: IMEStateProtocol = inputHandler.generateStateOfInputting()
defer { switchState(result) } //
if PrefMgr.shared.useSCPCTypingMode {
switchState(IMEState.ofCommitting(textToCommit: inputting.displayedText))
switchState(IMEState.ofCommitting(textToCommit: result.displayedText))
// selectedValue.value
if PrefMgr.shared.associatedPhrasesEnabled {
let associates = inputHandler.generateStateOfAssociates(
withPair: .init(keyArray: selectedValue.keyArray, value: selectedValue.value)
)
switchState(associates.candidates.isEmpty ? IMEState.ofEmpty() : associates)
result = associates.candidates.isEmpty ? IMEState.ofEmpty() : associates
} else {
switchState(IMEState.ofEmpty())
result = IMEState.ofEmpty()
}
} else {
switchState(inputting)
}
return
}
if state.type == .ofAssociates {
case .ofAssociates where (0 ..< state.candidates.count).contains(index):
let selectedValue = state.candidates[index]
var result: IMEStateProtocol = IMEState.ofEmpty()
defer { switchState(result) } //
switchState(IMEState.ofCommitting(textToCommit: selectedValue.value))
guard PrefMgr.shared.associatedPhrasesEnabled else { return }
// selectedValue.value
//
guard let valueKept = selectedValue.value.last else {
switchState(IMEState.ofEmpty())
return
}
if PrefMgr.shared.associatedPhrasesEnabled {
let associates = inputHandler.generateStateOfAssociates(
withPair: .init(keyArray: selectedValue.keyArray, value: String(valueKept))
)
if !associates.candidates.isEmpty {
switchState(associates)
return
}
}
switchState(IMEState.ofEmpty())
guard let valueKept = selectedValue.value.last?.description else { return }
let associates = inputHandler.generateStateOfAssociates(
withPair: .init(keyArray: selectedValue.keyArray, value: valueKept)
)
if !associates.candidates.isEmpty { result = associates }
default: return
}
}