Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176import AppKitpublic 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>) -> 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<UInt32>() 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>) -> 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>) -> 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<UInt32>, 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> ) -> UInt32? { nativeHoverBackdropLookup()(point, owner, ignoredWindows) }
@MainActor public static func nativeHoverBackdropLookup() -> @MainActor (CGPoint, UInt32, Set<UInt32>) -> 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<UInt32> = [], 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) }}