Update widget docs to newest version

This commit is contained in:
david-swift 2024-03-16 07:48:37 +01:00
parent e3be7299eb
commit 8792437357

View File

@ -28,7 +28,7 @@ struct CustomText: Widget {
var text: String var text: String
public func container(modifiers: [(View) -> View]) -> ViewStorage { } public func container(modifiers: [(View) -> View]) -> ViewStorage { }
public func update(_ storage: ViewStorage, modifiers: [(View) -> View]) { } public func update(_ storage: ViewStorage, modifiers: [(View) -> View], updateProperties: Bool) { }
} }
``` ```
@ -44,20 +44,22 @@ func container(modifiers: [(View) -> View]) -> ViewStorage {
} }
``` ```
## The `update(_:modifiers:)` Function ## The `update(_:modifiers:updateProperties:)` Function
Whenever a state of the app changes, the `update(_:)` function of the widget gets called. Whenever a state of the app changes, the `update(_:modifiers:updateProperties:)` function of the widget gets called.
You get the view storage that you have previously initialized as a parameter. You get the view storage that you have previously initialized as a parameter.
Update the storage to reflect the current state of the widget: Update the storage to reflect the current state of the widget:
```swift ```swift
func update(_ storage: ViewStorage, modifiers: [(View) -> View]) { func update(_ storage: ViewStorage, modifiers: [(View) -> View], updateProperties: Bool) {
gtk_label_set_label(storage.pointer, text) if updateProperties {
gtk_label_set_label(storage.pointer, text)
}
} }
``` ```
## Containers ## Containers
Some widgets act as containers that accept other widgets as children. Some widgets act as containers that accept other widgets as children.
In that case, use the `ViewStorage`'s `content` property for storing their view storages. In that case, use the `ViewStorage`'s `content` property for storing their view storages.
In the `update(_:modifiers:)` function, update the children's storages. In the `update(_:modifiers:updateProperties:)` function, update the children's storages.
An example showcasing how to implement containers is the [Box][1] (it is auto-generated). An example showcasing how to implement containers is the [Box][1] (it is auto-generated).
[1]: ../../Sources/Adwaita/View/Generated/Box.swift [1]: ../../Sources/Adwaita/View/Generated/Box.swift