Skip to content

Commit 8013da1

Browse files
committed
make sliders generic for binary floating point
1 parent 575241b commit 8013da1

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

Sources/RangeSlider.swift

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ import SwiftUI
3939
///
4040
/// You can also use a step parameter to provide incremental steps along the path of the slider. For example,
4141
/// if you have a slider with a range of 0 to 100, and you set the step value to 5, the slider’s increments would be 0, 5, 10, and so on.
42-
public struct RangeSlider: View {
42+
public struct RangeSlider<V: BinaryFloatingPoint>: View {
4343
// MARK: - Properties
44-
44+
4545
/// The value of thumb on the left-hand-side.
46-
@Binding var lowValue: Double
46+
@Binding var lowValue: V
4747

4848
/// The value of thumb on the right-hand-side.
49-
@Binding var highValue: Double
49+
@Binding var highValue: V
5050

5151
/// The zIndex for the thumb on the right-hand-side.
5252
@State private var zIndexRHS: Double = 0
@@ -67,16 +67,16 @@ public struct RangeSlider: View {
6767
let displayBounds: ClosedRange<Double>?
6868

6969
/// The lower-bound for the slider.
70-
let minimumValue: Double
70+
let minimumValue: V
7171

7272
/// The upper-bound for the slider.
73-
let maximumValue: Double
73+
let maximumValue: V
7474

7575
/// The distance between the upper and lower bounds.
76-
let totalDistance: Double
76+
let totalDistance: V
7777

7878
/// The distance between each value. Defaults to zero.
79-
let step: Double?
79+
let step: V?
8080

8181
/// Whether or not to display the low and high slider values.
8282
let showValues: Bool
@@ -92,10 +92,10 @@ public struct RangeSlider: View {
9292

9393
/// The width of the sliders track.
9494
let lineWidth: CGFloat
95-
95+
9696
/// A callback for when editing begins and ends.
9797
let onEditingChanged: (Bool) -> Void
98-
98+
9999
public var body: some View {
100100
VStack {
101101
if title != nil {
@@ -182,7 +182,6 @@ public struct RangeSlider: View {
182182
}
183183

184184
// MARK: - Methods
185-
186185
/// Creates a slider to select two values from a given range.
187186
///
188187
/// The values of the created instance are equal to the position of the given values within bounds, mapped into 0...1.
@@ -195,11 +194,11 @@ public struct RangeSlider: View {
195194
/// - Parameter onEditingChanged: A callback for when editing begins and ends.
196195
public init(
197196
_ title: String? = nil,
198-
lowValue: Binding<Double>,
199-
highValue: Binding<Double>,
197+
lowValue: Binding<V>,
198+
highValue: Binding<V>,
200199
in bounds: ClosedRange<Double>,
201200
displayBounds: ClosedRange<Double>? = nil,
202-
step: Double? = nil,
201+
step: V? = nil,
203202
showValues: Bool = true,
204203
showDifferenceOnEditing: Bool = false,
205204
color: Color = .blue,
@@ -208,8 +207,8 @@ public struct RangeSlider: View {
208207
onEditingChanged: @escaping (Bool) -> Void) {
209208
_lowValue = lowValue
210209
_highValue = highValue
211-
minimumValue = bounds.lowerBound
212-
maximumValue = bounds.upperBound
210+
minimumValue = V(bounds.lowerBound)
211+
maximumValue = V(bounds.upperBound)
213212
totalDistance = maximumValue - minimumValue
214213
_displayTitle = State(initialValue: title ?? "")
215214
self.title = showDifferenceOnEditing && title == nil ? "" : title
@@ -224,18 +223,18 @@ public struct RangeSlider: View {
224223
self.onEditingChanged = onEditingChanged
225224
}
226225

227-
private func scale(_ value: Double, translate: Bool = false) -> CGFloat {
226+
private func scale(_ value: V, translate: Bool = false) -> V {
228227
guard let display = displayBounds else { return value }
229228

230-
let normal = value / (bounds.upperBound - bounds.lowerBound)
229+
let normal = value / (maximumValue - minimumValue)
231230

232-
return normal * (display.upperBound - display.lowerBound)
231+
return normal * V((display.upperBound - display.lowerBound))
233232
}
234233

235-
private func translate(_ value: Double) -> CGFloat {
236-
guard let display = displayBounds else { return value }
234+
private func translate(_ value: V) -> Double {
235+
guard let display = displayBounds else { return Double(value) }
237236

238-
return value + display.lowerBound - bounds.lowerBound
237+
return Double(value) + display.lowerBound - bounds.lowerBound
239238
}
240239

241240
private func dragGesture(side: Thumb.Side, geo: GeometryProxy) -> some Gesture {
@@ -254,7 +253,7 @@ public struct RangeSlider: View {
254253
}
255254

256255
if showDifferenceOnEditing {
257-
displayTitle = String(format: "%0.2f", scale(highValue - lowValue))
256+
displayTitle = String(format: "%0.2f", Double(scale(highValue - lowValue)))
258257
}
259258
}
260259
.onEnded { _ in
@@ -275,16 +274,16 @@ public struct RangeSlider: View {
275274
}
276275
}
277276

278-
private func getNewValue(for side: Thumb.Side, _ translationX: CGFloat, _ geo: GeometryProxy) -> Double? {
277+
private func getNewValue(for side: Thumb.Side, _ translationX: CGFloat, _ geo: GeometryProxy) -> V? {
279278
if startX == nil {
280279
startX = xPosition(side: side, geo)
281280
onEditingChanged(true)
282281
}
283282

284283
guard let startX = startX else { return nil }
285284

286-
var newValue: Double {
287-
var value = Double((startX + translationX) / geo.size.width) * totalDistance
285+
var newValue: V {
286+
var value = V((startX + translationX) / geo.size.width) * totalDistance
288287

289288
if let step = step {
290289
value = value - value.remainder(dividingBy: step)
@@ -305,7 +304,6 @@ public struct RangeSlider: View {
305304
}
306305

307306
// MARK: - Components
308-
309307
private struct Thumb: View {
310308
enum Side {
311309
case lhs
@@ -348,3 +346,4 @@ private struct Track: View {
348346
.frame(height: lineWidth)
349347
}
350348
}
349+

Sources/RoundSlider.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ public struct RoundSlider<V: BinaryFloatingPoint>: View {
121121
.padding(.bottom, lineWidth)
122122
.transition(.opacity)
123123

124-
// We change the id parameter of the view when
125-
// we change its title. This resets the views
126-
// state, allowing us to animate this change.
124+
// We change the id parameter of the view when
125+
// we change its title. This resets the views
126+
// state, allowing us to animate this change.
127127
.id("RoundSlider" + displayTitle)
128128

129129
ZStack {
@@ -215,3 +215,4 @@ public struct RoundSlider<V: BinaryFloatingPoint>: View {
215215
start = translation
216216
}
217217
}
218+

0 commit comments

Comments
 (0)