|
| 1 | +import AppKit |
| 2 | +import SwiftUI |
| 3 | + |
| 4 | +enum AccessibilityManager { |
| 5 | + |
| 6 | + // MARK: - VoiceOver Announcements |
| 7 | + |
| 8 | + /// Posts a VoiceOver announcement with the given message. |
| 9 | + static func announce(_ message: String) { |
| 10 | + let userInfo: [NSAccessibility.NotificationUserInfoKey: Any] = [ |
| 11 | + .announcement: message, |
| 12 | + .priority: NSAccessibilityPriorityLevel.medium.rawValue |
| 13 | + ] |
| 14 | + NSAccessibility.post( |
| 15 | + element: NSApp as Any, |
| 16 | + notification: .announcementRequested, |
| 17 | + userInfo: userInfo |
| 18 | + ) |
| 19 | + } |
| 20 | + |
| 21 | + /// Posts a layout-changed notification to inform VoiceOver of UI updates. |
| 22 | + static func postLayoutChanged(for element: AnyObject = NSApp) { |
| 23 | + NSAccessibility.post(element: element, notification: .layoutChanged, userInfo: nil) |
| 24 | + } |
| 25 | + |
| 26 | + // MARK: - High Contrast Detection |
| 27 | + |
| 28 | + /// Returns true when the system high-contrast accessibility setting is enabled. |
| 29 | + static var isHighContrastEnabled: Bool { |
| 30 | + NSWorkspace.shared.accessibilityDisplayShouldIncreaseContrast |
| 31 | + } |
| 32 | + |
| 33 | + /// Returns the recommended minimum font size respecting accessibility preferences. |
| 34 | + static func effectiveFontSize(base: CGFloat) -> CGFloat { |
| 35 | + isHighContrastEnabled ? max(base, 18) : base |
| 36 | + } |
| 37 | + |
| 38 | + // MARK: - Focus Management |
| 39 | + |
| 40 | + /// Moves VoiceOver focus to the given NSView element. |
| 41 | + static func moveFocus(to element: NSView) { |
| 42 | + NSAccessibility.post(element: element, notification: .focusedUIElementChanged, userInfo: nil) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// MARK: - SwiftUI View Modifier Helpers |
| 47 | + |
| 48 | +extension View { |
| 49 | + /// Adds a combined accessibility element with label and hint. |
| 50 | + func accessibilityDescribe(label: String, hint: String? = nil) -> some View { |
| 51 | + self |
| 52 | + .accessibilityLabel(label) |
| 53 | + .accessibilityHint(hint ?? "") |
| 54 | + .accessibilityElement(children: .combine) |
| 55 | + } |
| 56 | +} |
0 commit comments