import Foundation import CoreGraphics nonisolated enum PanelLayout { static let baseCardHeight: CGFloat = 204 static func headerHeight(width: CGFloat) -> CGFloat { width >= 840 ? 32 : 64 } static func columns(width: CGFloat) -> Int { width >= 560 ? 2 : 1 } static func cardWidth(size: CGSize) -> CGFloat { let count = columns(width: size.width) return max(0, (size.width - 20 - CGFloat(count - 1) * 8) / CGFloat(count)) } /// Physical order: CPU/battery, GPU/storage, memory/network. static func rowHeights(columns: Int, coreRows: Int, fanCount: Int) -> [CGFloat] { let cpu: CGFloat = 220 + CGFloat(max(0, coreRows - 2)) * 26 let gpu = baseCardHeight + CGFloat(max(0, (fanCount + 1) / 2 - 1)) * 26 let heights = [cpu, baseCardHeight, gpu, baseCardHeight, 260, 260] let count = max(1, min(2, columns)) return stride(from: 0, to: heights.count, by: count).map { heights[$0.. CGFloat { rows.reduce(0, +) + CGFloat(max(0, rows.count - 1)) * 8 } static func needsResourceScroll(size: CGSize, rows: [CGFloat]) -> Bool { // Outer padding, common toolbar/footer, and two gaps. Process page is separate. resourceHeight(rows: rows) > size.height - 20 - headerHeight(width: size.width) - 18 - 16 } static func contentSize(visible: CGRect, chrome: CGSize = .zero) -> CGSize { CGSize(width: max(1, min(640, visible.width - 32 - max(0, chrome.width))), height: max(1, min(800, visible.height - 32 - max(0, chrome.height)))) } static func centeredFrame(_ frame: CGRect, visible: CGRect) -> CGRect { var result = constrainedFrame(frame, visible: visible) result.origin.x = visible.midX - result.width / 2 return result } static func constrainedFrame(_ frame: CGRect, visible: CGRect) -> CGRect { let safe = visible.insetBy(dx: 16, dy: 16) let width = min(frame.width, safe.width), height = min(frame.height, safe.height) return CGRect(x: min(max(frame.minX, safe.minX), safe.maxX - width), y: min(max(frame.minY, safe.minY), safe.maxY - height), width: width, height: height) } }