From ab9702b6a86ebce5a067e0875a626bee8b3a972d Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Wed, 8 Feb 2023 15:17:28 +0800 Subject: [PATCH] InputHandler // Extract handlePunctuationList(), etc. * Also fix an issue which commits unfinished composer / calligrapher contents. --- Source/Modules/InputHandler_HandleInput.swift | 48 ++++--------------- .../Modules/InputHandler_HandleStates.swift | 46 ++++++++++++++++++ 2 files changed, 55 insertions(+), 39 deletions(-) diff --git a/Source/Modules/InputHandler_HandleInput.swift b/Source/Modules/InputHandler_HandleInput.swift index 57636c25..f14c30f1 100644 --- a/Source/Modules/InputHandler_HandleInput.swift +++ b/Source/Modules/InputHandler_HandleInput.swift @@ -157,6 +157,15 @@ extension InputHandler { case .kBackSpace: return handleBackSpace(input: input) case .kWindowsDelete: return handleDelete(input: input) case .kCarriageReturn, .kLineFeed: return handleEnter(input: input) + case .kSymbolMenuPhysicalKeyJIS, .kSymbolMenuPhysicalKeyIntl: + let isJIS = keyCodeType == .kSymbolMenuPhysicalKeyJIS + switch input.modifierFlags { + case []: + return handlePunctuationList(alternative: false, isJIS: isJIS) + case [.option]: + return handlePunctuationList(alternative: true, isJIS: isJIS) + default: break + } case .kSpace: // 倘若沒有在偏好設定內將 Space 空格鍵設為選字窗呼叫用鍵的話……… // 空格字符輸入行為處理。 switch state.type { @@ -191,45 +200,6 @@ extension InputHandler { } } - // MARK: Punctuation list - - if input.isSymbolMenuPhysicalKey, !input.isShiftHold, !input.isControlHold, state.type != .ofDeactivated { - if input.isOptionHold { - if currentLM.hasUnigramsFor(keyArray: ["_punctuation_list"]) { - if isComposerOrCalligrapherEmpty, compositor.insertKey("_punctuation_list") { - walk() - // 一邊吃一邊屙(僅對位列黑名單的 App 用這招限制組字區長度)。 - let textToCommit = commitOverflownComposition - var inputting = generateStateOfInputting() - inputting.textToCommit = textToCommit - delegate.switchState(inputting) - // 開始決定是否切換至選字狀態。 - let newState = generateStateOfCandidates() - _ = newState.candidates.isEmpty ? delegate.callError("B5127D8A") : delegate.switchState(newState) - } else { // 不要在注音沒敲完整的情況下叫出統合符號選單。 - delegate.callError("17446655") - } - return true - } else { - let errorMessage = - NSLocalizedString( - "Please manually implement the symbols of this menu \nin the user phrase file with “_punctuation_list” key.", - comment: "" - ) - vCLog("8EB3FB1A: " + errorMessage) - delegate.switchState(IMEState.ofEmpty()) - let isJIS: Bool = input.keyCode == KeyCode.kSymbolMenuPhysicalKeyJIS.rawValue - delegate.switchState(IMEState.ofCommitting(textToCommit: isJIS ? "_" : "`")) - return true - } - } else { - // 得在這裡先 commit buffer,不然會導致「在摁 ESC 離開符號選單時會重複輸入上一次的組字區的內容」的不當行為。 - delegate.switchState(IMEState.ofCommitting(textToCommit: state.displayedText)) - delegate.switchState(IMEState.ofSymbolTable(node: CandidateNode.root)) - return true - } - } - // MARK: 全形/半形阿拉伯數字輸入 (FW / HW Arabic Numbers Input) if state.type == .ofEmpty { diff --git a/Source/Modules/InputHandler_HandleStates.swift b/Source/Modules/InputHandler_HandleStates.swift index c8cce402..850c0d29 100644 --- a/Source/Modules/InputHandler_HandleStates.swift +++ b/Source/Modules/InputHandler_HandleStates.swift @@ -762,4 +762,50 @@ extension InputHandler { delegate.switchState(newState) return true } + + // MARK: - 處理符號選單 + + /// 處理符號選單。 + /// - Parameters: + /// - alternative: 使用另一個模式。 + /// - JIS: 是否為 JIS 鍵盤。 + /// - Returns: 將按鍵行為「是否有處理掉」藉由 SessionCtl 回報給 IMK。 + func handlePunctuationList(alternative: Bool, isJIS: Bool = false) -> Bool { + guard let delegate = delegate, delegate.state.type != .ofDeactivated else { return false } + if alternative { + if currentLM.hasUnigramsFor(keyArray: ["_punctuation_list"]) { + if isComposerOrCalligrapherEmpty, compositor.insertKey("_punctuation_list") { + walk() + // 一邊吃一邊屙(僅對位列黑名單的 App 用這招限制組字區長度)。 + let textToCommit = commitOverflownComposition + var inputting = generateStateOfInputting() + inputting.textToCommit = textToCommit + delegate.switchState(inputting) + // 開始決定是否切換至選字狀態。 + let newState = generateStateOfCandidates() + _ = newState.candidates.isEmpty ? delegate.callError("B5127D8A") : delegate.switchState(newState) + } else { // 不要在注音沒敲完整的情況下叫出統合符號選單。 + delegate.callError("17446655") + } + return true + } else { + let errorMessage = + NSLocalizedString( + "Please manually implement the symbols of this menu \nin the user phrase file with “_punctuation_list” key.", + comment: "" + ) + vCLog("8EB3FB1A: " + errorMessage) + let textToCommit = generateStateOfInputting(sansReading: true).displayedText + delegate.switchState(IMEState.ofCommitting(textToCommit: textToCommit)) + delegate.switchState(IMEState.ofCommitting(textToCommit: isJIS ? "_" : "`")) + return true + } + } else { + // 得在這裡先 commit buffer,不然會導致「在摁 ESC 離開符號選單時會重複輸入上一次的組字區的內容」的不當行為。 + let textToCommit = generateStateOfInputting(sansReading: true).displayedText + delegate.switchState(IMEState.ofCommitting(textToCommit: textToCommit)) + delegate.switchState(IMEState.ofSymbolTable(node: CandidateNode.root)) + return true + } + } }