From a2f0b948790e7e8f6a3422123e80e837c4d75c11 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Sat, 10 Sep 2022 17:20:57 +0800 Subject: [PATCH] KeyHandler // Reimplement commitOverflownComposition(). - Also handle reading-mismatched nodes. --- .../ControllerModules/KeyHandler_Core.swift | 49 +++++++++++++++++++ .../KeyHandler_HandleComposition.swift | 6 ++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/Source/Modules/ControllerModules/KeyHandler_Core.swift b/Source/Modules/ControllerModules/KeyHandler_Core.swift index d52cb05d..6a237942 100644 --- a/Source/Modules/ControllerModules/KeyHandler_Core.swift +++ b/Source/Modules/ControllerModules/KeyHandler_Core.swift @@ -438,3 +438,52 @@ public class KeyHandler { } } } + +// MARK: - Components for Popup Composition Buffer (PCB) Window. + +/// 組字區文字上限。 +/// - Remark: 該選項僅對不支援 IMKTextInput 協定的應用有用,就不交給 mgrPrefs 了。 +private let compositorWidthLimit = 20 + +extension KeyHandler { + /// 在爬取組字結果之前,先將即將從組字區溢出的內容遞交出去。 + /// + /// 在理想狀況之下,組字區多長都無所謂。但是,螢幕浮動組字窗的尺寸是有限的。 + /// 於是,有必要限定組字區的長度。超過該長度的內容會在爬軌之前先遞交出去, + /// 使其不再記入最大相似度估算的估算對象範圍。 + /// 用比較形象且生動卻有點噁心的解釋的話,蒼蠅一邊吃一邊屙。 + var commitOverflownComposition: String { + guard !compositor.walkedNodes.isEmpty, + compositor.width > compositorWidthLimit, + let identifier = delegate?.clientBundleIdentifier, + mgrPrefs.clientsIMKTextInputIncapable.contains(identifier) + else { + return "" + } + // 回頭在這裡插上對 Steam 的 Client Identifier 的要求。 + var textToCommit = "" + while compositor.width > compositorWidthLimit { + var delta = compositor.width - compositorWidthLimit + let node = compositor.walkedNodes[0] + if node.isReadingMismatched { + delta = node.keyArray.count + textToCommit += node.currentPair.value + } else { + delta = min(delta, node.keyArray.count) + textToCommit += node.currentPair.value.charComponents[0..