feat(menubar): 实现紧凑独立双列与按内容定高

This commit is contained in:
2026-11-08 18:47:01 +08:00
parent 8821089872
commit 8d3891966a
22 changed files with 259 additions and 128 deletions
+8 -2
View File
@@ -5,9 +5,12 @@ import SwiftUI
for count in 0...128 {
for width in [120.0, 280.0, 430.0, 800.0] {
let rows = CPUCoreGridLayout.rowCounts(count: count, width: width)
precondition(rows.reduce(0, +) == count && rows.count <= 3)
precondition(rows.reduce(0, +) == count)
if let first = rows.first, let last = rows.last {
precondition(last > 0 && first - last <= 1)
let columns = rows.max()!
let cellWidth = (width - Double(columns - 1) * 6) / Double(columns)
precondition(cellWidth >= 16, "Core text must fit a readable cell instead of being forced into three rows")
}
}
}
@@ -25,10 +28,13 @@ import SwiftUI
precondition(widths.reduce(0, +) <= width + 0.001)
for (group, groupWidth) in zip(groups, widths) {
let rows = CPUCoreGridLayout.rowCounts(count: group.count, width: groupWidth)
precondition(rows.count <= 3 && rows.reduce(0, +) == group.count)
precondition(rows.reduce(0, +) == group.count)
}
}
}
for width in [CGFloat.nan, .infinity, -.infinity, 0] {
precondition(CPUCoreGridLayout.rowCounts(count: 128, width: width).reduce(0, +) == 128)
}
precondition(CPUCoreGrid.groups(count: 3, kinds: [0: .efficiency]).count == 2)
print("PASS: balanced core rows at multiple widths and counts")
}
+28 -15
View File
@@ -14,26 +14,39 @@ import CoreGraphics
precondition(visible.insetBy(dx: 16, dy: 16).contains(centered))
}
}
let compact = PanelLayout.cardHeights(coreRows: 2, fanCount: 2)
precondition(PanelLayout.cardColumns(columns: 2) == [[.cpu, .gpu, .memory], [.battery, .storage, .network]])
precondition(PanelLayout.cardColumns(columns: 1).first == PanelCard.allCases)
for width: CGFloat in [480, 559, 560, 600, 640, 960] {
let size = CGSize(width: width, height: 820)
let columns = PanelLayout.columns(width: width)
let cardWidth = PanelLayout.cardWidth(size: size)
precondition(abs(CGFloat(columns) * cardWidth + CGFloat(columns - 1) * 8 - (width - 20)) < 0.001)
let rows = PanelLayout.rowHeights(columns: columns, coreRows: 3, fanCount: 6)
let requirements: [CGFloat] = [246, 204, 256, 204, 260, 260]
for index in requirements.indices {
precondition(rows[index / columns] >= requirements[index])
}
precondition(rows.count == (6 + columns - 1) / columns)
precondition(PanelLayout.needsResourceScroll(size: CGSize(width: width, height: 600), rows: rows))
precondition(!PanelLayout.needsResourceScroll(size: CGSize(width: width, height: 2400), rows: rows))
let cards = PanelLayout.cardColumns(columns: columns).flatMap { $0 }
precondition(cards.count == 6 && Set(cards) == Set(PanelCard.allCases))
precondition(PanelLayout.needsResourceScroll(size: CGSize(width: width, height: 500), heights: compact))
precondition(!PanelLayout.needsResourceScroll(size: CGSize(width: width, height: 2400), heights: compact))
}
precondition(PanelLayout.rowHeights(columns: 2, coreRows: 2, fanCount: 0) == [220, 204, 260])
precondition(PanelLayout.columns(width: 960) == 2 && PanelLayout.columns(width: 559) == 1 && PanelLayout.columns(width: 640) == 2)
precondition(!PanelLayout.needsResourceScroll(size: CGSize(width: 960, height: 800), rows: [220, 204, 260]))
precondition(!PanelLayout.needsResourceScroll(size: CGSize(width: 640, height: 800), rows: [220, 204, 260]),
"Moving the interval to the footer must let the standard two-column overview fit")
precondition(PanelLayout.needsResourceScroll(size: CGSize(width: 640, height: 760), rows: [220, 204, 260]))
precondition(PanelLayout.columnHeights(columns: 2, heights: compact) == [612, 564])
precondition(PanelLayout.preferredHeight(width: 640, heights: compact) == 706)
precondition(!PanelLayout.needsResourceScroll(size: CGSize(width: 640, height: 706), heights: compact))
precondition(PanelLayout.needsResourceScroll(size: CGSize(width: 640, height: 680), heights: compact))
precondition(PanelLayout.needsResourceScroll(size: CGSize(width: 480, height: 800), heights: compact))
precondition(PanelLayout.columns(width: 640) == 2 && PanelLayout.columns(width: 559) == 1)
let tallerCPU = PanelLayout.cardHeights(coreRows: 6, fanCount: 2)
precondition(tallerCPU[.cpu]! > compact[.cpu]! && tallerCPU[.battery] == compact[.battery],
"A taller CPU card must not stretch the opposite battery card")
let moreFans = PanelLayout.cardHeights(coreRows: 2, fanCount: 6)
precondition(moreFans[.gpu]! > compact[.gpu]! && moreFans[.storage] == compact[.storage])
let noFans = PanelLayout.cardHeights(coreRows: 2, fanCount: 0)
precondition(noFans[.gpu] == 124 && PanelLayout.preferredHeight(width: 640, heights: noFans) < 706)
precondition(PanelLayout.cardHeights(coreRows: 2, fanCount: nil) == compact,
"Discovery placeholders must reserve the ordinary fan row")
let manyCores = PanelLayout.cardHeights(coreRows: 30, fanCount: 10)
precondition(PanelLayout.preferredHeight(width: 640, heights: manyCores) == 800)
precondition(PanelLayout.needsResourceScroll(size: CGSize(width: 640, height: 800), heights: manyCores))
let requested = PanelLayout.contentSize(visible: CGRect(x: 0, y: 0, width: 1512, height: 900), preferredHeight: 732)
precondition(requested.height == 732, "A hardware-driven height above the default must be preserved")
let input: [ProcessMetrics] = (0..<2000).map { (index: Int) -> ProcessMetrics in
let id = ProcessIdentity(pid: Int32(index + 1), seconds: 1, microseconds: 0)
let path: String? = index == 42 ? "/test/needle" : nil
@@ -61,6 +74,6 @@ import CoreGraphics
}
let pidSorted = ProcessTableProjection.rows(input, query: "", sorting: [ProcessSort(key: .pid)], order: [])
precondition(pidSorted.first?.id.pid == 2000)
print("PASS: physical-row heights, column breakpoints, width conservation, resource scrolling, visible-frame bounds/chrome/negative origins, 2000-row CPU/footprint projection, frozen order, path search and empty results")
print("PASS: independent columns, hardware-driven heights, scroll and screen limits, column breakpoints, width conservation, resource scrolling, visible-frame bounds/chrome/negative origins, 2000-row CPU/footprint projection, frozen order, path search and empty results")
}
}
+11
View File
@@ -22,6 +22,17 @@ import SwiftUI
try? await Task.sleep(for: .milliseconds(150))
precondition(controller.panel.isVisible && visible)
precondition(controller.panel.firstResponder === controller.panel, "Opening must not focus the interval editor")
let initialSize = controller.panel.contentLayoutRect.size
controller.setPreferredContentHeight(initialSize.height - 24)
let resizeDeadline = ContinuousClock.now.advanced(by: .seconds(2))
while observedSize.height != initialSize.height - 24 && ContinuousClock.now < resizeDeadline { try? await Task.sleep(for: .milliseconds(20)) }
precondition(observedSize.height == initialSize.height - 24)
let stableFrame = controller.panel.frame
for _ in 0..<10 { controller.setPreferredContentHeight(initialSize.height - 24) }
controller.setPreferredContentHeight(.nan)
precondition(controller.panel.frame == stableFrame, "Unchanged or invalid budgets must not resize the panel")
controller.setPreferredContentHeight(initialSize.height)
try? await Task.sleep(for: .milliseconds(100))
let originalSize = controller.panel.contentLayoutRect.size
let compact = CGSize(width: min(760, originalSize.width), height: min(600, originalSize.height))
controller.panel.setContentSize(compact)