Fix onAppear(_:) and onUpdate(_:)

This commit is contained in:
david-swift 2023-11-29 05:52:10 +01:00
parent 1db1513a3b
commit 5ab9387317
4 changed files with 17 additions and 25 deletions

View File

@ -34,7 +34,6 @@
- [Submenu](structs/Submenu.md) - [Submenu](structs/Submenu.md)
- [Text](structs/Text.md) - [Text](structs/Text.md)
- [ToolbarView](structs/ToolbarView.md) - [ToolbarView](structs/ToolbarView.md)
- [UpdateObserver](structs/UpdateObserver.md)
- [VStack](structs/VStack.md) - [VStack](structs/VStack.md)
- [Window](structs/Window.md) - [Window](structs/Window.md)

View File

@ -102,10 +102,10 @@ Add a style class to the view.
- Parameter style: The style class. - Parameter style: The style class.
- Returns: A view. - Returns: A view.
### `onAppear(_:)` ### `onUpdate(_:)`
Run a function when the view appears for the first time. Run a function when the view gets an update.
- Parameter closure: The function. - Parameter onUpdate: The function.
- Returns: A view. - Returns: A view.
### `stopModifiers()` ### `stopModifiers()`
@ -128,9 +128,3 @@ Add a bottom toolbar to the view.
- toolbar: The toolbar's content. - toolbar: The toolbar's content.
- visible: Whether the toolbar is visible. - visible: Whether the toolbar is visible.
- Returns: A view. - Returns: A view.
### `onUpdate(_:)`
Run a function when the view gets an update.
- Parameter onUpdate: The function.
- Returns: A view.

View File

@ -1,17 +1,17 @@
// //
// UpdateObserver.swift // AppearObserver.swift
// Adwaita // Adwaita
// //
// Created by david-swift on 10.09.23. // Created by david-swift on 29.11.23.
// //
import Libadwaita import Libadwaita
/// A widget which executes a custom code when being updated. /// A widget which executes a custom code when being rendered for the first time.
struct UpdateObserver: Widget { struct AppearObserver: Widget {
/// The function. /// The function.
var onUpdate: () -> Void var onAppear: () -> Void
/// The content. /// The content.
var content: View var content: View
@ -19,7 +19,7 @@ struct UpdateObserver: Widget {
/// - Parameter modifiers: Modify views before being updated. /// - Parameter modifiers: Modify views before being updated.
/// - Returns: The content's container. /// - Returns: The content's container.
func container(modifiers: [(View) -> View]) -> ViewStorage { func container(modifiers: [(View) -> View]) -> ViewStorage {
onUpdate() onAppear()
return content.storage(modifiers: modifiers) return content.storage(modifiers: modifiers)
} }
@ -28,7 +28,6 @@ struct UpdateObserver: Widget {
/// - storage: The content's storage. /// - storage: The content's storage.
/// - modifiers: Modify views before being updated. /// - modifiers: Modify views before being updated.
func update(_ storage: ViewStorage, modifiers: [(View) -> View]) { func update(_ storage: ViewStorage, modifiers: [(View) -> View]) {
onUpdate()
content.updateStorage(storage, modifiers: modifiers) content.updateStorage(storage, modifiers: modifiers)
} }
@ -36,11 +35,11 @@ struct UpdateObserver: Widget {
extension View { extension View {
/// Run a function when the view gets an update. /// Run a function when the view appears for the first time.
/// - Parameter onUpdate: The function. /// - Parameter closure: The function.
/// - Returns: A view. /// - Returns: A view.
public func onUpdate(_ onUpdate: @escaping () -> Void) -> View { public func onAppear(_ closure: @escaping () -> Void) -> View {
UpdateObserver(onUpdate: onUpdate, content: self) AppearObserver(onAppear: closure, content: self)
} }
} }

View File

@ -111,11 +111,11 @@ extension View {
inspect { _ = $0?.addStyle(style) } inspect { _ = $0?.addStyle(style) }
} }
/// Run a function when the view appears for the first time. /// Run a function when the view gets an update.
/// - Parameter closure: The function. /// - Parameter onUpdate: The function.
/// - Returns: A view. /// - Returns: A view.
public func onAppear(_ closure: @escaping () -> Void) -> View { public func onUpdate(_ onUpdate: @escaping () -> Void) -> View {
inspect { _ in closure() } inspect { _ in onUpdate() }
} }
} }