SessionCtl // Use GCD for certain state tasks.

This commit is contained in:
ShikiSuen 2023-03-20 14:38:44 +08:00
parent 0f44861440
commit 852eed07e2
2 changed files with 24 additions and 10 deletions

View File

@ -43,6 +43,9 @@ public class SessionCtl: IMKInputController {
/// ///
public var isServingIMEItself: Bool = false public var isServingIMEItself: Bool = false
///
public var stateQueueSet = [String]()
/// bundleIdentifier /// bundleIdentifier
/// ///
public var clientBundleIdentifier: String = "" { public var clientBundleIdentifier: String = "" {

View File

@ -12,6 +12,20 @@ import Shared
// MARK: - 調 (State Handling) // MARK: - 調 (State Handling)
public extension SessionCtl { public extension SessionCtl {
private func performStateTask(_ doTask: @escaping () -> Void) {
let queueID = UUID().uuidString
DispatchQueue.main.async { [self] in
stateQueueSet.append(queueID)
while stateQueueSet.first != queueID {
Thread.sleep(forTimeInterval: 0.001)
}
doTask()
if stateQueueSet.first == queueID {
stateQueueSet.removeFirst()
}
}
}
/// 調 /// 調
/// ///
/// ///
@ -43,11 +57,8 @@ public extension SessionCtl {
switch newState.type { switch newState.type {
case .ofDeactivated: case .ofDeactivated:
// commitComposition() // commitComposition()
// clearInlineDisplay()
// IMK inputMode.didSet() // IMK inputMode.didSet()
candidateUI?.visible = false hidePalettes()
popupCompositionBuffer.hide()
tooltipInstance.hide()
inputHandler?.clear() inputHandler?.clear()
if ![.ofAbortion, .ofEmpty].contains(previous.type), !previous.displayedText.isEmpty { if ![.ofAbortion, .ofEmpty].contains(previous.type), !previous.displayedText.isEmpty {
clearInlineDisplay() clearInlineDisplay()
@ -68,23 +79,23 @@ public extension SessionCtl {
commit(text: previous.displayedText) commit(text: previous.displayedText)
} }
// //
showTooltip(newState.tooltip, duration: newState.tooltipDuration) performStateTask { self.showTooltip(newState.tooltip, duration: newState.tooltipDuration) }
clearInlineDisplay() clearInlineDisplay()
inputHandler?.clear() inputHandler?.clear()
case .ofInputting: case .ofInputting:
candidateUI?.visible = false candidateUI?.visible = false
if !newState.textToCommit.isEmpty { commit(text: newState.textToCommit) } if !newState.textToCommit.isEmpty { commit(text: newState.textToCommit) }
setInlineDisplayWithCursor()
// //
showTooltip(newState.tooltip, duration: newState.tooltipDuration) performStateTask { self.showTooltip(newState.tooltip, duration: newState.tooltipDuration) }
setInlineDisplayWithCursor()
case .ofMarking: case .ofMarking:
candidateUI?.visible = false candidateUI?.visible = false
performStateTask { self.showTooltip(newState.tooltip) }
setInlineDisplayWithCursor() setInlineDisplayWithCursor()
showTooltip(newState.tooltip)
case .ofCandidates, .ofAssociates, .ofSymbolTable: case .ofCandidates, .ofAssociates, .ofSymbolTable:
tooltipInstance.hide() performStateTask { self.showCandidates() }
setInlineDisplayWithCursor() setInlineDisplayWithCursor()
showCandidates() tooltipInstance.hide()
} }
// //
updatePopupDisplayWithCursor() updatePopupDisplayWithCursor()