1
0

HelpModifier.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. extension View {
  2. /// Adds help text to a view using a string that you provide.
  3. ///
  4. /// This configures a hover tooltip where applicable, i.e. desktop and
  5. /// visionOS. When using touch screens, most users will not be able to
  6. /// access the help text in a simple way, though it can configure the
  7. /// accessibility hint text.
  8. public func help(_ text: String) -> some View {
  9. HelpView(helpText: text, content: self)
  10. }
  11. }
  12. struct HelpView<Content: View>: View, TypeSafeView {
  13. var helpText: String
  14. var content: Content
  15. var body: TupleView1<Content> { content }
  16. typealias Children = TupleView1<Content>.Children
  17. func children<Backend: BaseAppBackend>(
  18. backend: Backend,
  19. snapshots: [ViewGraphSnapshotter.NodeSnapshot]?,
  20. environment: EnvironmentValues
  21. ) -> Children {
  22. body.children(
  23. backend: backend,
  24. snapshots: snapshots,
  25. environment: environment
  26. )
  27. }
  28. func asWidget<Backend: BaseAppBackend>(
  29. _ children: Children,
  30. backend: Backend
  31. ) -> Backend.Widget {
  32. func castBackend_inner<NewBackend: BaseAppBackend & BackendFeatures.Tooltips>(_ backend: NewBackend) -> NewBackend.Widget {
  33. return backend.createTooltipContainer(wrapping: children.child0.widget.into())
  34. }
  35. guard let castedBackend = backend as? any (BaseAppBackend & BackendFeatures.Tooltips) else {
  36. fatalError("Backend does not implement BackendFeatures.Tooltips")
  37. }
  38. return castBackend_inner(castedBackend) as! Backend.Widget
  39. }
  40. func computeLayout<Backend: BaseAppBackend>(
  41. _ widget: Backend.Widget,
  42. children: Children,
  43. proposedSize: ProposedViewSize,
  44. environment: EnvironmentValues,
  45. backend: Backend
  46. ) -> ViewLayoutResult {
  47. children.child0.computeLayout(
  48. with: body.view0,
  49. proposedSize: proposedSize,
  50. environment: environment
  51. )
  52. }
  53. func commit<Backend: BaseAppBackend>(
  54. _ widget: Backend.Widget,
  55. children: Children,
  56. layout: ViewLayoutResult,
  57. environment: EnvironmentValues,
  58. backend: Backend
  59. ) {
  60. func castBackend_inner<NewBackend: BaseAppBackend & BackendFeatures.Tooltips>(_ backend: NewBackend) {
  61. let widget = widget as! NewBackend.Widget
  62. let size = children.child0.commit().size
  63. backend.setSize(of: widget, to: size.vector)
  64. backend.updateTooltipContainer(widget, tooltip: helpText)
  65. }
  66. guard let castedBackend = backend as? any (BaseAppBackend & BackendFeatures.Tooltips) else {
  67. fatalError("Backend does not implement BackendFeatures.Tooltips")
  68. }
  69. castBackend_inner(castedBackend)
  70. }
  71. }