62 lines
1.7 KiB
Swift
62 lines
1.7 KiB
Swift
import Foundation
|
|
|
|
@MainActor
|
|
final class PHBlock: ObservableObject, Decodable, Identifiable {
|
|
let id = UUID()
|
|
|
|
let name: String
|
|
let _command: String?
|
|
let textName: String?
|
|
@Published var text: PHThemeText = .default
|
|
let styleName: String?
|
|
@Published var style: PHThemeStyle = .default
|
|
let refresh: Double?
|
|
let centered: Bool?
|
|
var debug: Bool = false
|
|
|
|
var command: String {
|
|
_command ?? PHPaths.configDirectory.appending(path: "scripts/\(name)").relativePath
|
|
}
|
|
|
|
var visible: Bool {
|
|
label != nil && !label!.isEmpty
|
|
}
|
|
|
|
/// System events that should trigger a refresh (e.g. `["volume", "network"]`).
|
|
/// `nil` when omitted from config.
|
|
let events: [PHEvent]?
|
|
|
|
/// Variables made visible to block scripts. Defaults to phbar's process
|
|
/// environment; `BarController` overlays the `[env]` section on top.
|
|
var environment: PHEnvironment = .process
|
|
|
|
/// Event source registry used to subscribe to system events. Defaults to the
|
|
/// shared instance; inject a custom one for testing.
|
|
var registry: PHEventRegistry = .shared
|
|
|
|
@Published var label: String?
|
|
|
|
/// The repeating interval task, if any.
|
|
var intervalTask: Task<Void, Never>?
|
|
|
|
/// Active event subscriptions, torn down in `stopAutoRefresh`.
|
|
var subscriptions: [PHEventSubscription] = []
|
|
|
|
/// Guards against stacking concurrent updates during rapid event bursts
|
|
/// (e.g. dragging the volume slider fires many events in quick succession).
|
|
var updateScheduled = false
|
|
|
|
private enum CodingKeys: String, CodingKey {
|
|
case name
|
|
case _command = "command"
|
|
case textName = "text"
|
|
case styleName = "style"
|
|
case refresh, events, centered
|
|
}
|
|
|
|
deinit {
|
|
intervalTask?.cancel()
|
|
subscriptions.forEach { $0.cancel() }
|
|
}
|
|
}
|