From b375da5c9566e7c33fb3c26ac984b563eee66fbf Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Fri, 11 Aug 2023 16:18:55 +0800 Subject: [PATCH] InputHandler // Properly handle arrow keys for certain submodes. --- Source/Modules/InputHandler_Core.swift | 6 +++--- .../InputHandler_HandleComposition.swift | 5 +++-- .../Modules/InputHandler_HandleStates.swift | 20 ++++++++++++++----- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/Source/Modules/InputHandler_Core.swift b/Source/Modules/InputHandler_Core.swift index 13f9edf0..6c143e27 100644 --- a/Source/Modules/InputHandler_Core.swift +++ b/Source/Modules/InputHandler_Core.swift @@ -31,7 +31,7 @@ public protocol InputHandlerProtocol { func ensureKeyboardParser() func triageInput(event input: InputSignalProtocol) -> Bool func generateStateOfCandidates() -> IMEStateProtocol - func generateStateOfInputting(sansReading: Bool) -> IMEStateProtocol + func generateStateOfInputting(sansReading: Bool, guarded: Bool) -> IMEStateProtocol func generateStateOfAssociates(withPair pair: Megrez.KeyValuePaired) -> IMEStateProtocol func consolidateNode( candidate: (keyArray: [String], value: String), respectCursorPushing: Bool, preConsolidate: Bool, skipObservation: Bool @@ -41,8 +41,8 @@ public protocol InputHandlerProtocol { } extension InputHandlerProtocol { - func generateStateOfInputting(sansReading: Bool = false) -> IMEStateProtocol { - generateStateOfInputting(sansReading: sansReading) + func generateStateOfInputting(sansReading: Bool = false, guarded: Bool = false) -> IMEStateProtocol { + generateStateOfInputting(sansReading: sansReading, guarded: guarded) } func consolidateNode(candidate: (keyArray: [String], value: String), respectCursorPushing: Bool, preConsolidate: Bool) { diff --git a/Source/Modules/InputHandler_HandleComposition.swift b/Source/Modules/InputHandler_HandleComposition.swift index f4c8a7c2..184771b9 100644 --- a/Source/Modules/InputHandler_HandleComposition.swift +++ b/Source/Modules/InputHandler_HandleComposition.swift @@ -391,7 +391,7 @@ extension InputHandler { case 0 ..< 4: if strCodePointBuffer.count < 3 { strCodePointBuffer.append(input.text) - var updatedState = generateStateOfInputting() + var updatedState = generateStateOfInputting(guarded: true) updatedState.tooltipDuration = 0 updatedState.tooltip = tooltipCodePointInputMode delegate.switchState(updatedState) @@ -418,7 +418,8 @@ extension InputHandler { } // 某些舊版 macOS 會在這裡生成的字元後面插入垃圾字元。這裡只保留起始字元。 if char.count > 1 { char = char.map(\.description)[0] } - var updatedState = IMEState.ofCommitting(textToCommit: char) + delegate.switchState(IMEState.ofCommitting(textToCommit: char)) + var updatedState = generateStateOfInputting(guarded: true) updatedState.tooltipDuration = 0 updatedState.tooltip = tooltipCodePointInputMode delegate.switchState(updatedState) diff --git a/Source/Modules/InputHandler_HandleStates.swift b/Source/Modules/InputHandler_HandleStates.swift index 550c0e56..6fd6342c 100644 --- a/Source/Modules/InputHandler_HandleStates.swift +++ b/Source/Modules/InputHandler_HandleStates.swift @@ -18,8 +18,12 @@ extension InputHandler { // MARK: - 構築狀態(State Building) /// 生成「正在輸入」狀態。相關的內容會被拿給狀態機械用來處理在電腦螢幕上顯示的內容。 - public func generateStateOfInputting(sansReading: Bool = false) -> IMEStateProtocol { - if isConsideredEmptyForNow { return IMEState.ofAbortion() } + /// - Parameters: + /// - sansReading: 不顯示組音區/組筆區。 + /// - guarded: 是否在該狀態的顯示文字為空的時候顯示替補空格,否則 InputMethodKit 無法正常攔截方向鍵事件。 + /// - Returns: 生成的「正在輸入」狀態。 + public func generateStateOfInputting(sansReading: Bool = false, guarded: Bool = false) -> IMEStateProtocol { + if isConsideredEmptyForNow, !guarded { return IMEState.ofAbortion() } var segHighlightedAt: Int? let cpInput = isCodePointInputMode && !sansReading /// 「更新內文組字區 (Update the composing buffer)」是指要求客體軟體將組字緩衝區的內容 @@ -68,6 +72,12 @@ extension InputHandler { cursor: cursor, highlightAt: segHighlightedAt ) result.marker = cursorSansReading + /// 特殊情形,否則方向鍵事件無法正常攔截。 + if guarded, result.displayTextSegments.joined().isEmpty { + result.data.displayTextSegments = [" "] + result.cursor = 0 + result.marker = 0 + } return result } @@ -440,7 +450,7 @@ extension InputHandler { if isCodePointInputMode { if !strCodePointBuffer.isEmpty { func refreshState() { - var updatedState = generateStateOfInputting() + var updatedState = generateStateOfInputting(guarded: true) updatedState.tooltipDuration = 0 updatedState.tooltip = tooltipCodePointInputMode delegate.switchState(updatedState) @@ -895,7 +905,7 @@ extension InputHandler { } var updatedState = generateStateOfInputting(sansReading: true) delegate.switchState(IMEState.ofCommitting(textToCommit: updatedState.displayedText)) - updatedState = IMEState.ofEmpty() + updatedState = generateStateOfInputting(guarded: true) updatedState.tooltipDuration = 0 updatedState.tooltip = tooltipCodePointInputMode delegate.switchState(updatedState) @@ -915,7 +925,7 @@ extension InputHandler { } var updatedState = generateStateOfInputting(sansReading: true) delegate.switchState(IMEState.ofCommitting(textToCommit: updatedState.displayedText)) - updatedState = IMEState.ofEmpty() + updatedState = generateStateOfInputting(guarded: true) updatedState.tooltipDuration = 0 updatedState.tooltip = Self.tooltipHaninKeyboardSymbolMode delegate.switchState(updatedState)