Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Sources/Textual/Attachment/Attachment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ public protocol Attachment: Sendable, Hashable, CustomStringConvertible {
/// Implement this to provide PNG-encoded data for serialization, copy/paste,
/// or export workflows. The default implementation returns `nil`.
func pngData() -> Data?

/// The text assistive technologies read in place of the attachment.
///
/// Textual draws attachments over unlabeled placeholders, so VoiceOver skips them. When an
/// attachment returns a label, VoiceOver reads the surrounding text with the label in the
/// attachment's place. The default implementation returns `nil`.
var accessibilityLabel: String? { get }
}

extension Attachment {
Expand All @@ -57,6 +64,10 @@ extension Attachment {
public func pngData() -> Data? {
nil
}

public var accessibilityLabel: String? {
nil
}
}

/// A type-erased ``Attachment``.
Expand Down Expand Up @@ -106,6 +117,10 @@ public struct AnyAttachment: Attachment {
base.pngData()
}

public var accessibilityLabel: String? {
base.accessibilityLabel
}

public static func == (lhs: Self, rhs: Self) -> Bool {
AnyHashable(lhs.base) == AnyHashable(rhs.base)
}
Expand Down
22 changes: 22 additions & 0 deletions Sources/Textual/Internal/TextFragment/TextBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import SwiftUI
extension TextFragment {
@MainActor @Observable final class TextBuilder {
var text: Text
/// The text with each attachment's accessibility label in its place, when any attachment has one
@ObservationIgnored let accessibilityLabel: String?

@ObservationIgnored private let content: Content
@ObservationIgnored private let cache: NSCache<KeyBox<[AttachmentKey: CGSize]>, Box<Text>>
Expand All @@ -32,6 +34,7 @@ extension TextFragment {
in: environment
)
self.content = content
self.accessibilityLabel = content.accessibilityLabel()
self.cache = NSCache()
self.cache.countLimit = 10

Expand Down Expand Up @@ -107,6 +110,25 @@ extension Text {
}

extension AttributedStringProtocol {
fileprivate func accessibilityLabel() -> String? {
var hasLabel = false
let label = runs.map { run in
guard let attachment = run.textual.attachment else {
return String(self[run.range].characters)
}

guard let label = attachment.accessibilityLabel else {
return ""
}

hasLabel = true
return label
}
.joined()

return hasLabel ? label : nil
}

fileprivate func attachmentSizes(
for proposal: ProposedViewSize, in environment: TextEnvironmentValues
) -> [AttachmentKey: CGSize] {
Expand Down
10 changes: 9 additions & 1 deletion Sources/Textual/Internal/TextFragment/TextFragment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,15 @@ struct TextFragment<Content: AttributedStringProtocol>: View {
}

private var text: Text {
textBuilder?.text ?? Text(verbatim: "")
guard let textBuilder else {
return Text(verbatim: "")
}

guard let accessibilityLabel = textBuilder.accessibilityLabel else {
return textBuilder.text
}

return textBuilder.text.accessibilityLabel(Text(verbatim: accessibilityLabel))
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import SwiftUI
import Testing

@testable import Textual

@MainActor
struct AttachmentAccessibilityLabelTests {
@Test func readsTheLabelInPlaceOfTheAttachment() {
let builder = TextFragment<AttributedString>.TextBuilder(
Self.text("Ask ", Mention(label: "Editor"), " to make a clip."),
environment: .init()
)

#expect(builder.accessibilityLabel == "Ask Editor to make a clip.")
}

@Test func leavesTextWithoutLabelsAlone() {
let unlabeled = TextFragment<AttributedString>.TextBuilder(
Self.text("Ask ", Mention(label: nil), " to make a clip."),
environment: .init()
)
let plain = TextFragment<AttributedString>.TextBuilder(
AttributedString("Ask to make a clip."),
environment: .init()
)

#expect(unlabeled.accessibilityLabel == nil)
#expect(plain.accessibilityLabel == nil)
}

private static func text(_ prefix: String, _ mention: Mention, _ suffix: String)
-> AttributedString
{
var attachment = AttributedString("\u{FFFC}")
attachment.textual.attachment = AnyAttachment(mention)
return AttributedString(prefix) + attachment + AttributedString(suffix)
}
}

private struct Mention: Textual.Attachment {
let label: String?

var description: String {
label ?? ""
}

var accessibilityLabel: String? {
label
}

var body: some View {
Color.clear
}

func sizeThatFits(_: ProposedViewSize, in _: TextEnvironmentValues) -> CGSize {
CGSize(width: 24, height: 24)
}
}