Repo // Introducing Broadcaster for KVO operations.

This commit is contained in:
ShikiSuen 2023-08-23 00:00:09 +08:00
parent a56b55125a
commit cd33a21587
5 changed files with 33 additions and 3 deletions

View File

@ -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()
}

View File

@ -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()
}
}
}

View File

@ -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)
}

View File

@ -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

View File

@ -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 {