Open History to see the last commit
1import SwiftUI
2import UniformTypeIdentifiers
3
4/// How many of an incoming batch of images the composer can take, separated
5/// from the SwiftUI plumbing so the cap and the overflow accounting can be
6/// tested without a live drag session or pasteboard.
7///
8/// Paste and drop are two doors into the same room: both produce item
9/// providers, both land in the attachment strip, and both must respect the
10/// attachment cap while earlier images are still being prepared.
11struct FeatureComposerImageIntakePlan: Equatable {
12 let acceptedCount: Int
13 let firstOrdinal: Int
14 let droppedCount: Int
15
16 /// Returns nil when nothing can be accepted, either because the batch is
17 /// empty or the cap is already spent by attached and in-flight images.
18 static func forProviders(
19 providerCount: Int,
20 attachmentCount: Int,
21 pendingCount: Int,
22 maximumCount: Int = FeatureImageAttachmentLimits.maximumCount
23 ) -> FeatureComposerImageIntakePlan? {
24 guard providerCount > 0 else { return nil }
25 let remaining = max(0, maximumCount - attachmentCount - pendingCount)
26 let accepted = min(providerCount, remaining)
27 guard accepted > 0 else { return nil }
28
29 return FeatureComposerImageIntakePlan(
30 acceptedCount: accepted,
31 firstOrdinal: attachmentCount + pendingCount + 1,
32 droppedCount: providerCount - accepted
33 )
34 }
35}
36
37/// Accepts images dragged onto the composer from another app.
38///
39/// Registering only `.image` lets the drag session itself reject anything
40/// else, so a dropped PDF never lands and there is no failure to explain
41/// afterwards.
42struct FeatureComposerImageDrop: ViewModifier {
43 let isEnabled: Bool
44 let shape: RoundedRectangle
45 let onDropImages: ([NSItemProvider]) -> Bool
46
47 @State private var isTargeted = false
48
49 func body(content: Content) -> some View {
50 content
51 .overlay {
52 if isTargeted {
53 shape
54 .fill(T3Colors.accent.opacity(0.1))
55 .overlay {
56 shape.strokeBorder(T3Colors.accent, lineWidth: 2)
57 }
58 .allowsHitTesting(false)
59 .accessibilityHidden(true)
60 }
61 }
62 .animation(.easeOut(duration: 0.12), value: isTargeted)
63 .onDrop(
64 of: [.image],
65 delegate: FeatureComposerImageDropDelegate(
66 isEnabled: isEnabled,
67 isTargeted: $isTargeted,
68 onDropImages: onDropImages
69 )
70 )
71 }
72}
73
74/// Tracks targeting through explicit enter and exit callbacks so the highlight
75/// cannot outlive the session, and states the drop operation outright.
76private struct FeatureComposerImageDropDelegate: DropDelegate {
77 let isEnabled: Bool
78 @Binding var isTargeted: Bool
79 let onDropImages: ([NSItemProvider]) -> Bool
80
81 func validateDrop(info: DropInfo) -> Bool {
82 isEnabled && info.hasItemsConforming(to: [.image])
83 }
84
85 func dropEntered(info: DropInfo) {
86 isTargeted = true
87 }
88
89 // A system-sourced drag (the screenshot thumbnail, Photos) is refused
90 // under SwiftUI's default proposal and the session dies mid-air with the
91 // highlight still lit. Asking for a copy explicitly is what lets it land.
92 func dropUpdated(info: DropInfo) -> DropProposal? {
93 DropProposal(operation: .copy)
94 }
95
96 func dropExited(info: DropInfo) {
97 isTargeted = false
98 }
99
100 func performDrop(info: DropInfo) -> Bool {
101 isTargeted = false
102 return onDropImages(info.itemProviders(for: [.image]))
103 }
104}