Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add utilities for highlighting hit test regions #119

Merged
merged 1 commit into from
Jan 19, 2024

Conversation

NickEntin
Copy link
Collaborator

@NickEntin NickEntin commented Mar 12, 2023

This introduces new snapshotting methods for snapshotting a view with its hit targets highlighted.

@NickEntin NickEntin force-pushed the entin/hit-test-snapshot branch from ec833a8 to 403fd06 Compare August 16, 2023 23:30
@NickEntin NickEntin force-pushed the entin/hit-test-snapshot branch from 403fd06 to c668896 Compare August 30, 2023 23:25
@NickEntin NickEntin force-pushed the entin/hit-test-snapshot branch 2 times, most recently from 0a2c426 to cff0bb8 Compare December 22, 2023 01:34
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
<dict>
<key>baselineAverage</key>
<real>48.792810</real>
Copy link
Collaborator Author

@NickEntin NickEntin Dec 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now this is very slow, but I have some plans for speeding it up (with the trade-off of potentially missing very small touch targets). I wanted to land this version first so we have a baseline for the extremely accurate version and can iterate from here.

@NickEntin NickEntin force-pushed the entin/hit-test-snapshot branch 3 times, most recently from 4631461 to 9f7f45b Compare December 22, 2023 06:16
@NickEntin NickEntin force-pushed the entin/hit-test-snapshot branch 4 times, most recently from 88581fb to 85ffea5 Compare January 4, 2024 03:17
@NickEntin NickEntin marked this pull request as ready for review January 4, 2024 03:23
@NickEntin NickEntin changed the title [RFC] Add utilities for highlighting hit test regions Add utilities for highlighting hit test regions Jan 4, 2024
This adds new snapshotting methods for drawing the snapshotted view with hit test regions highlighted.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome!

@@ -786,161 +785,6 @@ private extension AccessibilitySnapshotView {

// MARK: -

private extension UIView {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for other reviewers: these methods are just being moved (without any edits) to new files

var lastHit: (CGFloat, UIView?)? = nil

// Step through every pixel along the X axis.
for x in stride(from: bounds.minX, to: bounds.maxX, by: pixelWidth) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine this nested loop is a factor in the slow performance you commented on above. If we knew beforehand that all of the hit targets in question were rectangular, we could probably optimize this to just look for the overall width/height of the target, but this scanning seems necessary for anything non-rectangular.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...sure enough, this is addressed in #177

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, exactly. Guaranteed rectangular hit targets would make this way easier, but given how flexible the hit test APIs are, this is the only way I can think of to ensure we always get it correct.


private func monochromeSnapshot(for snapshot: UIImage) throws -> UIImage? {
if ProcessInfo().operatingSystemVersion.majorVersion == 13 {
// On iOS 13, the image filter silently fails for large images, "successfully" producing a blank output
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gotta love when xcode fails successfully

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't even understand why it has to here. It could just return a nil outputImage, but nope 🤷‍♂️

Copy link
Collaborator

@dnagler dnagler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it!

@NickEntin NickEntin merged commit a79b01b into master Jan 19, 2024
6 checks passed
@NickEntin NickEntin deleted the entin/hit-test-snapshot branch January 19, 2024 00:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants