Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Commit

Permalink
Update properties based on previous state
Browse files Browse the repository at this point in the history
  • Loading branch information
david-swift committed Jul 31, 2024
1 parent e551fc0 commit 05617d2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Sources/Model/User Interface/Scene/SceneStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class SceneStorage {
public var destroy = false
/// Show the scene element (including moving into the foreground, if possible).
public var show: () -> Void
/// The previous state of the scene element.
public var previousState: SceneElement?

/// The pointer as an opaque pointer, as this may be needed with backends interoperating with C or C++.
public var opaquePointer: OpaquePointer? {
Expand Down
6 changes: 5 additions & 1 deletion Sources/Model/User Interface/View/ViewStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class ViewStorage {
var state: [String: StateProtocol] = [:]
/// Various properties of a widget.
public var fields: [String: Any] = [:]
/// The previous state of the widget.
public var previousState: Widget?

/// The pointer as an opaque pointer, as this is needed with backends interoperating with C or C++.
public var opaquePointer: OpaquePointer? {
Expand All @@ -35,10 +37,12 @@ public class ViewStorage {
/// - content: The view's content for container widgets.
public init(
_ pointer: Any?,
content: [String: [ViewStorage]] = [:]
content: [String: [ViewStorage]] = [:],
state: Widget? = nil
) {
self.pointer = pointer
self.content = content
self.previousState = state
}

}
4 changes: 3 additions & 1 deletion Tests/SampleBackends/Backend1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ public enum Backend1 {
public func update<Data>(_ storage: ViewStorage, modifiers: [(any AnyView) -> any AnyView], updateProperties: Bool, type: Data.Type) {
storage.fields["action"] = action
if updateProperties {
print("Update button (label = \(label))")
if (storage.previousState as? Self)?.label != label {
print("Update button (label = \(label))")
}
} else {
print("Do not update button (label = \(label))")
}
Expand Down

0 comments on commit 05617d2

Please sign in to comment.