Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions Sources/SwiftCrossUI/Scenes/AlertScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,21 @@ public final class AlertSceneNode: SceneGraphNode {
self.scene = scene
}

public func update<Backend: AppBackend>(
_ newScene: AlertScene?,
backend: Backend,
public func updateNode(
_ newScene: NodeScene?,
environment: EnvironmentValues
) -> SceneUpdateResult {
) -> SceneNodeUpdateResult {
if let newScene {
self.scene = newScene
}

return .leafScene()
}

public func update<Backend: AppBackend>(
backend: Backend,
environment: EnvironmentValues
) {
if scene.isPresented, alert == nil {
let alert = backend.createAlert()
backend.updateAlert(
Expand All @@ -75,7 +81,5 @@ public final class AlertSceneNode: SceneGraphNode {
backend.dismissAlert(alert as! Backend.Alert, window: nil)
self.alert = nil
}

return .leafScene()
}
}
21 changes: 15 additions & 6 deletions Sources/SwiftCrossUI/Scenes/Graph/SceneGraphNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,29 @@ public protocol SceneGraphNode: AnyObject {
environment: EnvironmentValues
)

/// Updates the scene's node without committing anything to screen or
/// propagating the update to child views.
///
/// - Parameters:
/// - newScene: The recomputed scene if the update is due to it being
/// recomputed.
/// - environment: The current root-level environment.
/// - Returns: The result of updating the scene node.
func updateNode(
_ newScene: NodeScene?,
environment: EnvironmentValues
) -> SceneNodeUpdateResult

/// Updates the scene.
///
/// Unlike views (which have state), scenes are only ever updated when
/// they're recomputed or immediately after they're created.
///
/// - Parameters:
/// - newScene: The recomputed scene if the update is due to it being
/// recomputed.
/// - backend: The app's backend.
/// - environment: The current root-level environment.
/// - Returns: The result of updating the scene.
/// - environment: The current environment.
func update<Backend: AppBackend>(
_ newScene: NodeScene?,
backend: Backend,
environment: EnvironmentValues
) -> SceneUpdateResult
)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// The result of updating a scene.
public struct SceneUpdateResult: Sendable {
/// The result of updating a scene graph node.
public struct SceneNodeUpdateResult: Sendable {
/// The preference values produced by the scene and its children.
public var preferences: ScenePreferenceValues

Expand All @@ -9,13 +9,13 @@ public struct SceneUpdateResult: Sendable {

/// Creates an update result by combining the preference values of a scene's
/// children.
public init(childResults: [SceneUpdateResult]) {
public init(childResults: [SceneNodeUpdateResult]) {
preferences = ScenePreferenceValues(merging: childResults.map(\.preferences))
}

/// Creates the layout result of a leaf scene (one with no children and no
/// special preference behaviour). Uses ``ScenePreferenceValues/default``.
public static func leafScene() -> Self {
SceneUpdateResult(preferences: .default)
SceneNodeUpdateResult(preferences: .default)
}
}
19 changes: 12 additions & 7 deletions Sources/SwiftCrossUI/Scenes/Modifiers/CommandsModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,26 @@ final class CommandsModifierNode<Content: Scene>: SceneGraphNode {
)
}

func update<Backend: AppBackend>(
func updateNode(
_ newScene: NodeScene?,
backend: Backend,
environment: EnvironmentValues
) -> SceneUpdateResult {
) -> SceneNodeUpdateResult {
if let newScene {
self.commands = newScene.commands
}

var result = contentNode.update(
newScene?.content,
var result = contentNode.updateNode(newScene?.content, environment: environment)
result.preferences.commands = result.preferences.commands.overlayed(with: commands)
return result
}

func update<Backend: AppBackend>(
backend: Backend,
environment: EnvironmentValues
) {
contentNode.update(
backend: backend,
environment: environment
)
result.preferences.commands = result.preferences.commands.overlayed(with: commands)
return result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,25 @@ final class SceneEnvironmentModifierNode<Content: Scene>: SceneGraphNode {
)
}

func update<Backend: AppBackend>(
func updateNode(
_ newScene: NodeScene?,
backend: Backend,
environment: EnvironmentValues
) -> SceneUpdateResult {
) -> SceneNodeUpdateResult {
if let newScene {
self.modification = newScene.modification
}

return contentNode.update(
return contentNode.updateNode(
newScene?.content,
environment: modification(environment)
)
}

func update<Backend: AppBackend>(
backend: Backend,
environment: EnvironmentValues
) {
contentNode.update(
backend: backend,
environment: modification(environment)
)
Expand Down
Loading
Loading