Repo // Simplify the processing of InputMode.
This commit is contained in:
parent
38f85e4915
commit
bb2ffe4c50
|
@ -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:
|
||||||
|
|
|
@ -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
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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() {
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue