|
| 1 | +// |
| 2 | +// EaselView.swift |
| 3 | +// Easel |
| 4 | +// |
| 5 | +// Created by Marquis Kurt on 25-08-2025. |
| 6 | +// |
| 7 | + |
| 8 | +import MapKit |
| 9 | +import PencilKit |
| 10 | +import SwiftUI |
| 11 | + |
| 12 | +/// A view that provides a canvas layer for creating drawings using the Apple Pencil. |
| 13 | +/// |
| 14 | +/// Easel views are used to allow players to draw over content views, such as maps. The tool picker can be shown or |
| 15 | +/// hidden programmatically, and the drawing is bound for quick access and storage. |
| 16 | +@available(iOS 18.0, *) |
| 17 | +public struct EaselView<CanvasBackground: View> { |
| 18 | + /// The coordinator used to listen for delegate events. |
| 19 | + public class Coordinator: NSObject, EaselViewControllerDelegate { |
| 20 | + @Binding var drawing: PKDrawing |
| 21 | + |
| 22 | + init(drawing: Binding<PKDrawing>) { |
| 23 | + self._drawing = drawing |
| 24 | + } |
| 25 | + |
| 26 | + func easelViewController(_: EaselViewController, didChangeDrawing drawing: PKDrawing) { |
| 27 | + self.drawing = drawing |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + @Binding var drawing: PKDrawing |
| 32 | + @Binding var isToolPickerPresented: Bool |
| 33 | + |
| 34 | + private var drawingPolicy: PKCanvasViewDrawingPolicy |
| 35 | + private var canvasBackground: (() -> CanvasBackground)? |
| 36 | + |
| 37 | + /// Create an easel view with a drawing. |
| 38 | + /// - Parameter drawing: The drawing that the easel view will manage. |
| 39 | + /// - Parameter canvasBackground: The background to use. |
| 40 | + public init( |
| 41 | + drawing: Binding<PKDrawing>, |
| 42 | + canvasBackground: (() -> CanvasBackground)? = nil |
| 43 | + ) { |
| 44 | + self._drawing = drawing |
| 45 | + self._isToolPickerPresented = .constant(true) |
| 46 | + self.drawingPolicy = .default |
| 47 | + self.canvasBackground = canvasBackground |
| 48 | + } |
| 49 | + |
| 50 | + private init( |
| 51 | + drawing: Binding<PKDrawing>, |
| 52 | + picker: Binding<Bool>, |
| 53 | + policy: PKCanvasViewDrawingPolicy, |
| 54 | + background: (() -> CanvasBackground)? |
| 55 | + ) { |
| 56 | + self._drawing = drawing |
| 57 | + self._isToolPickerPresented = picker |
| 58 | + self.drawingPolicy = policy |
| 59 | + self.canvasBackground = background |
| 60 | + } |
| 61 | + |
| 62 | + /// Sets the drawing policy on the easel view. |
| 63 | + /// - Parameter policy: The easel view's drawing policy. |
| 64 | + public func drawingPolicy(_ policy: PKCanvasViewDrawingPolicy) -> EaselView { |
| 65 | + EaselView( |
| 66 | + drawing: $drawing, |
| 67 | + picker: $isToolPickerPresented, |
| 68 | + policy: policy, |
| 69 | + background: canvasBackground |
| 70 | + ) |
| 71 | + } |
| 72 | + |
| 73 | + /// Sets the visibility of the tool picker. |
| 74 | + /// |
| 75 | + /// When the tool picker is presented, the background view loses its interaction capabilities to prevent |
| 76 | + /// interference between the canvas and the background. User interaction with the background is restored when the |
| 77 | + /// tool picker is hidden. |
| 78 | + /// |
| 79 | + /// - Parameter isPresented: Whether the tool picker should be visible. |
| 80 | + public func toolPicker(isPresented: Binding<Bool>) -> EaselView { |
| 81 | + EaselView( |
| 82 | + drawing: $drawing, |
| 83 | + picker: isPresented, |
| 84 | + policy: drawingPolicy, |
| 85 | + background: canvasBackground |
| 86 | + ) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +extension EaselView where CanvasBackground == EmptyView { |
| 91 | + /// Create an easel view with no background. |
| 92 | + /// - Parameter drawing: The drawing that the easel will manage. |
| 93 | + public init(drawing: Binding<PKDrawing>) { |
| 94 | + self._drawing = drawing |
| 95 | + self._isToolPickerPresented = .constant(true) |
| 96 | + self.drawingPolicy = .default |
| 97 | + self.canvasBackground = nil |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +extension EaselView: UIViewControllerRepresentable { |
| 102 | + public typealias UIViewControllerType = EaselViewController |
| 103 | + |
| 104 | + public func makeCoordinator() -> Coordinator { |
| 105 | + Coordinator(drawing: $drawing) |
| 106 | + } |
| 107 | + |
| 108 | + public func makeUIViewController(context: Context) -> EaselViewController { |
| 109 | + let viewController = EaselViewController() |
| 110 | + viewController.easelViewDelegate = context.coordinator |
| 111 | + if let canvasBackground { |
| 112 | + let hostingController = UIHostingController(rootView: canvasBackground()) |
| 113 | + viewController.canvasBackgroundController = hostingController |
| 114 | + } |
| 115 | + if isToolPickerPresented { |
| 116 | + viewController.activateToolPicker() |
| 117 | + } |
| 118 | + return viewController |
| 119 | + } |
| 120 | + |
| 121 | + public func updateUIViewController(_ uiViewController: EaselViewController, context: Context) { |
| 122 | + if isToolPickerPresented { |
| 123 | + uiViewController.activateToolPicker() |
| 124 | + } else { |
| 125 | + uiViewController.deactivateToolPicker() |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +#if os(iOS) |
| 131 | +#Preview { |
| 132 | + @Previewable @State var drawing = PKDrawing() |
| 133 | + @Previewable @State var toolPickerActive = true |
| 134 | + |
| 135 | + NavigationStack { |
| 136 | + EaselView(drawing: $drawing) { |
| 137 | + Map(interactionModes: .all) |
| 138 | + } |
| 139 | + .toolPicker(isPresented: $toolPickerActive) |
| 140 | + .drawingPolicy(.anyInput) |
| 141 | + .navigationTitle("Drawing Canvas") |
| 142 | + .navigationBarTitleDisplayMode(.inline) |
| 143 | + .toolbarRole(.editor) |
| 144 | + .ignoresSafeArea() |
| 145 | + .toolbar { |
| 146 | + Button("Tools", systemImage: "pencil.tip.crop.circle") { |
| 147 | + toolPickerActive.toggle() |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | +#endif |
0 commit comments