Files
Kymo/kymo/Features/MenuBar/PanelLayout.swift
T
wangairnan 6300db3d15 feat(panel): 调整资源卡片读数布局并保留趋势平移
逐核显示 CPU 占用,在 GPU 标题展示核数并补充分项读数。无电池设备显示整机功率。

存储与网络将曲线排在详情下方,CPU、GPU、内存和电池保留整卡趋势。数值直接刷新,曲线随采样平滑平移。顶栏展示版本号,底栏调整开机时间和刷新间隔。
2026-11-15 22:03:46 +08:00

86 lines
3.9 KiB
Swift

import Foundation
import CoreGraphics
nonisolated enum PanelCard: Int, CaseIterable {
case cpu, gpu, memory, battery, storage, network
}
nonisolated enum PanelLayout {
static let footerHeight: CGFloat = 26
static let maximumWidth: CGFloat = ResourceCardMetrics.preferredWidth * 3 + 32
static let maximumHeight: CGFloat = 800
static func headerHeight(width: CGFloat) -> CGFloat { 32 }
static func columns(width: CGFloat) -> Int { width >= 620 ? 3 : width >= 430 ? 2 : 1 }
static func cardWidth(size: CGSize) -> CGFloat {
let count = columns(width: size.width)
return max(0, (size.width - 20 - CGFloat(count - 1) * 6) / CGFloat(count))
}
/// Keep reading order stable across three-, two- and single-column grids.
static func cardRows(columns: Int) -> [[PanelCard]] {
let cards = PanelCard.allCases
let count = max(1, columns)
return stride(from: 0, to: cards.count, by: count).map { start in
Array(cards[start..<min(start + count, cards.count)])
}
}
/// Structural foreground height determines the room available for the trend below the text.
static func cardHeights(coreRows: Int, fanCount: Int?) -> [PanelCard: CGFloat] {
let base = ResourceCardMetrics.minimumHeight
let fanRows = fanCount.map { (max(0, $0) + 1) / 2 } ?? 1
return [.cpu: base + CGFloat(max(0, coreRows - 1)) * 15,
.gpu: base + CGFloat(max(0, fanRows - 1)) * 18,
.memory: base, .battery: base, .storage: base, .network: base]
}
static func rowHeights(columns: Int, heights: [PanelCard: CGFloat]) -> [CGFloat] {
Array(repeating: processHeight(heights: heights), count: cardRows(columns: columns).count)
}
static func processHeight(heights: [PanelCard: CGFloat]) -> CGFloat {
heights.values.max() ?? ResourceCardMetrics.minimumHeight
}
static func resourceHeight(columns: Int, heights: [PanelCard: CGFloat]) -> CGFloat {
let rows = rowHeights(columns: columns, heights: heights)
return rows.reduce(0, +) + CGFloat(max(0, rows.count - 1)) * 6
}
static func dashboardHeight(columns: Int, heights: [PanelCard: CGFloat]) -> CGFloat {
resourceHeight(columns: columns, heights: heights) + 6 + processHeight(heights: heights)
}
static func preferredHeight(width: CGFloat, heights: [PanelCard: CGFloat]) -> CGFloat {
min(maximumHeight, dashboardHeight(columns: columns(width: width), heights: heights)
+ 20 + headerHeight(width: width) + footerHeight + 12)
}
static var defaultHeight: CGFloat {
preferredHeight(width: maximumWidth, heights: cardHeights(coreRows: 1, fanCount: nil))
}
static func needsDashboardScroll(size: CGSize, heights: [PanelCard: CGFloat]) -> Bool {
dashboardHeight(columns: columns(width: size.width), heights: heights)
> size.height - 20 - headerHeight(width: size.width) - footerHeight - 12
}
static func contentSize(visible: CGRect, chrome: CGSize = .zero, preferredHeight: CGFloat? = nil) -> CGSize {
CGSize(width: max(1, min(maximumWidth, visible.width - 32 - max(0, chrome.width))),
height: max(1, min(maximumHeight, preferredHeight ?? defaultHeight,
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)
}
}