fix(ui): 修正风扇转轴与速率图并精简进程表

This commit is contained in:
2026-11-15 19:21:18 +08:00
parent 99e558625b
commit 60c146f8b7
21 changed files with 303 additions and 107 deletions
@@ -2,9 +2,8 @@ import SwiftUI
struct MemoryDetailsView: View {
let memory: MemoryMetrics
private var values: [UInt64] { [memory.app, memory.wired, memory.compressed, memory.available] }
private var values: [UInt64] { [memory.app, memory.wired, memory.compressed] }
private let labels: [LocalizedStringKey] = ["memory.appShort", "memory.wiredShort", "memory.compressedShort"]
private var colors: [Color] { [Color("MemoryApplication"), Color("MemoryWired"), Color("MemoryCompressed"), Color("MemoryAvailable")] }
var body: some View {
VStack(alignment: .leading, spacing: 4) {
@@ -21,12 +20,12 @@ struct MemoryDetailsView: View {
}
.font(.system(size: 8)).foregroundStyle(.secondary)
.lineLimit(1).minimumScaleFactor(0.75)
CapacityBar(values: values.map { Double($0) }, colors: colors, total: Double(memory.total), height: 4)
HStack(spacing: 4) {
Text("memory.available").foregroundStyle(.secondary)
MetricText(value: MetricFormat.memory(memory.available))
}.font(.system(size: 9)).lineLimit(1)
}
.help(Text("memory.distributionHelp"))
.accessibilityElement(children: .contain)
.accessibilityLabel(Text("memory.distribution"))
}
@@ -11,7 +11,7 @@ extension MenuPanelView {
PanelTrendChart(
dates: networkDates, first: networkDownloads, second: networkUploads, color: Color("MetricMemory"),
secondColor: Color("SeriesUpload"), fixedScale: nil, firstLabel: "network.download", secondLabel: "network.upload",
expectedInterval: chartInterval, boundaryDates: chartBoundaries, intervals: networkIntervals, visibleSeriesCount: 2
expectedInterval: chartInterval, boundaryDates: chartBoundaries, intervals: networkIntervals, visibleSeriesCount: 2, projection: .split(upwardSeries: 1)
).id(selectedInterface)
}
) {
@@ -141,7 +141,7 @@ extension MenuPanelView {
PanelTrendChart(
dates: samples.map(\.date), first: samples.map(\.read), second: samples.map(\.write), color: Color("MetricDisk"),
secondColor: Color("MetricNetwork"), fixedScale: nil, firstLabel: "disk.read", secondLabel: "disk.write",
expectedInterval: chartInterval, boundaryDates: chartBoundaries, intervals: chartIntervals, visibleSeriesCount: 2
expectedInterval: chartInterval, boundaryDates: chartBoundaries, intervals: chartIntervals, visibleSeriesCount: 2, projection: .split(upwardSeries: 0)
)
}
) {
+36 -10
View File
@@ -17,20 +17,22 @@ import SwiftUI
func makeCoordinator() -> Coordinator { Coordinator(self) }
func makeNSView(context: Context) -> NSScrollView {
let scroll = NSScrollView()
scroll.hasVerticalScroller = true; scroll.hasHorizontalScroller = true
let scroll = FittingScrollView()
scroll.hasVerticalScroller = true; scroll.hasHorizontalScroller = false
scroll.horizontalScrollElasticity = .none
scroll.autohidesScrollers = true; scroll.drawsBackground = false
let table = ProcessNSTableView()
table.style = .inset; table.rowHeight = 20
table.style = .plain; table.rowHeight = 20
table.usesAlternatingRowBackgroundColors = true; table.backgroundColor = .clear
table.allowsMultipleSelection = false; table.allowsEmptySelection = true
table.columnAutoresizingStyle = .sequentialColumnAutoresizingStyle
for (key, width) in [("pid", 48.0), ("name", 140), ("cpu", 65), ("memory", 85), ("power", 90), ("threads", 65), ("path", 250)] {
table.columnAutoresizingStyle = .noColumnAutoresizing
table.intercellSpacing.width = 0
for (key, width) in [("pid", 48.0), ("name", 140), ("cpu", 65), ("memory", 85), ("power", 90), ("threads", 65)] {
let column = NSTableColumn(identifier: NSUserInterfaceItemIdentifier(key))
column.width = width
column.minWidth = key == "path" ? 150 : key == "name" ? 90 : width
column.maxWidth = key == "name" ? 180 : key == "path" ? 10_000 : width
column.resizingMask = key == "path" ? [.autoresizingMask, .userResizingMask] : .userResizingMask
column.minWidth = 0
column.maxWidth = 10_000
column.resizingMask = []
column.sortDescriptorPrototype = NSSortDescriptor(key: key, ascending: false)
table.addTableColumn(column)
}
@@ -47,7 +49,32 @@ import SwiftUI
context.coordinator.apply(self, to: table)
}
private final class FittingScrollView: NSScrollView {
private var fitting = false
override func tile() {
super.tile()
guard !fitting, let table = documentView as? ProcessNSTableView else { return }
fitting = true; defer { fitting = false }
table.fitColumns(to: contentView.bounds.width)
}
}
final class ProcessNSTableView: NSTableView {
/// Allocate the complete viewport; AppKit's uniform resizing can leave columns clipped at narrow widths.
func fitColumns(to width: CGFloat) {
guard width.isFinite, width > 0, tableColumns.count == 6 else { return }
let minimum: [CGFloat] = [32,54,40,52,54,32]
let preferred: [CGFloat] = [48,140,65,85,90,65]
let minimumTotal = minimum.reduce(0, +)
let baseScale = min(1, width / minimumTotal)
let extraScale = max(0, width - minimumTotal) / (preferred.reduce(0, +) - minimumTotal)
for index in tableColumns.indices {
let value = minimum[index] * baseScale + (preferred[index] - minimum[index]) * extraScale
if abs(tableColumns[index].width - value) > 0.01 { tableColumns[index].width = value }
}
if abs(frame.width - width) > 0.01 { setFrameSize(CGSize(width: width, height: frame.height)) }
}
var contextMenu: ((Int) -> NSMenu?)?
var cancel: (() -> Void)?
override func keyDown(with event: NSEvent) {
@@ -145,7 +172,6 @@ import SwiftUI
switch key {
case "pid": text = String(process.id.pid)
case "name": text = process.name
case "path": text = process.path ?? "--"
case "cpu": text = process.cpu.map { String(format: "%.1f", $0 * 100) } ?? "--"
case "memory": text = process.footprintBytes.map { String(format: "%.1f MB", Double($0) / 1_048_576) } ?? "--"
case "power": text = process.formattedPower
@@ -228,7 +254,7 @@ import SwiftUI
text.font = ["pid", "cpu", "memory", "threads", "power"].contains(key) ? .monospacedDigitSystemFont(ofSize: 11, weight: .regular) : .systemFont(ofSize: 11)
text.alignment = ["cpu", "memory", "threads", "power"].contains(key) ? .right : .left
text.lineBreakMode = .byTruncatingMiddle
text.textColor = key == "path" ? .secondaryLabelColor : .labelColor
text.textColor = .labelColor
addSubview(text); textField = text
if key == "name" || key == "threads" {
let image = NSImageView(); image.imageScaling = .scaleProportionallyDown
+9 -15
View File
@@ -31,11 +31,6 @@ struct ProcessTableView: View {
Spacer(minLength: 0)
searchField.frame(maxWidth: 180)
}
HStack(spacing: 8) {
Text("panel.processScope").lineLimit(2)
Spacer(minLength: 4)
energyStatus
}.font(.system(size: 9)).foregroundStyle(.secondary)
if snapshot?.failed == true { Text("panel.processFailed") }
else if snapshot == nil { Text("metric.waiting").foregroundStyle(.secondary) }
NativeProcessTable(rows: rows, selection: $state.selected, sorting: $state.sortOrder, locale: locale, onExit: handleExit)
@@ -72,22 +67,21 @@ struct ProcessTableView: View {
private var processTitle: some View {
Label("panel.processes", systemImage: "list.bullet.rectangle")
.font(PanelStyle.title)
.help(Text("\(Text("panel.processScope"))\n\(energyStatus)"))
}
private var searchField: some View {
NativeSearchField(text: $state.search, isFocused: $searchFocused,
placeholder: AppLocalization.string("panel.search", locale: locale))
}
private var energyStatus: some View {
Group {
switch energy?.state {
case .unsupported: Text("energy.unsupported")
case .unavailable: Text("energy.unavailable")
case .available, .partial:
Text("energy.coverage \(energy?.measured ?? 0) \(energy?.candidates ?? 0)")
default: Text("energy.waiting")
}
}.help(Text("energy.scopeHelp")).lineLimit(1)
private var energyStatus: Text {
switch energy?.state {
case .unsupported: Text("energy.unsupported")
case .unavailable: Text("energy.unavailable")
case .available, .partial:
Text("energy.coverage \(energy?.measured ?? 0) \(energy?.candidates ?? 0)")
default: Text("energy.waiting")
}
}
private func rebuild(retainOrder: Bool) {