diff --git a/docs/architecture.md b/docs/architecture.md index 4a49d89..1cfb6e0 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -61,6 +61,7 @@ GPU 通过 IOAccelerator 的 PerformanceStatistics / Device Utilization % 读取 - Store 在每次发布时向 `MetricHistory` 追加一项,最多保留 60 个样本,包含上/下行和设备读/写;分接口历史按索引/名称有界保存,接口消失清理(跨度由真实时间戳确定);缺失值保留为空,在趋势图中断开,不伪造为零。暂停不追加历史,恢复保留历史并重新建立 CPU 基线;breakBefore 标记只在首次/暂停恢复处断开;每个样本及分接口历史保留自己的预期间隔,改频率不插入人为断点。60 点代表样本数,时间跨度随真实间隔变化。 - 当前正式入口为统一 `MenuPanelView`:第一排 CPU/GPU/电池,电池宽高比 4:3、CPU/GPU 均分余宽;第二排内存/存储/网络等宽。宽度按所属屏幕 visibleFrame 限制在 1240pt 内,高度上限 820pt;≥1160pt 三列、≥900pt 两列、其他单列。三列且高度至少 810pt 时两排完整布局,参考尺寸两排分别 230/240pt,进程区使用剩余空间(最小 170pt)。其他尺寸资源独立滚动,进程常驻;三列低高度仍保持上述比例与顺序。原生 Table 自带纵横滚动与单元格复用,不在资源滚动容器内。首次布局计入屏幕与窗口 chrome,布局后约束完整 frame 原点并水平居中。NSPanel 固定/关闭策略和 key/main 资格已有独立检查;实际外部点击与多屏专项待补。 - `PanelWindowObserver` 仅观察所属窗口的屏幕/移动/缩放,测量尺寸并约束居中;可见性由 MenuPanelController 开关与内容挂载生命周期通知 Store,不根据失焦推断关闭。 +- CPU 逻辑核心类型在启动后按需从 IODeviceTree 的 `logical-cpu-id` 与 `cluster-type` 一次读取并缓存,按编号关联 Mach 采样结果;E、M、P 分别对应较低、中间和较高等级。存在 M 时 P 显示为超性能核,否则 P 显示为性能核。未知字段或无法读取时保持中性色,不依据核心顺序或数量推断;此设备树字段并非稳定的公开分类接口。卡片图例与辅助功能说明类型,底部标注开机运行时间,负载提示说明 1/5/15 分钟口径。 - `PanelTrendChart` 只接收时间戳、最多三序列、颜色/量纲/图例文本;Canvas 按真实时间定位,带三段刻度、网格和独立分段面积。CPU 同时绘制总体/用户/系统,网络上下行、存储读写共用量级。曲线使用相邻点间不超调的三次贝塞尔;缺失值、显式中断标记和超过 max(5s, 相邻样本预期间隔较大值×2) 的间隙断线。图例始终显示最新值;悬浮时使用独立浮层显示历史时刻与各序列值,保留游标/交点,不覆盖图例数据。图表通过 Anchor Preference 上报绘图区锚点、悬浮位置与内容,ResourceCard 在卡片最外层 overlay 统一绘制浮层,避免局部 zIndex 无法越过时间刻度/卡片字段;按实际本地化内容尺寸测量并约束在卡片内。浮层不参与鼠标命中,不创建顶层窗口;背景使用 NSVisualEffectView 的 popover/withinWindow 材质模糊下方内容,轻叠资源色,减少透明度时回退实色;原生材质视图也透传鼠标。CPU 使用青绿/紫红/金色区分,所有数据序列统一使用圆滑实线,未知仍为 --。真实追加时只运行一次 0.35 秒时间轴平移/新点揭示;UI 渲染帧最多保留 61 点,包含一个移出样本,旧首尾域连续插值到新首尾域,左端线段随动画裁出,不能先删除左端点。动画任务由提交后的帧状态触发,无独立 Timer 或后台循环;暂停间隙与重置不插值,减少动态效果时立即更新。CPU/GPU/电量固定 0–100%,内存按物理总字节;动态量级扩大立即,缩小连续十次低于一半后调整。 - 各卡标题与彩色主值均为 13pt、共享基线同行,数值组件复用系统 numericText 数字翻动/模糊过渡并遵循减少动态效果;进程行不施加该动画。PanelBackground / PanelCard 深浅色均为不透明实色,主面板与按钮组不使用玻璃材质;NSPanel 保留透明窗口壳仅用于圆角外部。深色强调色为薄荷青 #35D6BD、莓粉 #F56DA6、嫩绿 #93D45B、晴蓝 #6FA0FF、暖金 #FFC15A、浅紫 #BE8AFF,浅色提供同色相较深版本以保证可读性。网络下载/上传为蓝/金,存储读/写为金/紫,CPU 总体/用户/系统为青/粉/金;图表浮窗及原生控件弹窗继续使用模糊材质,遵循减少透明度设置。操作强调色仍由偏好决定;刷新控件、图钉、保持唤醒、暂停/恢复、设置、退出胶囊位于顶栏右侧。顶栏左侧为品牌、版本及缓存的本机产品名/芯片,产品名不可读时退回 hw.model,不读取序列号。原采样状态/更新时间与底栏移除。 - NetworkPathObserver 从系统路径的偏好首位接口提供默认接口名称([NWPath 接口按偏好排序](https://developer.apple.com/documentation/network/nwpath)),网络卡默认直接展示该接口的 IPv4,若无则展示 IPv6;未知不随意选择另一个接口。手动切换接口时同步展示该接口地址。getifaddrs 元数据、接口速率和路径状态仍独立采集;不把接口 IP 当作公网出口地址。 diff --git a/kymo/Core/Services/CPUCoreTopology.swift b/kymo/Core/Services/CPUCoreTopology.swift new file mode 100644 index 0000000..88b2d4c --- /dev/null +++ b/kymo/Core/Services/CPUCoreTopology.swift @@ -0,0 +1,26 @@ +import Foundation +import IOKit + +/// The registry identifies logical CPUs individually; absence or unknown types stay unclassified. +nonisolated enum CPUCoreTopology { + enum Kind: String, Sendable { case efficiency = "E", middle = "M", performance = "P" } + + static let cores: [Int: Kind] = { + let root = IORegistryEntryFromPath(kIOMainPortDefault, "IODeviceTree:/cpus") + guard root != 0 else { return [:] } + defer { IOObjectRelease(root) } + var iterator: io_iterator_t = 0 + guard IORegistryEntryGetChildIterator(root, kIODeviceTreePlane, &iterator) == KERN_SUCCESS else { return [:] } + defer { IOObjectRelease(iterator) } + var result: [Int: Kind] = [:] + while case let entry = IOIteratorNext(iterator), entry != 0 { + defer { IOObjectRelease(entry) } + guard let id = IORegistryEntryCreateCFProperty(entry, "logical-cpu-id" as CFString, kCFAllocatorDefault, 0)?.takeRetainedValue() as? NSNumber, + let data = IORegistryEntryCreateCFProperty(entry, "cluster-type" as CFString, kCFAllocatorDefault, 0)?.takeRetainedValue() as? Data, + let raw = String(data: data, encoding: .utf8)?.trimmingCharacters(in: .controlCharacters), + let kind = Kind(rawValue: raw), id.intValue >= 0 else { continue } + result[id.intValue] = kind + } + return result + }() +} diff --git a/kymo/Features/MenuBar/MenuPanelView.swift b/kymo/Features/MenuBar/MenuPanelView.swift index ed8e2ae..bc0dee6 100644 --- a/kymo/Features/MenuBar/MenuPanelView.swift +++ b/kymo/Features/MenuBar/MenuPanelView.swift @@ -102,6 +102,26 @@ struct MenuPanelView: View { HStack(alignment: .firstTextBaseline) { Text(key).foregroundStyle(.secondary); Spacer(minLength: 6); AnimatedMetricText(value: value) } } + private func coreColor(_ kind: CPUCoreTopology.Kind?) -> Color { + switch kind { + case .efficiency: Color("MetricCPU") + case .middle: Color("MetricMemory") + case .performance: Color("MetricGPU") + case nil: .secondary + } + } + + private func coreLabel(_ kind: CPUCoreTopology.Kind) -> Text { + switch kind { + case .efficiency: Text("cpu.kind.efficiency", tableName: "CPUDetails") + case .middle: Text("cpu.kind.performance", tableName: "CPUDetails") + case .performance: + CPUCoreTopology.cores.values.contains(.middle) + ? Text("cpu.kind.super", tableName: "CPUDetails") + : Text("cpu.kind.performance", tableName: "CPUDetails") + } + } + private var cpuCard: some 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), @@ -110,19 +130,29 @@ struct MenuPanelView: View { firstLabel: "panel.total", secondLabel: "cpu.user", thirdLabel: "cpu.system", expectedInterval: chartInterval, boundaryDates: chartBoundaries, intervals: chartIntervals) if let cpu = snapshot.cpu { HStack { line("cpu.user", MetricFormat.percent(cpu.user)); line("cpu.system", MetricFormat.percent(cpu.system)) } + let kinds = [CPUCoreTopology.Kind.efficiency, .middle, .performance].filter { CPUCoreTopology.cores.values.contains($0) } + if !kinds.isEmpty { + HStack(spacing: 10) { + ForEach(kinds, id: \.rawValue) { kind in + HStack(spacing: 3) { Circle().fill(coreColor(kind)).frame(width: 6, height: 6); coreLabel(kind) } + } + }.font(.system(size: 9)).foregroundStyle(.secondary).frame(maxWidth: .infinity, alignment: .leading) + } LazyVGrid(columns: Array(repeating: GridItem(.flexible(), spacing: 6), count: 7), spacing: 5) { ForEach(Array(cpu.cores.enumerated()), id: \.offset) { index, value in + let kind = CPUCoreTopology.cores[index] HStack(spacing: 3) { Text(verbatim: "\(index + 1)").font(.system(size: 9)).frame(width: 12) - MetricProgressBar(value: value, color: Color("MetricCPU")).frame(height: 7) - }.help("\(index + 1): \(MetricFormat.percent(value))") - .accessibilityLabel(Text("cpu.core")).accessibilityValue("\(index + 1): \(MetricFormat.percent(value))") + MetricProgressBar(value: value, color: coreColor(kind)).frame(height: 7) + }.help((kind.map { coreLabel($0) + Text(verbatim: " · ") } ?? Text("cpu.core")) + Text(verbatim: "\(index + 1): \(MetricFormat.percent(value))")) + .accessibilityLabel(kind.map { coreLabel($0) } ?? Text("cpu.core")).accessibilityValue("\(index + 1): \(MetricFormat.percent(value))") } }.padding(.vertical, 3) } else { Text("metric.waiting").foregroundStyle(.secondary) } Spacer(minLength: 0) - HStack { line("system.load", snapshot.load.map(MetricFormat.decimal).joined(separator: " / ")); Spacer(minLength: 8); AnimatedMetricText(value: String(format: "%.1f h", snapshot.uptime / 3600)) } + HStack { line("system.load", snapshot.load.map(MetricFormat.decimal).joined(separator: " / ")).help(Text("cpu.load.help", tableName: "CPUDetails")); Spacer(minLength: 8); Text("panel.uptime").foregroundStyle(.secondary); AnimatedMetricText(value: String(format: "%.1f h", snapshot.uptime / 3600)) } + .lineLimit(1).minimumScaleFactor(0.7) } } diff --git a/kymo/Resources/CPUDetails.xcstrings b/kymo/Resources/CPUDetails.xcstrings new file mode 100644 index 0000000..756dfee --- /dev/null +++ b/kymo/Resources/CPUDetails.xcstrings @@ -0,0 +1,98 @@ +{ + "sourceLanguage": "en", + "strings": { + "cpu.kind.efficiency": { + "extractionState": "manual", + "localizations": { + "en": { + "stringUnit": { + "state": "translated", + "value": "Efficiency" + } + }, + "zh-Hans": { + "stringUnit": { + "state": "translated", + "value": "能效核" + } + }, + "zh-Hant": { + "stringUnit": { + "state": "translated", + "value": "能效核心" + } + } + } + }, + "cpu.kind.performance": { + "extractionState": "manual", + "localizations": { + "en": { + "stringUnit": { + "state": "translated", + "value": "Performance" + } + }, + "zh-Hans": { + "stringUnit": { + "state": "translated", + "value": "性能核" + } + }, + "zh-Hant": { + "stringUnit": { + "state": "translated", + "value": "效能核心" + } + } + } + }, + "cpu.kind.super": { + "extractionState": "manual", + "localizations": { + "en": { + "stringUnit": { + "state": "translated", + "value": "Super" + } + }, + "zh-Hans": { + "stringUnit": { + "state": "translated", + "value": "超性能核" + } + }, + "zh-Hant": { + "stringUnit": { + "state": "translated", + "value": "超效能核心" + } + } + } + }, + "cpu.load.help": { + "extractionState": "manual", + "localizations": { + "en": { + "stringUnit": { + "state": "translated", + "value": "Average system load over the last 1, 5 and 15 minutes." + } + }, + "zh-Hans": { + "stringUnit": { + "state": "translated", + "value": "最近 1、5、15 分钟的系统平均负载。" + } + }, + "zh-Hant": { + "stringUnit": { + "state": "translated", + "value": "最近 1、5、15 分鐘的系統平均負載。" + } + } + } + } + }, + "version": "1.0" +}