|
| 1 | +// |
| 2 | +// NSAttributedString+Accessibility.swift |
| 3 | +// OpenSwiftUICore |
| 4 | +// |
| 5 | +// Audited for 6.5.4 |
| 6 | +// Status: WIP |
| 7 | +// ID: 2BDB458C6E5F105189088F0894A913CC (SwiftUICore) |
| 8 | + |
| 9 | +package import Foundation |
| 10 | + |
| 11 | +// MARK: - Accessibility + Text resolve [WIP] |
| 12 | + |
| 13 | +extension AccessibilityCore { |
| 14 | + package static func textResolvesToEmpty(_ text: Text, in environment: EnvironmentValues) -> Bool { |
| 15 | + _openSwiftUIUnimplementedFailure() |
| 16 | + } |
| 17 | + |
| 18 | + package static func textsResolveToEmpty(_ texts: [Text], in environment: EnvironmentValues) -> Bool { |
| 19 | + _openSwiftUIUnimplementedFailure() |
| 20 | + } |
| 21 | + |
| 22 | + package static func textResolvedToPlainText( |
| 23 | + _ text: Text, |
| 24 | + in environment: EnvironmentValues, |
| 25 | + updateResolvableAttributes: Bool = false, |
| 26 | + idiom: AnyInterfaceIdiom? = nil |
| 27 | + ) -> String { |
| 28 | + _openSwiftUIUnimplementedFailure() |
| 29 | + } |
| 30 | + |
| 31 | + package static func textsResolvedToPlainText( |
| 32 | + _ texts: [Text], |
| 33 | + in environment: EnvironmentValues, |
| 34 | + updateResolvableAttributes: Bool = false, |
| 35 | + idiom: AnyInterfaceIdiom? = nil, |
| 36 | + separator: String = ", " |
| 37 | + ) -> String? { |
| 38 | + _openSwiftUIUnimplementedFailure() |
| 39 | + } |
| 40 | + |
| 41 | + package static func textResolvedToAttributedText( |
| 42 | + _ text: Text, |
| 43 | + in environment: EnvironmentValues, |
| 44 | + includeResolvableAttributes: Bool = false, |
| 45 | + includeDefaultAttributes: Bool = true, |
| 46 | + updateResolvableAttributes: Bool = true, |
| 47 | + idiom: AnyInterfaceIdiom? = nil |
| 48 | + ) -> NSAttributedString? { |
| 49 | + _openSwiftUIUnimplementedFailure() |
| 50 | + } |
| 51 | + |
| 52 | + @discardableResult |
| 53 | + package static func resolveAttributedTextAttributes( |
| 54 | + in string: inout NSAttributedString, |
| 55 | + environment: EnvironmentValues, |
| 56 | + includeResolvableAttributes: Bool = false |
| 57 | + ) -> Bool { |
| 58 | + _openSwiftUIUnimplementedFailure() |
| 59 | + } |
| 60 | + |
| 61 | + package static func resolveAttributedTextAttributes( |
| 62 | + _ attributes: inout [NSAttributedString.Key: Any], |
| 63 | + environment: EnvironmentValues |
| 64 | + ) { |
| 65 | + _openSwiftUIUnimplementedFailure() |
| 66 | + } |
| 67 | + |
| 68 | + package static func resolveAccessibilitySpeechAttributes( |
| 69 | + into attributes: inout [NSAttributedString.Key: Any], |
| 70 | + speechAttr: AccessibilitySpeechAttributes, |
| 71 | + environment: EnvironmentValues, |
| 72 | + includeDefaultAttributes: Bool = true |
| 73 | + ) { |
| 74 | + _openSwiftUIUnimplementedFailure() |
| 75 | + } |
| 76 | + |
| 77 | + package static func textsResolvedToAttributedText( |
| 78 | + _ texts: [Text], |
| 79 | + in environment: EnvironmentValues, |
| 80 | + includeResolvableAttributes: Bool = false, |
| 81 | + includeDefaultAttributes: Bool = true, |
| 82 | + updateResolvableAttributes: Bool = true, |
| 83 | + resolveSuffix: Bool = false, |
| 84 | + idiom: AnyInterfaceIdiom? = nil, |
| 85 | + separator: String = ", " |
| 86 | + ) -> NSAttributedString? { |
| 87 | + _openSwiftUIUnimplementedFailure() |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +extension Text { |
| 92 | + package func accessibilityResolvedText( |
| 93 | + in environment: EnvironmentValues, |
| 94 | + idiom: AnyInterfaceIdiom? = nil |
| 95 | + ) -> AccessibilityText? { |
| 96 | + _openSwiftUIUnimplementedFailure() |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// MARK: - AccessibilityTextAttributeResolver |
| 101 | + |
| 102 | +package protocol AccessibilityTextAttributeResolver { |
| 103 | + func resolveDefaultAttributes(_: inout [NSAttributedString.Key: Any]) |
| 104 | + |
| 105 | + func resolveTextStyleAttributes( |
| 106 | + _: inout [NSAttributedString.Key: Any], |
| 107 | + textStyle: Text.Style, |
| 108 | + environment: EnvironmentValues |
| 109 | + ) |
| 110 | + |
| 111 | + func resolveAccessibilitySpeechAttributes( |
| 112 | + into attributes: inout [NSAttributedString.Key: Any], |
| 113 | + speechAttr: AccessibilitySpeechAttributes, |
| 114 | + environment: EnvironmentValues, |
| 115 | + includeDefaultAttributes: Bool |
| 116 | + ) |
| 117 | +} |
| 118 | + |
| 119 | +extension EnvironmentValues { |
| 120 | + private struct AccessibilityTextAttributeResolverKey: EnvironmentKey { |
| 121 | + static let defaultValue: (any AccessibilityTextAttributeResolver)? = nil |
| 122 | + } |
| 123 | + |
| 124 | + package var accessibilityTextAttributeResolver: (any AccessibilityTextAttributeResolver)? { |
| 125 | + get { self[AccessibilityTextAttributeResolverKey.self] } |
| 126 | + set { self[AccessibilityTextAttributeResolverKey.self] = newValue } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +// MARK: - NSAttributedString + AX [WIP] |
| 131 | + |
| 132 | +extension NSAttributedString { |
| 133 | + convenience package init(axAttributedString: String) { |
| 134 | + _openSwiftUIUnimplementedFailure() |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +// MARK: - AX AttributedString Keys |
| 139 | + |
| 140 | +extension NSAttributedString.Key { |
| 141 | + package static var coreAXForegroundColor: NSAttributedString.Key { |
| 142 | + isUIKitBased() ? .init("UIAccessibilityTokenForegroundColor") : .init("AXForegroundColor") |
| 143 | + } |
| 144 | + |
| 145 | + package static var coreAXFontName: NSAttributedString.Key { |
| 146 | + .init("UIAccessibilityTokenFontName") |
| 147 | + } |
| 148 | + |
| 149 | + package static var coreAXFontSize: NSAttributedString.Key { |
| 150 | + .init("UIAccessibilityTokenFontSize") |
| 151 | + } |
| 152 | + |
| 153 | + package static var coreAXFontFamily: NSAttributedString.Key { |
| 154 | + .init("UIAccessibilityTokenFontFamily") |
| 155 | + } |
| 156 | + |
| 157 | + package static var coreAXStrikethrough: NSAttributedString.Key { |
| 158 | + isUIKitBased() ? .init("UIAccessibilityTokenStrikethrough") : .init("AXStrikethrough") |
| 159 | + } |
| 160 | + |
| 161 | + package static var coreAXUnderline: NSAttributedString.Key { |
| 162 | + isUIKitBased() ? .init("UIAccessibilityTokenUnderline") : .init("AXUnderline") |
| 163 | + } |
| 164 | + |
| 165 | + package static var coreAXAlignment: NSAttributedString.Key { |
| 166 | + isUIKitBased() ? .init("UIAccessibilityTokenParagraphAlignment") : .init("AXATextAlignmentValue") |
| 167 | + } |
| 168 | + |
| 169 | + package static var coreAXAttachment: NSAttributedString.Key { |
| 170 | + isUIKitBased() ? .init("UIAccessibilityTokenAttachment") : .init("AXAttachment") |
| 171 | + } |
| 172 | + |
| 173 | + package static var coreAXLink: NSAttributedString.Key { |
| 174 | + isUIKitBased() ? .init("UIAccessibilityTokenLink") : .init("AXLink") |
| 175 | + } |
| 176 | + |
| 177 | + package static var coreAXTextHeadingLevel: NSAttributedString.Key { |
| 178 | + isUIKitBased() ? .init("UIAccessibilityTextAttributeHeadingLevel") : .init("AXHeadingLevel") |
| 179 | + } |
| 180 | + |
| 181 | + package static var coreAXSpeechPitch: NSAttributedString.Key { |
| 182 | + isUIKitBased() ? .init("UIAccessibilitySpeechAttributePitch") : .init("AXPitch") |
| 183 | + } |
| 184 | + |
| 185 | + package static var coreAXSpeechPunctuation: NSAttributedString.Key { |
| 186 | + isUIKitBased() ? .init("UIAccessibilitySpeechAttributePunctuation") : .init("AXPunctuation") |
| 187 | + } |
| 188 | + |
| 189 | + package static var coreAXSpeechSpellOut: NSAttributedString.Key { |
| 190 | + isUIKitBased() ? .init("UIAccessibilitySpeechAttributeSpellOut") : .init("AXSpellOut") |
| 191 | + } |
| 192 | + |
| 193 | + package static var coreAXSpeechIPANotation: NSAttributedString.Key { |
| 194 | + isUIKitBased() ? .init("UIAccessibilitySpeechAttributeIPANotation") : .init("AXIPANotation") |
| 195 | + } |
| 196 | + |
| 197 | + package static var coreAXTextualContext: NSAttributedString.Key { |
| 198 | + isUIKitBased() ? .init("UIAccessibilityTextAttributeContext") : .init("AXTextualContext") |
| 199 | + } |
| 200 | + |
| 201 | + package static var coreAXSpeechAnnouncementPriority: NSAttributedString.Key { |
| 202 | + isUIKitBased() ? .init("UIAccessibilitySpeechAttributeAnnouncementPriority") : .init("AXAnnouncementPriority") |
| 203 | + } |
| 204 | + |
| 205 | + package static var coreAXSpeechLanguage: NSAttributedString.Key { |
| 206 | + isUIKitBased() ? .init("UIAccessibilitySpeechAttributeLanguage") : .init("AXLanguage") |
| 207 | + } |
| 208 | + |
| 209 | + package static let coreAXLabel: NSAttributedString.Key = .init("OpenSwiftUI.accessibilityLabel") |
| 210 | + |
| 211 | + package static var coreAXDurationTimeMMSS: NSAttributedString.Key { |
| 212 | + isUIKitBased() ? .init("UIAccessibilityTokenDurationTimeMMSS") : .init("AXTokenDurationTimeMMSS") |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +extension NSAttributedString.Key { |
| 217 | + package var isAccessibilityAttribute: Bool { |
| 218 | + rawValue.hasPrefix(isUIKitBased() ? "UIAccessibility" : "AX") |
| 219 | + } |
| 220 | +} |
0 commit comments