fix: keep anti-ghosting window refresh while restoring original SwiftUI layout
This commit is contained in:
parent
c4cc9c7db5
commit
dfc6e93d5a
@ -5,6 +5,7 @@ import SwiftUI
|
|||||||
final class FloatingWindowController: NSWindowController {
|
final class FloatingWindowController: NSWindowController {
|
||||||
private var hostingView: NSHostingView<ContentView>?
|
private var hostingView: NSHostingView<ContentView>?
|
||||||
private var visualEffect: NSVisualEffectView!
|
private var visualEffect: NSVisualEffectView!
|
||||||
|
private let tinyAlphaBackground = NSColor.white.withAlphaComponent(0.00001)
|
||||||
|
|
||||||
private let debugLogPath = NSHomeDirectory() + "/Desktop/fm_debug.log"
|
private let debugLogPath = NSHomeDirectory() + "/Desktop/fm_debug.log"
|
||||||
|
|
||||||
@ -132,20 +133,45 @@ final class FloatingWindowController: NSWindowController {
|
|||||||
|
|
||||||
private func applyBackdropEnabled(_ enabled: Bool) {
|
private func applyBackdropEnabled(_ enabled: Bool) {
|
||||||
guard let ve = visualEffect else { return }
|
guard let ve = visualEffect else { return }
|
||||||
|
guard let window = window else { return }
|
||||||
if enabled {
|
if enabled {
|
||||||
|
ve.isHidden = false
|
||||||
|
ve.state = .active
|
||||||
ve.blendingMode = .behindWindow
|
ve.blendingMode = .behindWindow
|
||||||
ve.alphaValue = 1.0
|
ve.alphaValue = 1.0
|
||||||
ve.layer?.backgroundColor = NSColor.black.withAlphaComponent(0.35).cgColor
|
ve.layer?.backgroundColor = NSColor.black.withAlphaComponent(0.35).cgColor
|
||||||
|
window.hasShadow = true
|
||||||
|
window.backgroundColor = .clear
|
||||||
} else {
|
} else {
|
||||||
|
ve.state = .inactive
|
||||||
ve.blendingMode = .withinWindow
|
ve.blendingMode = .withinWindow
|
||||||
ve.alphaValue = 0.0
|
ve.alphaValue = 1.0
|
||||||
ve.layer?.backgroundColor = NSColor.clear.cgColor
|
ve.layer?.backgroundColor = NSColor.clear.cgColor
|
||||||
|
ve.isHidden = true
|
||||||
|
window.hasShadow = false
|
||||||
|
// 纯 clear 在部分系统上会产生窗口合成残影;使用极小 alpha 避免 Ghosting。
|
||||||
|
window.backgroundColor = tinyAlphaBackground
|
||||||
}
|
}
|
||||||
|
window.contentView?.layerContentsRedrawPolicy = .onSetNeedsDisplay
|
||||||
|
window.contentView?.needsDisplay = true
|
||||||
|
window.contentView?.displayIfNeeded()
|
||||||
|
hostingView?.layerContentsRedrawPolicy = .onSetNeedsDisplay
|
||||||
|
hostingView?.needsDisplay = true
|
||||||
|
hostingView?.displayIfNeeded()
|
||||||
|
window.invalidateShadow()
|
||||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { [weak self] in
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { [weak self] in
|
||||||
self?.dumpGeometry("backdropEnabled=\(enabled)")
|
self?.dumpGeometry("backdropEnabled=\(enabled)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func refreshAfterDataUpdate() {
|
||||||
|
guard let window = window else { return }
|
||||||
|
window.contentView?.needsDisplay = true
|
||||||
|
hostingView?.needsDisplay = true
|
||||||
|
window.displayIfNeeded()
|
||||||
|
window.invalidateShadow()
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: - SwiftUI 嵌入
|
// MARK: - SwiftUI 嵌入
|
||||||
|
|
||||||
func embed(rootView: ContentView) {
|
func embed(rootView: ContentView) {
|
||||||
@ -153,6 +179,7 @@ final class FloatingWindowController: NSWindowController {
|
|||||||
|
|
||||||
let hv = NSHostingView(rootView: rootView)
|
let hv = NSHostingView(rootView: rootView)
|
||||||
hv.wantsLayer = true
|
hv.wantsLayer = true
|
||||||
|
hv.layerContentsRedrawPolicy = .onSetNeedsDisplay
|
||||||
hv.layer?.cornerRadius = Configuration.shared.backdropEnabled ? 16 : 0
|
hv.layer?.cornerRadius = Configuration.shared.backdropEnabled ? 16 : 0
|
||||||
hv.layer?.masksToBounds = true
|
hv.layer?.masksToBounds = true
|
||||||
hv.translatesAutoresizingMaskIntoConstraints = false
|
hv.translatesAutoresizingMaskIntoConstraints = false
|
||||||
|
|||||||
@ -46,6 +46,9 @@ struct ContentView: View {
|
|||||||
monitor.interval = interval
|
monitor.interval = interval
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
.onReceive(monitor.$info) { _ in
|
||||||
|
windowController?.refreshAfterDataUpdate()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - 顶部栏(精简版)
|
// MARK: - 顶部栏(精简版)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user