-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathAttachmentOverlay.swift
More file actions
37 lines (33 loc) · 1.14 KB
/
Copy pathAttachmentOverlay.swift
File metadata and controls
37 lines (33 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import SwiftUI
// MARK: - Overview
//
// `AttachmentOverlay` renders attachment views using resolved `Text.Layout` geometry.
//
// It’s applied at the text fragment level. The fragment exposes the resolved layout through the
// `Text.LayoutKey` preference; this modifier reads the anchored layout, converts its anchor to a
// concrete origin using `GeometryReader`, and installs an `AttachmentView` that draws attachments
// at their run bounds.
struct AttachmentOverlay: ViewModifier {
private let attachments: Set<AnyAttachment>
init(attachments: Set<AnyAttachment>) {
self.attachments = attachments
}
func body(content: Content) -> some View {
if #available(iOS 18, macOS 15, tvOS 18, watchOS 11, visionOS 2, *) {
content
.overlayPreferenceValue(Text.LayoutKey.self) { value in
if let anchoredLayout = value.first {
GeometryReader { geometry in
AttachmentView(
attachments: attachments,
origin: geometry[anchoredLayout.origin],
layout: anchoredLayout.layout
)
}
}
}
} else {
content
}
}
}