Skip to content

Commit

Permalink
[Accessibility] Turn UIKit Tab into an accessibility container
Browse files Browse the repository at this point in the history
  • Loading branch information
xavier-daleau authored and michael-zimmermann committed May 30, 2024
1 parent bcc4a6e commit 2d35c95
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
46 changes: 34 additions & 12 deletions core/Sources/Components/Tab/View/UIKit/TabItemUIView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ public final class TabItemUIView: UIControl {
return self.viewModel.isSelected
}
set {
if newValue {
self.accessibilityTraits.insert(.selected)
}else {
self.accessibilityTraits.remove(.selected)
}
guard newValue != self.viewModel.isSelected else { return }
self.viewModel.updateState(isSelected: newValue)
}
}
Expand Down Expand Up @@ -139,7 +145,7 @@ public final class TabItemUIView: UIControl {
newBadge.isHidden.toggle()
newBadge.isHidden.toggle()
}

self.invalidateIntrinsicContentSize()
}
}
Expand Down Expand Up @@ -236,15 +242,6 @@ public final class TabItemUIView: UIControl {
}
}

public override var isHighlighted: Bool {
get {
return self.viewModel.isPressed
}
set {
self.viewModel.updateState(isPressed: newValue)
}
}

/// A Boolean value indicating whether the control is in the enabled state.
///
/// Set the value of this property to true to enable the control or false to disable it. An enabled control is capable of responding to user interactions, whereas a disabled control ignores touch events and may draw itself differently.
Expand All @@ -254,7 +251,13 @@ public final class TabItemUIView: UIControl {
return self.viewModel.isEnabled
}
set {
self.viewModel.updateState(isEnabled: newValue)
if newValue {
self.accessibilityTraits.remove(.notEnabled)
}else {
self.accessibilityTraits.insert(.notEnabled)
}
guard newValue != self.viewModel.isEnabled else { return }
self.viewModel.isEnabled = newValue

Check failure on line 260 in core/Sources/Components/Tab/View/UIKit/TabItemUIView.swift

View workflow job for this annotation

GitHub Actions / core_build

cannot assign to property: 'isEnabled' setter is inaccessible
}
}

Expand Down Expand Up @@ -338,6 +341,8 @@ public final class TabItemUIView: UIControl {
self.setupConstraints()
self.enableTouch()
self.setupSubscriptions()
self.isAccessibilityElement = true
self.accessibilityTraits.insert(.button)
}

required init?(coder: NSCoder) {
Expand All @@ -363,6 +368,22 @@ public final class TabItemUIView: UIControl {
self.setNeedsLayout()
}

// MARK: - Control functions
public override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
self.viewModel.isPressed = true
}

public override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
self.viewModel.isPressed = false
}

public override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesCancelled(touches, with: event)
self.viewModel.isPressed = false
}

// MARK: - Private functions
private func setupSubscriptions() {
self.viewModel.$tabStateAttributes.subscribe(in: &self.subscriptions) { [weak self] attributes in
Expand Down Expand Up @@ -391,7 +412,7 @@ public final class TabItemUIView: UIControl {
self.bringSubviewToFront(self.bottomLine)

self.setupColors(attributes: self.viewModel.tabStateAttributes)

self.addOrRemoveIcon(self.viewModel.content.icon)
self.addOrRemoveTitle(self.viewModel.content.title)
}
Expand Down Expand Up @@ -471,6 +492,7 @@ public final class TabItemUIView: UIControl {
self.label.textColor = self.viewModel.tabStateAttributes.colors.label.uiColor

self.label.text = text
self.accessibilityLabel = text
self.label.isHidden = text == nil

self.invalidateIntrinsicContentSize()
Expand Down
11 changes: 8 additions & 3 deletions core/Sources/Components/Tab/View/UIKit/TabUIView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ public final class TabUIView: UIControl {
self.setupConstraints()
self.enableTouch()
self.setupSubscriptions()
self.setupAccessibility()
}

required init?(coder: NSCoder) {
Expand Down Expand Up @@ -462,9 +463,6 @@ public final class TabUIView: UIControl {

// MARK: - Private Functions
private func setupViews(items: [TabUIItemContent]) {

self.accessibilityIdentifier = TabAccessibilityIdentifier.tab

let tabItemViews = items.map{ item in
return TabItemUIView(
theme: theme,
Expand Down Expand Up @@ -517,6 +515,12 @@ public final class TabUIView: UIControl {
stackView.distribution = self.apportionsSegmentWidthsByContent ? .fill : .fillEqually
}

private func setupAccessibility() {
self.accessibilityTraits.insert(.tabBar)
self.isAccessibilityElement = false
self.accessibilityContainerType = .semanticGroup
}

private func setTabItems(content: [TabUIItemContent]) {
self.viewModel.content = content

Expand All @@ -530,6 +534,7 @@ public final class TabUIView: UIControl {
}
self.stackView.removeArrangedSubviews()
self.stackView.addArrangedSubviews(items)
self.accessibilityElements?.append(contentsOf: items)

self.updateAccessibilityIdentifiers()
self.invalidateIntrinsicContentSize()
Expand Down

0 comments on commit 2d35c95

Please sign in to comment.