fix: keep anti-ghosting window refresh while restoring original SwiftUI layout

This commit is contained in:
JOJO 2026-05-07 15:15:41 +08:00
parent c4cc9c7db5
commit dfc6e93d5a
2 changed files with 31 additions and 1 deletions

View File

@ -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

View File

@ -46,6 +46,9 @@ struct ContentView: View {
monitor.interval = interval monitor.interval = interval
} }
} }
.onReceive(monitor.$info) { _ in
windowController?.refreshAfterDataUpdate()
}
} }
// MARK: - // MARK: -