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
6 changes: 6 additions & 0 deletions Sources/Sliders/Base/LinearValueMath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ import SwiftUI
let validatedValue = min(bounds.upperBound, max(bounds.lowerBound, steppedNewValue))
return validatedValue
}

extension Comparable {
func clamped(to range: ClosedRange<Self>) -> Self {
min(max(self, range.lowerBound), range.upperBound)
}
}
8 changes: 4 additions & 4 deletions Sources/Sliders/PointSlider/PointSlider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ extension PointSlider {

self.init(
PointSliderStyleConfiguration(
x: Binding(get: { CGFloat(x.wrappedValue) }, set: { x.wrappedValue = V($0) }),
x: Binding(get: { CGFloat(x.wrappedValue.clamped(to: xBounds)) }, set: { x.wrappedValue = V($0) }),
xBounds: CGFloat(xBounds.lowerBound)...CGFloat(xBounds.upperBound),
xStep: CGFloat(xStep),
y: Binding(get: { CGFloat(y.wrappedValue) }, set: { y.wrappedValue = V($0) }),
y: Binding(get: { CGFloat(y.wrappedValue.clamped(to: yBounds)) }, set: { y.wrappedValue = V($0) }),
yBounds: CGFloat(yBounds.lowerBound)...CGFloat(yBounds.upperBound),
yStep: CGFloat(yStep),
onEditingChanged: onEditingChanged,
Expand Down Expand Up @@ -66,8 +66,8 @@ private struct PointSlidersPreview: View {
@State var pointX1 = 0.5
@State var pointY1 = 0.5

@State var pointX2 = 0.5
@State var pointY2 = 0.5
@State var pointX2 = 2.0
@State var pointY2 = -0.5

var body: some View {
VStack(spacing: 32) {
Expand Down
6 changes: 3 additions & 3 deletions Sources/Sliders/RangeSlider/RangeSlider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ extension RangeSlider {
self.init(
RangeSliderStyleConfiguration(
range: Binding(
get: { CGFloat(range.wrappedValue.lowerBound)...CGFloat(range.wrappedValue.upperBound) },
get: { CGFloat(range.wrappedValue.clamped(to: bounds).lowerBound)...CGFloat(range.wrappedValue.clamped(to: bounds).upperBound) },
set: { range.wrappedValue = V($0.lowerBound)...V($0.upperBound) }
),
bounds: CGFloat(bounds.lowerBound)...CGFloat(bounds.upperBound),
Expand Down Expand Up @@ -70,7 +70,7 @@ private struct HorizontalRangeSlidersPreview: View {
@State var range3 = 0.1...0.9
@State var range4 = 0.1...0.9
@State var range5 = 0.1...0.9
@State var range6 = 0.1...0.9
@State var range6 = -2.0...4.0

var body: some View {
VStack {
Expand Down Expand Up @@ -146,7 +146,7 @@ private struct HorizontalRangeSlidersPreview: View {
)
)

RangeSlider(range: $range6)
RangeSlider(range: $range6, in: 1.0 ... 3.0)
.cornerRadius(8)
.frame(height: 128)
.rangeSliderStyle(
Expand Down
6 changes: 3 additions & 3 deletions Sources/Sliders/ValueSlider/ValueSlider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extension ValueSlider {

self.init(
ValueSliderStyleConfiguration(
value: Binding(get: { CGFloat(value.wrappedValue) }, set: { value.wrappedValue = V($0) }),
value: Binding(get: { CGFloat(value.wrappedValue.clamped(to: bounds)) }, set: { value.wrappedValue = V($0) }),
bounds: CGFloat(bounds.lowerBound)...CGFloat(bounds.upperBound),
step: CGFloat(step),
onEditingChanged: onEditingChanged,
Expand Down Expand Up @@ -132,7 +132,7 @@ private struct VerticalValueSlidersPreview: View {
@State var value1 = 0.5
@State var value2 = 0.5
@State var value3 = 0.5
@State var value4 = 0.5
@State var value4 = 4.0

var body: some View {
HStack {
Expand All @@ -158,7 +158,7 @@ private struct VerticalValueSlidersPreview: View {
)
)

ValueSlider(value: $value4)
ValueSlider(value: $value4, in: 1.0 ... 3.0)
.valueSliderStyle(
VerticalValueSliderStyle(
track: LinearGradient(
Expand Down