From a2f7c03b8fbc2dd77bbbe2b719e22ab30c96dea1 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Wed, 19 Jun 2024 17:17:19 -0500 Subject: [PATCH] added to base classes the new accessiblity implmenation Signed-off-by: Matt Bruce --- VDS/BaseClasses/Control.swift | 11 +++++++++-- VDS/BaseClasses/View.swift | 12 +++++++++++- VDS/Components/Buttons/ButtonBase.swift | 11 +++++++++++ VDS/Components/Label/Label.swift | 6 +++++- .../TextFields/InputField/TextField.swift | 13 +++++++++++++ VDS/Components/TextFields/TextArea/TextView.swift | 13 +++++++++++++ VDS/Protocols/ViewProtocol.swift | 2 +- 7 files changed, 63 insertions(+), 5 deletions(-) diff --git a/VDS/BaseClasses/Control.swift b/VDS/BaseClasses/Control.swift index 6d212e22..434a1a88 100644 --- a/VDS/BaseClasses/Control.swift +++ b/VDS/BaseClasses/Control.swift @@ -53,6 +53,8 @@ open class Control: UIControl, ViewProtocol, UserInfoable, Clickable { open var surface: Surface = .light { didSet { setNeedsUpdate() } } + open var accessibilityAction: ((Control) -> Void)? + /// Whether the Control is selected or not. open override var isSelected: Bool { didSet { setNeedsUpdate() } } @@ -123,9 +125,14 @@ open class Control: UIControl, ViewProtocol, UserInfoable, Clickable { //-------------------------------------------------- /// Implement accessibilityActivate on an element in order to handle the default action. /// - Returns: Based on whether the userInteraction is enabled. - override open func accessibilityActivate() -> Bool { + open override func accessibilityActivate() -> Bool { // Hold state in case User wanted isAnimated to remain off. - guard isUserInteractionEnabled else { return false } + guard isEnabled, isUserInteractionEnabled else { return false } + + if let accessibilityAction { + accessibilityAction(self) + } + sendActions(for: .touchUpInside) return true } diff --git a/VDS/BaseClasses/View.swift b/VDS/BaseClasses/View.swift index 9ba21d9c..ec3f8dd0 100644 --- a/VDS/BaseClasses/View.swift +++ b/VDS/BaseClasses/View.swift @@ -55,6 +55,8 @@ open class View: UIView, ViewProtocol, UserInfoable { open var isEnabled: Bool = true { didSet { setNeedsUpdate() } } + open var accessibilityAction: ((View) -> Void)? + //-------------------------------------------------- // MARK: - Lifecycle //-------------------------------------------------- @@ -87,7 +89,15 @@ open class View: UIView, ViewProtocol, UserInfoable { surface = .light isEnabled = true } - + + open override func accessibilityActivate() -> Bool { + guard isEnabled, isUserInteractionEnabled else { return false } + if let accessibilityAction { + accessibilityAction(self) + } + return true + } + open override func layoutSubviews() { super.layoutSubviews() setNeedsUpdate() diff --git a/VDS/Components/Buttons/ButtonBase.swift b/VDS/Components/Buttons/ButtonBase.swift index a8443f58..56a23011 100644 --- a/VDS/Components/Buttons/ButtonBase.swift +++ b/VDS/Components/Buttons/ButtonBase.swift @@ -78,6 +78,8 @@ open class ButtonBase: UIButton, ViewProtocol, UserInfoable, Clickable { /// Whether the Button should handle the isHighlighted state. open var shouldHighlight: Bool { isHighlighting == false } + open var accessibilityAction: ((ButtonBase) -> Void)? + /// Whether the Control is highlighted or not. open override var isHighlighted: Bool { didSet { @@ -141,6 +143,15 @@ open class ButtonBase: UIButton, ViewProtocol, UserInfoable, Clickable { shouldUpdateView = true setNeedsUpdate() } + + open override func accessibilityActivate() -> Bool { + guard isEnabled, isUserInteractionEnabled else { return false } + if let accessibilityAction { + accessibilityAction(self) + } + sendActions(for: .touchUpInside) + return true + } //-------------------------------------------------- // MARK: - Private Methods diff --git a/VDS/Components/Label/Label.swift b/VDS/Components/Label/Label.swift index 1ec1de82..c5b760a7 100644 --- a/VDS/Components/Label/Label.swift +++ b/VDS/Components/Label/Label.swift @@ -132,6 +132,8 @@ open class Label: UILabel, ViewProtocol, UserInfoable { /// Line break mode for the label, default is set to word wrapping. open override var lineBreakMode: NSLineBreakMode { didSet { setNeedsUpdate() }} + open var accessibilityAction: ((Label) -> Void)? + /// Text that will be used in the label. private var _text: String! override open var text: String! { @@ -455,7 +457,9 @@ open class Label: UILabel, ViewProtocol, UserInfoable { } open override func accessibilityActivate() -> Bool { - return false + guard let accessibilityAction, isEnabled, isUserInteractionEnabled else { return false } + accessibilityAction(self) + return true } } diff --git a/VDS/Components/TextFields/InputField/TextField.swift b/VDS/Components/TextFields/InputField/TextField.swift index da109148..800d836b 100644 --- a/VDS/Components/TextFields/InputField/TextField.swift +++ b/VDS/Components/TextFields/InputField/TextField.swift @@ -77,6 +77,8 @@ open class TextField: UITextField, ViewProtocol, Errorable { open var lineBreakMode: NSLineBreakMode = .byClipping { didSet { setNeedsUpdate() } } + open var accessibilityAction: ((TextField) -> Void)? + open override var isEnabled: Bool { didSet { setNeedsUpdate() } } open var textColorConfiguration: AnyColorable = ViewColorConfiguration().with { @@ -211,6 +213,17 @@ open class TextField: UITextField, ViewProtocol, Errorable { return success } + open override func accessibilityActivate() -> Bool { + guard isEnabled, isUserInteractionEnabled else { return false } + + if let accessibilityAction { + accessibilityAction(self) + return true + } else { + return super.accessibilityActivate() + } + } + //-------------------------------------------------- // MARK: - Private Methods //-------------------------------------------------- diff --git a/VDS/Components/TextFields/TextArea/TextView.swift b/VDS/Components/TextFields/TextArea/TextView.swift index 6c9e92ab..3832d2e5 100644 --- a/VDS/Components/TextFields/TextArea/TextView.swift +++ b/VDS/Components/TextFields/TextArea/TextView.swift @@ -68,6 +68,8 @@ open class TextView: UITextView, ViewProtocol, Errorable { $0.setSurfaceColors(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark, forDisabled: false) }.eraseToAnyColorable(){ didSet { setNeedsUpdate() }} + open var accessibilityAction: ((TextView) -> Void)? + open var showError: Bool = false { didSet { setNeedsUpdate() } } open var errorText: String? { didSet { setNeedsUpdate() } } @@ -146,6 +148,17 @@ open class TextView: UITextView, ViewProtocol, Errorable { shouldUpdateView = true setNeedsUpdate() } + + open override func accessibilityActivate() -> Bool { + guard isEnabled, isUserInteractionEnabled else { return false } + + if let accessibilityAction { + accessibilityAction(self) + return true + } else { + return super.accessibilityActivate() + } + } //-------------------------------------------------- // MARK: - Private Methods diff --git a/VDS/Protocols/ViewProtocol.swift b/VDS/Protocols/ViewProtocol.swift index ba912684..4c2aa694 100644 --- a/VDS/Protocols/ViewProtocol.swift +++ b/VDS/Protocols/ViewProtocol.swift @@ -20,7 +20,7 @@ public protocol ViewProtocol: AnyObject, Initable, Resettable, Enabling, Surface var shouldUpdateAccessibility: Bool { get set } /// Used for setting an implementation for the default Accessible Action - var accessibilityDefaultAction: ((Self) -> Void)? { get set } + var accessibilityAction: ((Self) -> Void)? { get set } /// Executed on initialization for this View. func initialSetup()