|
| 1 | +// |
| 2 | +// Software Name: OUDS iOS |
| 3 | +// SPDX-FileCopyrightText: Copyright (c) Orange SA |
| 4 | +// SPDX-License-Identifier: MIT |
| 5 | +// |
| 6 | +// This software is distributed under the MIT license, |
| 7 | +// the text of which is available at https://opensource.org/license/MIT/ |
| 8 | +// or see the "LICENSE" file for more details. |
| 9 | +// |
| 10 | +// Authors: See CONTRIBUTORS.txt |
| 11 | +// Software description: A SwiftUI components library with code examples for Orange Unified Design System |
| 12 | +// |
| 13 | + |
| 14 | +import SwiftUI |
| 15 | + |
| 16 | +// MARK: - Accessible Navigation Title Modifier |
| 17 | + |
| 18 | +/// `ViewModifier` which defines a navigation title for the calling `View` and also uses `UIAccessibility` to notify for screen changed. |
| 19 | +struct AccessibleNavigationTitleModifier: ViewModifier { |
| 20 | + |
| 21 | + /// The title used as a `LocalizedStringKey` to add as navigation title |
| 22 | + let title: String |
| 23 | + |
| 24 | + /// Elapsed time to wait before sending an accessibility notification of a screen change with the `title` in argument |
| 25 | + let deadline: DispatchTime |
| 26 | + |
| 27 | + func body(content: Content) -> some View { |
| 28 | + content |
| 29 | + .navigationTitle(LocalizedStringKey(title)) |
| 30 | + .onAppear { |
| 31 | + DispatchQueue.main.asyncAfter(deadline: deadline) { |
| 32 | + UIAccessibility.post(notification: .screenChanged, argument: title) |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// MARK: - Request Accessible Focus Modifier |
| 39 | + |
| 40 | +/// `ViewModifier` to apply on a a `View` so as to request the focus after a given time. |
| 41 | +struct RequestAccessibleFocusModifier: ViewModifier { |
| 42 | + |
| 43 | + /// Flag to listen saying wether or not the `View` got the focus |
| 44 | + @AccessibilityFocusState var requestFocus: Bool |
| 45 | + |
| 46 | + /// Elapsed time to wait before requesting the focus |
| 47 | + let deadline: DispatchTime |
| 48 | + |
| 49 | + func body(content: Content) -> some View { |
| 50 | + content.onAppear { |
| 51 | + DispatchQueue.main.asyncAfter(deadline: deadline) { |
| 52 | + requestFocus = true |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// MARK: - Accessibility Focusable |
| 59 | + |
| 60 | +public enum AccessibilityFocusable: Hashable { |
| 61 | + case none |
| 62 | + case some(id: String) |
| 63 | +} |
| 64 | + |
| 65 | +// MARK: - Restricted Request Accessible Focus Modifier |
| 66 | + |
| 67 | +/// `ViewModifier` to apply on a `View` to request the focus on that `View` after a given time |
| 68 | +struct RestrictedRequestAccessibleFocusModifier: ViewModifier { |
| 69 | + |
| 70 | + /// Flag to listen saying wether or not the `View` got the focus |
| 71 | + @AccessibilityFocusState var requestFocus: AccessibilityFocusable? |
| 72 | + |
| 73 | + /// The target to give the focus after the deadLine` delay |
| 74 | + let target: AccessibilityFocusable |
| 75 | + |
| 76 | + /// Elapsed time to wait before requesting the focus |
| 77 | + let deadline: DispatchTime |
| 78 | + |
| 79 | + func body(content: Content) -> some View { |
| 80 | + content.onAppear { |
| 81 | + DispatchQueue.main.asyncAfter(deadline: deadline) { |
| 82 | + requestFocus = target |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments