diff --git a/docs/architecture.md b/docs/architecture.md index 83497df..870f23b 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -139,3 +139,13 @@ SMARTService/ 是独立 Target,不属于 kymo/ 的同步源码目录。共享 连接两侧设置代码签名要求:客户端由嵌入服务的 designated requirement 校验服务;服务由宿主应用的 designated requirement 校验调用方,且 uid 必须相同。无法取得要求时拒绝服务;支持本地 ad-hoc Debug 签名,不使用标识字符串白名单代替签名。客户端在回复/中断/错误/15 秒超时竞争时用锁保证 continuation 只恢复一次,失效后释放连接;服务内部查询十秒终止。无额外轮询 Timer。Xcode 构建依赖与 Embed XPC Services 阶段负责先构建、签名并嵌入服务。 此非沙盒服务只覆盖当前独立分发路径,不符合 Mac App Store 全组件沙盒要求,商店分发需另行设计。GPU 不接入本服务;当前 IOReport 订阅失败尚未证明与 SMART 同因。 + +### 保持唤醒(面板底部) + +App 持有进程级 `KeepAwakeService`(Core/Services,MainActor + Observable),通过入参交给 MenuPanelView。点击咖啡杯按钮同时创建 `PreventUserIdleDisplaySleep` 与 `PreventUserIdleSystemSleep` 两个 IOKit 电源断言,以视频播放类的系统机制阻止空闲息屏及睡眠。任一创建失败会释放已创建的断言,按钮不显示开启,提示可重试。再次点击释放两项,服务销毁或进程退出后系统也会释放。 + +状态不持久化,重启默认关闭;关闭面板、暂停资源采样均不影响已开启的保持唤醒,无额外定时器、辅助进程或权限变更。主动锁屏、主动睡眠、合盖、低电量保护与管理策略仍由系统决定;独立屏保/自动锁定策略的实际行为需在用户环境验收,不以电源断言创建成功替代锁屏实测。 + +面板底部四个按钮统一透明背景与圆角矩形细边框,咖啡杯开启时使用当前强调色,并提供三语帮助及无障碍开关状态。 + +2026-10-30 验证:Debug 构建通过;独立核心检查在普通进程与带主应用沙盒权限的临时 .app 中均通过,实际查询本进程断言,覆盖默认关闭、连续三次开关、开启后两类断言存在、关闭/服务销毁后消失。CLI 检查器直接加沙盒签名会在 libsecinit 初始化阶段退出,改用完整 .app 包后通过,未更改产品权限。当前没有 Xcode 测试 Target。UI 自动化 getApp 本轮构建超时;本轮仅退出自动化启动的测试副本,保留用户 Xcode 进程。底部实际视觉、关闭面板后长时间空闲不锁屏/息屏以及关闭开关后恢复系统策略,待用户验收。 diff --git a/kymo/App/kymoApp.swift b/kymo/App/kymoApp.swift index a5b73f9..b1ee653 100644 --- a/kymo/App/kymoApp.swift +++ b/kymo/App/kymoApp.swift @@ -14,13 +14,14 @@ import SwiftUI @main struct kymoApp: App { @State private var metrics = MetricsStore() + @State private var keepAwake = KeepAwakeService() @State private var appearance = AppearancePreferences() @State private var language = LanguagePreferences() var body: some Scene { MenuBarExtra { - MenuPanelView(metrics: metrics, preferences: appearance) + MenuPanelView(metrics: metrics, preferences: appearance, keepAwake: keepAwake) .environment(\.locale, language.locale) } label: { MenuBarLabel(snapshot: metrics.snapshot) diff --git a/kymo/Core/Services/KeepAwakeService.swift b/kymo/Core/Services/KeepAwakeService.swift new file mode 100644 index 0000000..0e881d7 --- /dev/null +++ b/kymo/Core/Services/KeepAwakeService.swift @@ -0,0 +1,38 @@ +import Foundation +import Observation +import IOKit.pwr_mgt + +/// Process-scoped power assertions; deliberately not persisted across launches. +@MainActor @Observable final class KeepAwakeService { + private(set) var isEnabled = false + private(set) var didFail = false + @ObservationIgnored private var assertions: [IOPMAssertionID] = [] + + func toggle() { + if isEnabled { disable(); return } + var acquired: [IOPMAssertionID] = [] + for type in [kIOPMAssertionTypePreventUserIdleDisplaySleep, kIOPMAssertionTypePreventUserIdleSystemSleep] { + var identifier: IOPMAssertionID = 0 + let result = IOPMAssertionCreateWithName(type as CFString, IOPMAssertionLevel(kIOPMAssertionLevelOn), + "Kymo Keep Awake" as CFString, &identifier) + guard result == kIOReturnSuccess else { + acquired.forEach { IOPMAssertionRelease($0) } + didFail = true + return + } + acquired.append(identifier) + } + assertions = acquired + didFail = false + isEnabled = true + } + + func disable() { + assertions.forEach { IOPMAssertionRelease($0) } + assertions.removeAll() + isEnabled = false + didFail = false + } + + deinit { assertions.forEach { IOPMAssertionRelease($0) } } +} diff --git a/kymo/Features/MenuBar/MenuPanelView.swift b/kymo/Features/MenuBar/MenuPanelView.swift index 0a5b8a0..da920fc 100644 --- a/kymo/Features/MenuBar/MenuPanelView.swift +++ b/kymo/Features/MenuBar/MenuPanelView.swift @@ -4,6 +4,7 @@ import AppKit struct MenuPanelView: View { let metrics: MetricsStore let preferences: AppearancePreferences + let keepAwake: KeepAwakeService @Environment(\.openSettings) private var openSettings var body: some View { @@ -195,6 +196,14 @@ struct MenuPanelView: View { .font(.system(size: 9)).foregroundStyle(.tertiary) } Spacer() + Button { keepAwake.toggle() } label: { + Image(systemName: keepAwake.isEnabled ? "cup.and.saucer.fill" : "cup.and.saucer") + .frame(width: 20, height: 20) + .foregroundStyle(keepAwake.isEnabled ? preferences.tintColor : Color.primary) + } + .help(keepAwake.didFail ? Text("awake.failed") : (keepAwake.isEnabled ? Text("awake.disable") : Text("awake.enable"))) + .accessibilityLabel(keepAwake.isEnabled ? Text("awake.disable") : Text("awake.enable")) + .accessibilityValue(keepAwake.isEnabled ? Text("awake.on") : Text("awake.off")) Button { metrics.togglePause() } label: { Image(systemName: metrics.isPaused ? "play.fill" : "pause.fill").frame(width: 20, height: 20) } @@ -209,8 +218,22 @@ struct MenuPanelView: View { } .help(Text("action.quit")).accessibilityLabel(Text("action.quit")) } - .buttonStyle(.glass) + .buttonStyle(FooterButtonStyle()) .controlSize(.small) .padding(.horizontal, 8).padding(.vertical, 6) } } + + +private struct FooterButtonStyle: ButtonStyle { + func makeBody(configuration: Configuration) -> some View { + configuration.label + .padding(.horizontal, 9).padding(.vertical, 6) + .contentShape(RoundedRectangle(cornerRadius: 6)) + .overlay { + RoundedRectangle(cornerRadius: 6) + .strokeBorder(.primary.opacity(configuration.isPressed ? 0.5 : 0.18), lineWidth: 1) + } + .opacity(configuration.isPressed ? 0.6 : 1) + } +} diff --git a/kymo/Resources/Localizable.xcstrings b/kymo/Resources/Localizable.xcstrings index 857fa96..e14ac2f 100644 --- a/kymo/Resources/Localizable.xcstrings +++ b/kymo/Resources/Localizable.xcstrings @@ -1649,6 +1649,116 @@ } } } + }, + "awake.enable": { + "localizations": { + "en": { + "stringUnit": { + "state": "translated", + "value": "Enable Keep Awake" + } + }, + "zh-Hans": { + "stringUnit": { + "state": "translated", + "value": "开启保持唤醒" + } + }, + "zh-Hant": { + "stringUnit": { + "state": "translated", + "value": "開啟保持喚醒" + } + } + } + }, + "awake.disable": { + "localizations": { + "en": { + "stringUnit": { + "state": "translated", + "value": "Disable Keep Awake" + } + }, + "zh-Hans": { + "stringUnit": { + "state": "translated", + "value": "关闭保持唤醒" + } + }, + "zh-Hant": { + "stringUnit": { + "state": "translated", + "value": "關閉保持喚醒" + } + } + } + }, + "awake.on": { + "localizations": { + "en": { + "stringUnit": { + "state": "translated", + "value": "On" + } + }, + "zh-Hans": { + "stringUnit": { + "state": "translated", + "value": "已开启" + } + }, + "zh-Hant": { + "stringUnit": { + "state": "translated", + "value": "已開啟" + } + } + } + }, + "awake.off": { + "localizations": { + "en": { + "stringUnit": { + "state": "translated", + "value": "Off" + } + }, + "zh-Hans": { + "stringUnit": { + "state": "translated", + "value": "已关闭" + } + }, + "zh-Hant": { + "stringUnit": { + "state": "translated", + "value": "已關閉" + } + } + } + }, + "awake.failed": { + "localizations": { + "en": { + "stringUnit": { + "state": "translated", + "value": "Unable to keep awake. Click to retry." + } + }, + "zh-Hans": { + "stringUnit": { + "state": "translated", + "value": "保持唤醒开启失败,点击重试" + } + }, + "zh-Hant": { + "stringUnit": { + "state": "translated", + "value": "保持喚醒開啟失敗,點擊重試" + } + } + } } }, "version": "1.0" diff --git a/kymoTests/KeepAwakeChecks.swift b/kymoTests/KeepAwakeChecks.swift new file mode 100644 index 0000000..5b620af --- /dev/null +++ b/kymoTests/KeepAwakeChecks.swift @@ -0,0 +1,33 @@ +import Foundation +import IOKit.pwr_mgt + +@main struct KeepAwakeChecks { + @MainActor static func main() { + let service = KeepAwakeService() + precondition(!service.isEnabled) + precondition(ownAssertions().isEmpty) + for _ in 0..<3 { + service.toggle() + precondition(service.isEnabled && !service.didFail) + let types = Set(ownAssertions().compactMap { $0[kIOPMAssertionTypeKey] as? String }) + precondition(types == Set([kIOPMAssertionTypePreventUserIdleDisplaySleep, kIOPMAssertionTypePreventUserIdleSystemSleep])) + service.toggle() + precondition(!service.isEnabled && !service.didFail) + precondition(ownAssertions().isEmpty) + } + var temporary: KeepAwakeService? = KeepAwakeService() + temporary?.toggle() + precondition(ownAssertions().count == 2) + temporary = nil + precondition(ownAssertions().isEmpty) + print("KeepAwakeChecks passed: real assertions, repeated toggle and lifetime release") + } + + static func ownAssertions() -> [[String: Any]] { + var value: Unmanaged? + precondition(IOPMCopyAssertionsByProcess(&value) == kIOReturnSuccess) + let processes = value!.takeRetainedValue() as NSDictionary + let assertions = processes[NSNumber(value: getpid())] as? [[String: Any]] ?? [] + return assertions.filter { $0[kIOPMAssertionNameKey] as? String == "Kymo Keep Awake" } + } +} diff --git a/kymoTests/README.md b/kymoTests/README.md index c1421f8..573720f 100644 --- a/kymoTests/README.md +++ b/kymoTests/README.md @@ -84,3 +84,14 @@ swiftc -module-cache-path /private/tmp/kymo-identity-cache kymo/Core/Services/Co ``` 验证各自合法签名通过、对方身份要求拒绝及缺失代码降级;不替代主应用与 XPC 的实际端到端采样。方案与进程生命周期见 [辅助服务实施方案](../docs/disk-smart-helper-plan.md)。 + +## 保持唤醒实际断言检查 + +```bash +swiftc -module-cache-path /private/tmp/kymo-check-cache \ + kymo/Core/Services/KeepAwakeService.swift kymoTests/KeepAwakeChecks.swift \ + -o /private/tmp/kymo-awake-checks +/private/tmp/kymo-awake-checks +``` + +检查真实 IOKit 请求的创建、重复开关与生命周期释放;测试结束不保留唤醒断言。不能替代实际空闲锁屏、息屏及面板视觉验收。