Files
phbar/Sources/phbar/Models/PHBarFactory.swift
T

40 lines
1.3 KiB
Swift

import AppKit
/// Builds a `BarController` for a screen by resolving its theme, blocks, and
/// window from the config.
///
/// Extracted from `AppDelegate` so the exact same resolution drives initial
/// launch, hot-plugged monitors (`syncScreens`), and live reload
/// (`reloadAll` / `reload(screenNamed:)`). Each screen's theme/blocks are
/// resolved independently, so a failure on one screen can be skipped without
/// taking down the others.
@MainActor
struct PHBarFactory {
let config: PHConfig
let environment: PHEnvironment
let debug: Bool
/// Resolve theme + blocks for `screen` and build its controller.
///
/// - Parameter screen: The screen to build a bar for. Its localized name
/// and index in `NSScreen.screens` select any per-monitor config override.
/// - Throws: `PHTheme.load` / `PHBlock.load` errors for the resolved names.
func make(for screen: NSScreen) throws -> BarController {
let name = screen.localizedName
let index = NSScreen.screens.firstIndex(of: screen)
let theme = try PHTheme.load(
config.theme(screenName: name, screenIndex: index),
environment: environment
)
let blocks = try PHBlock.load(config.blocks(screenName: name, screenIndex: index))
return BarController(
config: config,
screen: screen,
theme: theme,
blocks: blocks,
environment: environment,
debug: debug
)
}
}