refactor(project): 清理闲置展示链路与重复采样状态

This commit is contained in:
2026-11-09 00:41:09 +08:00
parent 7b3abe37c5
commit b73d51c953
57 changed files with 210 additions and 2822 deletions
View File
-18
View File
@@ -1,18 +0,0 @@
import Foundation
import CoreGraphics
/// Screen coordinates: prefer the parent's right side, otherwise use its left.
nonisolated enum DetailPlacement {
static func frame(anchor: CGRect, parent: CGRect, content: CGSize, visible: CGRect) -> CGRect {
let gap: CGFloat = 4
let width = min(content.width, visible.width)
let height = min(content.height, visible.height)
let right = parent.maxX + gap
let left = parent.minX - gap - width
let candidate = right + width <= visible.maxX ? right : left
let x = max(visible.minX, min(candidate, visible.maxX - width))
let y = max(visible.minY, min(anchor.maxY - height, visible.maxY - height))
return CGRect(x: x, y: y, width: width, height: height)
}
}
@@ -1,113 +0,0 @@
import SwiftUI
import AppKit
/// A non-activating child window: outside the parent layout, below no other app.
struct HoverDetailPresenter<Detail: View>: NSViewRepresentable {
@Environment(\.locale) private var locale
let isPresented: Bool
let detail: Detail
let onHover: (Bool) -> Void
let onDismiss: () -> Void
func makeNSView(context: Context) -> AnchorView { AnchorView() }
func updateNSView(_ view: AnchorView, context: Context) {
view.configure(presented: isPresented, detail: AnyView(detail.environment(\.locale, locale)), onHover: onHover, onDismiss: onDismiss)
}
static func dismantleNSView(_ view: AnchorView, coordinator: ()) { view.detach() }
final class AnchorView: NSView {
override func hitTest(_ point: NSPoint) -> NSView? { nil }
private var panel: DetailPanel?
private var host: NSHostingView<AnyView>?
private var tokens: [NSObjectProtocol] = []
private var presented = false
private var detail = AnyView(EmptyView())
private var onHover: (Bool) -> Void = { _ in }
private var onDismiss: () -> Void = {}
func configure(presented: Bool, detail: AnyView,
onHover: @escaping (Bool) -> Void, onDismiss: @escaping () -> Void) {
self.presented = presented
self.detail = detail
self.onHover = onHover
self.onDismiss = onDismiss
refresh()
}
override func viewDidMoveToWindow() {
super.viewDidMoveToWindow()
detach()
guard let window else { return }
for name in [NSWindow.willCloseNotification, NSWindow.didChangeOcclusionStateNotification] {
tokens.append(NotificationCenter.default.addObserver(forName: name, object: window, queue: .main) { [weak self, weak window] notification in
MainActor.assumeIsolated {
guard notification.name == NSWindow.willCloseNotification || window?.occlusionState.contains(.visible) != true else { return }
self?.hide()
let dismiss = self?.onDismiss
DispatchQueue.main.async { dismiss?() }
}
})
}
refresh()
}
func detach() {
hide()
for token in tokens { NotificationCenter.default.removeObserver(token) }
tokens.removeAll()
}
private func refresh() {
guard presented, let parent = window, parent.isVisible, let screen = parent.screen else { hide(); return }
let content = AnyView(detail
.font(.caption)
.padding(12)
.frame(width: 280)
.fixedSize(horizontal: false, vertical: true)
.background(Color("PanelCard"), in: .rect(cornerRadius: 12))
.overlay(RoundedRectangle(cornerRadius: 12).strokeBorder(.primary.opacity(0.14)))
.onHover { [weak self] inside in self?.onHover(inside) })
if panel == nil {
let panel = DetailPanel(contentRect: .zero, styleMask: [.borderless, .nonactivatingPanel], backing: .buffered, defer: false)
panel.isReleasedWhenClosed = false
panel.isOpaque = false
panel.backgroundColor = .clear
panel.hasShadow = false
panel.animationBehavior = .none
panel.hidesOnDeactivate = false
panel.level = parent.level
let host = NSHostingView(rootView: content)
panel.contentView = host
self.panel = panel
self.host = host
parent.addChildWindow(panel, ordered: .above)
}
guard let panel, let host else { return }
host.rootView = content
panel.appearance = parent.appearance
let row = parent.convertToScreen(convert(bounds, to: nil))
let size = host.fittingSize
panel.setFrame(DetailPlacement.frame(anchor: row, parent: parent.frame,
content: CGSize(width: 280, height: max(40, size.height)),
visible: screen.visibleFrame), display: true)
panel.orderFront(nil)
}
private func hide() {
guard let panel else { return }
panel.parent?.removeChildWindow(panel)
panel.orderOut(nil)
panel.contentView = nil
panel.close()
host = nil
self.panel = nil
}
deinit { for token in tokens { NotificationCenter.default.removeObserver(token) } }
}
final class DetailPanel: NSPanel {
override var canBecomeKey: Bool { false }
override var canBecomeMain: Bool { false }
}
}
-26
View File
@@ -1,26 +0,0 @@
import SwiftUI
/// A bounded percentage gauge; missing data keeps an empty track and an explicit -- label.
struct MetricRingGauge: View {
let title: LocalizedStringKey
let value: Double?
let color: Color
private var fraction: Double { value.flatMap { $0.isFinite ? min(1, max(0, $0)) : nil } ?? 0 }
var body: some View {
VStack(spacing: 3) {
ZStack {
NativeMetricRing(value: fraction, color: color, lineWidth: 3, trackOpacity: 0.17, inset: 2)
MetricText(value: MetricFormat.percent(value)).font(.system(size: 8, weight: .semibold))
.lineLimit(1).minimumScaleFactor(0.6).frame(width: 18, height: 12)
}.frame(width: 28, height: 28).accessibilityHidden(true)
Text(title).font(.system(size: 8)).foregroundStyle(.secondary)
.lineLimit(1).minimumScaleFactor(0.7)
}
.frame(maxWidth: .infinity)
.accessibilityElement(children: .ignore)
.accessibilityLabel(Text(title))
.accessibilityValue(Text(verbatim: MetricFormat.percent(value)))
}
}
-59
View File
@@ -1,59 +0,0 @@
import SwiftUI
struct MetricRow<Detail: View>: View {
let title: LocalizedStringKey
let symbol: String
let value: String
let usage: Double?
let history: [Double?]
let accent: Color
@ViewBuilder let detail: () -> Detail
@State private var visible = false
@State private var rowHovered = false
@State private var detailHovered = false
@State private var expanded = false
private var inside: Bool { rowHovered || detailHovered }
private var color: Color {
guard let usage else { return .secondary }
return usage > 0.95 ? .red : usage >= 0.8 ? .orange : accent
}
var body: some View {
Button { expanded.toggle() } label: {
HStack(spacing: 10) {
Image(systemName: symbol)
.font(.system(size: 13, weight: .medium))
.foregroundStyle(color)
.frame(width: 20)
Text(title).font(.system(size: 13, weight: .medium))
.frame(width: 52, alignment: .leading)
MiniTrendChart(values: history, color: color).frame(height: 24)
Text(verbatim: value)
.font(.system(size: 13, weight: .semibold, design: .rounded))
.monospacedDigit()
.frame(width: 46, alignment: .trailing)
}
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.accessibilityLabel(Text(title))
.accessibilityValue(Text(verbatim: value))
.accessibilityHint(expanded ? Text("detail.expanded") : Text("detail.collapsed"))
.padding(10)
.background(expanded ? Color.primary.opacity(0.035) : .clear, in: .rect(cornerRadius: 14))
.background(HoverDetailPresenter(isPresented: expanded && visible, detail: detail(),
onHover: { detailHovered = $0 },
onDismiss: { expanded = false; detailHovered = false }))
.onHover { rowHovered = $0 }
.task(id: inside) {
do { try await Task.sleep(for: .milliseconds(inside ? 150 : 120)) }
catch { return }
guard !Task.isCancelled else { return }
expanded = inside
}
.onAppear { visible = true }
.onDisappear { visible = false; rowHovered = false; detailHovered = false; expanded = false }
.transaction { $0.animation = nil; $0.disablesAnimations = true }
}
}
-91
View File
@@ -1,91 +0,0 @@
import AppKit
import SwiftUI
/// Static arc geometry; sampling publishes the final value directly.
struct NativeMetricRing: NSViewRepresentable {
let value: Double
let color: Color
let lineWidth: CGFloat
let trackOpacity: Double
let inset: CGFloat
func sizeThatFits(_ proposal: ProposedViewSize, nsView: RingView, context: Context) -> CGSize? {
CGSize(width: proposal.width.flatMap { $0.isFinite ? $0 : nil } ?? 10,
height: proposal.height.flatMap { $0.isFinite ? $0 : nil } ?? 10)
}
func makeNSView(context: Context) -> RingView { RingView() }
func updateNSView(_ view: RingView, context: Context) {
view.configure(Input(value: value.isFinite ? min(1, max(0, value)) : 0, color: color,
lineWidth: lineWidth, trackOpacity: trackOpacity, inset: inset))
}
struct Input: Equatable {
let value: Double
let color: Color
let lineWidth: CGFloat
let trackOpacity: Double
let inset: CGFloat
}
final class RingView: NSView {
private(set) var input: Input?
let track = CAShapeLayer()
let arc = CAShapeLayer()
override var isFlipped: Bool { true }
override func hitTest(_ point: NSPoint) -> NSView? { nil }
init() {
super.init(frame: .zero)
wantsLayer = true
for shape in [track, arc] {
shape.fillColor = nil; shape.lineCap = .round
layer?.addSublayer(shape)
}
setAccessibilityElement(false)
}
required init?(coder: NSCoder) { nil }
func configure(_ value: Input) {
guard value != input else { return }
let old = input
input = value
CATransaction.begin(); CATransaction.setDisableActions(true)
if old?.color != value.color || old?.trackOpacity != value.trackOpacity { updateColors() }
if old?.inset != value.inset || old?.lineWidth != value.lineWidth { updateGeometry() }
arc.strokeEnd = value.value
CATransaction.commit()
}
private func updateColors() {
guard let input else { return }
effectiveAppearance.performAsCurrentDrawingAppearance {
let color = NSColor(input.color)
track.strokeColor = color.withAlphaComponent(input.trackOpacity).cgColor
arc.strokeColor = color.cgColor
}
}
private func updateGeometry() {
guard let input else { return }
let radius = max(0, min(bounds.width, bounds.height) / 2 - input.inset)
let path = CGMutablePath()
path.addArc(center: CGPoint(x: bounds.midX, y: bounds.midY), radius: radius,
startAngle: -.pi / 2, endAngle: 3 * .pi / 2, clockwise: false)
for shape in [track, arc] {
shape.frame = bounds; shape.path = path; shape.lineWidth = input.lineWidth
}
}
override func setFrameSize(_ size: NSSize) {
let changed = frame.size != size
super.setFrameSize(size)
if changed {
CATransaction.begin(); CATransaction.setDisableActions(true)
updateGeometry(); CATransaction.commit()
}
}
override func viewDidMoveToWindow() {
super.viewDidMoveToWindow()
CATransaction.begin(); CATransaction.setDisableActions(true)
updateColors(); CATransaction.commit()
}
override func viewDidChangeEffectiveAppearance() {
super.viewDidChangeEffectiveAppearance()
CATransaction.begin(); CATransaction.setDisableActions(true)
updateColors(); CATransaction.commit()
}
}
}
-17
View File
@@ -3,7 +3,6 @@ import SwiftUI
/// Shared roles for the compact monitoring panel. Micro charts keep their own 8pt role.
enum PanelStyle {
static let title = Font.system(size: 13, weight: .semibold)
static let section = Font.system(size: 11, weight: .semibold)
static let label = Font.system(size: 10)
static let value = Font.system(size: 10, weight: .medium)
static let legend = Font.system(size: 9)
@@ -88,19 +87,3 @@ struct CapacityField: View {
.lineLimit(1)
}
}
struct CardSectionHeader: View {
let title: LocalizedStringKey
let valueLabel: LocalizedStringKey
let value: String
let color: Color
var body: some View {
HStack {
Text(title).font(PanelStyle.section)
Spacer(minLength: 4)
Text(valueLabel).font(PanelStyle.label).foregroundStyle(.secondary)
MetricText(value: value).font(PanelStyle.value).foregroundStyle(color)
}.frame(height: 16)
}
}