Skip to content

2.0 SwiftGlassUI Library #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

2.0 SwiftGlassUI Library #9

wants to merge 1 commit into from

Conversation

1998code
Copy link
Owner

This pull request introduces a new custom Switch component and a Toggle view in the Demo project, providing a visually rich and interactive toggle switch with drag gestures and accent color customization. The changes also include a gradient background for the Toggle view and a preview in dark mode.

New Components and Views:

  • Custom Switch Component: Added a reusable Switch view with drag gesture support, dynamic fill progress, and customizable accent colors. It includes animation for smooth state transitions and gesture handling for toggling between on/off states. ([Demo/Demo/Samples/Essential/Switch.swiftR1-R237](https://github.com/1998code/SwiftGlass/pull/9/files#diff-c6d00cd1533ee11104107fa46a998674756703c939bce9c44f63f5910fca5e84R1-R237))
  • Toggle View: Created a Toggle view showcasing multiple Switch components with various accent colors arranged in a grid layout. Includes a gradient background for visual enhancement. ([Demo/Demo/Samples/Essential/Switch.swiftR1-R237](https://github.com/1998code/SwiftGlass/pull/9/files#diff-c6d00cd1533ee11104107fa46a998674756703c939bce9c44f63f5910fca5e84R1-R237))

Enhancements:

  • Dark Mode Preview: Added a preview for the Toggle view in dark mode using the preferredColorScheme(.dark) modifier. ([Demo/Demo/Samples/Essential/Switch.swiftR1-R237](https://github.com/1998code/SwiftGlass/pull/9/files#diff-c6d00cd1533ee11104107fa46a998674756703c939bce9c44f63f5910fca5e84R1-R237))

@1998code 1998code requested a review from Copilot June 12, 2025 11:46
@1998code 1998code self-assigned this Jun 12, 2025
@1998code 1998code added the enhancement New feature or request label Jun 12, 2025
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces a reusable, gesture-driven Switch component with customizable accent colors and a Toggle demo view displaying multiple switches against a gradient background, including a dark-mode preview.

  • Adds Switch component supporting drag gestures, animated thumb, and dynamic fill.
  • Implements Toggle view to showcase various accent colors in a grid.
  • Provides a dark‐mode preview for the Toggle.
Comments suppressed due to low confidence (3)

Demo/Demo/Samples/Essential/Switch.swift:11

  • [nitpick] Naming this struct Toggle conflicts with SwiftUI's built-in Toggle view, which can lead to confusion. Consider renaming it to something like DemoToggle or SwitchGridView to improve clarity.
struct Toggle: View {

Demo/Demo/Samples/Essential/Switch.swift:96

  • The title property in Switch is never used in the view body. Either remove this parameter to simplify the API or use it to provide an accessible label for the switch.
let title: String

Demo/Demo/Samples/Essential/Switch.swift:95

  • The new Switch component has several interactive behaviors (drag and tap) but no accompanying tests. Consider adding unit or UI tests to cover its gesture handling and state transitions.
struct Switch: View {

Comment on lines +123 to +177
HStack {
ZStack {
// Base track (gray background)
RoundedRectangle(cornerRadius: 25)
.fill(LinearGradient(
colors: isOn ? [.accentColor.opacity(0.3), .accentColor.opacity(1.0)] : [.gray.opacity(0.3), .clear],
startPoint: .leading,
endPoint: .trailing
))
.frame(width: toggleWidth, height: 30)
.glass()

if isDragging {
// Progressive fill container with proper right-to-left gray unfill
RoundedRectangle(cornerRadius: 25)
.fill(Color.clear)
.frame(width: toggleWidth, height: 30)
.overlay(
ZStack {
// Base fill (toggleColor or gray depending on direction)
if dragDirection >= 0 && !isOn {
// Dragging right - toggleColor fill from left
HStack {
Rectangle()
.fill(
LinearGradient(
colors: [.accentColor.opacity(0.3), .accentColor.opacity(1.0)],
startPoint: .leading,
endPoint: .trailing
)
)
.frame(width: toggleWidth * fillProgress, height: 30)

Spacer(minLength: 0)
}
} else {
// Dragging left - gray "unfill" from right
HStack {
Spacer(minLength: 0)

Rectangle()
.fill(
LinearGradient(
colors: [Color.gray.opacity(0.8), Color.gray.opacity(0.1)],
startPoint: .leading,
endPoint: .trailing
)
)
.frame(width: toggleWidth * (1.0 - fillProgress), height: 30)
}
}
}
)
.clipShape(RoundedRectangle(cornerRadius: 25))
}
Copy link
Preview

Copilot AI Jun 12, 2025

Choose a reason for hiding this comment

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

[nitpick] The HStack here wraps only the ZStack and adds no extra modifiers or siblings. You can remove it and place the ZStack directly to simplify the view hierarchy.

Suggested change
HStack {
ZStack {
// Base track (gray background)
RoundedRectangle(cornerRadius: 25)
.fill(LinearGradient(
colors: isOn ? [.accentColor.opacity(0.3), .accentColor.opacity(1.0)] : [.gray.opacity(0.3), .clear],
startPoint: .leading,
endPoint: .trailing
))
.frame(width: toggleWidth, height: 30)
.glass()
if isDragging {
// Progressive fill container with proper right-to-left gray unfill
RoundedRectangle(cornerRadius: 25)
.fill(Color.clear)
.frame(width: toggleWidth, height: 30)
.overlay(
ZStack {
// Base fill (toggleColor or gray depending on direction)
if dragDirection >= 0 && !isOn {
// Dragging right - toggleColor fill from left
HStack {
Rectangle()
.fill(
LinearGradient(
colors: [.accentColor.opacity(0.3), .accentColor.opacity(1.0)],
startPoint: .leading,
endPoint: .trailing
)
)
.frame(width: toggleWidth * fillProgress, height: 30)
Spacer(minLength: 0)
}
} else {
// Dragging left - gray "unfill" from right
HStack {
Spacer(minLength: 0)
Rectangle()
.fill(
LinearGradient(
colors: [Color.gray.opacity(0.8), Color.gray.opacity(0.1)],
startPoint: .leading,
endPoint: .trailing
)
)
.frame(width: toggleWidth * (1.0 - fillProgress), height: 30)
}
}
}
)
.clipShape(RoundedRectangle(cornerRadius: 25))
}
ZStack {
// Base track (gray background)
RoundedRectangle(cornerRadius: 25)
.fill(LinearGradient(
colors: isOn ? [.accentColor.opacity(0.3), .accentColor.opacity(1.0)] : [.gray.opacity(0.3), .clear],
startPoint: .leading,
endPoint: .trailing
))
.frame(width: toggleWidth, height: 30)
.glass()
if isDragging {
// Progressive fill container with proper right-to-left gray unfill
RoundedRectangle(cornerRadius: 25)
.fill(Color.clear)
.frame(width: toggleWidth, height: 30)
.overlay(
ZStack {
// Base fill (toggleColor or gray depending on direction)
if dragDirection >= 0 && !isOn {
// Dragging right - toggleColor fill from left
HStack {
Rectangle()
.fill(
LinearGradient(
colors: [.accentColor.opacity(0.3), .accentColor.opacity(1.0)],
startPoint: .leading,
endPoint: .trailing
)
)
.frame(width: toggleWidth * fillProgress, height: 30)
Spacer(minLength: 0)
}
} else {
// Dragging left - gray "unfill" from right
HStack {
Spacer(minLength: 0)
Rectangle()
.fill(
LinearGradient(
colors: [Color.gray.opacity(0.8), Color.gray.opacity(0.1)],
startPoint: .leading,
endPoint: .trailing
)
)
.frame(width: toggleWidth * (1.0 - fillProgress), height: 30)
}
}
}
)
.clipShape(RoundedRectangle(cornerRadius: 25))
}

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant