perf(efficiency): 接入真实CPU能量并隔离展示与采样需求
This commit is contained in:
@@ -3,6 +3,7 @@ import SwiftUI
|
||||
import Observation
|
||||
|
||||
@MainActor @Observable final class PanelPresentation {
|
||||
fileprivate(set) var isOpen = false
|
||||
var isPinned = false { didSet { onPinChange?(isPinned) } }
|
||||
@ObservationIgnored var onPinChange: ((Bool) -> Void)?
|
||||
}
|
||||
@@ -11,10 +12,14 @@ import Observation
|
||||
@MainActor final class MenuPanelController: NSObject {
|
||||
let presentation = PanelPresentation()
|
||||
let panel = MonitoringPanel(contentRect: .zero, styleMask: [.borderless, .nonactivatingPanel], backing: .buffered, defer: false)
|
||||
#if DEBUG || KYMO_AUDIT
|
||||
private(set) weak var lastHiddenContent: NSView?
|
||||
#endif
|
||||
private var statusItem: NSStatusItem?
|
||||
private var localMouse: Any?
|
||||
private var globalMouse: Any?
|
||||
private var tokens: [NSObjectProtocol] = []
|
||||
private var visibilityObservation: NSKeyValueObservation?
|
||||
private let content: (PanelPresentation) -> NSView
|
||||
private let visibility: (Bool) -> Void
|
||||
private let menuTitle: (String) -> String
|
||||
@@ -52,6 +57,12 @@ import Observation
|
||||
tokens.append(NotificationCenter.default.addObserver(forName: NSApplication.didChangeScreenParametersNotification, object: nil, queue: .main) { [weak self] _ in
|
||||
MainActor.assumeIsolated { self?.place() }
|
||||
})
|
||||
visibilityObservation = panel.observe(\.isVisible, options: [.new]) { [weak self] _, _ in
|
||||
Task { @MainActor [weak self] in self?.publishVisibility() }
|
||||
}
|
||||
tokens.append(NotificationCenter.default.addObserver(forName: NSWindow.didChangeOcclusionStateNotification, object: panel, queue: .main) { [weak self] _ in
|
||||
MainActor.assumeIsolated { self?.publishVisibility() }
|
||||
})
|
||||
tokens.append(NotificationCenter.default.addObserver(forName: NSApplication.didResignActiveNotification, object: NSApp, queue: .main) { [weak self] _ in
|
||||
MainActor.assumeIsolated {
|
||||
guard let self, !self.showingStatusMenu, !self.isOverStatusItem(NSEvent.mouseLocation) else { return }
|
||||
@@ -98,6 +109,7 @@ import Observation
|
||||
func show() {
|
||||
guard !panel.isVisible else { panel.makeKeyAndOrderFront(nil); return }
|
||||
place()
|
||||
presentation.isOpen = true
|
||||
panel.contentView = content(presentation)
|
||||
panel.level = presentation.isPinned ? .floating : .normal
|
||||
panel.makeKeyAndOrderFront(nil)
|
||||
@@ -105,12 +117,20 @@ import Observation
|
||||
// Opening the panel must not start editing the first text field.
|
||||
// Explicit clicks and keyboard navigation can still focus its controls.
|
||||
panel.makeFirstResponder(panel)
|
||||
visibility(true)
|
||||
publishVisibility()
|
||||
installMonitors()
|
||||
}
|
||||
|
||||
private func publishVisibility() {
|
||||
visibility(panel.isVisible && panel.occlusionState.contains(.visible))
|
||||
}
|
||||
|
||||
func hide() {
|
||||
presentation.isOpen = false
|
||||
panel.orderOut(nil)
|
||||
#if DEBUG || KYMO_AUDIT
|
||||
if let content = panel.contentView { lastHiddenContent = content }
|
||||
#endif
|
||||
panel.contentView = nil
|
||||
visibility(false)
|
||||
removeMonitors()
|
||||
@@ -127,6 +147,7 @@ import Observation
|
||||
statusItem = nil
|
||||
tokens.forEach(NotificationCenter.default.removeObserver)
|
||||
tokens.removeAll()
|
||||
visibilityObservation = nil
|
||||
}
|
||||
|
||||
private func place() {
|
||||
|
||||
Reference in New Issue
Block a user