1
0

ElementaryView.swift 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /// A complimentary protocol for ``View`` to simplify implementation of
  2. /// elementary (i.e. atomic) views which have no children.
  3. ///
  4. /// Think of elementary views as the leaves at the end of the view tree.
  5. @MainActor
  6. protocol ElementaryView: View where Content == EmptyView {
  7. func asWidget<Backend: BaseAppBackend>(
  8. backend: Backend
  9. ) -> Backend.Widget
  10. func computeLayout<Backend: BaseAppBackend>(
  11. _ widget: Backend.Widget,
  12. proposedSize: ProposedViewSize,
  13. environment: EnvironmentValues,
  14. backend: Backend
  15. ) -> ViewLayoutResult
  16. func commit<Backend: BaseAppBackend>(
  17. _ widget: Backend.Widget,
  18. layout: ViewLayoutResult,
  19. environment: EnvironmentValues,
  20. backend: Backend
  21. )
  22. }
  23. extension ElementaryView {
  24. public var body: EmptyView {
  25. return EmptyView()
  26. }
  27. /// Do not implement yourself, implement ``ElementaryView/asWidget(backend:)`` instead.
  28. public func asWidget<Backend: BaseAppBackend>(
  29. _ children: any ViewGraphNodeChildren,
  30. backend: Backend
  31. ) -> Backend.Widget {
  32. asWidget(backend: backend)
  33. }
  34. /// Do not implement yourself, implement ``ElementaryView/update(_:proposedSize:environment:backend:)`` instead.
  35. public func computeLayout<Backend: BaseAppBackend>(
  36. _ widget: Backend.Widget,
  37. children: any ViewGraphNodeChildren,
  38. proposedSize: ProposedViewSize,
  39. environment: EnvironmentValues,
  40. backend: Backend
  41. ) -> ViewLayoutResult {
  42. computeLayout(
  43. widget,
  44. proposedSize: proposedSize,
  45. environment: environment,
  46. backend: backend
  47. )
  48. }
  49. public func commit<Backend: BaseAppBackend>(
  50. _ widget: Backend.Widget,
  51. children: any ViewGraphNodeChildren,
  52. layout: ViewLayoutResult,
  53. environment: EnvironmentValues,
  54. backend: Backend
  55. ) {
  56. commit(
  57. widget,
  58. layout: layout,
  59. environment: environment,
  60. backend: backend
  61. )
  62. }
  63. }