From 521319ef9344dfe425dd6ede2115a37052226658 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A0=81=E5=86=9C=E9=98=BF=E6=A5=A0?= Date: Sun, 8 Nov 2026 15:06:27 +0800 Subject: [PATCH] =?UTF-8?q?refactor(ui):=20=E7=BB=9F=E4=B8=80=E8=83=B6?= =?UTF-8?q?=E5=9B=8A=E9=A1=B5=E7=AD=BE=E5=B9=B6=E5=B0=86=E9=9D=A2=E6=9D=BF?= =?UTF-8?q?=E6=94=B6=E7=AA=84=E8=87=B3640=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 2 + docs/README.md | 1 + docs/architecture.md | 4 +- docs/compact-panel-refinement-plan.md | 52 +++++++++++++++ docs/free-test-distribution.md | 6 +- docs/testing.md | 2 + kymo.xcodeproj/project.pbxproj | 4 +- .../MenuBar/MenuPanelView+Network.swift | 1 + .../MenuBar/MenuPanelView+ResourceCards.swift | 6 +- kymo/Features/MenuBar/MenuPanelView.swift | 8 +-- kymo/Features/MenuBar/PanelLayout.swift | 4 +- .../Settings/AppearanceSettingsView.swift | 8 +-- kymo/Resources/Localizable.xcstrings | 22 +++++++ kymo/UI/Components/NeutralTabPicker.swift | 51 +++++++++++++++ kymo/UI/Components/PanelStyle.swift | 1 + kymoTests/PanelLayoutChecks.swift | 5 +- kymoTests/TabPickerChecks.swift | 64 +++++++++++++++++++ tools/check.py | 8 ++- 18 files changed, 223 insertions(+), 26 deletions(-) create mode 100644 docs/compact-panel-refinement-plan.md create mode 100644 kymo/UI/Components/NeutralTabPicker.swift create mode 100644 kymoTests/TabPickerChecks.swift diff --git a/CHANGELOG.md b/CHANGELOG.md index 1009860..a39f17d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ ### 界面与性能 +- 1.1(8):最大宽度由 960pt 缩至 640pt 并保留两列;监控与设置共享原生胶囊 Tab,窄卡片省略重复图例值、简写外接电源时间并为截断字段提供完整提示。卡片高度方案另列,当前行对齐与 800pt 高度上限保留。 + - 1.1(7)面板拆为概览 / 进程两页,每次打开回到概览;两列六卡片、设备与运行时间底栏,小屏独立滚动。 - 移除 GPU 细项统计和电池耗电前三,逐核圆环改为紧凑短条,存储改为必要文本信息。 - 概览停用进程 / footprint / CPU 能量读取,进程页卸载资源图表并停用概览慢读取与公网任务;基础趋势跨页连续。 diff --git a/docs/README.md b/docs/README.md index cb22a52..0f829f5 100644 --- a/docs/README.md +++ b/docs/README.md @@ -14,6 +14,7 @@ | [静态呈现与轻量图表](static-presentation-plan.md) | 当前:保留卡片配色,移除动效与特效,原生静态图表与验证 | | [后台趋势连续性修复](background-trend-continuity-fix.md) | 1.1(4):短暂隐藏未到后台采样截止时,重开曲线仍保持连续 | | [图表跳过无值点](chart-missing-value-policy.md) | 1.1(5):只绘制有效节点并接续,保留真实时间、有效 0 和主动停采断点 | +| [640pt 面板与高度方案](compact-panel-refinement-plan.md) | 本轮:中性色 Tab 与 640pt 宽度;后续:独立双列、按内容定高方案 | | [紧凑双页面板](compact-tab-panel-plan.md) | 已实现:概览 / 进程页签、CPU 功率列、按页采样及三轮对照与限制 | 首批实施证据见[轻量化实施记录](efficiency-first-batch.md);第二批见[能量列表与纯文字对照记录](efficiency-second-batch.md)。第三批见[原生绘制与进程表记录](efficiency-third-batch.md);第四批见[动效与效率排查](motion-and-efficiency-plan.md);其淡入实现已由[保留原动效的优化](motion-preservation-plan.md)替代。进程内存显示修正见[footprint 口径与单位对齐](process-memory-alignment-plan.md)。包体积已收敛,真实 CPU 能量列表已实现;内存与长期性能预算仍待通过。 diff --git a/docs/architecture.md b/docs/architecture.md index 00adde2..d14d4f5 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -23,7 +23,7 @@ 入口为 `kymoApp.main()`,由 `NSApplication` 与 `ApplicationLifecycle` 驱动,当前不使用 `MenuBarExtra`。应用以 accessory 模式启动,通过 `NSHostingSceneRepresentation` 注册 Settings 场景。 -`MenuPanelController` 拥有模板品牌状态项和无边框、可成为 key 的非激活 `MonitoringPanel`。左键切换面板;右键菜单提供打开 / 隐藏、设置和退出。面板不播放开关窗动画、不添加装饰阴影。窗口按状态项所在屏幕可见区域定位,水平居中、顶部预留 16pt;最大内容尺寸 960 × 800pt,并按屏幕缩小。面板顶部为概览 / 进程页签,每次显式打开重置概览。常规概览两列,CPU / 电池、GPU / 存储、内存 / 网络逐行配对;宽度小于 680pt 时退为单列,高度不足时滚动。进程页独占中间内容区。底栏显示设备及运行时间。宽度小于 840pt 时顶部操作组与页签分行,保证页签居中且不遮挡操作。几何观察器以实际 contentLayoutRect 和屏幕上限共同确定布局尺寸。 +`MenuPanelController` 拥有模板品牌状态项和无边框、可成为 key 的非激活 `MonitoringPanel`。左键切换面板;右键菜单提供打开 / 隐藏、设置和退出。面板不播放开关窗动画、不添加装饰阴影。窗口按状态项所在屏幕可见区域定位,水平居中、顶部预留 16pt;最大内容尺寸 640 × 800pt,并按屏幕缩小。面板顶部为概览 / 进程页签,每次显式打开重置概览。常规概览两列,CPU / 电池、GPU / 存储、内存 / 网络逐行配对;宽度小于 560pt 时退为单列,高度不足时滚动。进程页独占中间内容区。底栏显示设备及运行时间。宽度小于 840pt 时顶部操作组与页签分行,保证页签居中且不遮挡操作。几何观察器以实际 contentLayoutRect 和屏幕上限共同确定布局尺寸。 固定按钮切换普通 / floating 层级。未固定时外部交互或应用失活可收起,固定时仍可通过状态项或显式关闭收起。进程表的 Esc 依次处理搜索、选中和搜索焦点,随后关闭窗口。隐藏面板时先卸载内容视图、关闭底层窗口资源(保留控制器拥有的窗口对象供重开),并停止监听外部鼠标事件;仅显式启用后台趋势时保留常规采样。 @@ -107,3 +107,5 @@ PublicIPAddressService 默认持有专用 ephemeral URLSession,URLCache / HTTP ## 紧凑双页展示 CPU 逐核显示使用数值与静态短条,内存保留分布条;GPU 渲染器 / 分块器独立进度条和统计区、电池内的耗电前三列表已移除,存储重复容量条改为可用容量 / 健康 / 通电时长 / 卷入口文本。六张趋势保留既有真实时间及断点规则。页面布局和数据需求详见[实施记录](compact-tab-panel-plan.md)。 + +进一步收窄阶段中,概览与设置共享 UI/Components/NeutralTabPicker,保持中性色分段导航;窄卡片的内存趋势图例省略重复实时值,完整分类数值仍在内存分布区及悬浮中提供。外接电源的时间项简写为不适用,完整说明在提示中。独立双列和按内容定高尚为[下一阶段方案](compact-panel-refinement-plan.md),当前仍按物理行对齐,800pt 高度上限未变。 diff --git a/docs/compact-panel-refinement-plan.md b/docs/compact-panel-refinement-plan.md new file mode 100644 index 0000000..937b82d --- /dev/null +++ b/docs/compact-panel-refinement-plan.md @@ -0,0 +1,52 @@ +# 640pt 面板与卡片高度优化方案 + +2026-11-08。用户要求:Tab 与设置页一致;窗口宽度缩到现有 2/3;提出消除卡片留白、进一步缩短窗口的方案。 + +## 本轮范围与现状 + +- 第一阶段直接实施 Tab 与宽度:960 → 640pt;保留两列,只有更窄屏幕退为单列;对窄卡片内的图例和冗长值做必要适配。 +- 第二阶段仅设计高度方案,尚不改变卡片排列、窗口高度或刷新间隔入口。 +- 工作开始时工作区与暂存区干净,当前分支 codex/compact-tab-panel。 +- 修改前设置与面板都使用 SwiftUI segmented Picker,但面板继承蓝色 tint,设置位于工具栏上下文;改成共享中性色 Tab 控件,避免两个 Features 相互依赖。 +- 现状按行取 max(左右卡高),另有卡片最小高度和内部 Spacer。当前两列行高为 220 / 204 / 260pt(高核心数或多风扇时增加),不能仅靠取消行对齐就明显降低整体高度。 + +## 第一阶段实施与验证 + +- [x] UI:共享中性色 Tab,概览与设置共同使用,保留原绑定和辅助功能。 +- [x] MenuBar:最大窗口宽 640pt,配套下调两列断点,必要窄卡片文案 / 图例适配;当前高度上限仍为 800pt。 +- [x] 检查 Debug 构建、布局、三语和相关原生窗口 / 表格回归;实际检查 640pt 下概览与进程、Tab 外观及交互。 +- [x] 同步架构与验收记录,提交本轮相关修改,不推送。 + +## 第二阶段建议:按内容定高的独立双列 + +保持左列 CPU / GPU / 内存、右列电池 / 存储 / 网络,各列自上而下紧凑排列。只对齐两列顶部,允许中间卡片边缘错开;两列底部剩余高度差保留在列外,不填进卡片。不要按每轮数值动态变高,避免刷新时抖动。 + +同时移除无意义固定高度和 Spacer,卡片高度取决于明确的内容行数、可用宽度与硬件拓扑。保留固定尺寸子宿主,但由父层按内容预算一次确定尺寸;不引入每轮递归测量、定时器、动画或新依赖。 + +| 部分 | 压缩办法 | +| --- | --- | +| 顶部 | 将刷新间隔输入收进时钟按钮弹层;其余操作保留,品牌 / 居中 Tab / 按钮组在 640pt 下恢复一行 | +| 趋势区 | 六张图保持相同绘图区高度,可从 58pt 试降至 44–48pt;刻度、实际时间、悬浮和缺失值规则保留 | +| CPU | 按核心分组及实际行数预留高度,核心区不再以 Spacer 撑开 | +| GPU | 趋势 + 风扇文本行,删除底部空档;无风扇时不留空行 | +| 电池 | 保留四个信息项,按窄卡片宽度紧凑排布,外接电源时间项简写并保留完整提示 | +| 存储 | 维持必要文本行;较长标题用提示保留全称,不为重复容量图预留高度 | +| 内存 | 保留分布条及分类数值;压力 / 缓存 / Swap 改成紧凑信息行,避免重复图例数值 | +| 网络 | 状态与接口选择合并;公网 / 内网保持可复制,长 IPv6 地址截断但可查看完整值 | +| 公共底栏 | 根据设备名称长度截断,保持一行;窗口高度随较长列确定并受屏幕上限约束 | + +以当前 14 核机型为例,卡片高度设计预算为 CPU 200–210、GPU 150–170、内存 210–230pt;电池 160–180、存储 150–170、网络 210–230pt。这些是待实现后测定的目标,并非现有界面实测值。 + +设计目标为约 640×680–720pt,而不是强制把全部机器压进该尺寸。具体高度须用 14 / 32 / 高核心数、无电池 / 无风扇、简繁英三语与长地址验证后确定;小屏继续滚动。进程页沿用同一外框及横纵滚动,防止切页时窗口跳动。 + +## 风险与验收 + +- 640pt 下未计滚动条时每卡约 306pt,简单把宽度改小会触发现有单列断点并放大高度,必须同步调整布局规则。 +- 独立列会改变横向成对阅读关系,用户需要认可上下边缘错位;不做每次按最短列重新分配,避免卡片顺序变动。 +- 高度变化由窗口 / 内容配置或硬件结构变化驱动,瞬时值缺失和数值更新不触发布局跳动;核心槽位按静态硬件拓扑预算,风扇在同次打开时保留已发现的槽位。 +- 这轮只调整呈现,不改变两页采样、XPC、安全边界及后台记录偏好;布局更小不能直接当作功耗降低证据。 +- 后续高度实施需要验证图表提示位置、所有核心 / 风扇可达、两页表格交互、切页停止非当前页面工作及实际资源占用。 + +## 本轮结果 + +Tab 已改为共享 AppKit NSSegmentedControl(tabs role + capsule 样式),避免 SwiftUI 的 body / toolbar 语义造成不同外观。仅覆盖 tint 的试验未采用。已通过 7 项相关回归及新增 TabPickerChecks,后者验证原生控件双向绑定、三语即时更新和选择保留;目录共 213 项。实际 CUA 检查了 640pt 双列、监控页签来回切换、设置中的常规 / 关于切换及两种宿主的胶囊外观。最终 Debug 构建通过,1.1(8)普通 Release 测试包构建、签名 / 权限 / ZIP 解压复核通过;第二阶段仍为方案。已清理本轮临时副本、构建、脚本、日志和随机容器。测试前记录的 /Applications/kymo.app(PID 50403)及其 SMARTService(PID 50406)保留,未安装、推送或发布。 diff --git a/docs/free-test-distribution.md b/docs/free-test-distribution.md index 3866ea1..cd5517f 100644 --- a/docs/free-test-distribution.md +++ b/docs/free-test-distribution.md @@ -2,7 +2,7 @@ ## 适用范围 -当前产物:Kymo 1.1(build 7),macOS 27.0 及以上、Apple silicon arm64。Intel 或较低系统不在该产物支持范围。软件使用 ad-hoc 本地签名,未经过 Apple 公证,不是 Developer ID 官方分发包;下载后的系统提示取决于接收方安全策略。 +当前产物:Kymo 1.1(build 8),macOS 27.0 及以上、Apple silicon arm64。Intel 或较低系统不在该产物支持范围。软件使用 ad-hoc 本地签名,未经过 Apple 公证,不是 Developer ID 官方分发包;下载后的系统提示取决于接收方安全策略。 ## 构建与校验 @@ -10,10 +10,10 @@ python3 tools/package_release.py ``` -默认输出:`dist/kymo-1.1-7-arm64-adhoc.zip` 和同名 `.sha256`。首次打包前会通过签名、Hardened Runtime、权限和包内容检查,ZIP 解压后再次检查。再次构建不覆盖已存在同名产物,可用 `--output` 另选目录。校验下载文件: +默认输出:`dist/kymo-1.1-8-arm64-adhoc.zip` 和同名 `.sha256`。首次打包前会通过签名、Hardened Runtime、权限和包内容检查,ZIP 解压后再次检查。再次构建不覆盖已存在同名产物,可用 `--output` 另选目录。校验下载文件: ```bash -shasum -a 256 -c kymo-1.1-7-arm64-adhoc.zip.sha256 +shasum -a 256 -c kymo-1.1-8-arm64-adhoc.zip.sha256 ``` SHA-256 用于核对文件是否与发布者给出的文件一致,不等同于 Apple 身份认证;校验值应从可信渠道获取。 diff --git a/docs/testing.md b/docs/testing.md index 1462380..27cbfa0 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -344,3 +344,5 @@ PublicIPAddressChecks 使用生产 sessionConfiguration,验证缓存 / Cookie DetailDemandChecks 验证概览零进程枚举、切页时旧同步任务不回写、单在途、基础 CPU 差分和历史连续、暂停 / 隐藏优先。ProcessEnergyChecks 增加多于三项功率、完整身份合并和 PID 复用;ProcessSelectionChecks 增加功率双向排序、零值与缺失置后;NativeProcessTableChecks 验证真实原生功率列文本、统计窗口和排序回调;PanelWindowChecks 验证关闭重开默认概览。PanelLayoutChecks 覆盖两列配对行高和小屏滚动。 显式诊断构建(DEBUG 或 KYMO_AUDIT)可设置 KYMO_RESOURCE_AUDIT=1、KYMO_AUDIT_INTERACTIVE=1 打开固定面板供实际交互,可加 KYMO_AUDIT_SIZE=760x600 检查实际小 viewport;KYMO_AUDIT_PAGE=processes 选择资源循环中的进程页;KYMO_AUDIT_QUICK=1 使用 3 秒初始隐藏、每轮 30 秒展开 / 5 秒关闭,不执行暂停段。普通 Release 不包含驱动实现。此类诊断驱动不能替代实际输入事件,也不能证明长期性能预算。 + +TabPickerChecks 使用实际原生 Tab 检查双向选择绑定、三语标签原地更新及选择保留。640pt 面板与后续高度建议见[收窄方案](compact-panel-refinement-plan.md);本轮仅改变宽度和 Tab,不复用上一轮资源数据宣称进一步节能。 diff --git a/kymo.xcodeproj/project.pbxproj b/kymo.xcodeproj/project.pbxproj index 31c6887..ddd1ac7 100644 --- a/kymo.xcodeproj/project.pbxproj +++ b/kymo.xcodeproj/project.pbxproj @@ -425,7 +425,7 @@ ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 7; + CURRENT_PROJECT_VERSION = 8; DEAD_CODE_STRIPPING = YES; ENABLE_APP_SANDBOX = YES; ENABLE_OUTGOING_NETWORK_CONNECTIONS = YES; @@ -458,7 +458,7 @@ ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 7; + CURRENT_PROJECT_VERSION = 8; DEAD_CODE_STRIPPING = YES; ENABLE_APP_SANDBOX = YES; ENABLE_HARDENED_RUNTIME = YES; diff --git a/kymo/Features/MenuBar/MenuPanelView+Network.swift b/kymo/Features/MenuBar/MenuPanelView+Network.swift index fe0d9b8..805a17a 100644 --- a/kymo/Features/MenuBar/MenuPanelView+Network.swift +++ b/kymo/Features/MenuBar/MenuPanelView+Network.swift @@ -58,6 +58,7 @@ extension MenuPanelView { HStack(spacing: 4) { Text(title).foregroundStyle(.secondary) Text(verbatim: address ?? "--").font(PanelStyle.value).monospacedDigit().textSelection(.enabled) + .help(Text(verbatim: address ?? "--")) Spacer(minLength: 0) if let address { Button { diff --git a/kymo/Features/MenuBar/MenuPanelView+ResourceCards.swift b/kymo/Features/MenuBar/MenuPanelView+ResourceCards.swift index e3c3f1f..44dc4ed 100644 --- a/kymo/Features/MenuBar/MenuPanelView+ResourceCards.swift +++ b/kymo/Features/MenuBar/MenuPanelView+ResourceCards.swift @@ -88,7 +88,8 @@ extension MenuPanelView { thirdColor: Color("MemoryWired"), fourthColor: Color("MemoryCompressed"), fixedScale: Double(snapshot.memory?.total ?? 1), scaleLabel: MetricFormat.memory(snapshot.memory?.total), firstValueFormat: .memory, firstLabel: "gpu.totalUtilization", secondLabel: "memory.app", thirdLabel: "memory.wired", fourthLabel: "memory.compressed", - expectedInterval: chartInterval, boundaryDates: chartBoundaries, intervals: chartIntervals) + expectedInterval: chartInterval, boundaryDates: chartBoundaries, intervals: chartIntervals, + showsLegendValues: cardContentWidth >= 340) if let memory = snapshot.memory { MemoryDetailsView(memory: memory) } else { @@ -184,7 +185,8 @@ extension MenuPanelView { Text("battery.timeEstimate").foregroundStyle(.secondary) Spacer(minLength: 4) if battery.externalPower { - Text("battery.timeNotApplicable").font(PanelStyle.value) + Text("battery.notApplicableShort").font(PanelStyle.value) + .help(Text("battery.timeNotApplicable")) } else if let minutes = battery.minutesRemaining { MetricText(value: "\(minutes) min").font(PanelStyle.value) } else { diff --git a/kymo/Features/MenuBar/MenuPanelView.swift b/kymo/Features/MenuBar/MenuPanelView.swift index 3d82527..314bfa1 100644 --- a/kymo/Features/MenuBar/MenuPanelView.swift +++ b/kymo/Features/MenuBar/MenuPanelView.swift @@ -124,11 +124,9 @@ struct MenuPanelView: View { } private var pagePicker: some View { - Picker("panel.page", selection: Binding(get: { presentation.page }, set: { presentation.page = $0 })) { - Text("panel.overview").tag(PanelPage.overview) - Text("panel.processes").tag(PanelPage.processes) - } - .pickerStyle(.segmented).labelsHidden().frame(width: 164) + NeutralTabPicker(title: "panel.page", selection: Binding(get: { presentation.page }, set: { presentation.page = $0 }), + options: [(.overview, "panel.overview"), (.processes, "panel.processes")]) + .frame(width: 164) .accessibilityIdentifier("panel.tabs") } diff --git a/kymo/Features/MenuBar/PanelLayout.swift b/kymo/Features/MenuBar/PanelLayout.swift index f017900..d66bbce 100644 --- a/kymo/Features/MenuBar/PanelLayout.swift +++ b/kymo/Features/MenuBar/PanelLayout.swift @@ -4,7 +4,7 @@ import CoreGraphics nonisolated enum PanelLayout { static let baseCardHeight: CGFloat = 204 static func headerHeight(width: CGFloat) -> CGFloat { width >= 840 ? 32 : 64 } - static func columns(width: CGFloat) -> Int { width >= 680 ? 2 : 1 } + static func columns(width: CGFloat) -> Int { width >= 560 ? 2 : 1 } static func cardWidth(size: CGSize) -> CGFloat { let count = columns(width: size.width) return max(0, (size.width - 20 - CGFloat(count - 1) * 8) / CGFloat(count)) @@ -28,7 +28,7 @@ nonisolated enum PanelLayout { } static func contentSize(visible: CGRect, chrome: CGSize = .zero) -> CGSize { - CGSize(width: max(1, min(960, visible.width - 32 - max(0, chrome.width))), + CGSize(width: max(1, min(640, visible.width - 32 - max(0, chrome.width))), height: max(1, min(800, visible.height - 32 - max(0, chrome.height)))) } static func centeredFrame(_ frame: CGRect, visible: CGRect) -> CGRect { diff --git a/kymo/Features/Settings/AppearanceSettingsView.swift b/kymo/Features/Settings/AppearanceSettingsView.swift index 5d42b2a..d2c7de6 100644 --- a/kymo/Features/Settings/AppearanceSettingsView.swift +++ b/kymo/Features/Settings/AppearanceSettingsView.swift @@ -20,12 +20,8 @@ struct AppearanceSettingsView: View { .padding(12) .toolbar { ToolbarItem(placement: .principal) { - Picker("settings.title", selection: $selection) { - Text("settings.general").tag(SettingsPage.general) - Text("settings.about").tag(SettingsPage.about) - } - .pickerStyle(.segmented) - .labelsHidden() + NeutralTabPicker(title: "settings.title", selection: $selection, + options: [(.general, "settings.general"), (.about, "settings.about")]) } } .background(SettingsWindowLifecycle(title: AppLocalization.string("settings.title", locale: locale), diff --git a/kymo/Resources/Localizable.xcstrings b/kymo/Resources/Localizable.xcstrings index 5a303c5..cd35ce4 100644 --- a/kymo/Resources/Localizable.xcstrings +++ b/kymo/Resources/Localizable.xcstrings @@ -1,6 +1,28 @@ { "sourceLanguage" : "en", "strings" : { + "battery.notApplicableShort" : { + "localizations" : { + "en" : { + "stringUnit" : { + "state" : "translated", + "value" : "N/A" + } + }, + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "不适用" + } + }, + "zh-Hant" : { + "stringUnit" : { + "state" : "translated", + "value" : "不適用" + } + } + } + }, "panel.power" : { "localizations" : { "zh-Hans" : { diff --git a/kymo/UI/Components/NeutralTabPicker.swift b/kymo/UI/Components/NeutralTabPicker.swift new file mode 100644 index 0000000..5e00f7a --- /dev/null +++ b/kymo/UI/Components/NeutralTabPicker.swift @@ -0,0 +1,51 @@ +import AppKit +import SwiftUI + +/// Tab navigation uses AppKit's toolbar-style capsule in either hosting context. +struct NeutralTabPicker: NSViewRepresentable { + let title: String + @Binding var selection: Selection + let options: [(Selection, String)] + @Environment(\.locale) private var locale + + func makeCoordinator() -> NeutralTabPickerCoordinator { NeutralTabPickerCoordinator() } + + func makeNSView(context: Context) -> NSSegmentedControl { + let control = NSSegmentedControl() + control.trackingMode = .selectOne + control.segmentStyle = .capsule + control.role = .tabs + control.borderShape = .capsule + control.segmentDistribution = .fillEqually + control.controlSize = .regular + control.target = context.coordinator + control.action = #selector(NeutralTabPickerCoordinator.changed(_:)) + return control + } + + func updateNSView(_ control: NSSegmentedControl, context: Context) { + if control.segmentCount != options.count { control.segmentCount = options.count } + for (index, option) in options.enumerated() { + let label = AppLocalization.string(option.1, locale: locale) + if control.label(forSegment: index) != label { control.setLabel(label, forSegment: index) } + } + let selected = options.firstIndex { $0.0 == selection } ?? -1 + if control.selectedSegment != selected { control.selectedSegment = selected } + control.setAccessibilityLabel(AppLocalization.string(title, locale: locale)) + let binding = $selection, values = options.map(\.0) + context.coordinator.onSelect = { index in + guard values.indices.contains(index) else { return } + binding.wrappedValue = values[index] + } + } + + func sizeThatFits(_ proposal: ProposedViewSize, nsView: NSSegmentedControl, context: Context) -> CGSize? { + CGSize(width: proposal.width ?? nsView.intrinsicContentSize.width, + height: max(24, nsView.intrinsicContentSize.height)) + } +} + +@MainActor final class NeutralTabPickerCoordinator: NSObject { + var onSelect: ((Int) -> Void)? + @objc func changed(_ sender: NSSegmentedControl) { onSelect?(sender.selectedSegment) } +} diff --git a/kymo/UI/Components/PanelStyle.swift b/kymo/UI/Components/PanelStyle.swift index 7eebefe..67b5ac7 100644 --- a/kymo/UI/Components/PanelStyle.swift +++ b/kymo/UI/Components/PanelStyle.swift @@ -28,6 +28,7 @@ struct CardField: View { var body: some View { VStack(alignment: .leading, spacing: 4) { Text(title).font(PanelStyle.label).foregroundStyle(.secondary).frame(height: 14) + .help(Text(title)) content().font(PanelStyle.value).monospacedDigit().frame(height: 14) } .frame(maxWidth: .infinity, alignment: .leading) diff --git a/kymoTests/PanelLayoutChecks.swift b/kymoTests/PanelLayoutChecks.swift index ea917b1..20bc335 100644 --- a/kymoTests/PanelLayoutChecks.swift +++ b/kymoTests/PanelLayoutChecks.swift @@ -3,6 +3,7 @@ import CoreGraphics @main struct PanelLayoutChecks { static func main() { + precondition(PanelLayout.contentSize(visible: CGRect(x: 0, y: 0, width: 1512, height: 900)).width == 640) for visible in [CGRect(x: 0, y: 67, width: 1512, height: 882), CGRect(x: -1440, y: -200, width: 1440, height: 800), CGRect(x: 0, y: 24, width: 800, height: 600)] { let content = PanelLayout.contentSize(visible: visible, chrome: CGSize(width: 4, height: 12)) precondition(content.width + 4 <= visible.width - 32 && content.height + 12 <= visible.height - 32) @@ -13,7 +14,7 @@ import CoreGraphics precondition(visible.insetBy(dx: 16, dy: 16).contains(centered)) } } - for width: CGFloat in [640, 679, 680, 899, 900, 960] { + 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) @@ -28,7 +29,7 @@ import CoreGraphics precondition(!PanelLayout.needsResourceScroll(size: CGSize(width: width, height: 2400), rows: rows)) } 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.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])) let input: [ProcessMetrics] = (0..<2000).map { (index: Int) -> ProcessMetrics in let id = ProcessIdentity(pid: Int32(index + 1), seconds: 1, microseconds: 0) diff --git a/kymoTests/TabPickerChecks.swift b/kymoTests/TabPickerChecks.swift new file mode 100644 index 0000000..814eb3c --- /dev/null +++ b/kymoTests/TabPickerChecks.swift @@ -0,0 +1,64 @@ +import AppKit +import SwiftUI +import Observation + +@MainActor @Observable private final class TabState { + var selected = 0 + var locale = Locale(identifier: "en") +} + +private struct TabHost: View { + @Bindable var state: TabState + var body: some View { + NeutralTabPicker(title: "panel.page", selection: $state.selected, + options: [(0, "panel.overview"), (1, "panel.processes")]) + .environment(\.locale, state.locale) + .frame(width: 164, height: 28) + } +} + +@main struct TabPickerChecks { + @MainActor static func main() { + _ = NSApplication.shared + NSApp.setActivationPolicy(.accessory) + Task { @MainActor in + let state = TabState() + let window = NSWindow(contentRect: NSRect(x: 40, y: 40, width: 220, height: 70), + styleMask: [.titled], backing: .buffered, defer: false) + window.isReleasedWhenClosed = false + let host = NSHostingView(rootView: TabHost(state: state)) + window.contentView = host + window.makeKeyAndOrderFront(nil) + await until { find(host) != nil } + let control = find(host)! + await until { control.label(forSegment: 0) == "Overview" } + precondition(control.selectedSegment == 0) + state.selected = 1 + await until { control.selectedSegment == 1 } + control.selectedSegment = 0 + _ = control.sendAction(control.action, to: control.target) + await until { state.selected == 0 } + for (locale, title) in [("zh-Hans", "概览"), ("zh-Hant", "概覽"), ("en", "Overview")] { + state.locale = Locale(identifier: locale) + await until { control.label(forSegment: 0) == title } + precondition(find(host) === control, "Locale updates must retain the mounted native control") + precondition(control.selectedSegment == 0) + } + window.orderOut(nil); window.close() + print("PASS: native tab binding in both directions, localized labels in place and retained selection") + fflush(stdout); NSApp.terminate(nil) + } + NSApp.run() + } + + @MainActor private static func until(_ condition: () -> Bool) async { + let deadline = ContinuousClock.now.advanced(by: .seconds(3)) + while !condition() && ContinuousClock.now < deadline { try? await Task.sleep(for: .milliseconds(20)) } + precondition(condition(), "Timed out waiting for native tab update") + } + + @MainActor private static func find(_ view: NSView) -> NSSegmentedControl? { + if let control = view as? NSSegmentedControl { return control } + return view.subviews.lazy.compactMap { find($0) }.first + } +} diff --git a/tools/check.py b/tools/check.py index c56b203..1e23829 100644 --- a/tools/check.py +++ b/tools/check.py @@ -36,6 +36,8 @@ CHECKS.update({ "AppearancePreferencesChecks": ["kymo/Core/Services/AppearancePreferences.swift", "kymo/Core/Services/SamplingPreferences.swift"], "NetworkAddressChecks": ["kymo/Core/Monitors/NetworkMonitor.swift"], "ProcessIconCacheChecks": ["kymo/Features/MenuBar/ProcessIconCache.swift"], + "TabPickerChecks": ["kymo/UI/Components/NeutralTabPicker.swift", "kymo/UI/Components/AppLocalization.swift", + "kymo/Core/Services/LanguagePreferences.swift"], "NativeProcessTableChecks": ["kymo/Core/Models/ProcessMetrics.swift", "kymo/Features/MenuBar/ProcessTableProjection.swift", "kymo/Features/MenuBar/NativeProcessTable.swift", "kymo/Features/MenuBar/ProcessIconCache.swift", "kymo/Core/Services/LanguagePreferences.swift", "kymo/UI/Components/AppLocalization.swift", @@ -59,7 +61,7 @@ CHECKS.update({ "kymo/Features/MenuBar/PanelWindowObserver.swift", "kymo/UI/Components/IntervalControl.swift"], "SandboxNetworkChecks": ["kymo/Core/Services/PublicIPAddressService.swift"], }) -GUI = {"NativeMetricDrawingChecks", "NativeContentHostChecks", "NativeProcessTableChecks", "LanguageScopeChecks", "SettingsWindowChecks", "PanelWindowChecks"} +GUI = {"NativeMetricDrawingChecks", "NativeContentHostChecks", "NativeProcessTableChecks", "LanguageScopeChecks", "SettingsWindowChecks", "PanelWindowChecks", "TabPickerChecks"} NETWORK = {"SandboxNetworkChecks"} SERVICES = {"ReleaseServiceChecks"} @@ -93,7 +95,7 @@ def test_bundle(work, name, executable, host_entitlements=None, resources=None, (contents / "Info.plist").write_bytes(plistlib.dumps({ "CFBundleIdentifier": "cn.airnan.kymo.checks." + uuid.uuid4().hex, "CFBundleExecutable": name, "CFBundlePackageType": "APPL", - "LSUIElement": name in {"PanelWindowChecks", "NativeMetricDrawingChecks", "NativeContentHostChecks", "NativeProcessTableChecks", "NetworkAddressChecks", "SandboxNetworkChecks"}, + "LSUIElement": name in {"PanelWindowChecks", "NativeMetricDrawingChecks", "NativeContentHostChecks", "NativeProcessTableChecks", "NetworkAddressChecks", "SandboxNetworkChecks", "TabPickerChecks"}, })) if resources: shutil.copytree(resources, contents / "Resources") @@ -174,7 +176,7 @@ def main(): run(["swiftc", "-parse-as-library", "-module-cache-path", work / "cache", *CHECKS[name], f"kymoTests/{name}.swift", "-o", executable]) if name in GUI: - bundle = test_bundle(work, name, executable, resources=app / "Contents/Resources" if name == "NativeProcessTableChecks" else None) + bundle = test_bundle(work, name, executable, resources=app / "Contents/Resources" if name in {"NativeProcessTableChecks", "TabPickerChecks"} else None) # Open the complete app for native activation semantics, while collecting its output. output = work / (name + ".out") error = work / (name + ".err")