feat(menubar): 调整两排卡片布局并加入复合曲线与数值动效

This commit is contained in:
2026-11-03 10:10:44 +08:00
parent 0e8f598fe6
commit 906293b7c1
22 changed files with 400 additions and 122 deletions
+52 -44
View File
@@ -26,28 +26,18 @@ struct MenuPanelView: View {
var body: some View {
VStack(spacing: 8) {
header
if columns == 3 && panelSize.height >= 810 {
VStack(spacing: 8) {
HStack(alignment: .top, spacing: 8) {
cpuCard.frame(width: (panelSize.width - 36) * 0.35)
gpuCard.frame(width: (panelSize.width - 36) * 0.33)
memoryCard.frame(width: (panelSize.width - 36) * 0.32)
}.frame(height: panelSize.height - 590)
HStack(alignment: .top, spacing: 8) {
networkCard.frame(width: (panelSize.width - 36) * 0.42)
diskCard.frame(width: (panelSize.width - 36) * 0.27)
batteryCard.frame(width: (panelSize.width - 36) * 0.31)
}.frame(height: 240)
}
if columns == 3 {
if panelSize.height >= 810 { resourceRows }
else { ScrollView { resourceRows } }
} else {
ScrollView {
LazyVGrid(columns: Array(repeating: GridItem(.flexible(), spacing: 8, alignment: .top), count: columns), spacing: 8) {
cpuCard.frame(minHeight: 240)
gpuCard.frame(minHeight: 240)
batteryCard.aspectRatio(1, contentMode: .fit)
memoryCard.frame(minHeight: 240)
networkCard.frame(minHeight: 250)
diskCard.frame(minHeight: 250)
batteryCard.frame(minHeight: 250)
networkCard.frame(minHeight: 250)
}
}
}
@@ -55,7 +45,6 @@ struct MenuPanelView: View {
ProcessTableView(snapshot: snapshot.processes, wide: panelSize.width >= 900, logicalCPUCount: snapshot.cpu?.cores.count ?? 1).frame(maxWidth: .infinity, maxHeight: .infinity)
.frame(minHeight: 170)
}
footer
}
.padding(10)
.frame(width: panelSize.width, height: panelSize.height)
@@ -71,33 +60,49 @@ struct MenuPanelView: View {
.modifier(AppearanceStyle(preferences: preferences))
}
private var resourceRows: some View {
VStack(spacing: 8) {
HStack(alignment: .top, spacing: 8) {
cpuCard.frame(width: PanelLayout.firstRowWideCardWidth(size: panelSize))
gpuCard.frame(width: PanelLayout.firstRowWideCardWidth(size: panelSize))
batteryCard.frame(width: PanelLayout.resourceRowHeight(size: panelSize))
}.frame(height: PanelLayout.resourceRowHeight(size: panelSize))
HStack(alignment: .top, spacing: 8) {
memoryCard.frame(width: (panelSize.width - 36) / 3)
diskCard.frame(width: (panelSize.width - 36) / 3)
networkCard.frame(width: (panelSize.width - 36) / 3)
}.frame(height: 240)
}
}
private var header: some View {
HStack(spacing: 10) {
Image("BrandMark").renderingMode(.template)
Text("Kymo").font(.system(size: 18, weight: .semibold))
Text(verbatim: "v\(Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "--")")
.font(.system(size: 10)).foregroundStyle(.secondary)
if panelSize.width >= 900 {
Text(verbatim: "\(ProcessInfo.processInfo.processorCount) CPU · \(MetricFormat.bytes(snapshot.memory?.total))")
.font(.system(size: 11)).foregroundStyle(.secondary)
Text(verbatim: HardwareIdentity.displayName)
.font(.system(size: 11)).foregroundStyle(.secondary).lineLimit(1).truncationMode(.middle)
}
Spacer()
VStack(alignment: .trailing, spacing: 2) {
Label(metrics.isPaused ? "panel.paused" : "panel.sampling", systemImage: metrics.isPaused ? "pause.circle" : "circle.fill")
.foregroundStyle(metrics.isPaused ? Color.secondary : Color("MetricBattery"))
Text(snapshot.date, style: .time).foregroundStyle(.secondary)
}.font(.system(size: 10))
Spacer(minLength: 8)
controls
}.padding(.horizontal, 4)
}
private func chart(_ key: KeyPath<TrendSample, Double?>, color: Color) -> some View {
PanelTrendChart(dates: samples.map(\.date), first: samples.map { $0[keyPath: key] }, color: color)
private func chart(_ key: KeyPath<TrendSample, Double?>, color: Color, label: LocalizedStringKey = "panel.total") -> some View {
PanelTrendChart(dates: samples.map(\.date), first: samples.map { $0[keyPath: key] }, color: color, firstLabel: label)
}
private func line(_ key: LocalizedStringKey, _ value: String) -> some View {
HStack(alignment: .firstTextBaseline) { Text(key).foregroundStyle(.secondary); Spacer(minLength: 6); Text(verbatim: value).monospacedDigit() }
HStack(alignment: .firstTextBaseline) { Text(key).foregroundStyle(.secondary); Spacer(minLength: 6); AnimatedMetricText(value: value) }
}
private var cpuCard: some View {
ResourceCard(title: "metric.cpu", symbol: "cpu", color: Color("MetricCPU"), value: MetricFormat.percent(snapshot.cpu?.usage)) {
chart(\.cpu, color: Color("MetricCPU"))
PanelTrendChart(dates: samples.map(\.date), first: samples.map(\.cpu),
second: samples.map(\.cpuUser), third: samples.map(\.cpuSystem),
color: Color("MetricCPU"), secondColor: Color("MetricNetwork"), thirdColor: Color("MetricDisk"),
firstLabel: "panel.total", secondLabel: "cpu.user", thirdLabel: "cpu.system")
if let cpu = snapshot.cpu {
HStack { line("cpu.user", MetricFormat.percent(cpu.user)); line("cpu.system", MetricFormat.percent(cpu.system)) }
LazyVGrid(columns: Array(repeating: GridItem(.flexible(), spacing: 6), count: 7), spacing: 5) {
@@ -115,7 +120,7 @@ struct MenuPanelView: View {
} else { Text("metric.waiting").foregroundStyle(.secondary) }
Spacer(minLength: 0)
HStack { line("system.load", snapshot.load.map(MetricFormat.decimal).joined(separator: " / ")); Spacer(minLength: 8); Text(verbatim: String(format: "%.1f h", snapshot.uptime / 3600)) }
HStack { line("system.load", snapshot.load.map(MetricFormat.decimal).joined(separator: " / ")); Spacer(minLength: 8); AnimatedMetricText(value: String(format: "%.1f h", snapshot.uptime / 3600)) }
}
}
@@ -148,7 +153,7 @@ struct MenuPanelView: View {
line("memory.app", MetricFormat.bytes(m.used - min(m.used, m.wired + m.compressed)))
HStack { line("memory.wired", MetricFormat.bytes(m.wired)); line("memory.compressed", MetricFormat.bytes(m.compressed)) }
HStack { line("memory.available", MetricFormat.bytes(m.total - min(m.total, m.used))); line("memory.swap", MetricFormat.bytes(m.swapUsed)) }
HStack { Text("memory.pressure").foregroundStyle(.secondary); Spacer(); pressure(m.pressure); Text(verbatim: MetricFormat.percent(m.usage)).foregroundStyle(Color("MetricMemory")) }
HStack { Text("memory.pressure").foregroundStyle(.secondary); Spacer(); pressure(m.pressure); AnimatedMetricText(value: MetricFormat.percent(m.usage)).foregroundStyle(Color("MetricMemory")) }
} else { Text("metric.waiting") }
}
}
@@ -169,10 +174,11 @@ struct MenuPanelView: View {
}.labelsHidden().controlSize(.mini).frame(maxWidth: 200)
Spacer(minLength: 0)
}
if let item = network.first, let address = item.addresses.first {
Text(verbatim: "\(item.name) · \(address)").font(.system(size: 10)).lineLimit(1).truncationMode(.middle).textSelection(.enabled)
}
PanelTrendChart(dates: networkDates, first: networkDownloads, second: networkUploads, color: Color("MetricNetwork"), fixedScale: nil).id(selectedInterface)
if let item = addressInterface {
Text(verbatim: "\(item.name) · \(item.addresses.first(where: { !$0.contains(":") }) ?? item.addresses.first ?? "--")")
.font(.system(size: 10)).lineLimit(1).minimumScaleFactor(0.8).textSelection(.enabled)
} else { Text("panel.addressUnknown").foregroundStyle(.secondary) }
PanelTrendChart(dates: networkDates, first: networkDownloads, second: networkUploads, color: Color("MetricNetwork"), fixedScale: nil, firstLabel: "network.download", secondLabel: "network.upload").id(selectedInterface)
if !network.isEmpty && total(\.download) == nil { Text("panel.rateBaseline").foregroundStyle(.secondary) }
HStack { line("network.received", network.isEmpty ? "--" : MetricFormat.bytes(network.reduce(0) { $0 + $1.received })); line("network.sent", network.isEmpty ? "--" : MetricFormat.bytes(network.reduce(0) { $0 + $1.sent })) }
switch snapshot.networkPath {
@@ -208,6 +214,10 @@ struct MenuPanelView: View {
}
}
private var addressInterface: NetworkInterfaceMetrics? {
let name = selectedInterface.isEmpty ? snapshot.defaultNetworkInterface : network.first?.name
return snapshot.network?.first { $0.name == name && $0.active }
}
private var networkDates: [Date] { selectedInterface.isEmpty ? samples.map(\.date) : (metrics.history.interfaces[selectedInterface] ?? []).map(\.date) }
private var networkDownloads: [Double?] { selectedInterface.isEmpty ? samples.map(\.download) : (metrics.history.interfaces[selectedInterface] ?? []).map(\.download) }
private var networkUploads: [Double?] { selectedInterface.isEmpty ? samples.map(\.upload) : (metrics.history.interfaces[selectedInterface] ?? []).map(\.upload) }
@@ -215,7 +225,7 @@ struct MenuPanelView: View {
ResourceCard(title: "metric.disk", symbol: "internaldrive", color: Color("MetricDisk"), value: snapshot.disk.map { String(format: "%.0f / %.0f GB", Double($0.total - min($0.total, $0.available)) / 1_000_000_000, Double($0.total) / 1_000_000_000) } ?? "--") {
PanelTrendChart(dates: samples.map(\.date), first: samples.map(\.read), second: samples.map(\.write), color: Color("MetricDisk"), secondColor: Color("MetricMemory"), fixedScale: nil)
PanelTrendChart(dates: samples.map(\.date), first: samples.map(\.read), second: samples.map(\.write), color: Color("MetricDisk"), secondColor: Color("MetricMemory"), fixedScale: nil, firstLabel: "disk.read", secondLabel: "disk.write")
Text("disk.physicalIO").foregroundStyle(.secondary)
HStack { line("disk.read", MetricFormat.rate(snapshot.diskIO?.read)); line("disk.write", MetricFormat.rate(snapshot.diskIO?.written)) }
HStack { line("disk.remainingLife", MetricFormat.percent(snapshot.diskHealth?.remainingLife)); line("disk.powerOnHours", snapshot.diskHealth?.powerOnHours.map { "\($0) h" } ?? "--") }
@@ -243,7 +253,7 @@ struct MenuPanelView: View {
private var batteryCard: some View {
ResourceCard(title: "metric.battery", symbol: "battery.100percent", color: Color("MetricBattery"), value: MetricFormat.percent(snapshot.battery?.charge)) {
if let battery = snapshot.battery {
chart(\.battery, color: Color("MetricBattery"))
chart(\.battery, color: Color("MetricBattery"), label: "metric.battery")
Text(battery.charging ? "battery.charging" : battery.externalPower ? "battery.external" : "battery.discharging")
line("battery.cycles", battery.cycles.map(String.init) ?? "--")
line("battery.health", MetricFormat.percent(battery.health))
@@ -251,7 +261,7 @@ struct MenuPanelView: View {
Text(battery.charging ? "battery.timeFull" : "battery.timeEmpty").foregroundStyle(.secondary)
Spacer()
if battery.externalPower && !battery.charging { Text("battery.timeNotApplicable") }
else if let minutes = battery.minutesRemaining { Text(verbatim: "\(minutes) min") }
else if let minutes = battery.minutesRemaining { AnimatedMetricText(value: "\(minutes) min") }
else { Text("battery.timeUnknown") }
}
@@ -260,10 +270,8 @@ struct MenuPanelView: View {
}
}
}
private var footer: some View {
private var controls: some View {
HStack(spacing: 8) {
Text(verbatim: "v\(Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "--")").font(.system(size: 10)).foregroundStyle(.secondary)
Spacer()
HStack(spacing: 2) {
Button { keepAwake.toggle() } label: {
Image(systemName: keepAwake.isEnabled ? "cup.and.saucer.fill" : "cup.and.saucer")
@@ -292,7 +300,7 @@ struct MenuPanelView: View {
}
.help(Text("action.quit")).accessibilityLabel(Text("action.quit"))
}
.buttonStyle(FooterButtonStyle())
.buttonStyle(PanelControlButtonStyle())
.padding(4)
.glassEffect(.regular, in: .capsule)
}
@@ -302,15 +310,15 @@ struct MenuPanelView: View {
}
private struct FooterButtonStyle: ButtonStyle {
private struct PanelControlButtonStyle: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
FooterButtonBody(isPressed: configuration.isPressed) {
PanelControlButtonBody(isPressed: configuration.isPressed) {
configuration.label
}
}
}
private struct FooterButtonBody<Content: View>: View {
private struct PanelControlButtonBody<Content: View>: View {
let isPressed: Bool
@ViewBuilder var content: () -> Content
@State private var isHovered = false
+3
View File
@@ -2,6 +2,9 @@ import Foundation
import CoreGraphics
nonisolated enum PanelLayout {
static func resourceRowHeight(size: CGSize) -> CGFloat { min(230, max(220, size.height - 590)) }
static func firstRowWideCardWidth(size: CGSize) -> CGFloat { (size.width - 36 - resourceRowHeight(size: size)) / 2 }
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))))
+1 -1
View File
@@ -71,7 +71,7 @@ struct ProcessTableView: View {
}
}
.font(.system(size: 12)).padding(10)
.background(Color("PanelCard"), in: .rect(cornerRadius: 14))
.modifier(PanelCardBackground(radius: 14))
.overlay(RoundedRectangle(cornerRadius: 14).strokeBorder(.primary.opacity(0.1)))
.onAppear { rebuild(retainOrder: false) }
.onChange(of: snapshot?.date) { _, _ in rebuild(retainOrder: held && (byMemory ? rows.contains { $0.residentBytes != nil } : rows.contains { $0.cpu != nil })) }