feat(power): 展示电池实际充放电功率并及时响应电源切换
保留电量百分比和连续趋势,按历史采样展示电池、适配器和系统功率。面板可见时读取电池实时功率,插拔电源后立即刷新并复读。
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import SwiftUI
|
||||
import AppKit
|
||||
import IOKit.ps
|
||||
import notify
|
||||
|
||||
/// AppKit owns launch only; MenuBar owns windows, Core owns sampling and preferences.
|
||||
@main @MainActor struct kymoApp {
|
||||
@@ -22,9 +24,14 @@ import AppKit
|
||||
private var openSettingsAction: (() -> Void)?
|
||||
private var scenes: AnyObject?
|
||||
private var menuBar: MenuPanelController?
|
||||
private var powerSourceToken: Int32?
|
||||
private var workspaceTokens: [NSObjectProtocol] = []
|
||||
|
||||
func applicationWillFinishLaunching(_ notification: Notification) {
|
||||
var token: Int32 = 0
|
||||
if notify_register_dispatch(kIOPSNotifyPowerSource, &token, .main, { [weak self] _ in
|
||||
MainActor.assumeIsolated { self?.metrics.powerSourceDidChange() }
|
||||
}) == NOTIFY_STATUS_OK { powerSourceToken = token }
|
||||
let center = NSWorkspace.shared.notificationCenter
|
||||
for (name, sleeping) in [(NSWorkspace.willSleepNotification, true), (NSWorkspace.didWakeNotification, false)] {
|
||||
workspaceTokens.append(center.addObserver(forName: name, object: nil, queue: .main) { [weak self] _ in
|
||||
@@ -107,6 +114,7 @@ import AppKit
|
||||
}
|
||||
|
||||
func applicationWillTerminate(_ notification: Notification) {
|
||||
if let powerSourceToken { notify_cancel(powerSourceToken) }
|
||||
workspaceTokens.forEach(NSWorkspace.shared.notificationCenter.removeObserver)
|
||||
workspaceTokens.removeAll()
|
||||
menuBar?.shutdown()
|
||||
|
||||
Reference in New Issue
Block a user