feat(menubar): 实现趋势面板外侧详情与三语外观设置
接入有界趋势、平滑滚动、菜单式外侧详情、底部操作与三语文案,装配独立外观设置和 Dock 生命周期。 合并跟随系统外观刷新、自选颜色入口与色块绘制修复,保留用户确认的配色联动、重启保存及滚动验收。 多场景视觉与长期资源验证边界仍记录于 docs。
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
import SwiftUI
|
||||
import AppKit
|
||||
|
||||
/// A non-activating child window: outside the parent layout, below no other app.
|
||||
struct HoverDetailPresenter<Detail: View>: NSViewRepresentable {
|
||||
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), 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)
|
||||
.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 = true
|
||||
panel.hidesOnDeactivate = false
|
||||
panel.level = parent.level
|
||||
let material = NSVisualEffectView()
|
||||
material.material = .menu
|
||||
material.blendingMode = .behindWindow
|
||||
material.state = .active
|
||||
material.wantsLayer = true
|
||||
material.layer?.cornerRadius = 12
|
||||
material.layer?.masksToBounds = true
|
||||
let host = NSHostingView(rootView: content)
|
||||
host.translatesAutoresizingMaskIntoConstraints = false
|
||||
material.addSubview(host)
|
||||
NSLayoutConstraint.activate([
|
||||
host.leadingAnchor.constraint(equalTo: material.leadingAnchor),
|
||||
host.trailingAnchor.constraint(equalTo: material.trailingAnchor),
|
||||
host.topAnchor.constraint(equalTo: material.topAnchor),
|
||||
host.bottomAnchor.constraint(equalTo: material.bottomAnchor)
|
||||
])
|
||||
panel.contentView = material
|
||||
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 }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user