OnCommitModifier.swift 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. extension View {
  2. /// Adds an action to be performed when this view gets committed. Currently
  3. /// the action gets called before the view gets committed.
  4. ///
  5. /// - Parameter action: The action to perform when this view gets committed.
  6. func onCommit(perform action: @escaping @MainActor () -> Void) -> some View {
  7. OnCommitModifier(body: TupleView1(self), action: action)
  8. }
  9. }
  10. struct OnCommitModifier<Content: View>: View {
  11. var body: TupleView1<Content>
  12. var action: @MainActor () -> Void
  13. func asWidget<Backend: BaseAppBackend>(
  14. _ children: any ViewGraphNodeChildren,
  15. backend: Backend
  16. ) -> Backend.Widget {
  17. action()
  18. return defaultAsWidget(children, backend: backend)
  19. }
  20. func commit<Backend: BaseAppBackend>(
  21. _ widget: Backend.Widget,
  22. children: any ViewGraphNodeChildren,
  23. layout: ViewLayoutResult,
  24. environment: EnvironmentValues,
  25. backend: Backend
  26. ) {
  27. action()
  28. return defaultCommit(
  29. widget,
  30. children: children,
  31. layout: layout,
  32. environment: environment,
  33. backend: backend
  34. )
  35. }
  36. }