Files
Kymo/kymo/Features/MenuBar/PanelLayout.swift
T

46 lines
2.2 KiB
Swift

import Foundation
import CoreGraphics
nonisolated enum PanelLayout {
static let baseCardHeight: CGFloat = 280
static func columns(width: CGFloat) -> Int { width >= 1160 ? 3 : width >= 900 ? 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))
}
/// Heights are assigned by physical row, including the battery/memory pair at two columns.
static func rowHeights(columns: Int, coreRows: Int, fanCount: Int) -> [CGFloat] {
let cpu = baseCardHeight + CGFloat(max(0, coreRows - 2)) * 26
let gpu = baseCardHeight + CGFloat(max(0, (fanCount + 1) / 2 - 1)) * 26
let heights = [cpu, gpu, baseCardHeight, baseCardHeight, baseCardHeight, baseCardHeight]
let count = max(1, min(3, columns))
return stride(from: 0, to: heights.count, by: count).map {
heights[$0..<min($0 + count, heights.count)].max() ?? baseCardHeight
}
}
static func resourceHeight(rows: [CGFloat]) -> CGFloat {
rows.reduce(0, +) + CGFloat(max(0, rows.count - 1)) * 8
}
static func needsResourceScroll(size: CGSize, rows: [CGFloat]) -> Bool {
// Outer padding, toolbar budget, two gaps, and the minimum process area.
resourceHeight(rows: rows) > size.height - 20 - 40 - 16 - 170
}
static func contentSize(visible: CGRect, chrome: CGSize = .zero) -> CGSize {
CGSize(width: max(1, min(1240, visible.width - 32 - max(0, chrome.width))),
height: max(1, min(820, 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)
}
}