Skip to content

Commit 6aca9d8

Browse files
authored
Add fluentGlassEffect modifier (#2266)
1 parent 81c39c8 commit 6aca9d8

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

Sources/FluentUI_common/Core/Extensions/View+Modifiers.swift

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,28 @@ public extension View {
1313
func applyFluentShadow(shadowInfo: ShadowInfo) -> some View {
1414
modifier(ShadowModifier(shadowInfo: shadowInfo))
1515
}
16+
17+
/// Backwards compatible wrapper of the iOS26+ .glassEffect modifier
18+
/// - On iOS 26 / macOS 26 and newer: applies `glassEffect()`.
19+
/// - 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
20+
/// - Parameters:
21+
/// - interactive: Whether the glass effect should be interactive.
22+
/// - tint: The tint color to apply to the glass effect.
23+
/// - shape: The shape to apply the glass effect to.
24+
/// - Returns: The modified view with the glass effect applied.
25+
@ViewBuilder func fluentGlassEffect<T>(
26+
interactive: Bool = false,
27+
tint: Color? = nil,
28+
in shape: T = Capsule()
29+
) -> some View where T: Shape {
30+
self.modifier(
31+
FluentGlassEffectModifier(
32+
interactive: interactive,
33+
tint: tint,
34+
shape: shape
35+
)
36+
)
37+
}
1638
}
1739

1840
/// ViewModifier that applies both shadows from a ShadowInfo
@@ -36,3 +58,34 @@ private struct ShadowModifier: ViewModifier {
3658
}
3759
}
3860

61+
private struct FluentGlassEffectModifier<T: Shape>: ViewModifier {
62+
@Environment(\.fluentTheme) private var fluentTheme: FluentTheme
63+
64+
let interactive: Bool
65+
let tint: Color?
66+
let shape: T
67+
68+
func body(content: Content) -> some View {
69+
#if os(visionOS) || compiler(<6.2)
70+
content
71+
.background(.regularMaterial, in: shape)
72+
.applyFluentShadow(shadowInfo: fluentTheme.shadow(.shadow08))
73+
#elseif os(macOS)
74+
if #available(macOS 26, *) {
75+
content
76+
.glassEffect(.regular.interactive(interactive).tint(tint), in: shape)
77+
} else {
78+
content // don't add any background
79+
}
80+
#else
81+
if #available(iOS 26, *) {
82+
content
83+
.glassEffect(.regular.interactive(interactive).tint(tint), in: shape)
84+
} else {
85+
content
86+
.background(.regularMaterial, in: shape)
87+
.applyFluentShadow(shadowInfo: fluentTheme.shadow(.shadow08))
88+
}
89+
#endif
90+
}
91+
}

0 commit comments

Comments
 (0)