portico/Sources/Portico/App/SceneHandle.swift

42 lines
1.5 KiB
Swift

import Adw
/// A live window attached by a ``MountableScene``, plus the reactive resources
/// mounted into it.
@_spi(Portico) @MainActor public final class SceneHandle {
/// The presented window.
@_spi(Portico) public let window: Adw.ApplicationWindow
private let registry: NodeRegistry
private var resourcesReleased = false
/// `true` once ``dismantle()`` has destroyed the window.
@_spi(Portico) public private(set) var isDismantled = false
init(window: Adw.ApplicationWindow, registry: NodeRegistry) {
self.window = window
self.registry = registry
}
/// Cancels every reactive subscription mounted into this window, leaving the
/// window itself untouched. Called from the window's `close-request` handler,
/// where GTK finishes the close. Idempotent.
func releaseResources() {
guard !resourcesReleased else { return }
resourcesReleased = true
registry.teardown()
}
/// Releases reactive resources, retires the window's shutdown gate, and
/// destroys the window. Used when the runtime swaps to a different scene
/// branch. Idempotent.
@_spi(Portico) public func dismantle() {
guard !isDismantled else { return }
isDismantled = true
releaseResources()
PorticoRuntime._retireShutdownGate(on: window)
window.destroy()
}
/// Re-presents the window, for a second `activate` on a single-instance app.
@_spi(Portico) public func present() { window.present() }
}