Conversation
There was a problem hiding this comment.
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 {
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)) | ||
| } |
There was a problem hiding this comment.
[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)) | |
| } |
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.
Introduces a new GlassShape enum to support rounded rectangle, circle, and capsule shapes in the glass effect. Refactors GlassBackgroundModifier to handle shape selection and updates the glass() View extension accordingly. Adds new sample views for Capsule and Circle glass effects, updates advanced samples for better code reuse, and documents shape support in the README. Bumps project and marketing versions, and updates the Swift tools version to 5.9.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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))