39 lines
984 B
Swift
39 lines
984 B
Swift
import AppKit
|
|
|
|
@MainActor
|
|
final class AppDelegate: NSObject, NSApplicationDelegate {
|
|
var barController: PHController!
|
|
var window: BarWindow!
|
|
private var observers: [IPCObserver] = []
|
|
|
|
init(config: PHConfig, screen: NSScreen, theme: PHTheme, blocks: [PHBlock], debug: Bool = false) {
|
|
super.init()
|
|
|
|
self.barController = PHController(config: config, screen: screen, theme: theme, blocks: blocks)
|
|
barController.debug = debug
|
|
}
|
|
|
|
func applicationDidFinishLaunching(_ notification: Notification) {
|
|
window = BarWindow(controller: barController)
|
|
window.orderFront(nil)
|
|
barController.startAutoRefresh()
|
|
|
|
// Listen for refresh notifications from `phbar refresh`.
|
|
let token = IPC.observe(.refresh) { [weak self] _ in
|
|
Task { @MainActor in
|
|
self?.refresh()
|
|
}
|
|
}
|
|
observers.append(token)
|
|
}
|
|
|
|
func refresh() {
|
|
barController.refresh()
|
|
}
|
|
|
|
func applicationWillTerminate(_ notification: Notification) {
|
|
barController.stopAutoRefresh()
|
|
observers.removeAll()
|
|
}
|
|
}
|