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)
- [Text](structs/Text.md)
- [ToolbarView](structs/ToolbarView.md)
- [UpdateObserver](structs/UpdateObserver.md)
- [VStack](structs/VStack.md)
- [Window](structs/Window.md)

View File

@ -102,10 +102,10 @@ Add a style class to the view.
- Parameter style: The style class.
- Returns: A view.
### `onAppear(_:)`
### `onUpdate(_:)`
Run a function when the view appears for the first time.
- Parameter closure: The function.
Run a function when the view gets an update.
- Parameter onUpdate: The function.
- Returns: A view.
### `stopModifiers()`
@ -128,9 +128,3 @@ Add a bottom toolbar to the view.
- toolbar: The toolbar's content.
- visible: Whether the toolbar is visible.
- 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
//
// Created by david-swift on 10.09.23.
// Created by david-swift on 29.11.23.
//
import Libadwaita
/// A widget which executes a custom code when being updated.
struct UpdateObserver: Widget {
/// A widget which executes a custom code when being rendered for the first time.
struct AppearObserver: Widget {
/// The function.
var onUpdate: () -> Void
var onAppear: () -> Void
/// The content.
var content: View
@ -19,7 +19,7 @@ struct UpdateObserver: Widget {
/// - Parameter modifiers: Modify views before being updated.
/// - Returns: The content's container.
func container(modifiers: [(View) -> View]) -> ViewStorage {
onUpdate()
onAppear()
return content.storage(modifiers: modifiers)
}
@ -28,7 +28,6 @@ struct UpdateObserver: Widget {
/// - storage: The content's storage.
/// - modifiers: Modify views before being updated.
func update(_ storage: ViewStorage, modifiers: [(View) -> View]) {
onUpdate()
content.updateStorage(storage, modifiers: modifiers)
}
@ -36,11 +35,11 @@ struct UpdateObserver: Widget {
extension View {
/// Run a function when the view gets an update.
/// - Parameter onUpdate: The function.
/// Run a function when the view appears for the first time.
/// - Parameter closure: The function.
/// - Returns: A view.
public func onUpdate(_ onUpdate: @escaping () -> Void) -> View {
UpdateObserver(onUpdate: onUpdate, content: self)
public func onAppear(_ closure: @escaping () -> Void) -> View {
AppearObserver(onAppear: closure, content: self)
}
}

View File

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