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.
/// - 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:

View File

@ -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
)
}

View File

@ -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() {

View File

@ -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
}
}

View File

@ -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

View File

@ -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