feat(menubar): 支持固定居中面板与独立刷新频率及图表浮层
This commit is contained in:
@@ -6,7 +6,8 @@ struct MenuPanelView: View {
|
||||
let preferences: AppearancePreferences
|
||||
let keepAwake: KeepAwakeService
|
||||
let settingsPresentation: SettingsPresentation
|
||||
@Environment(\.openSettings) private var openSettings
|
||||
var presentation = PanelPresentation()
|
||||
var onOpenSettings: () -> Void = {}
|
||||
@State private var panelSize = PanelLayout.contentSize(visible: NSScreen.main?.visibleFrame ?? CGRect(x: 0, y: 0, width: 1000, height: 760))
|
||||
@State private var selectedInterface = ""
|
||||
@State private var showAddresses = false
|
||||
@@ -14,6 +15,8 @@ struct MenuPanelView: View {
|
||||
|
||||
private var snapshot: MetricsSnapshot { metrics.snapshot }
|
||||
private var samples: [TrendSample] { metrics.history.samples }
|
||||
private var chartInterval: TimeInterval { Double(metrics.effectiveMilliseconds) / 1000 }
|
||||
private var chartBoundaries: Set<Date> { Set(samples.filter(\.breakBefore).map(\.date)) }
|
||||
private var columns: Int { panelSize.width >= 1160 ? 3 : panelSize.width >= 900 ? 2 : 1 }
|
||||
private var network: [NetworkInterfaceMetrics] {
|
||||
(snapshot.network ?? []).filter { selectedInterface.isEmpty ? $0.isIncludedInTotal : $0.identity == selectedInterface }
|
||||
@@ -34,7 +37,7 @@ struct MenuPanelView: View {
|
||||
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)
|
||||
batteryCard.aspectRatio(4.0 / 3.0, contentMode: .fit)
|
||||
memoryCard.frame(minHeight: 240)
|
||||
diskCard.frame(minHeight: 250)
|
||||
networkCard.frame(minHeight: 250)
|
||||
@@ -48,15 +51,16 @@ struct MenuPanelView: View {
|
||||
}
|
||||
.padding(10)
|
||||
.frame(width: panelSize.width, height: panelSize.height)
|
||||
.background(PanelWindowObserver { size, visible in
|
||||
.background(PanelWindowObserver { size in
|
||||
if panelSize != size { panelSize = size }
|
||||
if metrics.isPanelVisible != visible { metrics.setPanelVisible(visible) }
|
||||
}.frame(width: 0, height: 0))
|
||||
.onAppear { metrics.setPanelVisible(true) }
|
||||
.onDisappear { metrics.setPanelVisible(false) }
|
||||
.onChange(of: snapshot.network?.map(\.identity)) { _, keys in
|
||||
if !selectedInterface.isEmpty && !(keys ?? []).contains(selectedInterface) { selectedInterface = "" }
|
||||
}
|
||||
.background(.ultraThinMaterial, in: .rect(cornerRadius: 18))
|
||||
.clipShape(.rect(cornerRadius: 18))
|
||||
.modifier(AppearanceStyle(preferences: preferences))
|
||||
}
|
||||
|
||||
@@ -65,7 +69,7 @@ struct MenuPanelView: View {
|
||||
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))
|
||||
batteryCard.frame(width: PanelLayout.batteryWidth(size: panelSize))
|
||||
}.frame(height: PanelLayout.resourceRowHeight(size: panelSize))
|
||||
HStack(alignment: .top, spacing: 8) {
|
||||
memoryCard.frame(width: (panelSize.width - 36) / 3)
|
||||
@@ -91,7 +95,7 @@ struct MenuPanelView: View {
|
||||
}
|
||||
|
||||
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)
|
||||
PanelTrendChart(dates: samples.map(\.date), first: samples.map { $0[keyPath: key] }, color: color, firstLabel: label, expectedInterval: chartInterval, boundaryDates: chartBoundaries)
|
||||
}
|
||||
private func line(_ key: LocalizedStringKey, _ value: String) -> some View {
|
||||
HStack(alignment: .firstTextBaseline) { Text(key).foregroundStyle(.secondary); Spacer(minLength: 6); AnimatedMetricText(value: value) }
|
||||
@@ -101,8 +105,8 @@ struct MenuPanelView: View {
|
||||
ResourceCard(title: "metric.cpu", symbol: "cpu", color: Color("MetricCPU"), value: MetricFormat.percent(snapshot.cpu?.usage)) {
|
||||
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")
|
||||
color: Color("MetricCPU"), secondColor: Color("SeriesUser"), thirdColor: Color("SeriesSystem"),
|
||||
firstLabel: "panel.total", secondLabel: "cpu.user", thirdLabel: "cpu.system", expectedInterval: chartInterval, boundaryDates: chartBoundaries)
|
||||
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) {
|
||||
@@ -139,7 +143,7 @@ struct MenuPanelView: View {
|
||||
PanelTrendChart(dates: samples.map(\.date),
|
||||
first: samples.map { $0.memory.map { $0 * Double(snapshot.memory?.total ?? 1) } },
|
||||
color: Color("MetricMemory"), fixedScale: Double(snapshot.memory?.total ?? 1),
|
||||
scaleLabel: MetricFormat.bytes(snapshot.memory?.total))
|
||||
scaleLabel: MetricFormat.bytes(snapshot.memory?.total), expectedInterval: chartInterval, boundaryDates: chartBoundaries)
|
||||
if let m = snapshot.memory {
|
||||
|
||||
GeometryReader { geometry in
|
||||
@@ -178,7 +182,7 @@ struct MenuPanelView: View {
|
||||
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)
|
||||
PanelTrendChart(dates: networkDates, first: networkDownloads, second: networkUploads, color: Color("MetricNetwork"), secondColor: Color("SeriesUpload"), fixedScale: nil, firstLabel: "network.download", secondLabel: "network.upload", expectedInterval: chartInterval, boundaryDates: chartBoundaries).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 {
|
||||
@@ -225,7 +229,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, firstLabel: "disk.read", secondLabel: "disk.write")
|
||||
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", expectedInterval: chartInterval, boundaryDates: chartBoundaries)
|
||||
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" } ?? "--") }
|
||||
@@ -273,6 +277,19 @@ struct MenuPanelView: View {
|
||||
private var controls: some View {
|
||||
HStack(spacing: 8) {
|
||||
HStack(spacing: 2) {
|
||||
HStack(spacing: 4) {
|
||||
Image(systemName: "timer").help(Text("sampling.panel"))
|
||||
IntervalControl(milliseconds: Binding(get: { metrics.sampling.panelMilliseconds }, set: { metrics.sampling.panelMilliseconds = $0 }))
|
||||
}.padding(.horizontal, 6).accessibilityLabel(Text("sampling.panel"))
|
||||
Divider().frame(height: 18).padding(.horizontal, 3)
|
||||
Button { presentation.isPinned.toggle() } label: {
|
||||
Image(systemName: presentation.isPinned ? "pin.fill" : "pin")
|
||||
.frame(width: 20, height: 20)
|
||||
.foregroundStyle(presentation.isPinned ? preferences.tintColor : Color.primary)
|
||||
}
|
||||
.help(Text(presentation.isPinned ? "panel.unpin" : "panel.pin"))
|
||||
.accessibilityLabel(Text(presentation.isPinned ? "panel.unpin" : "panel.pin"))
|
||||
.accessibilityValue(Text(presentation.isPinned ? "panel.pinned" : "panel.unpinned"))
|
||||
Button { keepAwake.toggle() } label: {
|
||||
Image(systemName: keepAwake.isEnabled ? "cup.and.saucer.fill" : "cup.and.saucer")
|
||||
.frame(width: 20, height: 20)
|
||||
@@ -289,7 +306,7 @@ struct MenuPanelView: View {
|
||||
Button {
|
||||
NSApp.setActivationPolicy(.regular)
|
||||
settingsPresentation.request()
|
||||
openSettings()
|
||||
onOpenSettings()
|
||||
} label: {
|
||||
Image(systemName: "gearshape").frame(width: 20, height: 20)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user