Skip to content

Commit

Permalink
Merge pull request #173 from cashapp/alex/AXCustomContent
Browse files Browse the repository at this point in the history
Support Accessibility Custom Content
  • Loading branch information
RoyalPineapple authored Feb 27, 2024
2 parents f039d27 + 2f24559 commit 379a4f7
Show file tree
Hide file tree
Showing 26 changed files with 453 additions and 58 deletions.
4 changes: 4 additions & 0 deletions Example/AccessibilitySnapshot.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
66E2CD14CD63946657E17B15 /* Pods_SnapshotTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3A3192D7B9B16BD10FB517A2 /* Pods_SnapshotTests.framework */; };
83A295842AC22D9D00DFBE4F /* UserInputLabelsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83A295832AC22D9D00DFBE4F /* UserInputLabelsViewController.swift */; };
83A295862AC22EEE00DFBE4F /* UserInputLabelsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83A295852AC22EEE00DFBE4F /* UserInputLabelsTests.swift */; };
AC725B842B06D07E009AD59B /* AccessibilityCustomContentViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AC725B832B06D07E009AD59B /* AccessibilityCustomContentViewController.swift */; };
C47F6C5316FB0C043BEB59F3 /* Pods_AccessibilitySnapshotDemo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6A886964D2E787399E137105 /* Pods_AccessibilitySnapshotDemo.framework */; };
D2F76EED2945C879000A453F /* HitTargetTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2F76EEC2945C879000A453F /* HitTargetTests.swift */; };
D38F6F4E508A3D067D677F69 /* Pods_UnitTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0BFCB4FD6BC17AB232B26E72 /* Pods_UnitTests.framework */; };
Expand Down Expand Up @@ -148,6 +149,7 @@
83A295832AC22D9D00DFBE4F /* UserInputLabelsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UserInputLabelsViewController.swift; sourceTree = "<group>"; };
83A295852AC22EEE00DFBE4F /* UserInputLabelsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UserInputLabelsTests.swift; sourceTree = "<group>"; };
88C33CBF672C290CE1EE86AF /* README.md */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
AC725B832B06D07E009AD59B /* AccessibilityCustomContentViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccessibilityCustomContentViewController.swift; sourceTree = "<group>"; };
C78F90CE7A2A315AADF80144 /* Pods-SnapshotTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SnapshotTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-SnapshotTests/Pods-SnapshotTests.debug.xcconfig"; sourceTree = "<group>"; };
CCFF2A604706B71DC0CBD38B /* Pods-AccessibilitySnapshotDemo.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AccessibilitySnapshotDemo.release.xcconfig"; path = "Pods/Target Support Files/Pods-AccessibilitySnapshotDemo/Pods-AccessibilitySnapshotDemo.release.xcconfig"; sourceTree = "<group>"; };
D2F76EEC2945C879000A453F /* HitTargetTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HitTargetTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -211,6 +213,7 @@
3DDE7FF524C6D6BF00999ABA /* AccessibilityCustomActionsViewController.swift */,
1104A8D02B595FF600B6715F /* TextFieldViewController.swift */,
112909BB2B63E57B00B4EBEB /* TextViewViewController.swift */,
AC725B832B06D07E009AD59B /* AccessibilityCustomContentViewController.swift */,
);
name = "Accessibility Screens";
sourceTree = "<group>";
Expand Down Expand Up @@ -643,6 +646,7 @@
112909BC2B63E57B00B4EBEB /* TextViewViewController.swift in Sources */,
3DDE7FF624C6D6BF00999ABA /* AccessibilityCustomActionsViewController.swift in Sources */,
3DBAC28F2242E7C700EF4D0A /* ListContainerViewController.swift in Sources */,
AC725B842B06D07E009AD59B /* AccessibilityCustomContentViewController.swift in Sources */,
3DBEAA5B2222953E00FAE61D /* SwitchControlViewController.swift in Sources */,
3DBAC2912242F9B200EF4D0A /* LandmarkContainerViewController.swift in Sources */,
1104A8CF2B580AC500B6715F /* SwiftUITextEntry.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//
// Copyright 2024 Block Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import Accessibility
import Paralayout
import UIKit

@available(iOS 14.0, *)
final class AccessibilityCustomContentViewController: AccessibilityViewController {

// MARK: - UIViewController

override func loadView() {
view = View(
views: [
.init(includeLabel: true, includeHint: true),
.init(includeLabel: true, includeHint: false),
.init(includeLabel: false, includeHint: true),
.init(includeLabel: false, includeHint: false),
]
)
}

}

// MARK: -
@available(iOS 14.0, *)
private extension AccessibilityCustomContentViewController {

final class View: UIView {

// MARK: - Life Cycle

init(views: [CustomContentView], frame: CGRect = .zero) {
self.views = views

super.init(frame: frame)

views.forEach(addSubview)
}

@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

// MARK: - Private Properties

private let views: [CustomContentView]

// MARK: - UIView

override func layoutSubviews() {
views.forEach { $0.bounds.size = .init(width: bounds.width / 2, height: 50) }

let statusBarHeight = window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0

var distributionSpecifiers: [ViewDistributionSpecifying] = [ statusBarHeight.fixed, 1.flexible ]
for subview in views {
distributionSpecifiers.append(subview)
distributionSpecifiers.append(1.flexible)
}
applyVerticalSubviewDistribution(distributionSpecifiers)
}

}

}

// MARK: -
@available(iOS 14.0, *)
private extension AccessibilityCustomContentViewController {

final class CustomContentView: UIView, AXCustomContentProvider {
// MARK: - Life Cycle

init(includeLabel: Bool, includeHint: Bool) {
super.init(frame: .zero)

backgroundColor = .gray

isAccessibilityElement = true

accessibilityLabel = includeLabel ? "Label" : nil
accessibilityHint = includeHint ? "Hint" : nil
}

@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

// MARK: - UIAccessibility
var accessibilityCustomContent: [AXCustomContent]! = {
let customContent = AXCustomContent(label: "Custom Content Label", value: "Custom Content Value")

let highImportance = AXCustomContent(label: "High Importance Label", value: "High Importance Value")
highImportance.importance = .high

return [customContent, highImportance]
}()
}
}
8 changes: 6 additions & 2 deletions Example/AccessibilitySnapshot/RootViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class RootViewController: UITableViewController {
// MARK: - Life Cycle

init() {
self.accessibilityScreens = [
var accessibilityScreens = [
("View Accessibility Properties", { _ in return ViewAccessibilityPropertiesViewController() }),
("Label Accessibility Properties", { _ in return LabelAccessibilityPropertiesViewController() }),
("Button Accessibility Traits", { _ in return ButtonAccessibilityTraitsViewController() }),
Expand Down Expand Up @@ -66,7 +66,11 @@ final class RootViewController: UITableViewController {
("Text View", { _ in return TextViewViewController() }),
("SwiftUI Text Entry", { _ in return UIHostingController(rootView: SwiftUITextEntry()) }),
]

if #available(iOS 14.0, *) {
accessibilityScreens.append( ("Accessibility Custom Content", { _ in return AccessibilityCustomContentViewController() }))
}
self.accessibilityScreens = accessibilityScreens

super.init(nibName: nil, bundle: nil)
}

Expand Down
117 changes: 69 additions & 48 deletions Example/AccessibilitySnapshot/SwiftUIView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,54 +28,75 @@ fileprivate struct Circle: View {
struct SwiftUIView: View {
var body: some View {
VStack(spacing: 30) {
// View with nothing.
Circle()
.accessibility(label: Text(""))
.accessibility(value: Text(""))
.accessibility(hint: Text(""))

// View with label.
Circle()
.accessibility(label: Text("Label"))
.accessibility(value: Text(""))
.accessibility(hint: Text(""))

// View with value.
Circle()
.accessibility(label: Text(""))
.accessibility(value: Text("Value"))
.accessibility(hint: Text(""))

// View with hint.
Circle()
.accessibility(label: Text(""))
.accessibility(value: Text(""))
.accessibility(hint: Text("Hint"))

// View with label and value.
Circle()
.accessibility(label: Text("Label"))
.accessibility(value: Text("Value"))
.accessibility(hint: Text(""))

// View with label and hint.
Circle()
.accessibility(label: Text("Label"))
.accessibility(value: Text(""))
.accessibility(hint: Text("Hint"))

// View with value and hint.
Circle()
.accessibility(label: Text(""))
.accessibility(value: Text("Value"))
.accessibility(hint: Text("Hint"))

// View with label, value, and hint.
Circle()
.accessibility(label: Text("Label"))
.accessibility(value: Text("Value"))
.accessibility(hint: Text("Hint"))

Group {
// View with nothing.
Circle()
.accessibility(label: Text(""))
.accessibility(value: Text(""))
.accessibility(hint: Text(""))

// View with label.
Circle()
.accessibility(label: Text("Label"))
.accessibility(value: Text(""))
.accessibility(hint: Text(""))

// View with value.
Circle()
.accessibility(label: Text(""))
.accessibility(value: Text("Value"))
.accessibility(hint: Text(""))

// View with hint.
Circle()
.accessibility(label: Text(""))
.accessibility(value: Text(""))
.accessibility(hint: Text("Hint"))

// View with label and value.
Circle()
.accessibility(label: Text("Label"))
.accessibility(value: Text("Value"))
.accessibility(hint: Text(""))

// View with label and hint.
Circle()
.accessibility(label: Text("Label"))
.accessibility(value: Text(""))
.accessibility(hint: Text("Hint"))

// View with value and hint.
Circle()
.accessibility(label: Text(""))
.accessibility(value: Text("Value"))
.accessibility(hint: Text("Hint"))

// View with label, value, and hint.
Circle()
.accessibility(label: Text("Label"))
.accessibility(value: Text("Value"))
.accessibility(hint: Text("Hint"))

if #available(iOS 14.0, *) {
// View with label, value, hint, and Custom Actions.
Circle()
.accessibility(label: Text("Label"))
.accessibility(value: Text("Value"))
.accessibility(hint: Text("Hint"))
.accessibilityAction(named: "Custom") {}
}

if #available(iOS 15.0, *) {
// View with label, value, hint, and Custom Content.
Circle()
.accessibility(label: Text("Label"))
.accessibility(value: Text("Value"))
.accessibility(hint: Text("Hint"))
.accessibilityCustomContent("Key", "Value")
.accessibilityCustomContent("Important Key", "Important Value", importance: .high)
}
}

Spacer()
}
}
Expand Down
12 changes: 12 additions & 0 deletions Example/SnapshotTests/AccessibilityPropertiesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ final class AccessibilitySnapshotTests: SnapshotTestCase {
customActionsViewController.view.frame = UIScreen.main.bounds
SnapshotVerifyAccessibility(customActionsViewController.view)
}

@available(iOS 14.0, *)
func testCustomContent() throws {
try XCTSkipUnless(
ProcessInfo().operatingSystemVersion.majorVersion >= 14,
"This test only supports iOS 14 and later"
)

let customContentViewController = AccessibilityCustomContentViewController()
customContentViewController.view.frame = UIScreen.main.bounds
SnapshotVerifyAccessibility(customContentViewController.view)
}

func testLargeView() throws {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 1400, height: 1400))
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 10 additions & 2 deletions Scripts/ValidateLocalizedStrings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,18 @@ let translationRegex = Regex {
var allValid = true

for filePath in stringsFiles {
let translationKeys = try String(contentsOfFile: filePath).matches(of: translationRegex).map { $0.1 }
let translationKeys = try Set(String(contentsOfFile: filePath).matches(of: translationRegex).map { $0.1 })

if Set(translationKeys) != localizedKeysSet {
if translationKeys != localizedKeysSet {
print("\(filePath) does not match expected set of localized string keys")
let expected = translationKeys.subtracting(localizedKeysSet)
if expected.count != 0 {
print("Expected Keys not found: \(expected)")
}
let found = localizedKeysSet.subtracting(translationKeys)
if found.count != 0 {
print("No translation found for keys: \(found)")
}
allValid = false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
/* Description for an accessibility element indicating that it has custom actions available */
"custom_actions.description" = "Aktionen verfügbar";

/* Description for an accessibility element indicating that it has additional custom content available */
"custom_content.description" = "Weiterhin inhalte verfügbar";

/* Description for the 'text entry' accessibility trait */
"trait.text_field.description" = "Textfeld.";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
/* Description for an accessibility element indicating that it has custom actions available */
"custom_actions.description" = "Actions Available";

/* Description for an accessibility element indicating that it has additional custom content available */
"custom_content.description" = "More Content Available";

/* Description for the 'text entry' accessibility trait */
"trait.text_field.description" = "Text Field.";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
/* Description for an accessibility element indicating that it has custom actions available */
"custom_actions.description" = "Доступны действия";

/* Description for an accessibility element indicating that it has additional custom content available */
"custom_content.description" = "Доступно больше контента";

/* Description for the 'text entry' accessibility trait */
"trait.text_field.description" = "текстовое поле.";

Expand Down
Loading

0 comments on commit 379a4f7

Please sign in to comment.