SessionCtl & LMMgr // Simplify the handling of openUserDictFile().

This commit is contained in:
ShikiSuen 2022-12-04 19:01:54 +08:00
parent 291b4c9237
commit 19c2e24948
2 changed files with 72 additions and 90 deletions

View File

@ -200,16 +200,16 @@ public enum LMMgr {
public static func loadUserPhrasesData(type: vChewingLM.ReplacableUserDataType? = nil) { public static func loadUserPhrasesData(type: vChewingLM.ReplacableUserDataType? = nil) {
guard let type = type else { guard let type = type else {
Self.lmCHT.loadUserPhrasesData( Self.lmCHT.loadUserPhrasesData(
path: userPhrasesDataURL(.imeModeCHT).path, path: userDictDataURL(mode: .imeModeCHT, type: .thePhrases).path,
filterPath: userFilteredDataURL(.imeModeCHT).path filterPath: userDictDataURL(mode: .imeModeCHT, type: .theFilter).path
) )
Self.lmCHS.loadUserPhrasesData( Self.lmCHS.loadUserPhrasesData(
path: userPhrasesDataURL(.imeModeCHS).path, path: userDictDataURL(mode: .imeModeCHS, type: .thePhrases).path,
filterPath: userFilteredDataURL(.imeModeCHS).path filterPath: userDictDataURL(mode: .imeModeCHS, type: .theFilter).path
) )
Self.lmCHT.loadUserSymbolData(path: userSymbolDataURL(.imeModeCHT).path) Self.lmCHT.loadUserSymbolData(path: userDictDataURL(mode: .imeModeCHT, type: .theSymbols).path)
Self.lmCHS.loadUserSymbolData(path: userSymbolDataURL(.imeModeCHS).path) Self.lmCHS.loadUserSymbolData(path: userDictDataURL(mode: .imeModeCHS, type: .theSymbols).path)
if PrefMgr.shared.associatedPhrasesEnabled { Self.loadUserAssociatesData() } if PrefMgr.shared.associatedPhrasesEnabled { Self.loadUserAssociatesData() }
if PrefMgr.shared.phraseReplacementEnabled { Self.loadUserPhraseReplacement() } if PrefMgr.shared.phraseReplacementEnabled { Self.loadUserPhraseReplacement() }
if PrefMgr.shared.useSCPCTypingMode { Self.loadUserSCPCSequencesData() } if PrefMgr.shared.useSCPCTypingMode { Self.loadUserSCPCSequencesData() }
@ -223,38 +223,42 @@ public enum LMMgr {
switch type { switch type {
case .thePhrases, .theFilter: case .thePhrases, .theFilter:
Self.lmCHT.loadUserPhrasesData( Self.lmCHT.loadUserPhrasesData(
path: userPhrasesDataURL(.imeModeCHT).path, path: userDictDataURL(mode: .imeModeCHT, type: .thePhrases).path,
filterPath: userFilteredDataURL(.imeModeCHT).path filterPath: userDictDataURL(mode: .imeModeCHT, type: .theFilter).path
) )
Self.lmCHS.loadUserPhrasesData( Self.lmCHS.loadUserPhrasesData(
path: userPhrasesDataURL(.imeModeCHS).path, path: userDictDataURL(mode: .imeModeCHS, type: .thePhrases).path,
filterPath: userFilteredDataURL(.imeModeCHS).path filterPath: userDictDataURL(mode: .imeModeCHS, type: .theFilter).path
) )
case .theReplacements: case .theReplacements:
if PrefMgr.shared.phraseReplacementEnabled { Self.loadUserPhraseReplacement() } if PrefMgr.shared.phraseReplacementEnabled { Self.loadUserPhraseReplacement() }
case .theAssociates: case .theAssociates:
if PrefMgr.shared.associatedPhrasesEnabled { Self.loadUserAssociatesData() } if PrefMgr.shared.associatedPhrasesEnabled { Self.loadUserAssociatesData() }
case .theSymbols: case .theSymbols:
Self.lmCHT.loadUserSymbolData(path: userSymbolDataURL(.imeModeCHT).path) Self.lmCHT.loadUserSymbolData(
Self.lmCHS.loadUserSymbolData(path: userSymbolDataURL(.imeModeCHS).path) path: Self.userDictDataURL(mode: .imeModeCHT, type: .theSymbols).path
)
Self.lmCHS.loadUserSymbolData(
path: Self.userDictDataURL(mode: .imeModeCHS, type: .theSymbols).path
)
} }
} }
public static func loadUserAssociatesData() { public static func loadUserAssociatesData() {
Self.lmCHT.loadUserAssociatesData( Self.lmCHT.loadUserAssociatesData(
path: Self.userAssociatesDataURL(.imeModeCHT).path path: Self.userDictDataURL(mode: .imeModeCHT, type: .theAssociates).path
) )
Self.lmCHS.loadUserAssociatesData( Self.lmCHS.loadUserAssociatesData(
path: Self.userAssociatesDataURL(.imeModeCHS).path path: Self.userDictDataURL(mode: .imeModeCHS, type: .theAssociates).path
) )
} }
public static func loadUserPhraseReplacement() { public static func loadUserPhraseReplacement() {
Self.lmCHT.loadReplacementsData( Self.lmCHT.loadReplacementsData(
path: Self.userReplacementsDataURL(.imeModeCHT).path path: Self.userDictDataURL(mode: .imeModeCHT, type: .theReplacements).path
) )
Self.lmCHS.loadReplacementsData( Self.lmCHS.loadReplacementsData(
path: Self.userReplacementsDataURL(.imeModeCHS).path path: Self.userDictDataURL(mode: .imeModeCHS, type: .theReplacements).path
) )
} }
@ -317,45 +321,24 @@ public enum LMMgr {
// MARK: - 使 // MARK: - 使
// Swift appendingPathComponent URL .path // Swift appendingPathComponent URL
/// 使 /// 使
/// - Parameter mode: /// - Parameters:
/// - mode:
/// - type:
/// - Returns: URL /// - Returns: URL
public static func userPhrasesDataURL(_ mode: Shared.InputMode) -> URL { public static func userDictDataURL(mode: Shared.InputMode, type: vChewingLM.ReplacableUserDataType) -> URL {
let fileName = (mode == .imeModeCHT) ? "userdata-cht.txt" : "userdata-chs.txt" var fileName: String = {
return URL(fileURLWithPath: dataFolderPath(isDefaultFolder: false)).appendingPathComponent(fileName) switch type {
} case .thePhrases: return "userdata"
case .theFilter: return "exclude-phrases"
/// 使 case .theReplacements: return "phrases-replacement"
/// - Parameter mode: case .theAssociates: return "associatedPhrases"
/// - Returns: URL case .theSymbols: return "usersymbolphrases"
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) fileName.append((mode == .imeModeCHT) ? "-cht.txt" : "-chs.txt")
}
/// 使
/// - Parameter mode:
/// - Returns: URL
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: 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: Shared.InputMode) -> URL {
let fileName = (mode == .imeModeCHT) ? "phrases-replacement-cht.txt" : "phrases-replacement-chs.txt"
return URL(fileURLWithPath: dataFolderPath(isDefaultFolder: false)).appendingPathComponent(fileName) return URL(fileURLWithPath: dataFolderPath(isDefaultFolder: false)).appendingPathComponent(fileName)
} }
@ -426,20 +409,27 @@ public enum LMMgr {
/// CandidateNode UserOverrideModel /// CandidateNode UserOverrideModel
/// ///
/// ///
if !ensureFileExists(userPhrasesDataURL(mode), deployTemplate: kTemplateNameUserPhrases) var failed = false
|| !ensureFileExists( caseCheck: for type in vChewingLM.ReplacableUserDataType.allCases {
userAssociatesDataURL(mode), let templateName = Self.templateName(for: type, mode: mode)
deployTemplate: mode == .imeModeCHS ? kTemplateNameUserAssociatesCHS : kTemplateNameUserAssociatesCHT if !ensureFileExists(userDictDataURL(mode: mode, type: type), deployTemplate: templateName) {
) failed = true
|| !ensureFileExists(userSCPCSequencesURL(mode)) break caseCheck
|| !ensureFileExists(userFilteredDataURL(mode), deployTemplate: kTemplateNameUserFilterList) }
|| !ensureFileExists(userReplacementsDataURL(mode), deployTemplate: kTemplateNameUserReplacements)
|| !ensureFileExists(userSymbolDataURL(mode), deployTemplate: kTemplateNameUserSymbolPhrases)
{
return false
} }
failed = failed || !ensureFileExists(userSCPCSequencesURL(mode))
return !failed
}
return true private static func templateName(for type: vChewingLM.ReplacableUserDataType, mode: Shared.InputMode) -> String {
switch type {
case .thePhrases: return kTemplateNameUserPhrases
case .theFilter: return kTemplateNameUserFilterList
case .theReplacements: return kTemplateNameUserReplacements
case .theSymbols: return kTemplateNameUserSymbolPhrases
case .theAssociates:
return mode == .imeModeCHS ? kTemplateNameUserAssociatesCHS : kTemplateNameUserAssociatesCHT
}
} }
// MARK: - 使 // MARK: - 使
@ -578,7 +568,8 @@ public enum LMMgr {
return false return false
} }
let theURL = areWeDeleting ? userFilteredDataURL(mode) : userPhrasesDataURL(mode) let theType: vChewingLM.ReplacableUserDataType = areWeDeleting ? .theFilter : .thePhrases
let theURL = userDictDataURL(mode: mode, type: theType)
if areWeDuplicating, !areWeDeleting { if areWeDuplicating, !areWeDeleting {
// Do not use ASCII characters to comment here. // Do not use ASCII characters to comment here.
@ -640,6 +631,13 @@ public enum LMMgr {
return true return true
} }
public static func openUserDictFile(type: vChewingLM.ReplacableUserDataType, dual: Bool = false, alt: Bool) {
let app: String = alt ? "" : "Finder"
openPhraseFile(fromURL: userDictDataURL(mode: IMEApp.currentInputMode, type: type), app: app)
guard dual else { return }
openPhraseFile(fromURL: userDictDataURL(mode: IMEApp.currentInputMode.reversed, type: type), app: app)
}
/// ///
/// - Remark: App Sandbox app "vim" Sandbox /// - Remark: App Sandbox app "vim" Sandbox
/// - Parameters: /// - Parameters:

View File

@ -22,9 +22,9 @@ extension Bool {
// //
extension SessionCtl { extension SessionCtl {
public override func menu() -> NSMenu! { var optionKeyPressed: Bool { NSEvent.modifierFlags.contains(.option) }
let optionKeyPressed = NSEvent.modifierFlags.contains(.option)
override public func menu() -> NSMenu! {
let menu = NSMenu(title: "Input Method Menu") let menu = NSMenu(title: "Input Method Menu")
let useSCPCTypingModeItem = menu.addItem( let useSCPCTypingModeItem = menu.addItem(
@ -364,39 +364,23 @@ extension SessionCtl {
} }
@objc public func openUserPhrases(_: Any? = nil) { @objc public func openUserPhrases(_: Any? = nil) {
LMMgr.openPhraseFile(fromURL: LMMgr.userPhrasesDataURL(IMEApp.currentInputMode)) LMMgr.openUserDictFile(type: .thePhrases, dual: optionKeyPressed, alt: optionKeyPressed)
if NSEvent.modifierFlags.contains(.option) {
LMMgr.openPhraseFile(fromURL: LMMgr.userPhrasesDataURL(IMEApp.currentInputMode.reversed))
}
} }
@objc public func openExcludedPhrases(_: Any? = nil) { @objc public func openExcludedPhrases(_: Any? = nil) {
LMMgr.openPhraseFile(fromURL: LMMgr.userFilteredDataURL(IMEApp.currentInputMode)) LMMgr.openUserDictFile(type: .theFilter, dual: optionKeyPressed, alt: optionKeyPressed)
if NSEvent.modifierFlags.contains(.option) {
LMMgr.openPhraseFile(fromURL: LMMgr.userFilteredDataURL(IMEApp.currentInputMode.reversed))
}
} }
@objc public func openUserSymbols(_: Any? = nil) { @objc public func openUserSymbols(_: Any? = nil) {
LMMgr.openPhraseFile(fromURL: LMMgr.userSymbolDataURL(IMEApp.currentInputMode)) LMMgr.openUserDictFile(type: .theSymbols, dual: optionKeyPressed, alt: optionKeyPressed)
if NSEvent.modifierFlags.contains(.option) {
LMMgr.openPhraseFile(fromURL: LMMgr.userSymbolDataURL(IMEApp.currentInputMode.reversed))
}
} }
@objc public func openPhraseReplacement(_: Any? = nil) { @objc public func openPhraseReplacement(_: Any? = nil) {
LMMgr.openPhraseFile(fromURL: LMMgr.userReplacementsDataURL(IMEApp.currentInputMode)) LMMgr.openUserDictFile(type: .theReplacements, dual: optionKeyPressed, alt: optionKeyPressed)
if NSEvent.modifierFlags.contains(.option) {
LMMgr.openPhraseFile(fromURL: LMMgr.userReplacementsDataURL(IMEApp.currentInputMode.reversed))
}
} }
@objc public func openAssociatedPhrases(_: Any? = nil) { @objc public func openAssociatedPhrases(_: Any? = nil) {
LMMgr.openPhraseFile(fromURL: LMMgr.userAssociatesDataURL(IMEApp.currentInputMode)) LMMgr.openUserDictFile(type: .theAssociates, dual: optionKeyPressed, alt: optionKeyPressed)
if NSEvent.modifierFlags.contains(.option) {
LMMgr.openPhraseFile(
fromURL: LMMgr.userAssociatesDataURL(IMEApp.currentInputMode.reversed))
}
} }
@objc public func reloadUserPhrasesData(_: Any? = nil) { @objc public func reloadUserPhrasesData(_: Any? = nil) {