Skip to content

Commit

Permalink
Improve readiness checks (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriellanata authored Oct 4, 2021
1 parent 82e06c3 commit 7318148
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
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

0 comments on commit 7318148

Please sign in to comment.