feat(menubar): 将资源概览调整为三列两行布局

This commit is contained in:
2026-11-15 14:39:55 +08:00
parent 585fb0e130
commit 47e3fe6ca8
6 changed files with 65 additions and 46 deletions
+10 -9
View File
@@ -53,10 +53,10 @@ struct MenuPanelView: View {
header
if presentation.page == .overview {
if PanelLayout.needsResourceScroll(size: panelSize, heights: cardHeights) {
ScrollView { resourceColumns }
ScrollView { resourceGrid }
.frame(maxWidth: .infinity, maxHeight: .infinity)
} else {
resourceColumns
resourceGrid
.frame(maxHeight: .infinity, alignment: .top)
}
} else if presentation.isOpen {
@@ -91,15 +91,16 @@ struct MenuPanelView: View {
.modifier(AppearanceStyle(preferences: preferences))
}
private var resourceColumns: some View {
let groups = PanelLayout.cardColumns(columns: columns)
return HStack(alignment: .top, spacing: PanelStyle.gap) {
ForEach(groups.indices, id: \.self) { column in
VStack(spacing: PanelStyle.gap) {
ForEach(groups[column], id: \.self) { card in
private var resourceGrid: some View {
let rows = PanelLayout.cardRows(columns: columns)
let heights = PanelLayout.rowHeights(columns: columns, heights: cardHeights)
return VStack(spacing: PanelStyle.gap) {
ForEach(rows.indices, id: \.self) { row in
HStack(alignment: .top, spacing: PanelStyle.gap) {
ForEach(rows[row], id: \.self) { card in
resourceCard(card)
.frame(maxWidth: .infinity)
.frame(height: cardHeights[card])
.frame(height: heights[row])
.onGeometryChange(for: CGFloat.self) { $0.size.width } action: { width in
if card == .cpu { measuredCardWidth = width }
}
+17 -11
View File
@@ -2,22 +2,27 @@ import Foundation
import CoreGraphics
nonisolated enum PanelCard: Int, CaseIterable {
case cpu, battery, gpu, storage, memory, network
case cpu, gpu, memory, battery, storage, network
}
nonisolated enum PanelLayout {
static let footerHeight: CGFloat = 26
static let maximumWidth: CGFloat = 960
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 columns(width: CGFloat) -> Int { width >= 900 ? 3 : 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))
}
/// 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]
/// 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)])
}
}
/// Fixed content rows with a 48pt plot. Nil fans reserve a discovery row.
@@ -28,14 +33,15 @@ nonisolated enum PanelLayout {
.memory: 180, .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 rowHeights(columns: Int, heights: [PanelCard: CGFloat]) -> [CGFloat] {
cardRows(columns: columns).map { cards in
cards.map { heights[$0] ?? 0 }.max() ?? 0
}
}
static func resourceHeight(columns: Int, heights: [PanelCard: CGFloat]) -> CGFloat {
columnHeights(columns: columns, heights: heights).max() ?? 0
let rows = rowHeights(columns: columns, heights: heights)
return rows.reduce(0, +) + CGFloat(max(0, rows.count - 1)) * 8
}
static func preferredHeight(width: CGFloat, heights: [PanelCard: CGFloat]) -> CGFloat {
@@ -44,7 +50,7 @@ nonisolated enum PanelLayout {
}
static var defaultHeight: CGFloat {
preferredHeight(width: 640, heights: cardHeights(coreRows: 2, fanCount: nil))
preferredHeight(width: maximumWidth, heights: cardHeights(coreRows: 2, fanCount: nil))
}
static func needsResourceScroll(size: CGSize, heights: [PanelCard: CGFloat]) -> Bool {
@@ -53,7 +59,7 @@ nonisolated enum PanelLayout {
}
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))),
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))))
}