Skip to content
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
18 changes: 17 additions & 1 deletion Sources/Hammer/EventGenerator/EventGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,23 @@ public final class EventGenerator {
}

public var isWindowReady: Bool {
return self.hasVisibleWindowContent && self.window.isKeyWindow
return self.hasVisibleWindowContent && self.window.isKeyWindow && self.mapsCGEventLocations
}

/// Whether `NSEvent(cgEvent:)` maps window locations with the window's current frame.
///
/// Dragged events go through a `CGEvent` to carry deltas. `NSEvent(cgEvent:)` maps the
/// global location back with window server geometry instead of `NSWindow.frame`, and
/// AppKit updates that geometry asynchronously after the window is created or moved.
private var mapsCGEventLocations: Bool {
let location = NSPoint.zero
guard let event = NSEvent.mouseEvent(
with: .mouseMoved, location: location, modifierFlags: [], timestamp: 0,
windowNumber: self.window.windowNumber, context: nil, eventNumber: 0, clickCount: 0, pressure: 0
), let cgEvent = event.cgEvent, let mappedEvent = NSEvent(cgEvent: cgEvent) else {
return false
}
return mappedEvent.locationInWindow == location
}

private var hasVisibleWindowContent: Bool {
Expand Down
31 changes: 28 additions & 3 deletions Sources/Hammer/Utilties/HammerWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,17 @@ import AppKit

/// A reusable test window that receives events without taking application focus.
///
/// The window stays outside all screens by default. Set `HAMMER_SHOW_TEST_WINDOW=1`
/// or pass `showWindow: true` to display it while debugging.
/// The window stays at a fixed position outside all screens by default. Set
/// `HAMMER_SHOW_TEST_WINDOW=1` or pass `showWindow: true` to display it while debugging.
@MainActor
public final class HammerWindow: NSWindow {
private var isClosed = false

// The window server applies frame changes asynchronously, and NSEvent(cgEvent:) maps
// locations with its geometry. Like WebKitTestRunnerWindow, keep the position fixed and
// apply only size changes so synthetic events do not race a pending move.
private var keepsOriginFixed = false

// AppKit must treat this test surface as key to dispatch the first mouse down.
// Keep this state local to the window instead of changing NSApplication.keyWindow.
public override var isKeyWindow: Bool { !self.isClosed && self.isVisible }
Expand All @@ -169,9 +174,11 @@ public final class HammerWindow: NSWindow {
public init(size: CGSize,
showWindow: Bool = ProcessInfo.processInfo.environment["HAMMER_SHOW_TEST_WINDOW"] == "1") {
let screens = NSScreen.screens.reduce(NSRect.zero) { $0.union($1.frame) }
let origin = NSPoint(x: screens.minX - size.width - 100, y: screens.minY)
// Leave room for later size changes, because the origin no longer follows them.
let origin = NSPoint(x: screens.minX - 10_000, y: screens.minY)
super.init(contentRect: NSRect(origin: origin, size: size),
styleMask: showWindow ? [.titled] : [.borderless], backing: .buffered, defer: false)
self.keepsOriginFixed = !showWindow
self.isReleasedWhenClosed = false
self.animationBehavior = .none
self.hasShadow = false
Expand All @@ -190,5 +197,23 @@ public final class HammerWindow: NSWindow {
self.isClosed = true
super.close()
}

public override func setFrameOrigin(_ point: NSPoint) {
guard !self.keepsOriginFixed else { return }
super.setFrameOrigin(point)
}

public override func setFrame(_ frameRect: NSRect, display flag: Bool) {
super.setFrame(self.frameKeepingOrigin(frameRect), display: flag)
}

public override func setFrame(_ frameRect: NSRect, display displayFlag: Bool, animate animateFlag: Bool) {
super.setFrame(self.frameKeepingOrigin(frameRect), display: displayFlag, animate: animateFlag)
}

private func frameKeepingOrigin(_ frameRect: NSRect) -> NSRect {
guard self.keepsOriginFixed else { return frameRect }
return NSRect(origin: self.frame.origin, size: frameRect.size)
}
}
#endif
Loading