-
-
Notifications
You must be signed in to change notification settings - Fork 17
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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
Switchcomponent supporting drag gestures, animated thumb, and dynamic fill. - Implements
Toggleview 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
Toggleconflicts with SwiftUI's built-inToggleview, which can lead to confusion. Consider renaming it to something likeDemoToggleorSwitchGridViewto improve clarity.
struct Toggle: View {
Demo/Demo/Samples/Essential/Switch.swift:96
- The
titleproperty inSwitchis 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
Switchcomponent 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 {
| 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)) | ||
| } |
Copilot
AI
Jun 12, 2025
There was a problem hiding this comment.
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.
| 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)) | |
| } |
This pull request introduces a new custom
Switchcomponent and aToggleview in theDemoproject, providing a visually rich and interactive toggle switch with drag gestures and accent color customization. The changes also include a gradient background for theToggleview and a preview in dark mode.New Components and Views:
SwitchComponent: Added a reusableSwitchview 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))ToggleView: Created aToggleview showcasing multipleSwitchcomponents 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:
Toggleview in dark mode using thepreferredColorScheme(.dark)modifier. ([Demo/Demo/Samples/Essential/Switch.swiftR1-R237](https://github.com/1998code/SwiftGlass/pull/9/files#diff-c6d00cd1533ee11104107fa46a998674756703c939bce9c44f63f5910fca5e84R1-R237))