Pickers.swift 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. extension BackendFeatures {
  2. /// Backend methods for pickers.
  3. ///
  4. /// These are used by ``Picker``.
  5. @MainActor
  6. public protocol Pickers: Core {
  7. /// The supported picker styles.
  8. var supportedPickerStyles: [BackendPickerStyle] { get }
  9. /// The picker style used by ``PickerStyle/automatic``.
  10. var defaultPickerStyle: BackendPickerStyle { get }
  11. /// Creates a picker for selecting from a finite set of options (e.g. a radio button group,
  12. /// a drop-down, a picker wheel).
  13. ///
  14. /// Predominantly used by ``Picker``.
  15. ///
  16. /// - Parameter style: The picker's style.
  17. /// - Returns: A picker.
  18. func createPicker(style: BackendPickerStyle) -> Widget
  19. /// Sets the options for a picker to display, along with a change handler for when its
  20. /// selected option changes.
  21. ///
  22. /// The change handler
  23. ///
  24. /// - Parameters:
  25. /// - picker: The picker to update.
  26. /// - options: The picker's options.
  27. /// - environment: The current environment.
  28. /// - onChange: The action to perform when the selected option changes.
  29. /// This handler replaces any existing change handlers and is called
  30. /// whenever a selection is made, even if the same option is picked
  31. /// again.
  32. func updatePicker(
  33. _ picker: Widget,
  34. options: [String],
  35. environment: EnvironmentValues,
  36. onChange: @escaping (Int?) -> Void
  37. )
  38. /// Sets the index of the selected option of a picker.
  39. ///
  40. /// - Parameters:
  41. /// - picker: The picker.
  42. /// - selectedOption: The index of the option to select. If `nil`, all
  43. /// options should be deselected.
  44. func setSelectedOption(ofPicker picker: Widget, to selectedOption: Int?)
  45. }
  46. }
  47. // MARK: Default Implementations
  48. extension BackendFeatures.Pickers {
  49. public var defaultPickerStyle: BackendPickerStyle {
  50. supportedPickerStyles.first ?? .menu
  51. }
  52. }