import AppKit public import CoreGraphics extension WindowHitTest { public enum NativeHit: Equatable, Sendable { case empty case window(DragWindow) case unavailable } @MainActor public static func nativeWindowNumber(at point: CGPoint) -> UInt32? { nativeWindowNumber(at: point, ignoring: []) } @MainActor public static func nativeWindowNumber(at point: CGPoint, ignoring ignoredWindows: Set) -> UInt32? { guard point.x.isFinite, point.y.isFinite else { return nil } _ = NSApplication.shared guard let top = NSScreen.screens.first?.frame.maxY else { return nil } let location = NSPoint(x: point.x, y: top - point.y) var below = 0 var visited = Set() while true { guard let number = UInt32( exactly: NSWindow.windowNumber( at: location, belowWindowWithWindowNumber: below)) else { return nil } guard number != kCGNullWindowID, ignoredWindows.contains(number) else { return number } guard visited.insert(number).inserted else { return nil } below = Int(number) } } @MainActor public static func nativeHit(at point: CGPoint, windows: [DragWindow]) -> NativeHit { resolveNativeHit(number: nativeWindowNumber(at: point), windows: windows) } @MainActor public static func nativeHoverWindowNumber(at point: CGPoint) -> UInt32? { nativeHoverWindowNumber(at: point, ignoring: []) } @MainActor public static func nativeHoverWindowNumber(at point: CGPoint, ignoring ignoredWindows: Set) -> UInt32? { guard let number = nativeWindowNumber(at: point, ignoring: ignoredWindows) else { return nil } if number == kCGNullWindowID { return number } guard let records = CGWindowListCopyWindowInfo(.optionIncludingWindow, number) as? [NSDictionary], let record = records.first, let layer = record[kCGWindowLayer] as? NSNumber, let bounds = record[kCGWindowBounds] as? NSDictionary, let x = bounds["X"] as? NSNumber, let y = bounds["Y"] as? NSNumber, let width = bounds["Width"] as? NSNumber, let height = bounds["Height"] as? NSNumber else { return nil } let frame = CGRect(x: x.doubleValue, y: y.doubleValue, width: width.doubleValue, height: height.doubleValue) return hoverHit(number: number, layer: layer.int32Value, bounds: frame, at: point) } @MainActor public static func nativeFrontmostWindowNumber(in candidates: Set) -> UInt32? { guard let records = CGWindowListCopyWindowInfo(.optionOnScreenOnly, kCGNullWindowID) as? [NSDictionary] else { return nil } return frontmostHoverWindow(in: candidates, records: records.compactMap(hoverStackRecord)) } static func frontmostHoverWindow(in candidates: Set, records: [HoverStackRecord]) -> UInt32? { guard !candidates.isEmpty, !candidates.contains(kCGNullWindowID) else { return nil } let matching = records.filter { candidates.contains($0.number) } guard matching.count == candidates.count, Set(matching.map(\.number)) == candidates, matching.allSatisfy({ $0.layer == CGWindowLevelForKey(.normalWindow) && validHoverBounds($0.bounds) }) else { return nil } return matching.first?.number } @MainActor public static func nativeHoverBackdropWindowNumber( at point: CGPoint, owner: UInt32, ignoring ignoredWindows: Set ) -> UInt32? { nativeHoverBackdropLookup()(point, owner, ignoredWindows) } @MainActor public static func nativeHoverBackdropLookup() -> @MainActor (CGPoint, UInt32, Set) -> UInt32? { guard let records = CGWindowListCopyWindowInfo(.optionOnScreenOnly, kCGNullWindowID) as? [NSDictionary] else { return { _, _, _ in nil } } let snapshot = records.compactMap(hoverStackRecord) let indexed = Dictionary(grouping: snapshot, by: \.number) return { point, owner, ignoredWindows in guard let number = nativeWindowNumber(at: point, ignoring: ignoredWindows) else { return nil } let normalized = hoverSnapshotHit(number: number, records: indexed[number] ?? [], at: point) return hoverBackdropHit( number: normalized, owner: owner, records: snapshot, ignoring: ignoredWindows, at: point) } } struct HoverStackRecord: Sendable { let number: UInt32 let layer: Int32 let bounds: CGRect } static func hoverSnapshotHit(number: UInt32?, records: [HoverStackRecord], at point: CGPoint) -> UInt32? { guard point.x.isFinite, point.y.isFinite, let number else { return nil } if number == kCGNullWindowID { return number } guard records.count == 1, let record = records.first, record.number == number else { return nil } return hoverHit(number: number, layer: record.layer, bounds: record.bounds, at: point) } private static func hoverStackRecord(_ record: NSDictionary) -> HoverStackRecord? { guard let rawNumber = record[kCGWindowNumber] as? NSNumber, let number = UInt32(exactly: rawNumber.doubleValue), number != kCGNullWindowID, let rawLayer = record[kCGWindowLayer] as? NSNumber, let layer = Int32(exactly: rawLayer.doubleValue), let bounds = record[kCGWindowBounds] as? NSDictionary, let x = bounds["X"] as? NSNumber, let y = bounds["Y"] as? NSNumber, let width = bounds["Width"] as? NSNumber, let height = bounds["Height"] as? NSNumber else { return nil } return HoverStackRecord( number: number, layer: layer, bounds: CGRect(x: x.doubleValue, y: y.doubleValue, width: width.doubleValue, height: height.doubleValue)) } static func hoverBackdropHit( number: UInt32?, owner: UInt32, records: [HoverStackRecord], ignoring ignoredWindows: Set = [], at point: CGPoint ) -> UInt32? { guard point.x.isFinite, point.y.isFinite, owner != kCGNullWindowID, let number, !ignoredWindows.contains(owner), !ignoredWindows.contains(number) else { return nil } let ranked = records.filter { !ignoredWindows.contains($0.number) } let owners = ranked.indices.filter { ranked[$0].number == owner } guard owners.count == 1, let ownerRank = owners.first else { return nil } let ownerRecord = ranked[ownerRank] guard validHoverBounds(ownerRecord.bounds), ownerRecord.layer == CGWindowLevelForKey(.normalWindow), !ownerRecord.bounds.contains(point) else { return nil } if number == kCGNullWindowID || number == owner { return kCGNullWindowID } let hits = ranked.indices.filter { ranked[$0].number == number } guard hits.count == 1, let hitRank = hits.first else { return nil } let hit = ranked[hitRank] guard validHoverBounds(hit.bounds) else { return nil } guard hit.layer == CGWindowLevelForKey(.normalWindow) else { return number } guard hit.bounds.contains(point) else { return nil } return hitRank > ownerRank ? kCGNullWindowID : number } private static func validHoverBounds(_ bounds: CGRect) -> Bool { [bounds.minX, bounds.minY, bounds.width, bounds.height].allSatisfy(\.isFinite) && bounds.width > 0 && bounds.height > 0 } static func hoverHit(number: UInt32, layer: Int32, bounds: CGRect, at point: CGPoint) -> UInt32? { guard [bounds.minX, bounds.minY, bounds.width, bounds.height].allSatisfy(\.isFinite), bounds.width > 0, bounds.height > 0 else { return nil } if layer == CGWindowLevelForKey(.desktopWindow) || layer == CGWindowLevelForKey(.desktopIconWindow) { return kCGNullWindowID } // Native resize hit regions extend outside normal windows. Passive chrome can // occupy that empty visual margin without claiming or changing its mouse input. if layer == CGWindowLevelForKey(.normalWindow), !bounds.contains(point) { return kCGNullWindowID } return number } static func resolveNativeHit(number: UInt32?, windows: [DragWindow]) -> NativeHit { guard let number else { return .unavailable } if number == kCGNullWindowID { return .empty } guard let window = windows.first(where: { $0.windowNumber == number }) else { return .unavailable } return .window(window) } }