public import ParcelConfig public import ParcelCore public struct EngineCallbackInvocation: Equatable, Sendable { public let event: CallbackEvent public let arguments: [String] public let environment: [String: String] public init(event: CallbackEvent, arguments: [String], environment: [String: String]) { self.event = event self.arguments = arguments self.environment = environment } } extension ParcelEngine { func publishCallbacks(previous: State, current: State, isConfigurationRefresh: Bool = false) { defer { callbacksPrimed = true } guard callbacksPrimed, !isConfigurationRefresh, !configuration.callbacks.commands.isEmpty, let onCallback else { return } for invocation in EngineCallbackRouter.invocations( previous: previous, current: current, configuration: configuration.callbacks) { onCallback(invocation) } } } public enum EngineCallbackRouter { public static func invocations( previous: State, current: State, configuration: CallbackConfiguration ) -> [EngineCallbackInvocation] { guard configuration.commands.values.contains(where: { !$0.isEmpty }) else { return [] } var result: [EngineCallbackInvocation] = [] func append(_ event: CallbackEvent, state: State, window: WindowID? = nil, workspace: WorkspaceKey? = nil) { guard let arguments = configuration.commands[event], !arguments.isEmpty else { return } var environment = ["PARCEL_EVENT": event.rawValue] let key = workspace ?? window.flatMap { state.workspace(containing: $0) } ?? state.activeWorkspace if let window, let value = state.windows[window] { environment["PARCEL_WINDOW_ID"] = String(window.rawValue) environment["PARCEL_APP_PID"] = String(value.app.pid) environment["PARCEL_BUNDLE_ID"] = value.app.bundleID environment["PARCEL_WINDOW_TITLE"] = value.title } if let key { environment["PARCEL_DISPLAY_ID"] = key.display.rawValue environment["PARCEL_SPACE_ID"] = String(key.space.rawValue) environment["PARCEL_LAYOUT"] = state.workspaces[key]?.layout.rawValue } result.append(EngineCallbackInvocation(event: event, arguments: arguments, environment: environment)) } let before = Set(previous.windows.keys) let after = Set(current.windows.keys) for window in after.subtracting(before).sorted() { append(.windowOpened, state: current, window: window) } for window in before.subtracting(after).sorted() { append(.windowClosed, state: previous, window: window) } if previous.focusedWindow != current.focusedWindow { append(.focusChanged, state: current, window: current.focusedWindow) } for key in Set(previous.workspaces.keys).intersection(current.workspaces.keys).sorted() where previous.workspaces[key]?.layout != current.workspaces[key]?.layout { append(.layoutChanged, state: current, window: current.workspaces[key]?.focus, workspace: key) } for display in Set(previous.activeSpaces.keys).intersection(current.activeSpaces.keys).sorted() where previous.activeSpaces[display] != current.activeSpaces[display] { if let space = current.activeSpaces[display] { let key = WorkspaceKey(display: display, space: space) append(.spaceChanged, state: current, window: current.workspaces[key]?.focus, workspace: key) } } return result } }