diff --git a/Packages/vChewing_MainAssembly/Sources/MainAssembly/AppDelegate.swift b/Packages/vChewing_MainAssembly/Sources/MainAssembly/AppDelegate.swift index 3ce948c4..dcb98342 100644 --- a/Packages/vChewing_MainAssembly/Sources/MainAssembly/AppDelegate.swift +++ b/Packages/vChewing_MainAssembly/Sources/MainAssembly/AppDelegate.swift @@ -47,7 +47,6 @@ extension AppDelegate { // 先執行 initUserLangModels() 可以在目標辭典檔案不存在的情況下先行生成空白範本檔案。 if PrefMgr.shared.shouldAutoReloadUserDataFiles || forced { LMMgr.initUserLangModels() } DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) { - if #available(macOS 10.15, *) { FileObserveProject.shared.touch() } if PrefMgr.shared.phraseEditorAutoReloadExternalModifications { Broadcaster.shared.eventForReloadingPhraseEditor = .init() } diff --git a/Packages/vChewing_MainAssembly/Sources/MainAssembly/LangModelManager/LMMgr_KimoDataParser.swift b/Packages/vChewing_MainAssembly/Sources/MainAssembly/LangModelManager/LMMgr_KimoDataParser.swift index 0e9ff265..c34d3456 100644 --- a/Packages/vChewing_MainAssembly/Sources/MainAssembly/LangModelManager/LMMgr_KimoDataParser.swift +++ b/Packages/vChewing_MainAssembly/Sources/MainAssembly/LangModelManager/LMMgr_KimoDataParser.swift @@ -8,6 +8,7 @@ import Foundation import LineReader +import Shared public extension LMMgr { /// 匯入自奇摩輸入法使用者自訂詞資料庫匯出的 TXT 檔案。 @@ -63,7 +64,11 @@ public extension LMMgr { fileHandlerCHT.seekToEndOfFile() fileHandlerCHT.write(outputDataCHT) - return allPhrasesCHT.count + let result = allPhrasesCHT.count + if result > 0 { + Broadcaster.shared.eventForReloadingPhraseEditor = .init() + } + return result } } diff --git a/Packages/vChewing_MainAssembly/Sources/MainAssembly/LangModelManager/LMMgr_Utilities.swift b/Packages/vChewing_MainAssembly/Sources/MainAssembly/LangModelManager/LMMgr_Utilities.swift index 4c690f34..a1d3426a 100644 --- a/Packages/vChewing_MainAssembly/Sources/MainAssembly/LangModelManager/LMMgr_Utilities.swift +++ b/Packages/vChewing_MainAssembly/Sources/MainAssembly/LangModelManager/LMMgr_Utilities.swift @@ -240,7 +240,6 @@ public extension LMMgr { } // The new FolderMonitor module does NOT monitor cases that files are modified // by the current application itself, requiring additional manual loading process here. - if #available(macOS 10.15, *) { FileObserveProject.shared.touch() } if PrefMgr.shared.phraseEditorAutoReloadExternalModifications { Broadcaster.shared.eventForReloadingPhraseEditor = .init() } diff --git a/Packages/vChewing_PhraseEditorUI/Sources/PhraseEditorUI/PhraseEditorUI.swift b/Packages/vChewing_PhraseEditorUI/Sources/PhraseEditorUI/PhraseEditorUI.swift index 8111848d..3a65ee31 100644 --- a/Packages/vChewing_PhraseEditorUI/Sources/PhraseEditorUI/PhraseEditorUI.swift +++ b/Packages/vChewing_PhraseEditorUI/Sources/PhraseEditorUI/PhraseEditorUI.swift @@ -17,19 +17,15 @@ import SwiftUI private let loc: String = (UserDefaults.current.array(forKey: UserDef.kAppleLanguages.rawValue) as? [String] ?? ["auto"])[0] -@available(macOS 13, *) -extension VwrPhraseEditorUI { - @AppStorage("PhraseEditorAutoReloadExternalModifications") - private static var autoReloadExternalModifications: Bool = true -} - @available(macOS 13, *) public struct VwrPhraseEditorUI: View { static var txtContentStorage: String = NSLocalizedString( "Please select Simplified / Traditional Chinese mode above.", comment: "" ) @Binding public var txtContent: String - @ObservedObject public var fileChangeIndicator = FileObserveProject.shared + @ObservedObject public var fileChangeIndicator = PEReloadEventObserver.shared + @AppStorage("PhraseEditorAutoReloadExternalModifications") + private var autoReloadExternalModifications: Bool = true @State private var selAutoReloadExternalModifications: Bool = UserDefaults.current.bool( forKey: UserDef.kPhraseEditorAutoReloadExternalModifications.rawValue) @State var lblAddPhraseTag1 = PETerms.AddPhrases.locPhrase.localized.0 @@ -252,7 +248,7 @@ public struct VwrPhraseEditorUI: View { .frame(minWidth: 320, minHeight: 240) .onChange(of: fileChangeIndicator.id) { _ in Task { - if Self.autoReloadExternalModifications { update() } + if autoReloadExternalModifications { update() } } } } @@ -291,7 +287,7 @@ public struct VwrPhraseEditorUI: View { Toggle( LocalizedStringKey("This editor only: Auto-reload modifications happened outside of this editor"), isOn: $selAutoReloadExternalModifications.didChange { - Self.autoReloadExternalModifications = selAutoReloadExternalModifications + autoReloadExternalModifications = selAutoReloadExternalModifications } ) .controlSize(.small) diff --git a/Packages/vChewing_Shared/Sources/Shared/Shared.swift b/Packages/vChewing_Shared/Sources/Shared/Shared.swift index 5d91ddd0..6fc456f4 100644 --- a/Packages/vChewing_Shared/Sources/Shared/Shared.swift +++ b/Packages/vChewing_Shared/Sources/Shared/Shared.swift @@ -255,14 +255,21 @@ public enum Shared { } } -// MARK: - Observable Object +// MARK: - PEReloadEventObserver @available(macOS 10.15, *) -public class FileObserveProject: ObservableObject, Equatable { - public static let shared = FileObserveProject() +public class PEReloadEventObserver: ObservableObject, Equatable { + public static let shared = PEReloadEventObserver() + private var observation: NSKeyValueObservation? @Published public var id = UUID().uuidString - public static func == (lhs: FileObserveProject, rhs: FileObserveProject) -> Bool { lhs.id == rhs.id } + public init() { + observation = Broadcaster.shared.observe(\.eventForReloadingPhraseEditor, options: [.new]) { _, _ in + self.touch() + } + } + + public static func == (lhs: PEReloadEventObserver, rhs: PEReloadEventObserver) -> Bool { lhs.id == rhs.id } public func touch() { id = UUID().uuidString