Skip to content

Commit 83a4cde

Browse files
committed
Remove a11y dep
1 parent c3dab4a commit 83a4cde

File tree

9 files changed

+107
-83
lines changed

9 files changed

+107
-83
lines changed

Examples/DemoApp/DemoApp.xcodeproj/project.pbxproj

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
FA4F5ADA2A7A3E7700B268FF /* Snapshotting in Embed Frameworks */ = {isa = PBXBuildFile; productRef = FA4F5AD82A7A3E7700B268FF /* Snapshotting */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
4848
FA5E82972A96448A008DE3F0 /* StateView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA5E82962A96448A008DE3F0 /* StateView.swift */; };
4949
FA7EFE132B9235D100EF534F /* SnapshotPreferences in Frameworks */ = {isa = PBXBuildFile; productRef = FA7EFE122B9235D100EF534F /* SnapshotPreferences */; };
50+
FA8E2F9E2E8D797000B7B9EB /* AccessibilitySnapshotCore in Frameworks */ = {isa = PBXBuildFile; productRef = FA8E2F9D2E8D797000B7B9EB /* AccessibilitySnapshotCore */; };
5051
FA8F8B7E2C66C4C4007CEA33 /* DemoAppPreviewTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA8F8B7D2C66C4C4007CEA33 /* DemoAppPreviewTest.swift */; };
5152
FA8F8B852C66C4CA007CEA33 /* SnapshottingTests in Frameworks */ = {isa = PBXBuildFile; productRef = FA8F8B842C66C4CA007CEA33 /* SnapshottingTests */; };
5253
FA9C8E442A56959300DC4574 /* RatingPreviews.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA9C8E432A56959300DC4574 /* RatingPreviews.swift */; };
@@ -255,6 +256,7 @@
255256
isa = PBXFrameworksBuildPhase;
256257
buildActionMask = 2147483647;
257258
files = (
259+
FA8E2F9E2E8D797000B7B9EB /* AccessibilitySnapshotCore in Frameworks */,
258260
FA8F8B852C66C4CA007CEA33 /* SnapshottingTests in Frameworks */,
259261
);
260262
runOnlyForDeploymentPostprocessing = 0;
@@ -554,6 +556,7 @@
554556
name = DemoAppTests;
555557
packageProductDependencies = (
556558
FA8F8B842C66C4CA007CEA33 /* SnapshottingTests */,
559+
FA8E2F9D2E8D797000B7B9EB /* AccessibilitySnapshotCore */,
557560
);
558561
productName = DemoAppTests;
559562
productReference = FA8F8B7B2C66C4C4007CEA33 /* DemoAppTests.xctest */;
@@ -652,6 +655,7 @@
652655
);
653656
mainGroup = FA1671B62A5367A800A42DB0;
654657
packageReferences = (
658+
FA8E2F9C2E8D797000B7B9EB /* XCRemoteSwiftPackageReference "AccessibilitySnapshot" */,
655659
);
656660
productRefGroup = FA1671C02A5367A800A42DB0 /* Products */;
657661
projectDirPath = "";
@@ -1458,6 +1462,17 @@
14581462
};
14591463
/* End XCConfigurationList section */
14601464

1465+
/* Begin XCRemoteSwiftPackageReference section */
1466+
FA8E2F9C2E8D797000B7B9EB /* XCRemoteSwiftPackageReference "AccessibilitySnapshot" */ = {
1467+
isa = XCRemoteSwiftPackageReference;
1468+
repositoryURL = "https://github.com/EmergeTools/AccessibilitySnapshot.git";
1469+
requirement = {
1470+
kind = exactVersion;
1471+
version = 1.0.2;
1472+
};
1473+
};
1474+
/* End XCRemoteSwiftPackageReference section */
1475+
14611476
/* Begin XCSwiftPackageProductDependency section */
14621477
FA0D85422A72A5BD002A28ED /* SnapshottingTests */ = {
14631478
isa = XCSwiftPackageProductDependency;
@@ -1487,6 +1502,11 @@
14871502
isa = XCSwiftPackageProductDependency;
14881503
productName = SnapshotPreferences;
14891504
};
1505+
FA8E2F9D2E8D797000B7B9EB /* AccessibilitySnapshotCore */ = {
1506+
isa = XCSwiftPackageProductDependency;
1507+
package = FA8E2F9C2E8D797000B7B9EB /* XCRemoteSwiftPackageReference "AccessibilitySnapshot" */;
1508+
productName = AccessibilitySnapshotCore;
1509+
};
14901510
FA8F8B842C66C4CA007CEA33 /* SnapshottingTests */ = {
14911511
isa = XCSwiftPackageProductDependency;
14921512
productName = SnapshottingTests;

Examples/DemoApp/DemoAppTests/DemoAppSnapshotTest.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
//
77

88
import Foundation
9+
#if canImport(UIKit)
10+
import UIKit
11+
#endif
912
import SnapshottingTests
13+
import AccessibilitySnapshotCore
1014

1115
class DemoAppSnapshotTest: SnapshotTest {
1216
override class func snapshotPreviews() -> [String]? {
@@ -16,4 +20,37 @@ class DemoAppSnapshotTest: SnapshotTest {
1620
override class func excludedSnapshotPreviews() -> [String]? {
1721
return nil
1822
}
23+
24+
#if canImport(UIKit) && !os(watchOS) && !os(visionOS) && !os(tvOS)
25+
override open class func setupA11y() -> ((UIViewController, UIWindow, PreviewLayout) -> UIView)? {
26+
return { (controller: UIViewController, window: UIWindow, layout: PreviewLayout) in
27+
let containerVC = controller.parent
28+
let containedView: UIView
29+
switch layout {
30+
case .device:
31+
containedView = containerVC?.view ?? controller.view
32+
default:
33+
containedView = controller.view
34+
}
35+
let a11yView = AccessibilitySnapshotView(
36+
containedView: containedView,
37+
viewRenderingMode: controller.view.bounds.size.requiresCoreAnimationSnapshot ? .renderLayerInContext : .drawHierarchyInRect,
38+
activationPointDisplayMode: .never,
39+
showUserInputLabels: true)
40+
41+
a11yView.center = window.center
42+
window.addSubview(a11yView)
43+
44+
_ = try? a11yView.parseAccessibility(useMonochromeSnapshot: false)
45+
a11yView.sizeToFit()
46+
return a11yView
47+
}
48+
}
49+
#endif
50+
}
51+
52+
extension CGSize {
53+
var requiresCoreAnimationSnapshot: Bool {
54+
height >= UIScreen.main.bounds.size.height * 2
55+
}
1956
}

Package.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ let package = Package(
3636
],
3737
dependencies: [
3838
.package(url: "https://github.com/swhitty/FlyingFox.git", exact: "0.16.0"),
39-
.package(url: "https://github.com/EmergeTools/AccessibilitySnapshot.git", exact: "1.0.2"),
4039
.package(url: "https://github.com/EmergeTools/SimpleDebugger.git", exact: "1.0.0"),
4140
],
4241
targets: [
@@ -47,7 +46,7 @@ let package = Package(
4746
.target(name: "SnapshottingTests", dependencies: ["SnapshotPreviewsCore", "SnapshottingTestsObjc"]),
4847
.target(name: "SnapshotSharedModels"),
4948
// Core functionality
50-
.target(name: "SnapshotPreviewsCore", dependencies: ["PreviewsSupport", "SnapshotSharedModels", .product(name: "AccessibilitySnapshotCore", package: "AccessibilitySnapshot", condition: .when(platforms: [.iOS, .macCatalyst]))]),
49+
.target(name: "SnapshotPreviewsCore", dependencies: ["PreviewsSupport", "SnapshotSharedModels"]),
5150
.target(name: "SnapshotPreferences", dependencies: ["SnapshotSharedModels"]),
5251
// Inserted dylib
5352
.target(name: "Snapshotting", dependencies: ["SnapshottingSwift"]),

Sources/SnapshotPreviewsCore/AppKitRenderingStrategy.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public class AppKitRenderingStrategy: RenderingStrategy {
5555
image: image != nil ? .success(image!) : .failure(RenderingError.failedRendering(window?.frame.size ?? .zero)),
5656
precision: precision,
5757
accessibilityEnabled: accessibilityEnabled,
58-
accessibilityMarkers: nil,
5958
colorScheme: _colorScheme,
6059
appStoreSnapshot: appStoreSnapshot))
6160
}

Sources/SnapshotPreviewsCore/RenderingStrategy.swift

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,39 +23,24 @@ public enum MarkerShape {
2323
#endif
2424
}
2525

26-
public protocol AccessibilityMark {
27-
var description: String { get }
28-
var hint: String? { get }
29-
var userInputLabels: [String]? { get }
30-
var accessibilityShape: MarkerShape { get }
31-
var activationPoint: CGPoint { get }
32-
var usesDefaultActivationPoint: Bool { get }
33-
var customActions: [String] { get }
34-
var accessibilityLanguage: String? { get }
35-
36-
}
37-
3826
public struct SnapshotResult {
3927
public init(
4028
image: Result<ImageType, Error>,
4129
precision: Float?,
4230
accessibilityEnabled: Bool?,
43-
accessibilityMarkers: [AccessibilityMark]?,
4431
colorScheme: ColorScheme?,
4532
appStoreSnapshot: Bool?)
4633
{
4734
self.image = image
4835
self.precision = precision
4936
self.accessibilityEnabled = accessibilityEnabled
50-
self.accessibilityMarkers = accessibilityMarkers
5137
self.colorScheme = colorScheme
5238
self.appStoreSnapshot = appStoreSnapshot
5339
}
5440

5541
public let image: Result<ImageType, Error>
5642
public let precision: Float?
5743
public let accessibilityEnabled: Bool?
58-
public let accessibilityMarkers: [AccessibilityMark]?
5944
public let colorScheme: ColorScheme?
6045
public let appStoreSnapshot: Bool?
6146
}

Sources/SnapshotPreviewsCore/SwiftUIRenderingStrategy.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ public class SwiftUIRenderingStrategy: RenderingStrategy {
3535
let image = renderer.nsImage
3636
#endif
3737
if let image {
38-
completion(SnapshotResult(image: .success(image), precision: wrappedView.precision, accessibilityEnabled: wrappedView.accessibilityEnabled, accessibilityMarkers: [], colorScheme: colorScheme, appStoreSnapshot: wrappedView.appStoreSnapshot))
38+
completion(SnapshotResult(image: .success(image), precision: wrappedView.precision, accessibilityEnabled: wrappedView.accessibilityEnabled, colorScheme: colorScheme, appStoreSnapshot: wrappedView.appStoreSnapshot))
3939
} else {
40-
completion(SnapshotResult(image: .failure(RenderingError.failedRendering(.zero)), precision: wrappedView.precision, accessibilityEnabled: wrappedView.accessibilityEnabled, accessibilityMarkers: [], colorScheme: colorScheme, appStoreSnapshot: wrappedView.appStoreSnapshot))
40+
completion(SnapshotResult(image: .failure(RenderingError.failedRendering(.zero)), precision: wrappedView.precision, accessibilityEnabled: wrappedView.accessibilityEnabled, colorScheme: colorScheme, appStoreSnapshot: wrappedView.appStoreSnapshot))
4141
}
4242
}
4343
}

Sources/SnapshotPreviewsCore/UIKitRenderingStrategy.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import SwiftUI
1212

1313
public class UIKitRenderingStrategy: RenderingStrategy {
1414

15-
public init() {
15+
public init(a11yWrapper: ((UIViewController, UIWindow, PreviewLayout) -> UIView)? = nil) {
1616
let windowScene = UIApplication.shared
1717
.connectedScenes
1818
.filter { $0.activationState == .foregroundActive }
@@ -23,13 +23,15 @@ public class UIKitRenderingStrategy: RenderingStrategy {
2323
window.backgroundColor = UIColor.systemBackground
2424
window.makeKeyAndVisible()
2525
self.window = window
26+
self.a11yWrapper = a11yWrapper
2627
}
2728

2829
private var windowScene: UIWindowScene? {
2930
window.windowScene
3031
}
3132

3233
private let window: UIWindow
34+
private let a11yWrapper: ((UIViewController, UIWindow, PreviewLayout) -> UIView)?
3335
private var geometryUpdateError: Error?
3436

3537
@MainActor
@@ -64,15 +66,15 @@ public class UIKitRenderingStrategy: RenderingStrategy {
6466
) {
6567
if let geometryUpdateError {
6668
if (geometryUpdateError as NSError).userInfo["BSErrorCodeDescription"] as? String == "timeout" {
67-
completion(SnapshotResult(image: .failure(RenderingError.orientationChangeTimeout), precision: nil, accessibilityEnabled: nil, accessibilityMarkers: nil, colorScheme: nil, appStoreSnapshot: nil))
69+
completion(SnapshotResult(image: .failure(RenderingError.orientationChangeTimeout), precision: nil, accessibilityEnabled: nil, colorScheme: nil, appStoreSnapshot: nil))
6870
return
6971
}
70-
completion(SnapshotResult(image: .failure(geometryUpdateError), precision: nil, accessibilityEnabled: nil, accessibilityMarkers: nil, colorScheme: nil, appStoreSnapshot: nil))
72+
completion(SnapshotResult(image: .failure(geometryUpdateError), precision: nil, accessibilityEnabled: nil, colorScheme: nil, appStoreSnapshot: nil))
7173
return
7274
}
7375
guard attempts > 0 else {
7476
let timeoutError = NSError(domain: "OrientationChangeTimeout", code: 0, userInfo: [NSLocalizedDescriptionKey: "Orientation change timed out"])
75-
completion(SnapshotResult(image: .failure(timeoutError), precision: nil, accessibilityEnabled: nil, accessibilityMarkers: nil, colorScheme: nil, appStoreSnapshot: nil))
77+
completion(SnapshotResult(image: .failure(timeoutError), precision: nil, accessibilityEnabled: nil, colorScheme: nil, appStoreSnapshot: nil))
7678
return
7779
}
7880

@@ -96,7 +98,8 @@ public class UIKitRenderingStrategy: RenderingStrategy {
9698
layout: preview.layout,
9799
controller: controller,
98100
window: window,
99-
async: false) { result in
101+
async: false,
102+
a11yWrapper: a11yWrapper) { result in
100103
completion(result)
101104
}
102105
}

Sources/SnapshotPreviewsCore/View+Snapshot.swift

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,8 @@ public enum RenderingError: Error {
1818
import Foundation
1919
import SwiftUI
2020
import UIKit
21-
import AccessibilitySnapshotCore
2221
import SnapshotSharedModels
2322

24-
extension AccessibilityMarker: AccessibilityMark {
25-
public var accessibilityShape: MarkerShape {
26-
switch shape {
27-
case .frame(let frame):
28-
return .frame(frame)
29-
case .path(let path):
30-
return .path(path)
31-
}
32-
}
33-
}
34-
3523
private var _colorScheme: ColorScheme? = nil
3624

3725
extension View {
@@ -59,6 +47,7 @@ extension View {
5947
controller: ExpandingViewController,
6048
window: UIWindow,
6149
async: Bool,
50+
a11yWrapper: ((UIViewController, UIWindow, PreviewLayout) -> UIView)? = nil,
6251
completion: @escaping (SnapshotResult) -> Void)
6352
{
6453
controller.expansionSettled = { [weak controller, weak window] renderingMode, precision, accessibilityEnabled, appStoreSnapshot, error in
@@ -68,44 +57,26 @@ extension View {
6857

6958
if let error {
7059
DispatchQueue.main.async {
71-
completion(SnapshotResult(image: .failure(error), precision: precision, accessibilityEnabled: accessibilityEnabled, accessibilityMarkers: nil, colorScheme: _colorScheme, appStoreSnapshot: appStoreSnapshot))
60+
completion(SnapshotResult(image: .failure(error), precision: precision, accessibilityEnabled: accessibilityEnabled, colorScheme: _colorScheme, appStoreSnapshot: appStoreSnapshot))
7261
}
7362
return
7463
}
7564

7665
if async {
7766
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
7867
let imageResult = Self.takeSnapshot(layout: layout, renderingMode: renderingMode, window: window, rootVC: containerVC, targetView: controller.view)
79-
completion(SnapshotResult(image: imageResult.mapError { $0 }, precision: precision, accessibilityEnabled: accessibilityEnabled, accessibilityMarkers: nil, colorScheme: _colorScheme, appStoreSnapshot: appStoreSnapshot))
68+
completion(SnapshotResult(image: imageResult.mapError { $0 }, precision: precision, accessibilityEnabled: accessibilityEnabled, colorScheme: _colorScheme, appStoreSnapshot: appStoreSnapshot))
8069
}
8170
} else {
8271
DispatchQueue.main.async {
83-
if let accessibilityEnabled, accessibilityEnabled {
84-
let containedView: UIView
85-
switch layout {
86-
case .device:
87-
containedView = containerVC.view
88-
default:
89-
containedView = controller.view
90-
}
91-
let mode = controller.view.bounds.size.requiresCoreAnimationSnapshot ? AccessibilitySnapshotView.ViewRenderingMode.renderLayerInContext : renderingMode?.a11yRenderingMode
92-
let a11yView = AccessibilitySnapshotView(
93-
containedView: containedView,
94-
viewRenderingMode: mode ?? .drawHierarchyInRect,
95-
activationPointDisplayMode: .never,
96-
showUserInputLabels: true)
97-
98-
a11yView.center = window.center
99-
window.addSubview(a11yView)
100-
101-
let elements = try? a11yView.parseAccessibility(useMonochromeSnapshot: false)
102-
a11yView.sizeToFit()
72+
if let a11yWrapper, let accessibilityEnabled, accessibilityEnabled {
73+
let a11yView = a11yWrapper(controller, window, layout)
10374
let result = Self.takeSnapshot(layout: .sizeThatFits, renderingMode: renderingMode, window: window, rootVC: containerVC, targetView: a11yView)
10475
a11yView.removeFromSuperview()
105-
completion(SnapshotResult(image: result.mapError { $0 }, precision: precision, accessibilityEnabled: accessibilityEnabled, accessibilityMarkers: elements, colorScheme: _colorScheme, appStoreSnapshot: appStoreSnapshot))
76+
completion(SnapshotResult(image: result.mapError { $0 }, precision: precision, accessibilityEnabled: accessibilityEnabled, colorScheme: _colorScheme, appStoreSnapshot: appStoreSnapshot))
10677
} else {
10778
let imageResult = Self.takeSnapshot(layout: layout, renderingMode: renderingMode, window: window, rootVC: containerVC, targetView: controller.view)
108-
completion(SnapshotResult(image: imageResult.mapError { $0 }, precision: precision, accessibilityEnabled: accessibilityEnabled, accessibilityMarkers: nil, colorScheme: _colorScheme, appStoreSnapshot: appStoreSnapshot))
79+
completion(SnapshotResult(image: imageResult.mapError { $0 }, precision: precision, accessibilityEnabled: accessibilityEnabled, colorScheme: _colorScheme, appStoreSnapshot: appStoreSnapshot))
10980
}
11081
}
11182
}
@@ -223,17 +194,6 @@ extension UIView {
223194
}
224195
}
225196

226-
extension EmergeRenderingMode {
227-
var a11yRenderingMode: AccessibilitySnapshotView.ViewRenderingMode {
228-
switch self {
229-
case .coreAnimation:
230-
return .renderLayerInContext
231-
case .window, .uiView:
232-
return .drawHierarchyInRect
233-
}
234-
}
235-
}
236-
237197
extension CALayer {
238198

239199
var layerForSnapshot: Self {

0 commit comments

Comments
 (0)