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

Improve readiness checks #33

Merged
merged 1 commit into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ extension EventGenerator {
public func fingerDown(_ indices: [FingerIndex?] = .automatic, at locations: [HammerLocatable]) throws {
let indices = try self.fillNextFingerIndices(indices, withExpected: locations.count)
let locations = try locations.map { try $0.windowHitPoint(for: self) }
try self.checkPointsAreHittable(locations)
try self.sendEvent(hand: HandInfo(fingers: zip(locations, indices).map { location, index in
FingerInfo(fingerIndex: index, location: location, phase: .began,
pressure: 0, twist: 0, majorRadius: kDefaultRadius, minorRadius: kDefaultRadius)
Expand Down Expand Up @@ -443,6 +442,8 @@ extension EventGenerator {
///
/// - parameter hand: The event to send.
private func sendEvent(hand: HandInfo) throws {
try checkPointsAreHittable(hand.fingers.map(\.location))

let machTime = mach_absolute_time()
let isTouching = hand.isTouching

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ extension EventGenerator {
/// - parameter key: The keyboard key for the event.
/// - parameter isKeyDown: If the key is currently pressed down.
private func sendKeyboardEvent(key: KeyboardKey, isKeyDown: Bool) throws {
guard self.isWindowReady else {
throw HammerError.windowIsNotReadyForInteraction
}

guard self.window.isKeyWindow else {
throw HammerError.windowIsNotKey
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ extension EventGenerator {
azimuth: CGFloat = 0, altitude: CGFloat = 0, pressure: CGFloat = 0) throws
{
let location = try (location ?? self.mainView).windowHitPoint(for: self)
try self.checkPointsAreHittable([location])
try self.sendEvent(stylus: StylusInfo(location: location, phase: .began,
pressure: pressure, twist: 0,
altitude: altitude, azimuth: azimuth))
Expand Down Expand Up @@ -163,6 +162,8 @@ extension EventGenerator {
///
/// - parameter stylus: The event to send.
private func sendEvent(stylus: StylusInfo) throws {
try self.checkPointsAreHittable([stylus.location])

let machTime = mach_absolute_time()
let isTouching = stylus.phase.isTouching

Expand Down
7 changes: 2 additions & 5 deletions Sources/Hammer/EventGenerator/EventGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ public final class EventGenerator {

/// Returns if the window is ready to receive user interaction events
public var isWindowReady: Bool {
guard self.window.isHidden == false
guard !UIApplication.shared.isIgnoringInteractionEvents
&& self.window.isHidden == false
&& self.window.isUserInteractionEnabled
&& self.window.rootViewController?.viewIfLoaded != nil
&& self.window.rootViewController?.isBeingPresented == false
Expand All @@ -132,10 +133,6 @@ public final class EventGenerator {
guard self.window.windowScene?.activationState == .foregroundActive else {
return false
}
} else {
guard !UIApplication.shared.isIgnoringInteractionEvents else {
return false
}
}

return true
Expand Down
16 changes: 16 additions & 0 deletions Sources/Hammer/Utilties/Subviews.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ extension EventGenerator {
///
/// - returns: If the view is hittable
public func viewIsHittable(_ view: UIView, atPoint point: CGPoint? = nil) -> Bool {
guard self.isWindowReady else {
return false
}

guard self.viewIsVisible(view) else {
return false
}
Expand Down Expand Up @@ -270,6 +274,10 @@ extension EventGenerator {
///
/// - returns: If the point is hittable
public func pointIsHittable(_ point: CGPoint) -> Bool {
guard self.isWindowReady else {
return false
}

return self.window.hitTest(point, with: nil) != nil
}

Expand All @@ -279,6 +287,10 @@ extension EventGenerator {
///
/// - throws: If one of the points is not hittable
func checkPointsAreHittable(_ points: [CGPoint]) throws {
guard self.isWindowReady else {
throw HammerError.windowIsNotReadyForInteraction
}

for point in points {
if !self.pointIsHittable(point) {
throw HammerError.pointIsNotHittable(point)
Expand All @@ -298,6 +310,10 @@ extension EventGenerator {
throw HammerError.viewIsNotInHierarchy(view)
}

guard self.isWindowReady else {
throw HammerError.windowIsNotReadyForInteraction
}

guard self.viewIsVisible(view) else {
throw HammerError.viewIsNotVisible(view)
}
Expand Down