feat(menubar): 新增保持唤醒开关并统一底部透明按钮样式

This commit is contained in:
2026-10-30 16:10:51 +08:00
parent 7e9728899d
commit df0d338d72
7 changed files with 228 additions and 2 deletions
+2 -1
View File
@@ -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)
+38
View File
@@ -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) } }
}
+24 -1
View File
@@ -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)
}
}
+110
View File
@@ -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"