feat(menubar): 实现紧凑独立双列与按内容定高

This commit is contained in:
2026-11-08 18:47:01 +08:00
parent 8821089872
commit 8d3891966a
22 changed files with 259 additions and 128 deletions
+44 -19
View File
@@ -1,36 +1,61 @@
import Foundation
import CoreGraphics
nonisolated enum PanelCard: Int, CaseIterable {
case cpu, battery, gpu, storage, memory, network
}
nonisolated enum PanelLayout {
static let baseCardHeight: CGFloat = 204
static let footerHeight: CGFloat = 26
static let maximumHeight: CGFloat = 800
static func headerHeight(width: CGFloat) -> CGFloat { width >= 560 ? 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..<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, common toolbar/footer, and two gaps. Process page is separate.
resourceHeight(rows: rows) > size.height - 20 - headerHeight(width: size.width) - footerHeight - 16
/// Preserve location and order; never rebalance columns when values change.
static func cardColumns(columns: Int) -> [[PanelCard]] {
columns > 1 ? [[.cpu, .gpu, .memory], [.battery, .storage, .network]] : [PanelCard.allCases]
}
static func contentSize(visible: CGRect, chrome: CGSize = .zero) -> CGSize {
/// Fixed content rows with a 48pt plot. Nil fans reserve a discovery row.
static func cardHeights(coreRows: Int, fanCount: Int?) -> [PanelCard: CGFloat] {
let fanRows = fanCount.map { (max(0, $0) + 1) / 2 } ?? 1
return [.cpu: 152 + CGFloat(max(1, coreRows)) * 26,
.gpu: fanRows == 0 ? 124 : 144 + CGFloat(fanRows) * 20,
.memory: 228, .battery: 170, .storage: 170, .network: 208]
}
static func columnHeights(columns: Int, heights: [PanelCard: CGFloat]) -> [CGFloat] {
cardColumns(columns: columns).map { cards in
cards.reduce(0) { $0 + (heights[$1] ?? 0) } + CGFloat(max(0, cards.count - 1)) * 8
}
}
static func resourceHeight(columns: Int, heights: [PanelCard: CGFloat]) -> CGFloat {
columnHeights(columns: columns, heights: heights).max() ?? 0
}
static func preferredHeight(width: CGFloat, heights: [PanelCard: CGFloat]) -> CGFloat {
min(maximumHeight, resourceHeight(columns: columns(width: width), heights: heights)
+ 20 + headerHeight(width: width) + footerHeight + 16)
}
static var defaultHeight: CGFloat {
preferredHeight(width: 640, heights: cardHeights(coreRows: 2, fanCount: nil))
}
static func needsResourceScroll(size: CGSize, heights: [PanelCard: CGFloat]) -> Bool {
resourceHeight(columns: columns(width: size.width), heights: heights)
> size.height - 20 - headerHeight(width: size.width) - footerHeight - 16
}
static func contentSize(visible: CGRect, chrome: CGSize = .zero, preferredHeight: CGFloat? = nil) -> 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))))
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)