Phase 1: Notch-Shell, Design-System, Menüleisten-Infrastruktur
Zustandsmaschine und Geometrie sind testgetrieben entstanden und tragen 40 Tests — sie sind frei von AppKit, damit jeder Übergang ohne Fenster und ohne Warten prüfbar ist. Zeit kommt nur als Ereignis herein. Zwei Entscheidungen, die im Code begründet sind: Der Zeiger wird über einen globalen Ereignismonitor verfolgt, nicht über ein unsichtbares Fenster auf der Notch. Ein solches Fenster müsste Mausereignisse annehmen, um sie zu bemerken, und würde damit Menüleiste und Fensterknöpfe darunter unbenutzbar machen. "Nur internes Display" fällt auf ein externes zurück, wenn kein eingebautes da ist. Am Dock mit geschlossenem Deckel hieße die Einstellung wörtlich genommen, dass Onyx unerreichbar wird. Design: NSVisualEffectView mit eigenem Tint statt Liquid Glass — Onyx ist Stein, kein Glas. Alle Farben und Maße liegen als Tokens in OnyxDesign. Menüleiste: jedes Modul bekommt ein eigenes NSStatusItem. Elemente, denen macOS mangels Platz keine Breite gibt, werden erkannt und gemeldet, statt still zu verschwinden. App-Target über XcodeGen, damit die Projektdefinition im Diff lesbar bleibt. Nicht sandboxed, mit App-Group-Entitlement — baut, startet, signiert mit PP34X97WS3.
This commit is contained in:
138
Packages/OnyxKit/Sources/OnyxMenuBar/MenuBarController.swift
Normal file
138
Packages/OnyxKit/Sources/OnyxMenuBar/MenuBarController.swift
Normal file
@@ -0,0 +1,138 @@
|
||||
import AppKit
|
||||
import SwiftUI
|
||||
|
||||
/// Verwaltet beliebig viele Menüleisten-Elemente, je eines pro aktiviertem Modul.
|
||||
///
|
||||
/// Die Reihenfolge merkt sich macOS selbst über `autosaveName`; Onyx speichert sie
|
||||
/// nicht doppelt. Was Onyx speichert, ist welche Module an sind und wie sie
|
||||
/// aussehen sollen.
|
||||
@MainActor
|
||||
public final class MenuBarController {
|
||||
|
||||
/// Ein Modul, dessen Element keine Breite bekommen hat.
|
||||
///
|
||||
/// Die Menüleiste ist endlich, und auf einem Notch-Display ist die Mitte
|
||||
/// zusätzlich blockiert. Ein Element, das dort nicht mehr hinpasst, wird von
|
||||
/// macOS kommentarlos weggelassen — für den Nutzer sieht das aus, als wäre
|
||||
/// die Einstellung wirkungslos. Deshalb wird der Fall erkannt und gemeldet.
|
||||
public private(set) var hiddenModuleIDs: Set<String> = []
|
||||
|
||||
/// Wird gerufen, wenn sich `hiddenModuleIDs` ändert — die Einstellungen
|
||||
/// zeigen daraufhin einen Hinweis am betroffenen Modul.
|
||||
public var onVisibilityChanged: ((Set<String>) -> Void)?
|
||||
|
||||
private struct Entry {
|
||||
let module: any MenuBarModule
|
||||
let item: NSStatusItem
|
||||
let popover: NSPopover
|
||||
var settings: MenuBarModuleSettings
|
||||
}
|
||||
|
||||
private var entries: [String: Entry] = [:]
|
||||
private var visibilityTimer: Timer?
|
||||
|
||||
public init() {}
|
||||
|
||||
public func start() {
|
||||
// Ob ein Element sichtbar ist, meldet AppKit nicht. Es bleibt nur,
|
||||
// gelegentlich nachzusehen.
|
||||
visibilityTimer = Timer.scheduledTimer(withTimeInterval: 5, repeats: true) { _ in
|
||||
MainActor.assumeIsolated { [weak self] in self?.checkVisibility() }
|
||||
}
|
||||
}
|
||||
|
||||
public func stop() {
|
||||
visibilityTimer?.invalidate()
|
||||
visibilityTimer = nil
|
||||
for id in entries.keys { setEnabled(false, forModuleID: id) }
|
||||
}
|
||||
|
||||
// MARK: - Module
|
||||
|
||||
public func register(_ module: any MenuBarModule, settings: MenuBarModuleSettings) {
|
||||
guard entries[module.id] == nil else { return }
|
||||
|
||||
let item = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
|
||||
item.autosaveName = "onyx.menubar.\(module.id)"
|
||||
item.isVisible = false
|
||||
|
||||
let popover = NSPopover()
|
||||
popover.behavior = .transient
|
||||
popover.animates = true
|
||||
|
||||
entries[module.id] = Entry(module: module, item: item, popover: popover, settings: settings)
|
||||
apply(settings, toModuleID: module.id)
|
||||
}
|
||||
|
||||
public func setEnabled(_ enabled: Bool, forModuleID id: String) {
|
||||
guard var entry = entries[id] else { return }
|
||||
entry.settings.isEnabled = enabled
|
||||
entries[id] = entry
|
||||
apply(entry.settings, toModuleID: id)
|
||||
}
|
||||
|
||||
public func setPresentation(_ presentation: MenuBarPresentation, forModuleID id: String) {
|
||||
guard var entry = entries[id] else { return }
|
||||
entry.settings.presentation = presentation
|
||||
entries[id] = entry
|
||||
apply(entry.settings, toModuleID: id)
|
||||
}
|
||||
|
||||
public func settings(forModuleID id: String) -> MenuBarModuleSettings? {
|
||||
entries[id]?.settings
|
||||
}
|
||||
|
||||
private func apply(_ settings: MenuBarModuleSettings, toModuleID id: String) {
|
||||
guard let entry = entries[id] else { return }
|
||||
|
||||
entry.item.isVisible = settings.isEnabled
|
||||
|
||||
if settings.isEnabled {
|
||||
let view = entry.module.makeStatusView(presentation: settings.presentation)
|
||||
if let button = entry.item.button {
|
||||
button.subviews.forEach { $0.removeFromSuperview() }
|
||||
view.frame = button.bounds
|
||||
view.autoresizingMask = [.width, .height]
|
||||
button.addSubview(view)
|
||||
button.target = self
|
||||
button.action = #selector(statusItemClicked(_:))
|
||||
// Beide Tasten annehmen: rechts öffnet später das Kontextmenü.
|
||||
button.sendAction(on: [.leftMouseUp, .rightMouseUp])
|
||||
button.identifier = NSUserInterfaceItemIdentifier(id)
|
||||
}
|
||||
entry.item.length = view.fittingSize.width > 0 ? view.fittingSize.width : NSStatusItem.variableLength
|
||||
entry.module.activate()
|
||||
} else {
|
||||
entry.module.deactivate()
|
||||
}
|
||||
checkVisibility()
|
||||
}
|
||||
|
||||
// MARK: - Interaktion
|
||||
|
||||
@objc private func statusItemClicked(_ sender: NSStatusBarButton) {
|
||||
guard let id = sender.identifier?.rawValue, let entry = entries[id] else { return }
|
||||
|
||||
if entry.popover.isShown {
|
||||
entry.popover.performClose(nil)
|
||||
return
|
||||
}
|
||||
entry.popover.contentViewController = NSHostingController(
|
||||
rootView: entry.module.makePopoverView())
|
||||
entry.popover.show(relativeTo: sender.bounds, of: sender, preferredEdge: .minY)
|
||||
// Ohne das bleibt das Popover hinter dem aktiven Fenster.
|
||||
entry.popover.contentViewController?.view.window?.makeKey()
|
||||
}
|
||||
|
||||
private func checkVisibility() {
|
||||
var hidden = Set<String>()
|
||||
for (id, entry) in entries where entry.settings.isEnabled {
|
||||
// `isVisible` bleibt true, auch wenn das Element mangels Platz nicht
|
||||
// gezeichnet wird. Der verlässliche Hinweis ist ein Knopf ohne Fenster.
|
||||
if entry.item.button?.window == nil { hidden.insert(id) }
|
||||
}
|
||||
guard hidden != hiddenModuleIDs else { return }
|
||||
hiddenModuleIDs = hidden
|
||||
onVisibilityChanged?(hidden)
|
||||
}
|
||||
}
|
||||
64
Packages/OnyxKit/Sources/OnyxMenuBar/MenuBarModule.swift
Normal file
64
Packages/OnyxKit/Sources/OnyxMenuBar/MenuBarModule.swift
Normal file
@@ -0,0 +1,64 @@
|
||||
import AppKit
|
||||
import SwiftUI
|
||||
|
||||
/// Wie ein Modul seinen Messwert in der Menüleiste zeichnet.
|
||||
public enum MenuBarPresentation: String, Codable, Sendable, CaseIterable, Identifiable {
|
||||
/// Nur die Zahl, z. B. `34 %` oder `↓ 2,4 MB/s`.
|
||||
case value
|
||||
/// Schmales Verlaufsdiagramm über die letzten Messungen.
|
||||
case graph
|
||||
/// Ein Balken je Cluster bzw. je Richtung.
|
||||
case bars
|
||||
case valueAndGraph
|
||||
/// Nur ein Glyph, nach Grenzwert eingefärbt.
|
||||
case symbol
|
||||
|
||||
public var id: String { rawValue }
|
||||
}
|
||||
|
||||
/// Die Menüleisten-Entsprechung zu einem Widget.
|
||||
///
|
||||
/// Ein Modul liefert nur Darstellung. Die Daten kommen aus demselben Provider,
|
||||
/// den auch das Panel-Widget benutzt — es gibt keine zweite Messschleife.
|
||||
@MainActor
|
||||
public protocol MenuBarModule: AnyObject {
|
||||
var id: String { get }
|
||||
var displayName: String { get }
|
||||
|
||||
/// Zeichnet den kompakten Zustand. Muss eine feste Breite je Darstellungsart
|
||||
/// liefern, sonst springt die ganze Menüleiste bei jedem Messwert.
|
||||
func makeStatusView(presentation: MenuBarPresentation) -> NSView
|
||||
|
||||
/// Der Inhalt des Popovers beim Klick.
|
||||
func makePopoverView() -> AnyView
|
||||
|
||||
/// Wird gerufen, wenn das Modul sichtbar wird bzw. verschwindet — hier
|
||||
/// meldet sich das Modul beim Provider an und wieder ab.
|
||||
func activate()
|
||||
func deactivate()
|
||||
}
|
||||
|
||||
/// Was der Nutzer je Modul eingestellt hat.
|
||||
public struct MenuBarModuleSettings: Codable, Sendable, Equatable {
|
||||
public var isEnabled: Bool
|
||||
public var presentation: MenuBarPresentation
|
||||
/// Abtastintervall in Sekunden, 1–10.
|
||||
public var refreshInterval: TimeInterval
|
||||
public var usesThresholdColors: Bool
|
||||
|
||||
public init(isEnabled: Bool = false,
|
||||
presentation: MenuBarPresentation = .value,
|
||||
refreshInterval: TimeInterval = 2,
|
||||
usesThresholdColors: Bool = true) {
|
||||
self.isEnabled = isEnabled
|
||||
self.presentation = presentation
|
||||
self.refreshInterval = refreshInterval.clamped(to: 1...10)
|
||||
self.usesThresholdColors = usesThresholdColors
|
||||
}
|
||||
}
|
||||
|
||||
extension TimeInterval {
|
||||
func clamped(to range: ClosedRange<TimeInterval>) -> TimeInterval {
|
||||
Swift.min(Swift.max(self, range.lowerBound), range.upperBound)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user