-
-
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
Open
1998code
wants to merge
1
commit into
main
Choose a base branch
from
2.0
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,237 @@ | ||
// | ||
// Switch.swift | ||
// Demo | ||
// | ||
// Created by Ming on 12/6/2025. | ||
// | ||
|
||
import SwiftUI | ||
import SwiftGlass | ||
|
||
struct Toggle: View { | ||
@State private var isOn: Bool = false | ||
|
||
var body: some View { | ||
ZStack { | ||
bg | ||
HStack { | ||
VStack(spacing: 20) { | ||
Switch("", isOn: $isOn) | ||
.accentColor(.green) | ||
|
||
Switch("", isOn: $isOn) | ||
.accentColor(.clear) | ||
|
||
Switch("", isOn: $isOn) | ||
.accentColor(.black) | ||
|
||
Switch("", isOn: $isOn) | ||
.accentColor(.blue) | ||
|
||
Switch("", isOn: $isOn) | ||
.accentColor(.brown) | ||
|
||
Switch("", isOn: $isOn) | ||
.accentColor(.cyan) | ||
|
||
Spacer() | ||
} | ||
|
||
VStack(spacing: 20) { | ||
Switch("", isOn: $isOn) | ||
.accentColor(.indigo) | ||
|
||
Switch("", isOn: $isOn) | ||
.accentColor(.mint) | ||
|
||
Switch("", isOn: $isOn) | ||
.accentColor(.orange) | ||
|
||
Switch("", isOn: $isOn) | ||
.accentColor(.pink) | ||
|
||
Switch("", isOn: $isOn) | ||
.accentColor(.purple) | ||
|
||
Switch("", isOn: $isOn) | ||
.accentColor(.red) | ||
|
||
Spacer() | ||
} | ||
|
||
VStack(spacing: 20) { | ||
Switch("", isOn: $isOn) | ||
.accentColor(.teal) | ||
|
||
Switch("", isOn: $isOn) | ||
.accentColor(.white) | ||
|
||
Switch("", isOn: $isOn) | ||
.accentColor(.yellow) | ||
|
||
Switch("", isOn: $isOn) | ||
.accentColor(.accentColor) | ||
|
||
Switch("", isOn: $isOn) | ||
.accentColor(.primary) | ||
|
||
Switch("", isOn: $isOn) | ||
.accentColor(.secondary) | ||
|
||
Spacer() | ||
} | ||
Spacer() | ||
} | ||
.padding() | ||
} | ||
} | ||
|
||
var bg: some View { | ||
LinearGradient(colors: [Color.clear, Color.blue.opacity(0.5)], startPoint: .topLeading, endPoint: .bottomTrailing) | ||
.ignoresSafeArea() | ||
} | ||
} | ||
|
||
struct Switch: View { | ||
let title: String | ||
@Binding var isOn: Bool | ||
|
||
@State private var dragOffset: CGFloat = 0 | ||
@State private var isDragging: Bool = false | ||
@State private var dragDirection: CGFloat = 0 | ||
@State private var lastDragValue: CGFloat = 0 | ||
|
||
private let toggleWidth: CGFloat = 60 | ||
private let thumbSize: CGFloat = 26 | ||
private let maxOffset: CGFloat = 15 | ||
|
||
init(_ title: String, isOn: Binding<Bool>) { | ||
self.title = title | ||
self._isOn = isOn | ||
} | ||
|
||
private var fillProgress: CGFloat { | ||
if isDragging { | ||
let progress = (dragOffset + maxOffset) / (maxOffset * 2) | ||
return max(0, min(1, progress)) | ||
} else { | ||
return isOn ? 1.0 : 0.0 | ||
} | ||
} | ||
|
||
var body: some 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)) | ||
} | ||
|
||
// Toggle thumb | ||
Circle() | ||
.fill(isDragging ? Color.white.opacity(0.9) : Color.white.opacity(0.85)) | ||
.frame(width: thumbSize, height: thumbSize) | ||
.glass() | ||
.offset(x: isDragging ? dragOffset : (isOn ? maxOffset : -maxOffset)) | ||
.scaleEffect(isDragging ? 1.25 : 1.0) | ||
.animation(.easeInOut(duration: 0.3), value: isDragging ? dragOffset : (isOn ? maxOffset : -maxOffset)) | ||
.gesture( | ||
DragGesture() | ||
.onChanged { value in | ||
if !isDragging { | ||
isDragging = true | ||
lastDragValue = value.translation.width | ||
} else { | ||
// Calculate drag direction based on movement | ||
dragDirection = value.translation.width - lastDragValue | ||
lastDragValue = value.translation.width | ||
} | ||
|
||
// Calculate position based on drag from initial position | ||
let startPosition = isOn ? maxOffset : -maxOffset | ||
let newOffset = startPosition + value.translation.width | ||
dragOffset = min(maxOffset, max(-maxOffset, newOffset)) | ||
} | ||
.onEnded { value in | ||
let threshold: CGFloat = 0.0 // Use center as threshold | ||
|
||
let newState = dragOffset > threshold | ||
|
||
// Only animate if state actually changes | ||
if newState != isOn { | ||
withAnimation(.easeInOut(duration: 0.3)) { | ||
isOn = newState | ||
isDragging = false | ||
} | ||
} else { | ||
isDragging = false | ||
} | ||
|
||
dragOffset = 0 | ||
dragDirection = 0 | ||
lastDragValue = 0 | ||
} | ||
) | ||
} | ||
} | ||
.onTapGesture { | ||
withAnimation { | ||
isOn.toggle() | ||
} | ||
} | ||
} | ||
} | ||
|
||
#Preview("Dark") { | ||
Toggle() | ||
.preferredColorScheme(.dark) | ||
} |
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.
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 theZStack
and adds no extra modifiers or siblings. You can remove it and place theZStack
directly to simplify the view hierarchy.Copilot uses AI. Check for mistakes.