From 8076b083fee9875bf18a395956c40272be3b347b Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Sat, 18 Jun 2022 13:59:27 +0800 Subject: [PATCH] ctlIME // Move kanji conversion to IME module. --- Source/Modules/ControllerModules/InputState.swift | 4 ++-- .../ControllerModules/KeyHandler_Core.swift | 15 +++++++++++++++ Source/Modules/IMEModules/ctlInputMethod.swift | 15 +-------------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/Source/Modules/ControllerModules/InputState.swift b/Source/Modules/ControllerModules/InputState.swift index a2cd6052..0b9b5d24 100644 --- a/Source/Modules/ControllerModules/InputState.swift +++ b/Source/Modules/ControllerModules/InputState.swift @@ -146,7 +146,7 @@ class InputState { var attributedString: NSAttributedString { let attributedString = NSAttributedString( - string: composingBuffer, + string: IME.kanjiConversionIfRequired(composingBuffer), attributes: [ .underlineStyle: NSUnderlineStyle.single.rawValue, .markedClauseSegment: 0, @@ -260,7 +260,7 @@ class InputState { } override var attributedString: NSAttributedString { - let attributedString = NSMutableAttributedString(string: composingBuffer) + let attributedString = NSMutableAttributedString(string: IME.kanjiConversionIfRequired(composingBuffer)) let end = markedRange.upperBound attributedString.setAttributes( diff --git a/Source/Modules/ControllerModules/KeyHandler_Core.swift b/Source/Modules/ControllerModules/KeyHandler_Core.swift index 604d7845..1cf5db7b 100644 --- a/Source/Modules/ControllerModules/KeyHandler_Core.swift +++ b/Source/Modules/ControllerModules/KeyHandler_Core.swift @@ -446,4 +446,19 @@ class KeyHandler { func deleteCompositorReadingToTheFrontOfCursor() { compositor.deleteReadingToTheFrontOfCursor() } + + // MARK: - 其他工具函式 + + func kanjiConversionIfRequired(_ text: String) -> String { + if inputMode == InputMode.imeModeCHT { + switch (mgrPrefs.chineseConversionEnabled, mgrPrefs.shiftJISShinjitaiOutputEnabled) { + case (false, true): return vChewingKanjiConverter.cnvTradToJIS(text) + case (true, false): return vChewingKanjiConverter.cnvTradToKangXi(text) + // 本來這兩個開關不該同時開啟的,但萬一被開啟了的話就這樣處理: + case (true, true): return vChewingKanjiConverter.cnvTradToJIS(text) + case (false, false): return text + } + } + return text + } } diff --git a/Source/Modules/IMEModules/ctlInputMethod.swift b/Source/Modules/IMEModules/ctlInputMethod.swift index 5a3a4aec..c9d35bd9 100644 --- a/Source/Modules/IMEModules/ctlInputMethod.swift +++ b/Source/Modules/IMEModules/ctlInputMethod.swift @@ -298,20 +298,7 @@ extension ctlInputMethod { /// 遞交組字區內容。 private func commit(text: String) { - func kanjiConversionIfRequired(_ text: String) -> String { - if keyHandler.inputMode == InputMode.imeModeCHT { - switch (mgrPrefs.chineseConversionEnabled, mgrPrefs.shiftJISShinjitaiOutputEnabled) { - case (false, true): return vChewingKanjiConverter.cnvTradToJIS(text) - case (true, false): return vChewingKanjiConverter.cnvTradToKangXi(text) - // 本來這兩個開關不該同時開啟的,但萬一被開啟了的話就這樣處理: - case (true, true): return vChewingKanjiConverter.cnvTradToJIS(text) - case (false, false): return text - } - } - return text - } - - let buffer = kanjiConversionIfRequired(text) + let buffer = keyHandler.kanjiConversionIfRequired(text) if buffer.isEmpty { return }