SelectableListViews.swift 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. extension BackendFeatures {
  2. /// Backend methods for list views that allow selecting items.
  3. ///
  4. /// These are used by ``List``.
  5. @MainActor
  6. public protocol SelectableListViews: Core {
  7. /// Creates a list with selectable rows.
  8. ///
  9. /// - Returns: A list with selectable rows.
  10. func createSelectableListView() -> Widget
  11. /// Updates a list with the current environment. Should update list view to
  12. /// respect ``EnvironmentValues/isEnabled``.
  13. func updateSelectableListView(
  14. _ selectableListView: Widget,
  15. environment: EnvironmentValues
  16. )
  17. /// Gets the amount of padding introduced by the backend around the content of
  18. /// each row.
  19. ///
  20. /// Ideally backends should get rid of base padding so that SwiftCrossUI can
  21. /// give developers more freedom, but this isn't always possible.
  22. ///
  23. /// - Parameter listView: The list view.
  24. /// - Returns: An `EdgeInsets` instance describing the amount of base
  25. /// padding around `listView`'s items.
  26. func baseItemPadding(ofSelectableListView listView: Widget) -> EdgeInsets
  27. /// Gets the minimum size for rows in the list view.
  28. ///
  29. /// This doesn't necessarily have to be just for hard requirements enforced
  30. /// by the backend, it can also just be an idiomatic minimum size for the
  31. /// platform.
  32. ///
  33. /// - Parameter listView: The list view.
  34. /// - Returns: The minimum size for rows in the list view.
  35. func minimumRowSize(ofSelectableListView listView: Widget) -> SIMD2<Int>
  36. /// Sets the items of a selectable list along with their heights.
  37. ///
  38. /// Row heights should include base item padding (i.e. they should be the
  39. /// external height of the row rather than the internal height).
  40. ///
  41. /// Implementations may assume that if the row count hasn't changed, the
  42. /// `items` array hasn't either. And furthermore, if the row count has
  43. /// increased, then the only new widgets are the widgets appended to the
  44. /// end of the previous items array to reach the new item count. Likewise,
  45. /// if the row count has decreased, then the `items` array will simply have
  46. /// been truncated to the new length.
  47. ///
  48. /// - Parameters:
  49. /// - listView: The list view.
  50. /// - items: An array of widgets to add to `listView`.
  51. /// - rowHeights: The row heights of `items`.
  52. func setItems(
  53. ofSelectableListView listView: Widget,
  54. to items: [Widget],
  55. withRowHeights rowHeights: [Int]
  56. )
  57. /// Sets the action to perform when a user selects an item in the list.
  58. ///
  59. /// - Parameters:
  60. /// - listView: The list view.
  61. /// - action: The selection handler. Receives the selected item's index.
  62. func setSelectionHandler(
  63. forSelectableListView listView: Widget,
  64. to action: @escaping (_ selectedIndex: Int) -> Void
  65. )
  66. /// Sets the list's selected item by index.
  67. ///
  68. /// - Parameters:
  69. /// - listView: The list view.
  70. /// - index: The index of the item to select.
  71. func setSelectedItem(
  72. ofSelectableListView listView: Widget,
  73. toItemAt index: Int?
  74. )
  75. }
  76. }