_BuiltinPickerStyle.swift 1.0 KB

123456789101112131415161718192021222324252627
  1. /// A built-in picker style backed by a backend-supported picker widget.
  2. public protocol _BuiltinPickerStyle {
  3. @MainActor
  4. func _asBackendPickerStyle<Backend: BaseAppBackend>(backend: Backend) -> BackendPickerStyle
  5. }
  6. extension PickerStyle where Self: _BuiltinPickerStyle {
  7. public func makeView<Value: Equatable>(
  8. options: [Value],
  9. selection: Binding<Value?>,
  10. environment: EnvironmentValues
  11. ) -> _BuiltinPickerImplementation {
  12. _BuiltinPickerImplementation(
  13. style: self._asBackendPickerStyle(backend: environment.backend),
  14. options: options.map { "\($0)" },
  15. selectedIndex: Binding {
  16. selection.wrappedValue.flatMap(options.firstIndex(of:))
  17. } set: {
  18. selection.wrappedValue = $0.map { options[$0] }
  19. }
  20. )
  21. }
  22. public func isSupported<Backend: BaseAppBackend>(backend: Backend) -> Bool {
  23. backend.supportedPickerStyles.contains(_asBackendPickerStyle(backend: backend))
  24. }
  25. }