Skip to content

Instantly share code, notes, and snippets.

@bengottlieb
Created March 30, 2021 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bengottlieb/354f7a1f4f0b2931534d8720a07041a1 to your computer and use it in GitHub Desktop.
Save bengottlieb/354f7a1f4f0b2931534d8720a07041a1 to your computer and use it in GitHub Desktop.
a minimal SwiftUI view that implements background blur on macOS
import SwiftUI
public struct VisualEffectBackground: NSViewRepresentable {
var material: NSVisualEffectView.Material = .contentBackground
var blendingMode: NSVisualEffectView.BlendingMode = .behindWindow
var isEmphasized = false
public init(material: NSVisualEffectView.Material = .contentBackground, blendingMode: NSVisualEffectView.BlendingMode = .behindWindow, emphasized: Bool = false) {
self.material = material
self.blendingMode = blendingMode
self.isEmphasized = emphasized
}
public func makeNSView(context: Context) -> NSVisualEffectView {
NSVisualEffectView()
}
public func updateNSView(_ nsView: NSVisualEffectView, context: Context) {
nsView.material = material
nsView.blendingMode = blendingMode
nsView.isEmphasized = isEmphasized
}
}
public extension View {
func visualEffectBackground(material: NSVisualEffectView.Material = .sidebar, blendingMode: NSVisualEffectView.BlendingMode = .behindWindow, emphasized: Bool = false) -> some View {
background(
VisualEffectBackground(material: material, blendingMode: blendingMode, emphasized: emphasized)
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment