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