|
| 1 | +import SwiftUI |
| 2 | + |
| 3 | +public extension View { |
| 4 | + func colorPickerSheet( |
| 5 | + isPresented: Binding<Bool>, |
| 6 | + selection: Binding<Color>, |
| 7 | + supportsAlpha: Bool = true, |
| 8 | + title: String? = nil, |
| 9 | + animated: Bool = true |
| 10 | + ) -> some View { |
| 11 | + background( |
| 12 | + ColorPickerSheet( |
| 13 | + isPresented: isPresented, |
| 14 | + selection: selection, |
| 15 | + supportsAlpha: supportsAlpha, |
| 16 | + title: title, |
| 17 | + animated: animated |
| 18 | + ) |
| 19 | + ) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +private struct ColorPickerSheet: UIViewRepresentable { |
| 24 | + @Binding var isPresented: Bool |
| 25 | + @Binding var selection: Color |
| 26 | + let supportsAlpha: Bool |
| 27 | + let title: String? |
| 28 | + let animated: Bool |
| 29 | + func makeCoordinator() -> Coordinator { |
| 30 | + Coordinator(selection: $selection, isPresented: $isPresented) |
| 31 | + } |
| 32 | + |
| 33 | + class Coordinator: NSObject, UIColorPickerViewControllerDelegate, UIAdaptivePresentationControllerDelegate { |
| 34 | + @Binding var selection: Color |
| 35 | + @Binding var isPresented: Bool |
| 36 | + var didPresent = false |
| 37 | + |
| 38 | + init(selection: Binding<Color>, isPresented: Binding<Bool>) { |
| 39 | + _selection = selection |
| 40 | + _isPresented = isPresented |
| 41 | + } |
| 42 | + |
| 43 | + func colorPickerViewControllerDidSelectColor(_ viewController: UIColorPickerViewController) { |
| 44 | + selection = Color(viewController.selectedColor) |
| 45 | + } |
| 46 | + |
| 47 | + func colorPickerViewControllerDidFinish(_: UIColorPickerViewController) { |
| 48 | + isPresented = false |
| 49 | + didPresent = false |
| 50 | + } |
| 51 | + |
| 52 | + func presentationControllerDidDismiss(_: UIPresentationController) { |
| 53 | + isPresented = false |
| 54 | + didPresent = false |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + func getTopViewController(from view: UIView) -> UIViewController? { |
| 59 | + guard var top = view.window?.rootViewController else { |
| 60 | + return nil |
| 61 | + } |
| 62 | + while let next = top.presentedViewController { |
| 63 | + top = next |
| 64 | + } |
| 65 | + return top |
| 66 | + } |
| 67 | + |
| 68 | + func makeUIView(context _: Context) -> UIView { |
| 69 | + let view = UIView() |
| 70 | + view.isHidden = true |
| 71 | + return view |
| 72 | + } |
| 73 | + |
| 74 | + func updateUIView(_ uiView: UIView, context: Context) { |
| 75 | + if isPresented, !context.coordinator.didPresent { |
| 76 | + let modal = UIColorPickerViewController() |
| 77 | + modal.sheetPresentationController?.detents = [.medium(), .large()] |
| 78 | + modal.selectedColor = UIColor(selection) |
| 79 | + modal.supportsAlpha = supportsAlpha |
| 80 | + modal.title = title |
| 81 | + modal.delegate = context.coordinator |
| 82 | + modal.presentationController?.delegate = context.coordinator |
| 83 | + let top = getTopViewController(from: uiView) |
| 84 | + top?.present(modal, animated: true) |
| 85 | + context.coordinator.didPresent = true |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments