65 lines
1.4 KiB
Swift
65 lines
1.4 KiB
Swift
import AppKit
|
|
|
|
@MainActor
|
|
final class AppDelegate: NSObject, NSApplicationDelegate {
|
|
var controllers: [BarController] = []
|
|
var windows: [BarWindow] = []
|
|
private var observers: [IPCObserver] = []
|
|
|
|
/// Create one bar controller per screen. Each screen resolves its own
|
|
/// theme, window and block set from the config, so refresh state and layout stay
|
|
/// independent across monitors.
|
|
init(
|
|
config: PHConfig,
|
|
screens: [NSScreen],
|
|
debug: Bool
|
|
) throws {
|
|
super.init()
|
|
|
|
for screen in screens {
|
|
let theme = try PHTheme.load(config.theme(for: screen))
|
|
let blocks = try PHBlock.load(config.blocks(for: screen))
|
|
|
|
controllers.append(
|
|
BarController(
|
|
config: config,
|
|
screen: screen,
|
|
theme: theme,
|
|
blocks: blocks,
|
|
debug: debug
|
|
)
|
|
)
|
|
}
|
|
}
|
|
|
|
func applicationDidFinishLaunching(_ notification: Notification) {
|
|
for controller in controllers {
|
|
let window = BarWindow(controller: controller)
|
|
window.orderFront(nil)
|
|
windows.append(window)
|
|
controller.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() {
|
|
for controller in controllers {
|
|
controller.refresh()
|
|
}
|
|
}
|
|
|
|
func applicationWillTerminate(_ notification: Notification) {
|
|
for controller in controllers {
|
|
controller.stopAutoRefresh()
|
|
}
|
|
observers.removeAll()
|
|
}
|
|
}
|