From cd33a21587fa18f8d123dbe3b70524802d90a3cb Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Wed, 23 Aug 2023 00:00:09 +0800 Subject: [PATCH] Repo // Introducing Broadcaster for KVO operations. --- .../Sources/Shared/Broadcaster.swift | 17 +++++++++++++++++ Source/Modules/AppDelegate.swift | 2 +- Source/Modules/LMMgr_Utilities.swift | 2 +- .../WindowControllers/CtlPrefWindow.swift | 6 ++++++ .../WindowControllers/CtlRevLookupWindow.swift | 9 ++++++++- 5 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 Packages/vChewing_Shared/Sources/Shared/Broadcaster.swift diff --git a/Packages/vChewing_Shared/Sources/Shared/Broadcaster.swift b/Packages/vChewing_Shared/Sources/Shared/Broadcaster.swift new file mode 100644 index 00000000..d5f46e5b --- /dev/null +++ b/Packages/vChewing_Shared/Sources/Shared/Broadcaster.swift @@ -0,0 +1,17 @@ +// (c) 2011 and onwards The OpenVanilla Project (MIT License). +// All possible vChewing-specific modifications are of: +// (c) 2021 and onwards The vChewing Project (MIT-NTL License). +// ==================== +// This code is released under the MIT license (SPDX-License-Identifier: MIT) +// ... with NTL restriction stating that: +// No trademark license is granted to use the trade names, trademarks, service +// marks, or product names of Contributor, except as required to fulfill notice +// requirements defined in MIT License. + +import AppKit + +@objcMembers public class Broadcaster: NSObject { + public static var shared = Broadcaster() + public dynamic var eventForReloadingPhraseEditor = UUID() + public dynamic var eventForReloadingRevLookupData = UUID() +} diff --git a/Source/Modules/AppDelegate.swift b/Source/Modules/AppDelegate.swift index e0e96849..f3bc53d7 100644 --- a/Source/Modules/AppDelegate.swift +++ b/Source/Modules/AppDelegate.swift @@ -47,7 +47,7 @@ extension AppDelegate { DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) { if #available(macOS 10.15, *) { FileObserveProject.shared.touch() } if PrefMgr.shared.phraseEditorAutoReloadExternalModifications { - CtlPrefWindow.shared?.updatePhraseEditor() + Broadcaster.shared.eventForReloadingPhraseEditor = .init() } } } diff --git a/Source/Modules/LMMgr_Utilities.swift b/Source/Modules/LMMgr_Utilities.swift index 1b4db4ba..ad2e7bf2 100644 --- a/Source/Modules/LMMgr_Utilities.swift +++ b/Source/Modules/LMMgr_Utilities.swift @@ -280,7 +280,7 @@ public extension LMMgr { // by the current application itself, requiring additional manual loading process here. if #available(macOS 10.15, *) { FileObserveProject.shared.touch() } if PrefMgr.shared.phraseEditorAutoReloadExternalModifications { - CtlPrefWindow.shared?.updatePhraseEditor() + Broadcaster.shared.eventForReloadingPhraseEditor = .init() } loadUserPhrasesData(type: .thePhrases) } diff --git a/Source/Modules/WindowControllers/CtlPrefWindow.swift b/Source/Modules/WindowControllers/CtlPrefWindow.swift index b1ce2047..d26cc32f 100644 --- a/Source/Modules/WindowControllers/CtlPrefWindow.swift +++ b/Source/Modules/WindowControllers/CtlPrefWindow.swift @@ -61,6 +61,8 @@ class CtlPrefWindow: NSWindowController, NSWindowDelegate { public static var shared: CtlPrefWindow? + @objc var observation: NSKeyValueObservation? + static func show() { let resetPhraseEditor: Bool = shared?.window == nil || !(shared?.window?.isVisible ?? false) || shared == nil if shared == nil { shared = CtlPrefWindow(windowNibName: "frmPrefWindow") } @@ -83,6 +85,10 @@ class CtlPrefWindow: NSWindowController, NSWindowDelegate { super.windowDidLoad() window?.setPosition(vertical: .top, horizontal: .right, padding: 20) + observation = Broadcaster.shared.observe(\.eventForReloadingPhraseEditor, options: [.new]) { _, _ in + self.updatePhraseEditor() + } + chkFartSuppressor.isHidden = !Date.isTodayTheDate(from: 0401) chkFartSuppressor.isEnabled = !chkFartSuppressor.isHidden diff --git a/Source/Modules/WindowControllers/CtlRevLookupWindow.swift b/Source/Modules/WindowControllers/CtlRevLookupWindow.swift index be37568e..67f6a10c 100644 --- a/Source/Modules/WindowControllers/CtlRevLookupWindow.swift +++ b/Source/Modules/WindowControllers/CtlRevLookupWindow.swift @@ -11,6 +11,7 @@ import LangModelAssembly class CtlRevLookupWindow: NSWindowController, NSWindowDelegate { static var shared: CtlRevLookupWindow? + @objc var observation: NSKeyValueObservation? static func show() { if shared == nil { Self.shared = .init(window: FrmRevLookupWindow()) } @@ -20,10 +21,16 @@ class CtlRevLookupWindow: NSWindowController, NSWindowDelegate { window.setPosition(vertical: .bottom, horizontal: .right, padding: 20) window.orderFrontRegardless() // 逼著視窗往最前方顯示 window.level = .statusBar - window.titlebarAppearsTransparent = true shared.showWindow(shared) NSApp.popup() } + + override func windowDidLoad() { + super.windowDidLoad() + observation = Broadcaster.shared.observe(\.eventForReloadingRevLookupData, options: [.new]) { _, _ in + FrmRevLookupWindow.reloadData() + } + } } class FrmRevLookupWindow: NSWindow {