import AppKit @MainActor enum WindowPlacement { static func centeredFrame(size: NSSize, on screen: NSScreen? = NSScreen.main) -> NSRect { let bounds = (screen?.visibleFrame ?? NSScreen.screens.first?.visibleFrame ?? .zero) return self.centeredFrame(size: size, in: bounds) } static func topRightFrame( size: NSSize, padding: CGFloat, on screen: NSScreen? = NSScreen.main) -> NSRect { let bounds = (screen?.visibleFrame ?? NSScreen.screens.first?.visibleFrame ?? .zero) return self.topRightFrame(size: size, padding: padding, in: bounds) } static func centeredFrame(size: NSSize, in bounds: NSRect) -> NSRect { if bounds == .zero { return NSRect(origin: .zero, size: size) } let clampedWidth = min(size.width, bounds.width) let clampedHeight = min(size.height, bounds.height) let x = round(bounds.minX + (bounds.width - clampedWidth) / 2) let y = round(bounds.minY + (bounds.height - clampedHeight) / 2) return NSRect(x: x, y: y, width: clampedWidth, height: clampedHeight) } static func topRightFrame(size: NSSize, padding: CGFloat, in bounds: NSRect) -> NSRect { if bounds == .zero { return NSRect(origin: .zero, size: size) } let clampedWidth = min(size.width, bounds.width) let clampedHeight = min(size.height, bounds.height) let x = round(bounds.maxX - clampedWidth - padding) let y = round(bounds.maxY - clampedHeight - padding) return NSRect(x: x, y: y, width: clampedWidth, height: clampedHeight) } static func cascadedFrame(from frame: NSRect, offset: CGFloat = 24, in bounds: NSRect) -> NSRect { guard bounds != .zero else { return frame.offsetBy(dx: offset, dy: -offset) } let width = min(frame.width, bounds.width) let height = min(frame.height, bounds.height) let maxX = bounds.maxX - width let maxY = bounds.maxY - height let x = maxX >= bounds.minX ? min(max(frame.minX + offset, bounds.minX), maxX) : bounds.minX let y = maxY >= bounds.minY ? min(max(frame.minY - offset, bounds.minY), maxY) : bounds.minY return NSRect(x: x, y: y, width: width, height: height) } static func ensureOnScreen( window: NSWindow, defaultSize: NSSize, fallback: ((NSScreen?) -> NSRect)? = nil) { let frame = window.frame let targetScreens = NSScreen.screens.isEmpty ? [NSScreen.main].compactMap(\.self) : NSScreen.screens let isVisibleSomewhere = targetScreens.contains { screen in frame.intersects(screen.visibleFrame.insetBy(dx: 12, dy: 12)) } if isVisibleSomewhere { return } let screen = NSScreen.main ?? targetScreens.first let next = fallback?(screen) ?? self.centeredFrame(size: defaultSize, on: screen) window.setFrame(next, display: false) } }