SessionCtl // Add (Bool)replaceCurrent to handle(event).

This commit is contained in:
ShikiSuen 2022-10-10 19:59:53 +08:00
parent 0d79e83777
commit 0956e043fd
1 changed files with 17 additions and 18 deletions

View File

@ -21,10 +21,10 @@ extension SessionCtl {
/// 使 SessionCtl /// 使 SessionCtl
/// ///
/// - Parameter newState: /// - Parameter newState:
public func handle(state newState: IMEStateProtocol) { public func handle(state newState: IMEStateProtocol, replaceCurrent: Bool = true) {
let previous = state var previous = state
state = newState if replaceCurrent { state = newState }
switch state.type { switch newState.type {
case .ofDeactivated: case .ofDeactivated:
ctlCandidateCurrent.visible = false ctlCandidateCurrent.visible = false
popupCompositionBuffer.hide() popupCompositionBuffer.hide()
@ -43,20 +43,19 @@ extension SessionCtl {
for instance in Self.allInstances { for instance in Self.allInstances {
guard let imkC = instance.ctlCandidateCurrent as? CtlCandidateIMK else { continue } guard let imkC = instance.ctlCandidateCurrent as? CtlCandidateIMK else { continue }
if instance.state.isCandidateContainer, !imkC.visible { if instance.state.isCandidateContainer, !imkC.visible {
instance.handle(state: instance.state) instance.handle(state: instance.state, replaceCurrent: false)
} }
} }
} }
case .ofEmpty, .ofAbortion: case .ofEmpty, .ofAbortion:
var previous = previous if newState.type == .ofAbortion {
if state.type == .ofAbortion { previous = IMEState.ofEmpty()
state = IMEState.ofEmpty() if replaceCurrent { state = previous }
previous = state
} }
ctlCandidateCurrent.visible = false ctlCandidateCurrent.visible = false
tooltipInstance.hide() tooltipInstance.hide()
// .Abortion // .Abortion
if previous.hasComposition, state.type != .ofAbortion { if previous.hasComposition, newState.type != .ofAbortion {
commit(text: previous.displayedText) commit(text: previous.displayedText)
} }
// //
@ -67,7 +66,7 @@ extension SessionCtl {
case .ofCommitting: case .ofCommitting:
ctlCandidateCurrent.visible = false ctlCandidateCurrent.visible = false
tooltipInstance.hide() tooltipInstance.hide()
let textToCommit = state.textToCommit let textToCommit = newState.textToCommit
if !textToCommit.isEmpty { commit(text: textToCommit) } if !textToCommit.isEmpty { commit(text: textToCommit) }
clearInlineDisplay() clearInlineDisplay()
// //
@ -75,19 +74,19 @@ extension SessionCtl {
case .ofInputting: case .ofInputting:
ctlCandidateCurrent.visible = false ctlCandidateCurrent.visible = false
tooltipInstance.hide() tooltipInstance.hide()
let textToCommit = state.textToCommit let textToCommit = newState.textToCommit
if !textToCommit.isEmpty { commit(text: textToCommit) } if !textToCommit.isEmpty { commit(text: textToCommit) }
setInlineDisplayWithCursor() setInlineDisplayWithCursor()
if !state.tooltip.isEmpty { if !newState.tooltip.isEmpty {
show(tooltip: state.tooltip) show(tooltip: newState.tooltip)
} }
case .ofMarking: case .ofMarking:
ctlCandidateCurrent.visible = false ctlCandidateCurrent.visible = false
setInlineDisplayWithCursor() setInlineDisplayWithCursor()
if state.tooltip.isEmpty { if newState.tooltip.isEmpty {
tooltipInstance.hide() tooltipInstance.hide()
} else { } else {
show(tooltip: state.tooltip) show(tooltip: newState.tooltip)
} }
case .ofCandidates, .ofAssociates, .ofSymbolTable: case .ofCandidates, .ofAssociates, .ofSymbolTable:
tooltipInstance.hide() tooltipInstance.hide()
@ -96,10 +95,10 @@ extension SessionCtl {
default: break default: break
} }
// //
if state.hasComposition, PrefMgr.shared.clientsIMKTextInputIncapable.contains(clientBundleIdentifier) { if newState.hasComposition, PrefMgr.shared.clientsIMKTextInputIncapable.contains(clientBundleIdentifier) {
popupCompositionBuffer.isTypingDirectionVertical = isVerticalTyping popupCompositionBuffer.isTypingDirectionVertical = isVerticalTyping
popupCompositionBuffer.show( popupCompositionBuffer.show(
state: state, at: lineHeightRect(zeroCursor: true).origin state: newState, at: lineHeightRect(zeroCursor: true).origin
) )
} else { } else {
popupCompositionBuffer.hide() popupCompositionBuffer.hide()