Skip to content

Instantly share code, notes, and snippets.

@frzi
Created November 22, 2021 17:22
Show Gist options
  • Save frzi/7184986cb734d40466862389ca9bd350 to your computer and use it in GitHub Desktop.
Save frzi/7184986cb734d40466862389ca9bd350 to your computer and use it in GitHub Desktop.
Blur view for macOS
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