From bb2ffe4c50acf2f2c64990ec9baf36dc0bd63854 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Wed, 15 Jun 2022 13:11:58 +0800 Subject: [PATCH] Repo // Simplify the processing of InputMode. --- .../3rdParty/OpenCCBridge/OpenCCBridge.swift | 2 +- .../ControllerModules/InputState.swift | 4 +- .../ControllerModules/KeyHandler_Core.swift | 54 ++++++------------- Source/Modules/IMEModules/IME.swift | 15 +++++- .../Modules/IMEModules/ctlInputMethod.swift | 10 +--- .../CandidateUI/ctlCandidateUniversal.swift | 8 +-- 6 files changed, 37 insertions(+), 56 deletions(-) diff --git a/Source/3rdParty/OpenCCBridge/OpenCCBridge.swift b/Source/3rdParty/OpenCCBridge/OpenCCBridge.swift index bf5d9f1c..47c82ce6 100644 --- a/Source/3rdParty/OpenCCBridge/OpenCCBridge.swift +++ b/Source/3rdParty/OpenCCBridge/OpenCCBridge.swift @@ -47,7 +47,7 @@ public class OpenCCBridge: NSObject { /// - Parameter string: Text in Original Script. /// - Returns: Text converted to Different Script. public static func crossConvert(_ string: String) -> String? { - switch ctlInputMethod.currentKeyHandler.inputMode { + switch IME.currentInputMode { case InputMode.imeModeCHS: return shared.traditionalize?.convert(string) case InputMode.imeModeCHT: diff --git a/Source/Modules/ControllerModules/InputState.swift b/Source/Modules/ControllerModules/InputState.swift index b7a08cbf..afd3bd6e 100644 --- a/Source/Modules/ControllerModules/InputState.swift +++ b/Source/Modules/ControllerModules/InputState.swift @@ -201,7 +201,7 @@ class InputState { let selectedReadings = readings[literalMarkedRange] let joined = selectedReadings.joined(separator: "-") let exist = mgrLangModel.checkIfUserPhraseExist( - userPhrase: text, mode: ctlInputMethod.currentKeyHandler.inputMode, key: joined + userPhrase: text, mode: IME.currentInputMode, key: joined ) if exist { deleteTargetExists = exist @@ -292,7 +292,7 @@ class InputState { let selectedReadings = readings[literalMarkedRange] let joined = selectedReadings.joined(separator: "-") return mgrLangModel.checkIfUserPhraseExist( - userPhrase: text, mode: ctlInputMethod.currentKeyHandler.inputMode, key: joined + userPhrase: text, mode: IME.currentInputMode, key: joined ) } diff --git a/Source/Modules/ControllerModules/KeyHandler_Core.swift b/Source/Modules/ControllerModules/KeyHandler_Core.swift index 1697188d..4ba339b2 100644 --- a/Source/Modules/ControllerModules/KeyHandler_Core.swift +++ b/Source/Modules/ControllerModules/KeyHandler_Core.swift @@ -26,12 +26,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. import Cocoa -public enum InputMode: String { - case imeModeCHS = "org.atelierInmu.inputmethod.vChewing.IMECHS" - case imeModeCHT = "org.atelierInmu.inputmethod.vChewing.IMECHT" - case imeModeNULL = "" -} - // MARK: - Delegate. protocol KeyHandlerDelegate { @@ -50,7 +44,6 @@ class KeyHandler { let kEpsilon: Double = 0.000001 let kMaxComposingBufferNeedsToWalkSize: Int = 10 var _composer: Tekkon.Composer = .init() - var _inputMode: String = "" var _languageModel: vChewing.LMInstantiator = .init() var _userOverrideModel: vChewing.LMUserOverride = .init() var _builder: Megrez.BlockReadingBuilder @@ -58,46 +51,31 @@ class KeyHandler { var delegate: KeyHandlerDelegate? - var inputMode: InputMode { - get { - switch _inputMode { - case "org.atelierInmu.inputmethod.vChewing.IMECHS": - return InputMode.imeModeCHS - case "org.atelierInmu.inputmethod.vChewing.IMECHT": - return InputMode.imeModeCHT - default: - return InputMode.imeModeNULL - } - } - set { + var inputMode: InputMode = IME.currentInputMode { + willSet { + // 將新的簡繁輸入模式提報給 ctlInputMethod: + IME.currentInputMode = newValue + mgrPrefs.mostRecentInputMode = IME.currentInputMode.rawValue + let isCHS: Bool = (newValue == InputMode.imeModeCHS) + // Reinitiate language models if necessary + _languageModel = isCHS ? mgrLangModel.lmCHS : mgrLangModel.lmCHT + _userOverrideModel = isCHS ? mgrLangModel.uomCHS : mgrLangModel.uomCHT - // 緊接著將新的簡繁輸入模式提報給 ctlInputMethod: - ctlInputMethod.currentInputMode = isCHS ? InputMode.imeModeCHS.rawValue : InputMode.imeModeCHT.rawValue - mgrPrefs.mostRecentInputMode = ctlInputMethod.currentInputMode + // Synchronize the sub-languageModel state settings to the new LM. + syncBaseLMPrefs() - // 拿當前的 _inputMode 與 ctlInputMethod 的提報結果對比,不同的話則套用新設定: - if _inputMode != ctlInputMethod.currentInputMode { - // Reinitiate language models if necessary - _languageModel = isCHS ? mgrLangModel.lmCHS : mgrLangModel.lmCHT - _userOverrideModel = isCHS ? mgrLangModel.uomCHS : mgrLangModel.uomCHT - - // Synchronize the sub-languageModel state settings to the new LM. - syncBaseLMPrefs() - - // Create new grid builder and clear the composer. - createNewBuilder() - _composer.clear() - } - // 直接寫到衛星模組內,省得類型轉換 - _inputMode = ctlInputMethod.currentInputMode + // Create new grid builder and clear the composer. + createNewBuilder() + _composer.clear() } } public init() { _builder = Megrez.BlockReadingBuilder(lm: _languageModel, separator: "-") ensureParser() - inputMode = InputMode(rawValue: ctlInputMethod.currentInputMode) ?? InputMode.imeModeNULL + // 下面這句必須用 defer,否則不會觸發其 willSet 部分的內容。 + defer { inputMode = IME.currentInputMode } } func clear() { diff --git a/Source/Modules/IMEModules/IME.swift b/Source/Modules/IMEModules/IME.swift index e6648177..47c35d22 100644 --- a/Source/Modules/IMEModules/IME.swift +++ b/Source/Modules/IMEModules/IME.swift @@ -28,10 +28,21 @@ import Cocoa // The namespace of this input method. public enum vChewing {} +// The type of input modes. +public enum InputMode: String { + case imeModeCHS = "org.atelierInmu.inputmethod.vChewing.IMECHS" + case imeModeCHT = "org.atelierInmu.inputmethod.vChewing.IMECHT" + case imeModeNULL = "" +} + public enum IME { static let arrSupportedLocales = ["en", "zh-Hant", "zh-Hans", "ja"] static let dlgOpenPath = NSOpenPanel() + // MARK: - 輸入法的當前的簡繁體中文模式是? + + static var currentInputMode: InputMode = .init(rawValue: mgrPrefs.mostRecentInputMode) ?? .imeModeNULL + // MARK: - 開關判定當前應用究竟是? static var areWeUsingOurOwnPhraseEditor: Bool = false @@ -40,10 +51,10 @@ public enum IME { static func getInputMode(isReversed: Bool = false) -> InputMode { if isReversed { - return (ctlInputMethod.currentKeyHandler.inputMode == InputMode.imeModeCHT) + return (IME.currentInputMode == InputMode.imeModeCHT) ? InputMode.imeModeCHS : InputMode.imeModeCHT } else { - return ctlInputMethod.currentKeyHandler.inputMode + return IME.currentInputMode } } diff --git a/Source/Modules/IMEModules/ctlInputMethod.swift b/Source/Modules/IMEModules/ctlInputMethod.swift index 2c4b3d74..83717931 100644 --- a/Source/Modules/IMEModules/ctlInputMethod.swift +++ b/Source/Modules/IMEModules/ctlInputMethod.swift @@ -42,14 +42,6 @@ class ctlInputMethod: IMKInputController { private var keyHandler: KeyHandler = .init() private var state: InputState = .Empty() - // 想讓 KeyHandler 能夠被外界調查狀態與參數的話,就得對 KeyHandler 做常態處理。 - // 這樣 InputState 可以藉由這個 ctlInputMethod 了解到當前的輸入模式是簡體中文還是繁體中文。 - // 然而,要是直接對 keyHandler 做常態處理的話,反而會導致 InputSignal 無法協同處理。 - // 所以才需要「currentKeyHandler」這個假 KeyHandler。 - // 這個「currentKeyHandler」僅用來讓其他模組知道當前的輸入模式是什麼模式,除此之外別無屌用。 - static var currentKeyHandler: KeyHandler = .init() - @objc static var currentInputMode = mgrPrefs.mostRecentInputMode - // MARK: - Keyboard Layout Specifier @objc func setKeyLayout() { @@ -123,7 +115,7 @@ class ctlInputMethod: IMKInputController { } // 讓外界知道目前的簡繁體輸入模式。 - ctlInputMethod.currentKeyHandler.inputMode = keyHandler.inputMode + IME.currentInputMode = keyHandler.inputMode } // MARK: - IMKServerInput protocol methods diff --git a/Source/UI/CandidateUI/ctlCandidateUniversal.swift b/Source/UI/CandidateUI/ctlCandidateUniversal.swift index 568f5f03..c421f8d3 100644 --- a/Source/UI/CandidateUI/ctlCandidateUniversal.swift +++ b/Source/UI/CandidateUI/ctlCandidateUniversal.swift @@ -178,7 +178,7 @@ private class vwrCandidateUniversal: NSView { if index == highlightedIndex { let colorBlendAmount: CGFloat = IME.isDarkMode() ? 0.25 : 0 // The background color of the highlightened candidate - switch ctlInputMethod.currentKeyHandler.inputMode { + switch IME.currentInputMode { case InputMode.imeModeCHS: NSColor.systemRed.blended( withFraction: colorBlendAmount, @@ -202,7 +202,7 @@ private class vwrCandidateUniversal: NSView { } else { NSColor.controlBackgroundColor.setFill() } - switch ctlInputMethod.currentKeyHandler.inputMode { + switch IME.currentInputMode { case InputMode.imeModeCHS: if #available(macOS 12.0, *) { activeCandidateAttr[.languageIdentifier] = "zh-Hans" as AnyObject @@ -248,7 +248,7 @@ private class vwrCandidateUniversal: NSView { if index == highlightedIndex { let colorBlendAmount: CGFloat = IME.isDarkMode() ? 0.25 : 0 // The background color of the highlightened candidate - switch ctlInputMethod.currentKeyHandler.inputMode { + switch IME.currentInputMode { case InputMode.imeModeCHS: NSColor.systemRed.blended( withFraction: colorBlendAmount, @@ -272,7 +272,7 @@ private class vwrCandidateUniversal: NSView { } else { NSColor.controlBackgroundColor.setFill() } - switch ctlInputMethod.currentKeyHandler.inputMode { + switch IME.currentInputMode { case InputMode.imeModeCHS: if #available(macOS 12.0, *) { activeCandidateAttr[.languageIdentifier] = "zh-Hans" as AnyObject