feat(menubar): 拆分紧凑双页面板并按页采样
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
import Foundation
|
||||
|
||||
@main struct DetailDemandChecks {
|
||||
@MainActor static func main() async throws {
|
||||
let suite = "kymo-detail-checks-" + UUID().uuidString
|
||||
let defaults = UserDefaults(suiteName: suite)!
|
||||
defer { defaults.removePersistentDomain(forName: suite) }
|
||||
let preferences = SamplingPreferences(defaults: defaults)
|
||||
preferences.panelMilliseconds = 500
|
||||
preferences.backgroundEnabled = false
|
||||
let probe = ProcessProbe()
|
||||
let store = MetricsStore(sampling: preferences, sampler: SystemSampler(processRead: { probe.read(&$0) }), energyEnabled: false)
|
||||
store.setPanelVisible(true)
|
||||
try await until { store.history.samples.count >= 2 }
|
||||
precondition(probe.count == 0 && store.processSnapshot == nil && store.energy == nil,
|
||||
"Overview must not enumerate processes or publish process power")
|
||||
store.setDetailDemand(processes: true, slowMetrics: false)
|
||||
try await until { probe.count > 0 }
|
||||
// Withdraw demand while the synchronous OS read still holds the single slot.
|
||||
store.setDetailDemand(processes: false, slowMetrics: true)
|
||||
try await Task.sleep(for: .milliseconds(350))
|
||||
precondition(store.processSnapshot == nil, "A completion from the previous page must not publish")
|
||||
let stopped = probe.count
|
||||
try await Task.sleep(for: .milliseconds(650))
|
||||
precondition(probe.count == stopped && store.snapshot.cpu != nil)
|
||||
store.setDetailDemand(processes: true, slowMetrics: false)
|
||||
try await until { store.processSnapshot != nil }
|
||||
precondition(probe.peak == 1)
|
||||
precondition(store.history.samples.dropFirst().allSatisfy { !$0.breakBefore && $0.cpu != nil },
|
||||
"Switching detail demand must not reset base CPU deltas or split history")
|
||||
store.setDetailDemand(processes: false, slowMetrics: true)
|
||||
precondition(store.processSnapshot == nil && store.energy == nil)
|
||||
store.togglePause()
|
||||
let pausedCount = store.history.samples.count
|
||||
store.setDetailDemand(processes: true, slowMetrics: false)
|
||||
try await Task.sleep(for: .milliseconds(650))
|
||||
precondition(store.history.samples.count == pausedCount && store.processSnapshot == nil)
|
||||
store.togglePause()
|
||||
try await until { store.processSnapshot != nil }
|
||||
store.setPanelVisible(false)
|
||||
precondition(store.processSnapshot == nil && store.energy == nil)
|
||||
store.togglePause()
|
||||
print("PASS: overview performs no process reads, page withdrawal rejects stale completion, single in-flight read, continuous base history and pause/hidden precedence")
|
||||
}
|
||||
|
||||
@MainActor private static func until(_ condition: () -> Bool) async throws {
|
||||
let deadline = ContinuousClock.now.advanced(by: .seconds(4))
|
||||
while !condition() && ContinuousClock.now < deadline { try await Task.sleep(for: .milliseconds(10)) }
|
||||
precondition(condition(), "Timed out waiting for detail demand")
|
||||
}
|
||||
}
|
||||
|
||||
private final class ProcessProbe: @unchecked Sendable {
|
||||
private let lock = NSLock()
|
||||
private var reads = 0
|
||||
private var active = 0
|
||||
private var maximum = 0
|
||||
var count: Int { lock.lock(); defer { lock.unlock() }; return reads }
|
||||
var peak: Int { lock.lock(); defer { lock.unlock() }; return maximum }
|
||||
func read(_ monitor: inout ProcessMonitor) -> ProcessSnapshot {
|
||||
lock.lock(); reads += 1; active += 1; maximum = max(maximum, active); lock.unlock()
|
||||
defer { lock.lock(); active -= 1; lock.unlock() }
|
||||
Thread.sleep(forTimeInterval: 0.25)
|
||||
return monitor.sample()
|
||||
}
|
||||
}
|
||||
@@ -61,11 +61,22 @@ private struct Host: View {
|
||||
let coordinator = table.delegate as! NativeProcessTable.Coordinator
|
||||
let probe = DelegateProbe(state, coordinator)
|
||||
table.delegate = probe
|
||||
precondition(table.numberOfRows == 500 && table.tableColumns.count == 6)
|
||||
precondition(table.numberOfRows == 500 && table.tableColumns.count == 7)
|
||||
precondition(table.frame.width >= table.enclosingScrollView!.contentSize.width - 1, "Native table must fill its viewport")
|
||||
precondition(coordinator.createdCells > 0 && coordinator.createdCells < 200, "Rows must be virtualized")
|
||||
let memoryCell = table.view(atColumn: 4, row: 0, makeIfNecessary: true) as! NSTableCellView
|
||||
precondition(memoryCell.textField?.stringValue == "9.5 MB", "Memory column must match Activity Monitor binary MB display")
|
||||
let powerColumn = table.tableColumns.firstIndex { $0.identifier.rawValue == "power" }!
|
||||
let emptyPower = table.view(atColumn: powerColumn, row: 0, makeIfNecessary: true) as! NSTableCellView
|
||||
precondition(emptyPower.textField?.stringValue == "--")
|
||||
state.rows[0].cpuPower = 0.005
|
||||
state.rows[0].powerWindow = 5
|
||||
await until { emptyPower.textField?.stringValue == "5.0 mW" }
|
||||
precondition(emptyPower.toolTip?.contains("5.0 s") == true)
|
||||
table.sortDescriptors = [NSSortDescriptor(key: "power", ascending: false)]
|
||||
await until { state.sorting == [ProcessSort(key: .power)] }
|
||||
table.sortDescriptors = [NSSortDescriptor(key: "power", ascending: true)]
|
||||
await until { state.sorting == [ProcessSort(key: .power, order: .forward)] }
|
||||
let chosen = state.rows[10].id
|
||||
table.selectRowIndexes(IndexSet(integer: 10), byExtendingSelection: false)
|
||||
precondition(state.selection == nil, "Native selection must be deferred")
|
||||
|
||||
@@ -13,13 +13,13 @@ import CoreGraphics
|
||||
precondition(visible.insetBy(dx: 16, dy: 16).contains(centered))
|
||||
}
|
||||
}
|
||||
for width: CGFloat in [640, 899, 900, 1159, 1160, 1240] {
|
||||
for width: CGFloat in [640, 679, 680, 899, 900, 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] = [306, 332, 280, 280, 280, 280]
|
||||
let requirements: [CGFloat] = [246, 204, 256, 204, 260, 260]
|
||||
for index in requirements.indices {
|
||||
precondition(rows[index / columns] >= requirements[index])
|
||||
}
|
||||
@@ -27,7 +27,9 @@ import CoreGraphics
|
||||
precondition(PanelLayout.needsResourceScroll(size: CGSize(width: width, height: 600), rows: rows))
|
||||
precondition(!PanelLayout.needsResourceScroll(size: CGSize(width: width, height: 2400), rows: rows))
|
||||
}
|
||||
precondition(PanelLayout.rowHeights(columns: 2, coreRows: 2, fanCount: 0) == [280, 280, 280])
|
||||
precondition(PanelLayout.rowHeights(columns: 2, coreRows: 2, fanCount: 0) == [220, 204, 260])
|
||||
precondition(PanelLayout.columns(width: 960) == 2 && PanelLayout.columns(width: 679) == 1)
|
||||
precondition(!PanelLayout.needsResourceScroll(size: CGSize(width: 960, height: 800), rows: [220, 204, 260]))
|
||||
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
|
||||
@@ -55,6 +57,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/RSS projection, frozen order, path search and empty results")
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,10 +8,12 @@ import SwiftUI
|
||||
Task { @MainActor in
|
||||
var visible = false
|
||||
var settingsRequests = 0
|
||||
var observedSize = CGSize.zero
|
||||
let controller = MenuPanelController(installStatusItem: false, visibility: { visible = $0 },
|
||||
openSettings: { settingsRequests += 1 }) { _ in
|
||||
IntervalControl(milliseconds: .constant(1000))
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
.background(PanelWindowObserver { observedSize = $0 }.frame(width: 0, height: 0))
|
||||
}
|
||||
precondition(!controller.presentation.isPinned)
|
||||
precondition(controller.statusMenu().items.map(\.title) == ["open", "settings", "", "quit"])
|
||||
@@ -20,7 +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 originalSize = controller.panel.contentLayoutRect.size
|
||||
let compact = CGSize(width: min(760, originalSize.width), height: min(600, originalSize.height))
|
||||
controller.panel.setContentSize(compact)
|
||||
let geometryDeadline = ContinuousClock.now.advanced(by: .seconds(2))
|
||||
while observedSize != compact && ContinuousClock.now < geometryDeadline { try? await Task.sleep(for: .milliseconds(20)) }
|
||||
precondition(observedSize == compact, "Layout must follow the actual viewport, not assume screen maximum")
|
||||
controller.panel.setContentSize(originalSize)
|
||||
// Geometry reports and centering run on subsequent main-queue turns.
|
||||
try? await Task.sleep(for: .milliseconds(200))
|
||||
controller.presentation.isPinned = true
|
||||
controller.presentation.page = .processes
|
||||
let cover = NSWindow(contentRect: controller.panel.frame.insetBy(dx: -8, dy: -8),
|
||||
styleMask: [.borderless], backing: .buffered, defer: false)
|
||||
cover.isReleasedWhenClosed = false
|
||||
@@ -42,7 +54,9 @@ import SwiftUI
|
||||
while visible && ContinuousClock.now < hiddenDeadline { try? await Task.sleep(for: .milliseconds(20)) }
|
||||
precondition(!visible && controller.presentation.isOpen, "OS visibility loss must stop demand without closing content/state")
|
||||
controller.panel.makeKeyAndOrderFront(nil)
|
||||
try? await Task.sleep(for: .milliseconds(150))
|
||||
controller.panel.orderFrontRegardless()
|
||||
let restoredDeadline = ContinuousClock.now.advanced(by: .seconds(2))
|
||||
while !visible && ContinuousClock.now < restoredDeadline { try? await Task.sleep(for: .milliseconds(20)) }
|
||||
precondition(visible, "OS visibility restoration must restore sampling demand")
|
||||
precondition(controller.panel.canBecomeKey && !controller.panel.canBecomeMain)
|
||||
precondition(controller.statusMenu().items.first?.title == "hide")
|
||||
@@ -50,6 +64,7 @@ import SwiftUI
|
||||
precondition(!controller.panel.isVisible && !visible)
|
||||
controller.togglePanel()
|
||||
precondition(controller.panel.isVisible && visible)
|
||||
precondition(controller.presentation.page == .overview, "Explicit reopening must select overview")
|
||||
controller.statusMenu().performActionForItem(at: 1)
|
||||
precondition(settingsRequests == 1 && !controller.panel.isVisible)
|
||||
controller.show()
|
||||
|
||||
@@ -39,6 +39,22 @@ import Foundation
|
||||
precondition(unsupported.state == .unsupported && unsupported.rows.isEmpty)
|
||||
let unverified = monitor.update(processes: processes, readings: [reading(0, at: 25)], capability: .unverified)
|
||||
precondition(unverified.rows.isEmpty && unverified.readable == 0)
|
||||
// Keep more than three measured processes and join by full identity, never PID alone.
|
||||
let manyRows = (0..<12).map { row(ProcessIdentity(pid: Int32(100 + $0), seconds: 1, microseconds: 0), 0) }
|
||||
let many = ProcessSnapshot(date: Date(), enumerated: 12, readable: 12, rows: manyRows, failed: false)
|
||||
_ = monitor.update(processes: many, readings: manyRows.map { reading(0, at: 10, id: $0.id) }, capability: .verifiedCPU)
|
||||
let all = monitor.update(processes: many, readings: manyRows.enumerated().map {
|
||||
reading(UInt64($0.offset) * 1_000_000_000, at: 15, id: $0.element.id)
|
||||
}, capability: .verifiedCPU)
|
||||
precondition(all.rows.count == 12 && all.measured == 12 && all.state == .available)
|
||||
let joined = many.withEnergy(all)
|
||||
precondition(joined.rows[0].cpuPower == 0 && joined.rows[11].cpuPower == 2.2)
|
||||
precondition(joined.rows[1].formattedPower == "200.0 mW" && joined.rows[11].formattedPower == "2.20 W")
|
||||
var reused = manyRows[0]
|
||||
reused = row(ProcessIdentity(pid: reused.id.pid, seconds: 2, microseconds: 0), 0)
|
||||
let replacement = ProcessSnapshot(date: Date(), enumerated: 1, readable: 1, rows: [reused], failed: false)
|
||||
precondition(replacement.withEnergy(all).rows[0].cpuPower == nil)
|
||||
precondition(joined.withEnergy(nil).rows.allSatisfy { $0.cpuPower == nil && $0.powerWindow == nil })
|
||||
print("PASS: exact large-integer deltas, first/zero/reset/reuse/gap/error/time boundaries, energy-vs-CPU ranking, partial coverage and no CPU fallback")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +28,12 @@ import Foundation
|
||||
precondition(sorted.last?.id == replacement.id, "Missing footprint sorts last in both directions")
|
||||
precondition(sorted.first?.id == (direction == .forward ? row.id : large.id))
|
||||
}
|
||||
var low = row, high = large, zero = replacement
|
||||
low.cpuPower = 0.01; high.cpuPower = 3; zero.cpuPower = 0
|
||||
for direction in [SortOrder.forward, .reverse] {
|
||||
let sorted = ProcessTableProjection.rows([row, high, low, zero], query: "", sorting: [ProcessSort(key: .power, order: direction)], order: [])
|
||||
precondition(sorted.last?.cpuPower == nil && sorted.first?.cpuPower == (direction == .forward ? 0 : 3))
|
||||
}
|
||||
print("PASS: filtered/live selection, absent selection/snapshot, failed enumeration, actual exit and PID reuse")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import Observation
|
||||
preferences.panelMilliseconds = 500
|
||||
preferences.backgroundMilliseconds = 1_800_000
|
||||
let store = MetricsStore(sampling: preferences)
|
||||
store.setDetailDemand(processes: true, slowMetrics: false)
|
||||
store.setPanelVisible(true)
|
||||
try await until { store.processSnapshot != nil }
|
||||
precondition(store.effectiveMilliseconds == 500)
|
||||
@@ -74,6 +75,7 @@ import Observation
|
||||
changing.panelMilliseconds = 500
|
||||
changing.backgroundMilliseconds = 1_800_000
|
||||
let changingStore = MetricsStore(sampling: changing, sampler: SystemSampler(processRead: { slowRead.read(&$0) }))
|
||||
changingStore.setDetailDemand(processes: true, slowMetrics: false)
|
||||
changingStore.setPanelVisible(true)
|
||||
try await until { !changingStore.history.samples.isEmpty }
|
||||
precondition(changingStore.processSnapshot == nil)
|
||||
@@ -92,6 +94,7 @@ import Observation
|
||||
try await Task.sleep(for: .milliseconds(650))
|
||||
precondition(dormant.history.samples.isEmpty && dormant.snapshot.memory == nil,
|
||||
"Hidden without a consumer must not perform even an initial full sample")
|
||||
dormant.setDetailDemand(processes: true, slowMetrics: false)
|
||||
dormant.setPanelVisible(true)
|
||||
try await until { dormant.processSnapshot != nil }
|
||||
dormant.setPanelVisible(false)
|
||||
|
||||
@@ -53,6 +53,7 @@ import Foundation
|
||||
let reopened = await sampler.sample()
|
||||
precondition(reopened.processes == nil)
|
||||
let store = MetricsStore()
|
||||
store.setDetailDemand(processes: true, slowMetrics: false)
|
||||
store.setPanelVisible(true)
|
||||
for _ in 0..<40 {
|
||||
if store.processSnapshot != nil { break }
|
||||
|
||||
Reference in New Issue
Block a user