import Foundation import AppKit // NSScreen, NSDeviceDescriptionKey — macOS-only target, no portability concern /// A remembered rectangle, in CoreGraphics global display coordinates (origin top-left, /// y increasing downward — see the conversion note below). public struct CaptureRegion: Codable, Sendable, Equatable { public let displayID: CGDirectDisplayID public let rect: CGRect public let capturedScale: CGFloat public init(displayID: CGDirectDisplayID, rect: CGRect, capturedScale: CGFloat) { self.displayID = displayID self.rect = rect self.capturedScale = capturedScale } /// Converts an AppKit rect in GLOBAL AppKit coordinates (bottom-left origin, y increasing /// upward, anchored at the bottom-left of the PRIMARY screen — exactly what /// `NSWindow.frame`, `NSEvent.mouseLocation` and `NSScreen.frame` already report; no /// screen-local conversion is needed here) into global CoreGraphics coordinates /// (top-left origin, y increasing downward, same primary-screen anchor). Both coordinate /// systems share the SAME horizontal origin and the SAME anchor rectangle (the primary /// screen's bounds) — only the vertical axis is mirrored around the primary's height. /// That is why the flip below is correct for every attached screen, including one with a /// negative AppKit x (to the left of primary) or a y that places it above the primary /// (whose CG y then comes out negative): x is untouched, and the primary height is the /// one fixed reference both systems agree on regardless of which physical screen the /// rect is actually on. public static func fromAppKit(rect: CGRect, on screen: NSScreen) -> CaptureRegion { let displayID = (screen.deviceDescription[NSDeviceDescriptionKey("NSScreenNumber")] as? NSNumber)?.uint32Value ?? CGMainDisplayID() let primaryHeight = NSScreen.screens.first?.frame.height ?? screen.frame.height return fromAppKit(rect: rect, displayID: displayID, capturedScale: screen.backingScaleFactor, primaryHeight: primaryHeight) } /// Injectable overload for deterministic geometry tests — no dependency on real attached /// displays. The public overload above is a thin adapter over this. static func fromAppKit(rect: CGRect, displayID: CGDirectDisplayID, capturedScale: CGFloat, primaryHeight: CGFloat) -> CaptureRegion { let cgY = primaryHeight - rect.origin.y - rect.height let cgRect = CGRect(x: rect.origin.x, y: cgY, width: rect.width, height: rect.height) return CaptureRegion(displayID: displayID, rect: cgRect, capturedScale: capturedScale) } /// True when `displayID` is still attached AND `rect` still lies inside its current /// bounds. Uses `CGDisplayIsActive` (returns 0 safely for an unknown id, never traps) and /// `CGDisplayBounds` — both real CoreGraphics calls, no injection needed since this is a /// live-state check by design. public var isStillValid: Bool { guard CGDisplayIsActive(displayID) != 0 else { return false } let bounds = CGDisplayBounds(displayID) return bounds.contains(rect) } /// Persisted in UserDefaults under this key BY THE APP (WP-4a), never by this type. public static let defaultsKey = "ai.flowmaster.shotdeck.region" }