diff --git a/Source/Modules/SessionCtl_Core.swift b/Source/Modules/SessionCtl_Core.swift index 4e4d4965..489b6aaf 100644 --- a/Source/Modules/SessionCtl_Core.swift +++ b/Source/Modules/SessionCtl_Core.swift @@ -48,6 +48,9 @@ public class SessionCtl: IMKInputController { /// 浮動組字窗的副本。 public var popupCompositionBuffer = PopupCompositionBuffer() + /// 用來標記當前副本是否已處於活動狀態。 + public var isActivated = false + // MARK: - /// 當前 Caps Lock 按鍵是否被摁下。 @@ -207,6 +210,7 @@ extension SessionCtl { } switchState(IMEState.ofEmpty()) + isActivated = true // 登記啟用狀態。 Self.allInstances.insert(self) } @@ -214,6 +218,7 @@ extension SessionCtl { /// - Parameter sender: 呼叫了該函式的客體(無須使用)。 public override func deactivateServer(_ sender: Any!) { _ = sender // 防止格式整理工具毀掉與此對應的參數。 + isActivated = false resetInputHandler() // 這條會自動搞定 Empty 狀態。 switchState(IMEState.ofDeactivated()) Self.allInstances.remove(self) diff --git a/Source/Modules/SessionCtl_HandleStates.swift b/Source/Modules/SessionCtl_HandleStates.swift index 7bcd022e..604507b9 100644 --- a/Source/Modules/SessionCtl_HandleStates.swift +++ b/Source/Modules/SessionCtl_HandleStates.swift @@ -48,7 +48,7 @@ extension SessionCtl { if previous.hasComposition { commit(text: previous.displayedText) } - clearInlineDisplay() // 該函式有對 client 做 guard-let 保護,不會出現上游 #346 的問題。 + // clearInlineDisplay() // 不需要,否則會觸發無限迴圈。 // 最後一道保險 inputHandler.clear() // 特殊處理:deactivateServer() 可能會遲於另一個客體會話的 activateServer() 執行。 @@ -150,7 +150,7 @@ extension SessionCtl { /// 把 setMarkedText 包裝一下,按需啟用 GCD。 public func doSetMarkedText(_ string: Any!, selectionRange: NSRange, replacementRange: NSRange) { - guard let client = client() else { return } + guard isActivated, let client = client() else { return } if let myID = Bundle.main.bundleIdentifier, let clientID = client.bundleIdentifier(), myID == clientID { DispatchQueue.main.async { client.setMarkedText(string, selectionRange: selectionRange, replacementRange: replacementRange)