Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions Sources/FluentUI_common/Core/Extensions/View+Modifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,28 @@ public extension View {
func applyFluentShadow(shadowInfo: ShadowInfo) -> some View {
modifier(ShadowModifier(shadowInfo: shadowInfo))
}

/// Backwards compatible wrapper of the iOS26+ .glassEffect modifier
/// - On iOS 26 / macOS 26 and newer: applies `glassEffect()`.
/// - On older OSes and unsupported platforms: applies a background with `.regularMaterial` and `shadow08`, with the exception of older macOS versions where no background is applied
/// - Parameters:
/// - interactive: Whether the glass effect should be interactive.
/// - tint: The tint color to apply to the glass effect.
/// - shape: The shape to apply the glass effect to.
/// - Returns: The modified view with the glass effect applied.
@ViewBuilder func fluentGlassEffect<T>(
interactive: Bool = false,
tint: Color? = nil,
in shape: T = Capsule()
) -> some View where T: Shape {
self.modifier(
FluentGlassEffectModifier(
interactive: interactive,
tint: tint,
shape: shape
)
)
}
}

/// ViewModifier that applies both shadows from a ShadowInfo
Expand All @@ -36,3 +58,34 @@ private struct ShadowModifier: ViewModifier {
}
}

private struct FluentGlassEffectModifier<T: Shape>: ViewModifier {
@Environment(\.fluentTheme) private var fluentTheme: FluentTheme

let interactive: Bool
let tint: Color?
let shape: T

func body(content: Content) -> some View {
#if os(visionOS) || compiler(<6.2)
content
.background(.regularMaterial, in: shape)
.applyFluentShadow(shadowInfo: fluentTheme.shadow(.shadow08))
#elseif os(macOS)
if #available(macOS 26, *) {
content
.glassEffect(.regular.interactive(interactive).tint(tint), in: shape)
} else {
content // don't add any background
}
#else
if #available(iOS 26, *) {
content
.glassEffect(.regular.interactive(interactive).tint(tint), in: shape)
} else {
content
.background(.regularMaterial, in: shape)
.applyFluentShadow(shadowInfo: fluentTheme.shadow(.shadow08))
}
#endif
}
}
Loading