Repo // Pack InputMode typedefs into Shared package.

This commit is contained in:
ShikiSuen 2022-09-21 12:31:51 +08:00
parent a4a37feb16
commit f80c13331d
10 changed files with 85 additions and 83 deletions

View File

@ -238,4 +238,21 @@ public enum Shared {
]
public static let arrSupportedLocales: [String] = ["en", "zh-Hant", "zh-Hans", "ja"]
// The type of input modes.
public enum InputMode: String, CaseIterable {
case imeModeCHS = "org.atelierInmu.inputmethod.vChewing.IMECHS"
case imeModeCHT = "org.atelierInmu.inputmethod.vChewing.IMECHT"
case imeModeNULL = ""
public var reversed: Shared.InputMode {
switch self {
case .imeModeCHS:
return .imeModeCHT
case .imeModeCHT:
return .imeModeCHS
case .imeModeNULL:
return .imeModeNULL
}
}
}
}

View File

@ -57,7 +57,7 @@ public enum ChineseConverter {
if !PrefMgr.shared.currencyNumeralsEnabled { return }
for key in currencyNumeralDictTable.keys {
guard let result = currencyNumeralDictTable[key] else { continue }
if IMEApp.currentInputMode == InputMode.imeModeCHS {
if IMEApp.currentInputMode == .imeModeCHS {
target = target.replacingOccurrences(of: key, with: result.3) // Simplified Chinese
continue
}
@ -75,9 +75,9 @@ public enum ChineseConverter {
/// - Returns: Text converted to Different Script.
public static func crossConvert(_ string: String) -> String? {
switch IMEApp.currentInputMode {
case InputMode.imeModeCHS:
case .imeModeCHS:
return shared.convert(string, to: .zhHantTW)
case InputMode.imeModeCHT:
case .imeModeCHT:
return shared.convert(string, to: .zhHansCN)
default:
return string
@ -97,7 +97,7 @@ public enum ChineseConverter {
}
static func kanjiConversionIfRequired(_ text: String) -> String {
guard IMEApp.currentInputMode == InputMode.imeModeCHT else { return text }
guard IMEApp.currentInputMode == .imeModeCHT else { return text }
switch (PrefMgr.shared.chineseConversionEnabled, PrefMgr.shared.shiftJISShinjitaiOutputEnabled) {
case (false, true): return ChineseConverter.cnvTradToJIS(text)
case (true, false): return ChineseConverter.cnvTradToKangXi(text)

View File

@ -109,7 +109,7 @@ public enum LMMgr {
}
}
public static func loadDataModel(_ mode: InputMode) {
public static func loadDataModel(_ mode: Shared.InputMode) {
switch mode {
case .imeModeCHS:
if !Self.lmCHS.isMiscDataLoaded {
@ -156,56 +156,56 @@ public enum LMMgr {
public static func loadUserPhrasesData() {
Self.lmCHT.loadUserPhrasesData(
path: userPhrasesDataURL(InputMode.imeModeCHT).path,
filterPath: userFilteredDataURL(InputMode.imeModeCHT).path
path: userPhrasesDataURL(.imeModeCHT).path,
filterPath: userFilteredDataURL(.imeModeCHT).path
)
Self.lmCHS.loadUserPhrasesData(
path: userPhrasesDataURL(InputMode.imeModeCHS).path,
filterPath: userFilteredDataURL(InputMode.imeModeCHS).path
path: userPhrasesDataURL(.imeModeCHS).path,
filterPath: userFilteredDataURL(.imeModeCHS).path
)
Self.lmCHT.loadUserSymbolData(path: userSymbolDataURL(InputMode.imeModeCHT).path)
Self.lmCHS.loadUserSymbolData(path: userSymbolDataURL(InputMode.imeModeCHS).path)
Self.lmCHT.loadUserSymbolData(path: userSymbolDataURL(.imeModeCHT).path)
Self.lmCHS.loadUserSymbolData(path: userSymbolDataURL(.imeModeCHS).path)
Self.uomCHT.loadData(fromURL: userOverrideModelDataURL(InputMode.imeModeCHT))
Self.uomCHS.loadData(fromURL: userOverrideModelDataURL(InputMode.imeModeCHS))
Self.uomCHT.loadData(fromURL: userOverrideModelDataURL(.imeModeCHT))
Self.uomCHS.loadData(fromURL: userOverrideModelDataURL(.imeModeCHS))
CandidateNode.load(url: Self.userSymbolMenuDataURL())
}
public static func loadUserAssociatesData() {
Self.lmCHT.loadUserAssociatesData(
path: Self.userAssociatesDataURL(InputMode.imeModeCHT).path
path: Self.userAssociatesDataURL(.imeModeCHT).path
)
Self.lmCHS.loadUserAssociatesData(
path: Self.userAssociatesDataURL(InputMode.imeModeCHS).path
path: Self.userAssociatesDataURL(.imeModeCHS).path
)
}
public static func loadUserPhraseReplacement() {
Self.lmCHT.loadReplacementsData(
path: Self.userReplacementsDataURL(InputMode.imeModeCHT).path
path: Self.userReplacementsDataURL(.imeModeCHT).path
)
Self.lmCHS.loadReplacementsData(
path: Self.userReplacementsDataURL(InputMode.imeModeCHS).path
path: Self.userReplacementsDataURL(.imeModeCHS).path
)
}
public static func loadUserSCPCSequencesData() {
Self.lmCHT.loadUserSCPCSequencesData(
path: Self.userSCPCSequencesURL(InputMode.imeModeCHT).path
path: Self.userSCPCSequencesURL(.imeModeCHT).path
)
Self.lmCHS.loadUserSCPCSequencesData(
path: Self.userSCPCSequencesURL(InputMode.imeModeCHS).path
path: Self.userSCPCSequencesURL(.imeModeCHS).path
)
}
public static func checkIfUserPhraseExist(
userPhrase: String,
mode: InputMode,
mode: Shared.InputMode,
key unigramKey: String
) -> Bool {
let unigrams: [Megrez.Unigram] =
(mode == InputMode.imeModeCHT)
(mode == .imeModeCHT)
? Self.lmCHT.unigramsFor(key: unigramKey) : Self.lmCHS.unigramsFor(key: unigramKey)
for unigram in unigrams {
if unigram.value == userPhrase {
@ -253,48 +253,48 @@ public enum LMMgr {
/// 使
/// - Parameter mode:
/// - Returns: URL
public static func userPhrasesDataURL(_ mode: InputMode) -> URL {
let fileName = (mode == InputMode.imeModeCHT) ? "userdata-cht.txt" : "userdata-chs.txt"
public static func userPhrasesDataURL(_ mode: Shared.InputMode) -> URL {
let fileName = (mode == .imeModeCHT) ? "userdata-cht.txt" : "userdata-chs.txt"
return URL(fileURLWithPath: dataFolderPath(isDefaultFolder: false)).appendingPathComponent(fileName)
}
/// 使
/// - Parameter mode:
/// - Returns: URL
public static func userSymbolDataURL(_ mode: InputMode) -> URL {
let fileName = (mode == InputMode.imeModeCHT) ? "usersymbolphrases-cht.txt" : "usersymbolphrases-chs.txt"
public static func userSymbolDataURL(_ mode: Shared.InputMode) -> URL {
let fileName = (mode == .imeModeCHT) ? "usersymbolphrases-cht.txt" : "usersymbolphrases-chs.txt"
return URL(fileURLWithPath: dataFolderPath(isDefaultFolder: false)).appendingPathComponent(fileName)
}
/// 使
/// - Parameter mode:
/// - Returns: URL
public static func userAssociatesDataURL(_ mode: InputMode) -> URL {
let fileName = (mode == InputMode.imeModeCHT) ? "associatedPhrases-cht.txt" : "associatedPhrases-chs.txt"
public static func userAssociatesDataURL(_ mode: Shared.InputMode) -> URL {
let fileName = (mode == .imeModeCHT) ? "associatedPhrases-cht.txt" : "associatedPhrases-chs.txt"
return URL(fileURLWithPath: dataFolderPath(isDefaultFolder: false)).appendingPathComponent(fileName)
}
/// 使
/// - Parameter mode:
/// - Returns: URL
public static func userFilteredDataURL(_ mode: InputMode) -> URL {
let fileName = (mode == InputMode.imeModeCHT) ? "exclude-phrases-cht.txt" : "exclude-phrases-chs.txt"
public static func userFilteredDataURL(_ mode: Shared.InputMode) -> URL {
let fileName = (mode == .imeModeCHT) ? "exclude-phrases-cht.txt" : "exclude-phrases-chs.txt"
return URL(fileURLWithPath: dataFolderPath(isDefaultFolder: false)).appendingPathComponent(fileName)
}
/// 使
/// - Parameter mode:
/// - Returns: URL
public static func userReplacementsDataURL(_ mode: InputMode) -> URL {
let fileName = (mode == InputMode.imeModeCHT) ? "phrases-replacement-cht.txt" : "phrases-replacement-chs.txt"
public static func userReplacementsDataURL(_ mode: Shared.InputMode) -> URL {
let fileName = (mode == .imeModeCHT) ? "phrases-replacement-cht.txt" : "phrases-replacement-chs.txt"
return URL(fileURLWithPath: dataFolderPath(isDefaultFolder: false)).appendingPathComponent(fileName)
}
/// 使
/// - Parameter mode:
/// - Returns: URL
public static func userSCPCSequencesURL(_ mode: InputMode) -> URL {
let fileName = (mode == InputMode.imeModeCHT) ? "data-plain-bpmf-cht.plist" : "data-plain-bpmf-chs.plist"
public static func userSCPCSequencesURL(_ mode: Shared.InputMode) -> URL {
let fileName = (mode == .imeModeCHT) ? "data-plain-bpmf-cht.plist" : "data-plain-bpmf-chs.plist"
return URL(fileURLWithPath: dataFolderPath(isDefaultFolder: false)).appendingPathComponent(fileName)
}
@ -309,7 +309,7 @@ public enum LMMgr {
/// ~/Library/Application Support/vChewing/使
/// - Parameter mode:
/// - Returns: URL
public static func userOverrideModelDataURL(_ mode: InputMode) -> URL {
public static func userOverrideModelDataURL(_ mode: Shared.InputMode) -> URL {
let fileName: String = {
switch mode {
case .imeModeCHS: return "vChewing_override-model-data-chs.dat"
@ -350,7 +350,7 @@ public enum LMMgr {
return true
}
@discardableResult public static func chkUserLMFilesExist(_ mode: InputMode) -> Bool {
@discardableResult public static func chkUserLMFilesExist(_ mode: Shared.InputMode) -> Bool {
if !userDataFolderExists {
return false
}
@ -477,11 +477,11 @@ public enum LMMgr {
// MARK: - 使
public static func writeUserPhrase(
_ userPhrase: String?, inputMode mode: InputMode, areWeDuplicating: Bool, areWeDeleting: Bool
_ userPhrase: String?, inputMode mode: Shared.InputMode, areWeDuplicating: Bool, areWeDeleting: Bool
) -> Bool {
if var currentMarkedPhrase: String = userPhrase {
if !chkUserLMFilesExist(InputMode.imeModeCHS)
|| !chkUserLMFilesExist(InputMode.imeModeCHT)
if !chkUserLMFilesExist(.imeModeCHS)
|| !chkUserLMFilesExist(.imeModeCHT)
{
return false
}
@ -526,8 +526,8 @@ public enum LMMgr {
// MARK: - 使
public static func checkIfUserFilesExistBeforeOpening() -> Bool {
if !Self.chkUserLMFilesExist(InputMode.imeModeCHS)
|| !Self.chkUserLMFilesExist(InputMode.imeModeCHT)
if !Self.chkUserLMFilesExist(.imeModeCHS)
|| !Self.chkUserLMFilesExist(.imeModeCHT)
{
let content = String(
format: NSLocalizedString(
@ -562,19 +562,19 @@ public enum LMMgr {
let group = DispatchGroup()
group.enter()
globalQuene.async {
Self.uomCHT.saveData(toURL: userOverrideModelDataURL(InputMode.imeModeCHT))
Self.uomCHT.saveData(toURL: userOverrideModelDataURL(.imeModeCHT))
group.leave()
}
group.enter()
globalQuene.async {
Self.uomCHS.saveData(toURL: userOverrideModelDataURL(InputMode.imeModeCHS))
Self.uomCHS.saveData(toURL: userOverrideModelDataURL(.imeModeCHS))
group.leave()
}
_ = group.wait(timeout: .distantFuture)
group.notify(queue: DispatchQueue.main) {}
}
public static func removeUnigramsFromUserOverrideModel(_ mode: InputMode) {
public static func removeUnigramsFromUserOverrideModel(_ mode: Shared.InputMode) {
switch mode {
case .imeModeCHS:
Self.uomCHT.bleachUnigrams(saveCallback: { Self.uomCHT.saveData() })
@ -585,12 +585,12 @@ public enum LMMgr {
}
}
public static func clearUserOverrideModelData(_ mode: InputMode = .imeModeNULL) {
public static func clearUserOverrideModelData(_ mode: Shared.InputMode = .imeModeNULL) {
switch mode {
case .imeModeCHS:
Self.uomCHS.clearData(withURL: userOverrideModelDataURL(InputMode.imeModeCHS))
Self.uomCHS.clearData(withURL: userOverrideModelDataURL(.imeModeCHS))
case .imeModeCHT:
Self.uomCHT.clearData(withURL: userOverrideModelDataURL(InputMode.imeModeCHT))
Self.uomCHT.clearData(withURL: userOverrideModelDataURL(.imeModeCHT))
case .imeModeNULL:
break
}

View File

@ -48,11 +48,11 @@ public class ctlCandidateIMK: IMKCandidates, ctlCandidateProtocol {
attributes?[NSAttributedString.Key.font] = candidateFont
if PrefMgr.shared.handleDefaultCandidateFontsByLangIdentifier {
switch IMEApp.currentInputMode {
case InputMode.imeModeCHS:
case .imeModeCHS:
if #available(macOS 12.0, *) {
attributes?[NSAttributedString.Key.languageIdentifier] = "zh-Hans" as AnyObject
}
case InputMode.imeModeCHT:
case .imeModeCHT:
if #available(macOS 12.0, *) {
attributes?[NSAttributedString.Key.languageIdentifier] =
(PrefMgr.shared.shiftJISShinjitaiOutputEnabled || PrefMgr.shared.chineseConversionEnabled)

View File

@ -136,11 +136,11 @@ private class vwrCandidateUniversal: NSView {
func ensureLangIdentifier(for attr: inout [NSAttributedString.Key: AnyObject]) {
if PrefMgr.shared.handleDefaultCandidateFontsByLangIdentifier {
switch IMEApp.currentInputMode {
case InputMode.imeModeCHS:
case .imeModeCHS:
if #available(macOS 12.0, *) {
attr[.languageIdentifier] = "zh-Hans" as AnyObject
}
case InputMode.imeModeCHT:
case .imeModeCHT:
if #available(macOS 12.0, *) {
attr[.languageIdentifier] =
(PrefMgr.shared.shiftJISShinjitaiOutputEnabled || PrefMgr.shared.chineseConversionEnabled)
@ -160,9 +160,9 @@ private class vwrCandidateUniversal: NSView {
}
// The background color of the highlightened candidate
switch IMEApp.currentInputMode {
case InputMode.imeModeCHS:
case .imeModeCHS:
result = NSColor.systemRed
case InputMode.imeModeCHT:
case .imeModeCHT:
result = NSColor.systemBlue
default: break
}

View File

@ -168,14 +168,14 @@ class ctlInputMethod: IMKInputController {
override func setValue(_ value: Any!, forTag tag: Int, client sender: Any!) {
_ = tag //
_ = sender //
var newInputMode = InputMode(rawValue: value as? String ?? "") ?? InputMode.imeModeNULL
var newInputMode = Shared.InputMode(rawValue: value as? String ?? "") ?? Shared.InputMode.imeModeNULL
switch newInputMode {
case InputMode.imeModeCHS:
newInputMode = InputMode.imeModeCHS
case InputMode.imeModeCHT:
newInputMode = InputMode.imeModeCHT
case .imeModeCHS:
newInputMode = .imeModeCHS
case .imeModeCHT:
newInputMode = .imeModeCHT
default:
newInputMode = InputMode.imeModeNULL
newInputMode = .imeModeNULL
}
LMMgr.loadDataModel(newInputMode)
@ -195,7 +195,7 @@ class ctlInputMethod: IMKInputController {
/// InputMode
/// IME UserPrefs
var inputMode: InputMode = IMEApp.currentInputMode {
var inputMode: Shared.InputMode = IMEApp.currentInputMode {
willSet {
/// Prefs IME
IMEApp.currentInputMode = newValue

View File

@ -8,6 +8,8 @@
// marks, or product names of Contributor, except as required to fulfill notice
// requirements defined in MIT License.
import Shared
// MARK: - KeyHandler Delegate
extension ctlInputMethod: KeyHandlerDelegate {
@ -26,9 +28,8 @@ extension ctlInputMethod: KeyHandlerDelegate {
-> Bool
{
guard state.type == .ofMarking else { return false }
let refInputModeReversed: InputMode =
(inputMode == InputMode.imeModeCHT)
? InputMode.imeModeCHS : InputMode.imeModeCHT
let refInputModeReversed: Shared.InputMode =
(inputMode == .imeModeCHT) ? .imeModeCHS : .imeModeCHT
if !LMMgr.writeUserPhrase(
state.data.userPhraseDumped, inputMode: inputMode,
areWeDuplicating: state.data.chkIfUserPhraseExists,

View File

@ -192,9 +192,9 @@ extension ctlInputMethod {
let finalReturnFont: NSFont =
{
switch IMEApp.currentInputMode {
case InputMode.imeModeCHS:
case .imeModeCHS:
return CTFontCreateUIFontForLanguage(.system, size, "zh-Hans" as CFString)
case InputMode.imeModeCHT:
case .imeModeCHT:
return (PrefMgr.shared.shiftJISShinjitaiOutputEnabled || PrefMgr.shared.chineseConversionEnabled)
? CTFontCreateUIFontForLanguage(.system, size, "ja" as CFString)
: CTFontCreateUIFontForLanguage(.system, size, "zh-Hant" as CFString)

View File

@ -48,7 +48,7 @@ extension ctlInputMethod {
useCNS11643SupportItem.keyEquivalentModifierMask = [.command, .control]
useCNS11643SupportItem.state = PrefMgr.shared.cns11643Enabled.state
if IMEApp.currentInputMode == InputMode.imeModeCHT {
if IMEApp.currentInputMode == .imeModeCHT {
let chineseConversionItem = menu.addItem(
withTitle: NSLocalizedString("Force KangXi Writing", comment: ""),
action: #selector(toggleChineseConverter(_:)), keyEquivalent: PrefMgr.shared.usingHotKeyKangXi ? "K" : ""

View File

@ -74,27 +74,11 @@ NSApp.run()
// MARK: - Top-level Enums relating to Input Mode and Language Supports.
// The type of input modes.
public enum InputMode: String, CaseIterable {
case imeModeCHS = "org.atelierInmu.inputmethod.vChewing.IMECHS"
case imeModeCHT = "org.atelierInmu.inputmethod.vChewing.IMECHT"
case imeModeNULL = ""
var reversed: InputMode {
switch self {
case .imeModeCHS:
return .imeModeCHT
case .imeModeCHT:
return .imeModeCHS
case .imeModeNULL:
return .imeModeNULL
}
}
}
public enum IMEApp {
// MARK: -
public static var currentInputMode: InputMode = .init(rawValue: PrefMgr.shared.mostRecentInputMode) ?? .imeModeNULL
public static var currentInputMode: Shared.InputMode =
.init(rawValue: PrefMgr.shared.mostRecentInputMode) ?? .imeModeNULL
/// Fart or Beep?
static func buzz() {