OnAppearModifier.swift 1.0 KB

123456789101112131415161718192021222324252627
  1. extension View {
  2. /// Adds an action to be performed before this view appears.
  3. ///
  4. /// The exact moment that the action gets called is an internal detail and
  5. /// may change at any time, but it is guaranteed to be after accessing the
  6. /// view's ``View/body`` and before the view appears on screen. Currently,
  7. /// if these docs have been kept up to date, the action gets called just
  8. /// before creating the view's widget.
  9. ///
  10. /// - Parameter action: The action to perform when this view appears.
  11. public func onAppear(perform action: @escaping @MainActor () -> Void) -> some View {
  12. OnAppearModifier(body: TupleView1(self), action: action)
  13. }
  14. }
  15. struct OnAppearModifier<Content: View>: View {
  16. var body: TupleView1<Content>
  17. var action: @MainActor () -> Void
  18. func asWidget<Backend: BaseAppBackend>(
  19. _ children: any ViewGraphNodeChildren,
  20. backend: Backend
  21. ) -> Backend.Widget {
  22. action()
  23. return defaultAsWidget(children, backend: backend)
  24. }
  25. }