-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add tap handler, send span #7
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| // swift-tools-version: 6.1 | ||
| // swift-tools-version: 5.9 | ||
| import PackageDescription | ||
|
|
||
| let package = Package( | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import UIKit.UIWindow | ||
| import Common | ||
|
|
||
| public final class TapHandler { | ||
| private var startPoint: CGPoint? | ||
|
|
||
| public init() {} | ||
|
|
||
| public func handle(event: UIEvent, window: UIWindow, completion: (TouchEvent) -> Void) { | ||
| if let touches = event.allTouches, let touch = touches.first, let targetView = touch.view { | ||
| switch touch.phase { | ||
| case .began: | ||
| startPoint = touch.location(in: window) | ||
| case .ended: | ||
| if let startPoint { | ||
| let endPoint = touch.location(in: window) | ||
| let dx = endPoint.x - startPoint.x | ||
| let dy = endPoint.y - startPoint.y | ||
| if abs(dx) < 10 && abs(dy) < 10 { | ||
| let accessibilityIdentifier = targetView.accessibilityIdentifier | ||
| let targetClass = type(of: targetView) | ||
|
|
||
| let viewName = accessibilityIdentifier ?? String(describing: targetClass) | ||
|
|
||
| completion( | ||
| .init( | ||
| location: endPoint, | ||
| viewName: viewName, | ||
| accessibilityIdentifier: accessibilityIdentifier, | ||
| scale: targetView.window?.screen.scale ?? UIScreen.main.scale) | ||
| ) | ||
| } | ||
| } | ||
| startPoint = nil | ||
| default: | ||
| break | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import UIKit | ||
|
|
||
| public struct TouchEvent: Sendable, CustomStringConvertible { | ||
| public let location: CGPoint | ||
| public let viewName: String | ||
| public let accessibilityIdentifier: String? | ||
| public let scale: CGFloat | ||
|
|
||
| public var locationInPoints: CGPoint { | ||
| return location | ||
| } | ||
|
|
||
| public var locationInPixels: CGPoint { | ||
| return CGPoint(x: Int((location.x * scale).rounded()), y: Int((location.y * scale).rounded())) | ||
| } | ||
|
|
||
| public var description: String { | ||
| return "TouchEvent(\(location), \(viewName), \(accessibilityIdentifier ?? "nil"), \(scale)) coordinates In pixels: \(locationInPixels.x), \(locationInPixels.y)" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import UIKit.UIWindow | ||
|
|
||
| public final class UIWindowSendEvent { | ||
| typealias SendEventRef = @convention(c) (UIWindow, Selector, UIEvent) -> Void | ||
| private static let sendEvenSelector = #selector(UIWindow.sendEvent(_:)) | ||
|
|
||
| public static func inject( | ||
| into subclasses: [UIWindow.Type] = [], | ||
| block: @escaping (UIWindow, UIEvent) -> Void | ||
| ) { | ||
| guard let originalMethod = class_getInstanceMethod(UIWindow.self, sendEvenSelector) else { return } | ||
|
|
||
| var originalIMP = Optional<IMP>.none | ||
|
|
||
| let swizzledSendEventBlock: @convention(block) (UIWindow, UIEvent) -> Void = { window, event in | ||
| if let originalIMP = originalIMP { | ||
| let castedIMP = unsafeBitCast(originalIMP, to: SendEventRef.self) | ||
| castedIMP(window, sendEvenSelector, event) | ||
| } | ||
|
|
||
| guard !subclasses.isEmpty else { | ||
| return block(window, event) | ||
| } | ||
| if UIWindowSendEvent.shouldInject(into: window, subclasses: subclasses) { | ||
| block(window, event) | ||
| } | ||
| } | ||
|
|
||
| let swizzledIMP = imp_implementationWithBlock(unsafeBitCast(swizzledSendEventBlock, to: AnyObject.self)) | ||
| originalIMP = method_setImplementation(originalMethod, swizzledIMP) | ||
| } | ||
|
|
||
| private static func shouldInject(into receiver: Any, subclasses: [UIWindow.Type]) -> Bool { | ||
| let ids = subclasses.map { ObjectIdentifier($0) } | ||
| let receiverType = type(of: receiver) | ||
| return ids.contains(ObjectIdentifier(receiverType)) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import Foundation | ||
|
|
||
| extension CGFloat { | ||
| public func toString() -> String { | ||
| String(format: "%.2f", self) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how does this work with multiple touches that happen at the same time (multiple fingers) - would those be different touch events?