From b3f810500baffd4f48e652f916d9b60a1719c1a5 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Wed, 5 Jun 2024 16:15:53 -0500 Subject: [PATCH] split up accessibility Signed-off-by: Matt Bruce --- VDS/BaseClasses/Selector/SelectorBase.swift | 1 + .../Selector/SelectorItemBase.swift | 36 ++++++++-- VDS/Components/Buttons/ButtonBase.swift | 2 + VDS/Components/DatePicker/DatePicker.swift | 2 - VDS/Components/RadioBox/RadioBoxItem.swift | 67 ++++++++++++++++--- 5 files changed, 94 insertions(+), 14 deletions(-) diff --git a/VDS/BaseClasses/Selector/SelectorBase.swift b/VDS/BaseClasses/Selector/SelectorBase.swift index 0b29f4e2..809cb4aa 100644 --- a/VDS/BaseClasses/Selector/SelectorBase.swift +++ b/VDS/BaseClasses/Selector/SelectorBase.swift @@ -124,6 +124,7 @@ open class SelectorBase: Control, SelectorControlable { open override func updateAccessibility() { super.updateAccessibility() accessibilityLabel = "\(Self.self)\(showError ? ", error" : "")" + accessibilityHint = !isEnabled ? "" : "Double tap to open." } /// This will change the state of the Selector and execute the actionBlock if provided. diff --git a/VDS/BaseClasses/Selector/SelectorItemBase.swift b/VDS/BaseClasses/Selector/SelectorItemBase.swift index 66772ce0..46d54fb5 100644 --- a/VDS/BaseClasses/Selector/SelectorItemBase.swift +++ b/VDS/BaseClasses/Selector/SelectorItemBase.swift @@ -147,6 +147,30 @@ open class SelectorItemBase: Control, Errorable, open var accessibilityValueText: String? + open var accessibilityLabelText: String { + var accessibilityLabels = [String]() + + accessibilityLabels.append("\(Selector.self)") + + if let text = labelText { + accessibilityLabels.append(text) + } + + if let text = childText { + accessibilityLabels.append(text) + } + + if !isEnabled { + accessibilityLabels.append("dimmed") + } + + if let errorText, showError { + accessibilityLabels.append("error, \(errorText)") + } + + return accessibilityLabels.joined(separator: ", ") + } + //-------------------------------------------------- // MARK: - Overrides //-------------------------------------------------- @@ -162,9 +186,11 @@ open class SelectorItemBase: Control, Errorable, open override func setup() { super.setup() - selectorView.isAccessibilityElement = false - isAccessibilityElement = true - accessibilityTraits = .button + selectorView.isAccessibilityElement = true + selectorView.shouldUpdateAccessibility = false + + isAccessibilityElement = false + accessibilityElements = [selectorView, label, childLabel, errorLabel] addSubview(mainStackView) mainStackView.isUserInteractionEnabled = false @@ -195,8 +221,10 @@ open class SelectorItemBase: Control, Errorable, /// Used to update any Accessibility properties. open override func updateAccessibility() { super.updateAccessibility() - setAccessibilityLabel(for: [selectorView, label, childLabel, errorLabel]) + selectorView.accessibilityLabel = accessibilityLabelText + selectorView.accessibilityHint = !isEnabled ? "" : "Double tap to activate." accessibilityValue = accessibilityValueText + } /// Resets to default settings. diff --git a/VDS/Components/Buttons/ButtonBase.swift b/VDS/Components/Buttons/ButtonBase.swift index 80010ee5..9dd60781 100644 --- a/VDS/Components/Buttons/ButtonBase.swift +++ b/VDS/Components/Buttons/ButtonBase.swift @@ -51,6 +51,8 @@ open class ButtonBase: UIButton, ViewProtocol, UserInfoable, Clickable { /// Key of whether or not updateView() is called in setNeedsUpdate() open var shouldUpdateView: Bool = true + open var shouldUpdateAccessibility: Bool = true + open var surface: Surface = .light { didSet { setNeedsUpdate() } } /// Text that will be used in the titleLabel. diff --git a/VDS/Components/DatePicker/DatePicker.swift b/VDS/Components/DatePicker/DatePicker.swift index 7645cce5..c514001a 100644 --- a/VDS/Components/DatePicker/DatePicker.swift +++ b/VDS/Components/DatePicker/DatePicker.swift @@ -102,8 +102,6 @@ open class DatePicker: EntryFieldBase, DatePickerViewControllerDelegate, UIPopov super.setup() fieldStackView.isAccessibilityElement = true - fieldStackView.accessibilityLabel = "Date Picker" - fieldStackView.accessibilityHint = "Double Tap to open" // setting color config selectedDateLabel.textColorConfiguration = primaryColorConfiguration.eraseToAnyColorable() diff --git a/VDS/Components/RadioBox/RadioBoxItem.swift b/VDS/Components/RadioBox/RadioBoxItem.swift index 15f7c55a..b555a9b6 100644 --- a/VDS/Components/RadioBox/RadioBoxItem.swift +++ b/VDS/Components/RadioBox/RadioBoxItem.swift @@ -133,6 +133,30 @@ open class RadioBoxItem: Control, Changeable, FormFieldable, Groupable { open var accessibilityValueText: String? + open var accessibilityLabelText: String { + var accessibilityLabels = [String]() + + accessibilityLabels.append("Radiobox") + + if let text { + accessibilityLabels.append(text) + } + + if let text = subText { + accessibilityLabels.append(text) + } + + if let text = subTextRight { + accessibilityLabels.append(text) + } + + if !isEnabled { + accessibilityLabels.append("dimmed") + } + + return accessibilityLabels.joined(separator: ", ") + } + //-------------------------------------------------- // MARK: - Configuration Properties //-------------------------------------------------- @@ -171,8 +195,10 @@ open class RadioBoxItem: Control, Changeable, FormFieldable, Groupable { open override func setup() { super.setup() - isAccessibilityElement = true - accessibilityTraits = .button + isAccessibilityElement = false + selectorView.isAccessibilityElement = true + selectorView.accessibilityTraits = .button + addSubview(selectorView) selectorView.isUserInteractionEnabled = false @@ -242,12 +268,8 @@ open class RadioBoxItem: Control, Changeable, FormFieldable, Groupable { /// Used to update any Accessibility properties. open override func updateAccessibility() { super.updateAccessibility() - setAccessibilityLabel(for: [textLabel, subTextLabel, subTextRightLabel]) - if let currentAccessibilityLabel = accessibilityLabel { - accessibilityLabel = "Radiobox, \(currentAccessibilityLabel)" - } else { - accessibilityLabel = "Radiobox" - } + accessibilityLabel = accessibilityLabelText + if let accessibilityValueText { accessibilityValue = strikethrough ? "\(strikethroughAccessibilityText), \(accessibilityValueText)" @@ -259,6 +281,35 @@ open class RadioBoxItem: Control, Changeable, FormFieldable, Groupable { } } + open override var accessibilityElements: [Any]? { + get { + var items = [Any]() + items.append(selectorView) + + let elements = gatherAccessibilityElements(from: selectorView) + let views = elements.compactMap({ $0 as? UIView }) + + //update accessibilityLabel + selectorView.setAccessibilityLabel(for: views) + + //disabled + if !isEnabled { + if let label = selectorView.accessibilityLabel, !label.isEmpty { + selectorView.accessibilityLabel = "\(label), dimmed" + } else { + selectorView.accessibilityLabel = "dimmed" + } + } + + //append all children that are accessible + items.append(contentsOf: elements) + + return items + } + set {} + } + + //-------------------------------------------------- // MARK: - Private Methods //--------------------------------------------------