Created
November 22, 2021 17:22
-
-
Save frzi/7184986cb734d40466862389ca9bd350 to your computer and use it in GitHub Desktop.
Blur view for macOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
#if os(macOS) | |
import AppKit | |
struct Blur: NSViewRepresentable { | |
fileprivate var blending: NSVisualEffectView.BlendingMode | |
fileprivate var style: NSVisualEffectView.Material | |
init( | |
blending: NSVisualEffectView.BlendingMode = .behindWindow, | |
style: NSVisualEffectView.Material = .fullScreenUI | |
) { | |
self.blending = blending | |
self.style = style | |
} | |
func makeNSView(context: Context) -> NSVisualEffectView { | |
let view = NSVisualEffectView() | |
view.blendingMode = blending | |
view.material = style | |
view.state = .followsWindowActiveState | |
return view | |
} | |
func updateNSView(_ nsView: NSVisualEffectView, context: Context) { | |
nsView.blendingMode = blending | |
nsView.material = style | |
} | |
} | |
extension Blur: KeyPathEditable { | |
func effectBlending(_ blending: NSVisualEffectView.BlendingMode) -> Self { | |
var copy = self | |
copy.blending = blending | |
return copy | |
} | |
func effectStyle(_ style: NSVisualEffectView.Material) -> Self { | |
var copy = self | |
copy.style = style | |
return copy | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment