Skip to content

Commit 04a4ced

Browse files
committed
Refactor glass effect logic for toolbar and iOS versions
Reorganizes GlassBackgroundModifier to check toolbar context and iOS version before applying glass effects. Ensures correct effect is applied for iOS 26+, toolbars, and fallback scenarios. Also updates Package.swift to require Swift 5.7 instead of 5.9.
1 parent 9f7a9bd commit 04a4ced

File tree

2 files changed

+37
-35
lines changed

2 files changed

+37
-35
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version: 5.9
1+
// swift-tools-version: 5.7
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription

Sources/SwiftGlass/GlassBackgroundModifier.swift

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -76,45 +76,47 @@ 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+
// Check isInToolbar and iOS version first
80+
if isInToolbar {
81+
// Check if we're on iOS 26+
82+
if #available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *) {
83+
// On iOS 26+, return content with tint only (no glass effect, no border)
84+
return AnyView(content.tint(color))
85+
}
86+
// On iOS 18 and below, still apply glass effect when in toolbar
87+
return AnyView(fallbackGlassEffect(content: content))
88+
}
89+
90+
// Not in toolbar - apply glass effect based on iOS version
7991
#if swift(>=6.0) && canImport(SwiftUI, _version: 6.0)
80-
// Use new glass effect APIs available in newer Xcode/Swift versions
8192
if #available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *) {
82-
// Check if content is in a toolbar context
83-
if isInToolbar {
84-
AnyView(
85-
content
86-
.tint(color)
87-
)
88-
} else {
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-
)
111-
}
93+
return AnyView(
94+
content
95+
#if !os(visionOS)
96+
.glassEffect(.regular.tint(color.opacity(colorOpacity)).interactive(), in: .rect(cornerRadius: radius))
97+
#else
98+
.background(color.opacity(colorOpacity))
99+
.background(material)
100+
.cornerRadius(radius)
101+
.overlay(
102+
RoundedRectangle(cornerRadius: radius)
103+
.stroke(
104+
LinearGradient(
105+
gradient: Gradient(colors: gradientColors()),
106+
startPoint: .topLeading,
107+
endPoint: .bottomTrailing
108+
),
109+
lineWidth: strokeWidth
110+
)
111+
)
112+
#endif
113+
.shadow(color: shadowColor.opacity(shadowOpacity), radius: shadowRadius, x: shadowX, y: shadowY)
114+
)
112115
} else {
113-
AnyView(fallbackGlassEffect(content: content))
116+
return AnyView(fallbackGlassEffect(content: content))
114117
}
115118
#else
116-
// Fallback for older Xcode versions (16.4 and earlier)
117-
AnyView(fallbackGlassEffect(content: content))
119+
return AnyView(fallbackGlassEffect(content: content))
118120
#endif
119121
}
120122

0 commit comments

Comments
 (0)