feat(menubar): 实现紧凑独立双列与按内容定高
This commit is contained in:
@@ -8,6 +8,7 @@ struct MenuPanelView: View {
|
||||
let settingsPresentation: SettingsPresentation
|
||||
var presentation = PanelPresentation()
|
||||
var onOpenSettings: () -> Void = {}
|
||||
var onPreferredHeight: (CGFloat) -> Void = { _ in }
|
||||
@Environment(\.locale) var locale
|
||||
@State private var panelSize = PanelLayout.contentSize(visible: NSScreen.main?.visibleFrame ?? CGRect(x: 0, y: 0, width: 1000, height: 760))
|
||||
@State var selectedInterface = ""
|
||||
@@ -30,13 +31,15 @@ struct MenuPanelView: View {
|
||||
}
|
||||
var cardContentWidth: CGFloat { max(0, (measuredCardWidth ?? PanelLayout.cardWidth(size: panelSize)) - 20) }
|
||||
var columns: Int { PanelLayout.columns(width: panelSize.width) }
|
||||
private var rowHeights: [CGFloat] {
|
||||
let groups = CPUCoreGrid.groups(count: snapshot.cpu?.cores.count ?? 0, kinds: CPUCoreTopology.cores)
|
||||
let widths = CPUCoreGrid.groupWidths(groups: groups, width: max(0, (measuredCardWidth ?? PanelLayout.cardWidth(size: panelSize)) - 20))
|
||||
private var cardHeights: [PanelCard: CGFloat] {
|
||||
let coreCount = max(CPUCoreTopology.cores.count, ProcessInfo.processInfo.processorCount, snapshot.cpu?.cores.count ?? 0)
|
||||
let groups = CPUCoreGrid.groups(count: coreCount, kinds: CPUCoreTopology.cores)
|
||||
let widths = CPUCoreGrid.groupWidths(groups: groups, width: cardContentWidth)
|
||||
let coreRows = zip(groups, widths).map { CPUCoreGridLayout.rowCounts(count: $0.0.count, width: $0.1).count }.max() ?? 0
|
||||
return PanelLayout.rowHeights(columns: columns, coreRows: coreRows,
|
||||
fanCount: max(reservedFanCount, snapshot.fans?.count ?? 0))
|
||||
let fanCount = reservedFanCount > 0 ? max(reservedFanCount, snapshot.fans?.count ?? 0) : snapshot.fans?.count
|
||||
return PanelLayout.cardHeights(coreRows: coreRows, fanCount: fanCount)
|
||||
}
|
||||
private var preferredHeight: CGFloat { PanelLayout.preferredHeight(width: panelSize.width, heights: cardHeights) }
|
||||
var network: [NetworkInterfaceMetrics] {
|
||||
(snapshot.network ?? []).filter { selectedInterface.isEmpty ? $0.isIncludedInTotal : $0.identity == selectedInterface }
|
||||
}
|
||||
@@ -49,11 +52,11 @@ struct MenuPanelView: View {
|
||||
VStack(spacing: PanelStyle.gap) {
|
||||
if PanelResourceAudit.omitted != "header" { header }
|
||||
if presentation.page == .overview {
|
||||
if PanelLayout.needsResourceScroll(size: panelSize, rows: rowHeights) {
|
||||
ScrollView { resourceRows }
|
||||
if PanelLayout.needsResourceScroll(size: panelSize, heights: cardHeights) {
|
||||
ScrollView { resourceColumns }
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
} else {
|
||||
resourceRows
|
||||
resourceColumns
|
||||
.frame(maxHeight: .infinity, alignment: .top)
|
||||
}
|
||||
} else if presentation.isOpen && PanelResourceAudit.omitted != "processes" {
|
||||
@@ -67,6 +70,12 @@ struct MenuPanelView: View {
|
||||
.background(PanelWindowObserver { size in
|
||||
if panelSize != size { panelSize = size }
|
||||
}.frame(width: 0, height: 0))
|
||||
.task(id: preferredHeight) {
|
||||
// Resize only for structural changes, outside the current layout pass.
|
||||
await Task.yield()
|
||||
guard !Task.isCancelled else { return }
|
||||
onPreferredHeight(preferredHeight)
|
||||
}
|
||||
.onAppear {
|
||||
reservedFanCount = snapshot.fans?.count ?? 0
|
||||
}
|
||||
@@ -82,35 +91,36 @@ struct MenuPanelView: View {
|
||||
.modifier(AppearanceStyle(preferences: preferences))
|
||||
}
|
||||
|
||||
private var resourceRows: some View {
|
||||
VStack(spacing: PanelStyle.gap) {
|
||||
ForEach(rowHeights.indices, id: \.self) { row in
|
||||
HStack(spacing: PanelStyle.gap) {
|
||||
ForEach(0..<columns, id: \.self) { column in
|
||||
resourceCard(at: row * columns + column)
|
||||
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
|
||||
resourceCard(card)
|
||||
.frame(maxWidth: .infinity)
|
||||
.frame(height: rowHeights[row])
|
||||
.frame(height: cardHeights[card])
|
||||
.onGeometryChange(for: CGFloat.self) { $0.size.width } action: { width in
|
||||
if row == 0 && column == 0 { measuredCardWidth = width }
|
||||
if card == .cpu { measuredCardWidth = width }
|
||||
}
|
||||
}
|
||||
}
|
||||
.frame(maxWidth: .infinity, alignment: .top)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ViewBuilder private func resourceCard(at index: Int) -> some View {
|
||||
@ViewBuilder private func resourceCard(_ card: PanelCard) -> some View {
|
||||
if PanelResourceAudit.omitted == "resources" {
|
||||
Color.clear
|
||||
} else {
|
||||
switch index {
|
||||
case 0: cpuCard
|
||||
case 1: batteryCard
|
||||
case 2: gpuCard
|
||||
case 3: diskCard
|
||||
case 4: memoryCard
|
||||
case 5: networkCard
|
||||
default: EmptyView()
|
||||
switch card {
|
||||
case .cpu: cpuCard
|
||||
case .battery: batteryCard
|
||||
case .gpu: gpuCard
|
||||
case .storage: diskCard
|
||||
case .memory: memoryCard
|
||||
case .network: networkCard
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user