Skip to content

Commit a012c61

Browse files
committed
Fix crashing on older Xcode
1 parent 438790b commit a012c61

File tree

4 files changed

+43
-22
lines changed

4 files changed

+43
-22
lines changed

Demo/Demo/Samples/Essential/Basic.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct Basic: View {
4646
Text("HOME")
4747
.font(.largeTitle)
4848
.fontWeight(.heavy)
49-
.foregroundStyle(.primary)
49+
.foregroundStyle(.white)
5050
}
5151

5252
#if !os(watchOS)

Demo/Demo/Samples/Essential/Input.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct Input: View {
2020
.textFieldStyle(.plain)
2121
.padding(25)
2222
.glass(
23-
color: focus ? .accentColor : .primary,
23+
color: focus ? .accentColor : .white,
2424
shadowColor: focus ? .accentColor : .primary
2525
)
2626
.padding(50)

Sources/SwiftGlass/GlassBackgroundModifier.swift

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,51 @@ public struct GlassBackgroundModifier: ViewModifier {
7676
/// 2. Gradient stroke for edge highlighting
7777
/// 3. Shadow for depth perception
7878
public func body(content: Content) -> some View {
79+
#if swift(>=6.0) && canImport(SwiftUI, _version: 6.0)
80+
// Use new glass effect APIs available in newer Xcode/Swift versions
7981
if #available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *) {
8082
// Check if content is in a toolbar context
8183
if isInToolbar {
82-
content
83-
.tint(color)
84+
AnyView(
85+
content
86+
.tint(color)
87+
)
8488
} else {
85-
content
86-
#if !os(visionOS)
87-
.glassEffect(.regular.tint(color.opacity(colorOpacity)).interactive(), in: .rect(cornerRadius: radius))
88-
#endif
89-
.cornerRadius(radius)
90-
.shadow(color: shadowColor.opacity(shadowOpacity), radius: shadowRadius, x: shadowX, y: shadowY)
89+
AnyView(
90+
content
91+
#if !os(visionOS)
92+
.glassEffect(.regular.tint(color.opacity(colorOpacity)).interactive(), in: .rect(cornerRadius: radius))
93+
#else
94+
.background(color.opacity(colorOpacity))
95+
.background(material)
96+
.cornerRadius(radius)
97+
.overlay(
98+
RoundedRectangle(cornerRadius: radius)
99+
.stroke(
100+
LinearGradient(
101+
gradient: Gradient(colors: gradientColors()),
102+
startPoint: .topLeading,
103+
endPoint: .bottomTrailing
104+
),
105+
lineWidth: strokeWidth
106+
)
107+
)
108+
#endif
109+
.shadow(color: shadowColor.opacity(shadowOpacity), radius: shadowRadius, x: shadowX, y: shadowY)
110+
)
91111
}
92112
} else {
113+
AnyView(fallbackGlassEffect(content: content))
114+
}
115+
#else
116+
// Fallback for older Xcode versions (16.4 and earlier)
117+
AnyView(fallbackGlassEffect(content: content))
118+
#endif
119+
}
120+
121+
/// Fallback glass effect implementation for older Xcode versions
122+
private func fallbackGlassEffect(content: Content) -> AnyView {
123+
return AnyView(
93124
content
94125
.background(color.opacity(colorOpacity))
95126
.background(material) // Use the specified material for the frosted glass base
@@ -108,7 +139,7 @@ public struct GlassBackgroundModifier: ViewModifier {
108139
)
109140
// Adds shadow for depth and elevation
110141
.shadow(color: shadowColor.opacity(shadowOpacity), radius: shadowRadius, x: shadowX, y: shadowY)
111-
}
142+
)
112143
}
113144

114145
/// Generates the gradient colors based on the selected style

Sources/SwiftGlass/SwiftGlass.swift

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,7 @@ public extension View {
4040
func glass(
4141
displayMode: GlassBackgroundModifier.GlassBackgroundDisplayMode = .always,
4242
radius: CGFloat = 32,
43-
color: Color = {
44-
#if os(iOS) || os(tvOS)
45-
return Color(UIColor.systemBackground)
46-
#elseif os(watchOS)
47-
return Color(UIColor.black)
48-
#elseif os(macOS)
49-
return Color(NSColor.controlBackgroundColor)
50-
#else
51-
return Color.primary
52-
#endif
53-
}(),
43+
color: Color = .white,
5444
colorOpacity: Double = 0.1,
5545
material: Material = .ultraThinMaterial,
5646
gradientOpacity: Double = 0.5,

0 commit comments

Comments
 (0)