Skip to content

Commit

Permalink
Create View+Blur.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
eonist committed Nov 23, 2024
1 parent 7b6ee48 commit 7e2ea1a
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions Sources/BlurView/ext/View+Blur.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import SwiftUI
/**
* Translucent bg ext
* - Description: This extension provides a method to add a translucent
* underlay to any SwiftUI view. It is primarily used to create a
* blurred background effect for the lock screen view during user
* authentication.
* - Remark: Cover mainview while authenticating user
* - Note: Ref window code: https://www.bignerdranch.com/blog/intro-to-nswindow/
* - Note: Ref vibrancy: https://github.com/lukakerr/NSWindowStyles (awesome overview of window styles)
* - Note: More window stuff: https://www.albertopasca.it/whiletrue/swift-macos-how-to-make-cool-desktop-apps-using-cocoa/
* - Fixme: ⚠️️ attach this to some general shape protocol instead of view?
*/
extension View {
/**
* Adds translucent underlay (iOS and macOS)
* - Description: This function adds a translucent underlay to the view it is
* applied to. It is primarily used to create a blurred
* background effect for the lock screen view during user
* authentication. The function adjusts its behavior based on
* the operating system, applying different visual effects for
* iOS and macOS.
* - Note: How to add blur view: https://github.com/sentryco/Sentry/issues/918
* - Fixme: ⚠️️⚠️️ Try the legacy effectview as well, it might have nicer saturated black translucent etc
* - Fixme: ⚠️️ Add full example for this in readme
* - Fixme: ⚠️️ Add more customization in the method params
* ## Examples:
* let color = Color(light: Color.white.opacity(0.1), dark: Color.black.opacity(0.6))
* let rectangle = Rectangle().fill(.clear)
* rectangle.translucentUnderlay(overlayColor: color)
*/
public func translucentUnderlay(overlayColor: Color) -> some View {
self
// .fill() // ⚠️️ was using: .fill(Color.black.opacity(0.6))
.overlay(overlayColor) // mixin dark transperancy etc
#if os(iOS)
.background(.ultraThinMaterial).opacity(1.0) // ultraThickMaterial, ultraThinMaterial, thinMaterial, thickMaterial, regularMaterial
#elseif os(macOS)
.visualEffect( // Adds visual effect to the view
material: .hudWindow, // Material type for the visual effect
blendingMode: .withinWindow, // Blending mode for the visual effect
emphasized: false // Whether the effect is emphasized
)
#endif
.frame( // Set frame to fill maximum width and height of the parent view
maxWidth: .infinity, // Set the maximum width to fill the available space
maxHeight: .infinity // Set the maximum height to fill the available space
)
.ignoresSafeArea() // Ignore safe area insets for this view
}
}

0 comments on commit 7e2ea1a

Please sign in to comment.