Repo // Simplify the processing of InputMode.

This commit is contained in:
ShikiSuen 2022-06-15 13:11:58 +08:00
parent 38f85e4915
commit bb2ffe4c50
6 changed files with 37 additions and 56 deletions

View File

@ -47,7 +47,7 @@ public class OpenCCBridge: NSObject {
/// - Parameter string: Text in Original Script. /// - Parameter string: Text in Original Script.
/// - Returns: Text converted to Different Script. /// - Returns: Text converted to Different Script.
public static func crossConvert(_ string: String) -> String? { public static func crossConvert(_ string: String) -> String? {
switch ctlInputMethod.currentKeyHandler.inputMode { switch IME.currentInputMode {
case InputMode.imeModeCHS: case InputMode.imeModeCHS:
return shared.traditionalize?.convert(string) return shared.traditionalize?.convert(string)
case InputMode.imeModeCHT: case InputMode.imeModeCHT:

View File

@ -201,7 +201,7 @@ class InputState {
let selectedReadings = readings[literalMarkedRange] let selectedReadings = readings[literalMarkedRange]
let joined = selectedReadings.joined(separator: "-") let joined = selectedReadings.joined(separator: "-")
let exist = mgrLangModel.checkIfUserPhraseExist( let exist = mgrLangModel.checkIfUserPhraseExist(
userPhrase: text, mode: ctlInputMethod.currentKeyHandler.inputMode, key: joined userPhrase: text, mode: IME.currentInputMode, key: joined
) )
if exist { if exist {
deleteTargetExists = exist deleteTargetExists = exist
@ -292,7 +292,7 @@ class InputState {
let selectedReadings = readings[literalMarkedRange] let selectedReadings = readings[literalMarkedRange]
let joined = selectedReadings.joined(separator: "-") let joined = selectedReadings.joined(separator: "-")
return mgrLangModel.checkIfUserPhraseExist( return mgrLangModel.checkIfUserPhraseExist(
userPhrase: text, mode: ctlInputMethod.currentKeyHandler.inputMode, key: joined userPhrase: text, mode: IME.currentInputMode, key: joined
) )
} }

View File

@ -26,12 +26,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import Cocoa import Cocoa
public enum InputMode: String {
case imeModeCHS = "org.atelierInmu.inputmethod.vChewing.IMECHS"
case imeModeCHT = "org.atelierInmu.inputmethod.vChewing.IMECHT"
case imeModeNULL = ""
}
// MARK: - Delegate. // MARK: - Delegate.
protocol KeyHandlerDelegate { protocol KeyHandlerDelegate {
@ -50,7 +44,6 @@ class KeyHandler {
let kEpsilon: Double = 0.000001 let kEpsilon: Double = 0.000001
let kMaxComposingBufferNeedsToWalkSize: Int = 10 let kMaxComposingBufferNeedsToWalkSize: Int = 10
var _composer: Tekkon.Composer = .init() var _composer: Tekkon.Composer = .init()
var _inputMode: String = ""
var _languageModel: vChewing.LMInstantiator = .init() var _languageModel: vChewing.LMInstantiator = .init()
var _userOverrideModel: vChewing.LMUserOverride = .init() var _userOverrideModel: vChewing.LMUserOverride = .init()
var _builder: Megrez.BlockReadingBuilder var _builder: Megrez.BlockReadingBuilder
@ -58,46 +51,31 @@ class KeyHandler {
var delegate: KeyHandlerDelegate? var delegate: KeyHandlerDelegate?
var inputMode: InputMode { var inputMode: InputMode = IME.currentInputMode {
get { willSet {
switch _inputMode { // ctlInputMethod:
case "org.atelierInmu.inputmethod.vChewing.IMECHS": IME.currentInputMode = newValue
return InputMode.imeModeCHS mgrPrefs.mostRecentInputMode = IME.currentInputMode.rawValue
case "org.atelierInmu.inputmethod.vChewing.IMECHT":
return InputMode.imeModeCHT
default:
return InputMode.imeModeNULL
}
}
set {
let isCHS: Bool = (newValue == InputMode.imeModeCHS) let isCHS: Bool = (newValue == InputMode.imeModeCHS)
// Reinitiate language models if necessary
_languageModel = isCHS ? mgrLangModel.lmCHS : mgrLangModel.lmCHT
_userOverrideModel = isCHS ? mgrLangModel.uomCHS : mgrLangModel.uomCHT
// ctlInputMethod: // Synchronize the sub-languageModel state settings to the new LM.
ctlInputMethod.currentInputMode = isCHS ? InputMode.imeModeCHS.rawValue : InputMode.imeModeCHT.rawValue syncBaseLMPrefs()
mgrPrefs.mostRecentInputMode = ctlInputMethod.currentInputMode
// _inputMode ctlInputMethod // Create new grid builder and clear the composer.
if _inputMode != ctlInputMethod.currentInputMode { createNewBuilder()
// Reinitiate language models if necessary _composer.clear()
_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
} }
} }
public init() { public init() {
_builder = Megrez.BlockReadingBuilder(lm: _languageModel, separator: "-") _builder = Megrez.BlockReadingBuilder(lm: _languageModel, separator: "-")
ensureParser() ensureParser()
inputMode = InputMode(rawValue: ctlInputMethod.currentInputMode) ?? InputMode.imeModeNULL // defer willSet
defer { inputMode = IME.currentInputMode }
} }
func clear() { func clear() {

View File

@ -28,10 +28,21 @@ import Cocoa
// The namespace of this input method. // The namespace of this input method.
public enum vChewing {} 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 { public enum IME {
static let arrSupportedLocales = ["en", "zh-Hant", "zh-Hans", "ja"] static let arrSupportedLocales = ["en", "zh-Hant", "zh-Hans", "ja"]
static let dlgOpenPath = NSOpenPanel() static let dlgOpenPath = NSOpenPanel()
// MARK: -
static var currentInputMode: InputMode = .init(rawValue: mgrPrefs.mostRecentInputMode) ?? .imeModeNULL
// MARK: - // MARK: -
static var areWeUsingOurOwnPhraseEditor: Bool = false static var areWeUsingOurOwnPhraseEditor: Bool = false
@ -40,10 +51,10 @@ public enum IME {
static func getInputMode(isReversed: Bool = false) -> InputMode { static func getInputMode(isReversed: Bool = false) -> InputMode {
if isReversed { if isReversed {
return (ctlInputMethod.currentKeyHandler.inputMode == InputMode.imeModeCHT) return (IME.currentInputMode == InputMode.imeModeCHT)
? InputMode.imeModeCHS : InputMode.imeModeCHT ? InputMode.imeModeCHS : InputMode.imeModeCHT
} else { } else {
return ctlInputMethod.currentKeyHandler.inputMode return IME.currentInputMode
} }
} }

View File

@ -42,14 +42,6 @@ class ctlInputMethod: IMKInputController {
private var keyHandler: KeyHandler = .init() private var keyHandler: KeyHandler = .init()
private var state: InputState = .Empty() 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 // MARK: - Keyboard Layout Specifier
@objc func setKeyLayout() { @objc func setKeyLayout() {
@ -123,7 +115,7 @@ class ctlInputMethod: IMKInputController {
} }
// //
ctlInputMethod.currentKeyHandler.inputMode = keyHandler.inputMode IME.currentInputMode = keyHandler.inputMode
} }
// MARK: - IMKServerInput protocol methods // MARK: - IMKServerInput protocol methods

View File

@ -178,7 +178,7 @@ private class vwrCandidateUniversal: NSView {
if index == highlightedIndex { if index == highlightedIndex {
let colorBlendAmount: CGFloat = IME.isDarkMode() ? 0.25 : 0 let colorBlendAmount: CGFloat = IME.isDarkMode() ? 0.25 : 0
// The background color of the highlightened candidate // The background color of the highlightened candidate
switch ctlInputMethod.currentKeyHandler.inputMode { switch IME.currentInputMode {
case InputMode.imeModeCHS: case InputMode.imeModeCHS:
NSColor.systemRed.blended( NSColor.systemRed.blended(
withFraction: colorBlendAmount, withFraction: colorBlendAmount,
@ -202,7 +202,7 @@ private class vwrCandidateUniversal: NSView {
} else { } else {
NSColor.controlBackgroundColor.setFill() NSColor.controlBackgroundColor.setFill()
} }
switch ctlInputMethod.currentKeyHandler.inputMode { switch IME.currentInputMode {
case InputMode.imeModeCHS: case InputMode.imeModeCHS:
if #available(macOS 12.0, *) { if #available(macOS 12.0, *) {
activeCandidateAttr[.languageIdentifier] = "zh-Hans" as AnyObject activeCandidateAttr[.languageIdentifier] = "zh-Hans" as AnyObject
@ -248,7 +248,7 @@ private class vwrCandidateUniversal: NSView {
if index == highlightedIndex { if index == highlightedIndex {
let colorBlendAmount: CGFloat = IME.isDarkMode() ? 0.25 : 0 let colorBlendAmount: CGFloat = IME.isDarkMode() ? 0.25 : 0
// The background color of the highlightened candidate // The background color of the highlightened candidate
switch ctlInputMethod.currentKeyHandler.inputMode { switch IME.currentInputMode {
case InputMode.imeModeCHS: case InputMode.imeModeCHS:
NSColor.systemRed.blended( NSColor.systemRed.blended(
withFraction: colorBlendAmount, withFraction: colorBlendAmount,
@ -272,7 +272,7 @@ private class vwrCandidateUniversal: NSView {
} else { } else {
NSColor.controlBackgroundColor.setFill() NSColor.controlBackgroundColor.setFill()
} }
switch ctlInputMethod.currentKeyHandler.inputMode { switch IME.currentInputMode {
case InputMode.imeModeCHS: case InputMode.imeModeCHS:
if #available(macOS 12.0, *) { if #available(macOS 12.0, *) {
activeCandidateAttr[.languageIdentifier] = "zh-Hans" as AnyObject activeCandidateAttr[.languageIdentifier] = "zh-Hans" as AnyObject