Update properties based on previous state

This commit is contained in:
david-swift 2024-07-31 13:54:01 +02:00
parent e551fc078a
commit 05617d2966
3 changed files with 10 additions and 2 deletions

View File

@ -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? {

View File

@ -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? {
@ -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
}
}

View File

@ -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))")
}