portico/Sources/Portico/App/OptionalScene.swift

33 lines
1.2 KiB
Swift

import Adw
/// The scene produced by a bare `if` inside ``SceneBuilder``.
///
/// When the condition is false the app presents no window. Because GTK keeps an
/// application alive only while it owns at least one window, a false condition on
/// the *first* evaluation makes the process exit immediately.
public struct OptionalScene<Wrapped: Scene>: Scene {
let wrapped: Wrapped?
init(_ wrapped: Wrapped?) { self.wrapped = wrapped }
public var body: Never { fatalError() }
}
@_spi(Portico) extension OptionalScene: MountableScene {
@_spi(Portico) public var identity: SceneIdentity {
guard let wrapped else { return .empty }
guard let m = wrapped as? MountableScene else { fatalError(Self.message) }
return .present(m.identity)
}
@_spi(Portico) public func attach(to app: Adw.Application) -> SceneHandle? {
guard let wrapped else { return nil }
guard let m = wrapped as? MountableScene else { fatalError(Self.message) }
return m.attach(to: app)
}
private static var message: String {
"Portico: the body of a conditional Scene must conform to MountableScene "
+ "(use ApplicationWindow)"
}
}