Created
March 30, 2021 00:12
-
-
Save bengottlieb/354f7a1f4f0b2931534d8720a07041a1 to your computer and use it in GitHub Desktop.
a minimal SwiftUI view that implements background blur on 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 | |
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