Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Demo/Demo/Samples/Essential/Basic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct Basic: View {
Text("HOME")
.font(.largeTitle)
.fontWeight(.heavy)
.foregroundStyle(.primary)
.foregroundStyle(.white)
Copy link

Copilot AI Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Hardcoding .white as the foreground style may reduce readability on light backgrounds. Consider making this dynamic or theme-aware to ensure adequate contrast.

Suggested change
.foregroundStyle(.white)
.foregroundStyle(.primary)

Copilot uses AI. Check for mistakes.
}

#if !os(watchOS)
Expand Down
2 changes: 1 addition & 1 deletion Demo/Demo/Samples/Essential/Input.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct Input: View {
.textFieldStyle(.plain)
.padding(25)
.glass(
color: focus ? .accentColor : .primary,
color: focus ? .accentColor : .white,
shadowColor: focus ? .accentColor : .primary
)
.padding(50)
Expand Down
49 changes: 40 additions & 9 deletions Sources/SwiftGlass/GlassBackgroundModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,51 @@ public struct GlassBackgroundModifier: ViewModifier {
/// 2. Gradient stroke for edge highlighting
/// 3. Shadow for depth perception
public func body(content: Content) -> some View {
#if swift(>=6.0) && canImport(SwiftUI, _version: 6.0)
Copy link

Copilot AI Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fallback logic is duplicated in both the #available else branch and the outer compiler #else. Unify these paths by calling fallbackGlassEffect in one place to reduce duplication.

Copilot uses AI. Check for mistakes.
// Use new glass effect APIs available in newer Xcode/Swift versions
if #available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *) {
// Check if content is in a toolbar context
if isInToolbar {
content
.tint(color)
AnyView(
content
.tint(color)
)
} else {
content
#if !os(visionOS)
.glassEffect(.regular.tint(color.opacity(colorOpacity)).interactive(), in: .rect(cornerRadius: radius))
#endif
.cornerRadius(radius)
.shadow(color: shadowColor.opacity(shadowOpacity), radius: shadowRadius, x: shadowX, y: shadowY)
AnyView(
content
#if !os(visionOS)
.glassEffect(.regular.tint(color.opacity(colorOpacity)).interactive(), in: .rect(cornerRadius: radius))
#else
.background(color.opacity(colorOpacity))
.background(material)
.cornerRadius(radius)
.overlay(
RoundedRectangle(cornerRadius: radius)
.stroke(
LinearGradient(
gradient: Gradient(colors: gradientColors()),
startPoint: .topLeading,
endPoint: .bottomTrailing
),
lineWidth: strokeWidth
)
)
#endif
.shadow(color: shadowColor.opacity(shadowOpacity), radius: shadowRadius, x: shadowX, y: shadowY)
)
}
} else {
AnyView(fallbackGlassEffect(content: content))
}
#else
// Fallback for older Xcode versions (16.4 and earlier)
AnyView(fallbackGlassEffect(content: content))
Comment on lines +84 to +117
Copy link

Copilot AI Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using AnyView for type erasure can degrade SwiftUI performance by preventing view identity optimizations. Consider restructuring the conditional logic with view builders to avoid unnecessary type erasure.

Copilot uses AI. Check for mistakes.
#endif
}

/// Fallback glass effect implementation for older Xcode versions
private func fallbackGlassEffect(content: Content) -> AnyView {
return AnyView(
content
.background(color.opacity(colorOpacity))
.background(material) // Use the specified material for the frosted glass base
Expand All @@ -108,7 +139,7 @@ public struct GlassBackgroundModifier: ViewModifier {
)
// Adds shadow for depth and elevation
.shadow(color: shadowColor.opacity(shadowOpacity), radius: shadowRadius, x: shadowX, y: shadowY)
}
)
}

/// Generates the gradient colors based on the selected style
Expand Down
12 changes: 1 addition & 11 deletions Sources/SwiftGlass/SwiftGlass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,7 @@ public extension View {
func glass(
displayMode: GlassBackgroundModifier.GlassBackgroundDisplayMode = .always,
radius: CGFloat = 32,
color: Color = {
#if os(iOS) || os(tvOS)
return Color(UIColor.systemBackground)
#elseif os(watchOS)
return Color(UIColor.black)
#elseif os(macOS)
return Color(NSColor.controlBackgroundColor)
#else
return Color.primary
#endif
}(),
color: Color = .white,
colorOpacity: Double = 0.1,
material: Material = .ultraThinMaterial,
gradientOpacity: Double = 0.5,
Expand Down