Skip to content

Commit 255d8ef

Browse files
authored
value clamping (#48)
1 parent 9dbe0a4 commit 255d8ef

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

Sources/Sliders/Base/LinearValueMath.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,9 @@ import SwiftUI
2121
let validatedValue = min(bounds.upperBound, max(bounds.lowerBound, steppedNewValue))
2222
return validatedValue
2323
}
24+
25+
extension Comparable {
26+
func clamped(to range: ClosedRange<Self>) -> Self {
27+
min(max(self, range.lowerBound), range.upperBound)
28+
}
29+
}

Sources/Sliders/PointSlider/PointSlider.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ extension PointSlider {
2424

2525
self.init(
2626
PointSliderStyleConfiguration(
27-
x: Binding(get: { CGFloat(x.wrappedValue) }, set: { x.wrappedValue = V($0) }),
27+
x: Binding(get: { CGFloat(x.wrappedValue.clamped(to: xBounds)) }, set: { x.wrappedValue = V($0) }),
2828
xBounds: CGFloat(xBounds.lowerBound)...CGFloat(xBounds.upperBound),
2929
xStep: CGFloat(xStep),
30-
y: Binding(get: { CGFloat(y.wrappedValue) }, set: { y.wrappedValue = V($0) }),
30+
y: Binding(get: { CGFloat(y.wrappedValue.clamped(to: yBounds)) }, set: { y.wrappedValue = V($0) }),
3131
yBounds: CGFloat(yBounds.lowerBound)...CGFloat(yBounds.upperBound),
3232
yStep: CGFloat(yStep),
3333
onEditingChanged: onEditingChanged,
@@ -66,8 +66,8 @@ private struct PointSlidersPreview: View {
6666
@State var pointX1 = 0.5
6767
@State var pointY1 = 0.5
6868

69-
@State var pointX2 = 0.5
70-
@State var pointY2 = 0.5
69+
@State var pointX2 = 2.0
70+
@State var pointY2 = -0.5
7171

7272
var body: some View {
7373
VStack(spacing: 32) {

Sources/Sliders/RangeSlider/RangeSlider.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extension RangeSlider {
2525
self.init(
2626
RangeSliderStyleConfiguration(
2727
range: Binding(
28-
get: { CGFloat(range.wrappedValue.lowerBound)...CGFloat(range.wrappedValue.upperBound) },
28+
get: { CGFloat(range.wrappedValue.clamped(to: bounds).lowerBound)...CGFloat(range.wrappedValue.clamped(to: bounds).upperBound) },
2929
set: { range.wrappedValue = V($0.lowerBound)...V($0.upperBound) }
3030
),
3131
bounds: CGFloat(bounds.lowerBound)...CGFloat(bounds.upperBound),
@@ -70,7 +70,7 @@ private struct HorizontalRangeSlidersPreview: View {
7070
@State var range3 = 0.1...0.9
7171
@State var range4 = 0.1...0.9
7272
@State var range5 = 0.1...0.9
73-
@State var range6 = 0.1...0.9
73+
@State var range6 = -2.0...4.0
7474

7575
var body: some View {
7676
VStack {
@@ -146,7 +146,7 @@ private struct HorizontalRangeSlidersPreview: View {
146146
)
147147
)
148148

149-
RangeSlider(range: $range6)
149+
RangeSlider(range: $range6, in: 1.0 ... 3.0)
150150
.cornerRadius(8)
151151
.frame(height: 128)
152152
.rangeSliderStyle(

Sources/Sliders/ValueSlider/ValueSlider.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extension ValueSlider {
2424

2525
self.init(
2626
ValueSliderStyleConfiguration(
27-
value: Binding(get: { CGFloat(value.wrappedValue) }, set: { value.wrappedValue = V($0) }),
27+
value: Binding(get: { CGFloat(value.wrappedValue.clamped(to: bounds)) }, set: { value.wrappedValue = V($0) }),
2828
bounds: CGFloat(bounds.lowerBound)...CGFloat(bounds.upperBound),
2929
step: CGFloat(step),
3030
onEditingChanged: onEditingChanged,
@@ -132,7 +132,7 @@ private struct VerticalValueSlidersPreview: View {
132132
@State var value1 = 0.5
133133
@State var value2 = 0.5
134134
@State var value3 = 0.5
135-
@State var value4 = 0.5
135+
@State var value4 = 4.0
136136

137137
var body: some View {
138138
HStack {
@@ -158,7 +158,7 @@ private struct VerticalValueSlidersPreview: View {
158158
)
159159
)
160160

161-
ValueSlider(value: $value4)
161+
ValueSlider(value: $value4, in: 1.0 ... 3.0)
162162
.valueSliderStyle(
163163
VerticalValueSliderStyle(
164164
track: LinearGradient(

0 commit comments

Comments
 (0)