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
2 changes: 1 addition & 1 deletion Examples/Sources/ControlsExample/ControlsApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ struct ControlsApp: App {

VStack {
Text("Slider")
Slider($sliderValue, minimum: 0, maximum: 10)
Slider(value: $sliderValue, in: 0...10)
.frame(maxWidth: 200)
Text("Value: \(String(format: "%.02f", sliderValue))")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,22 @@ struct RandomNumberGeneratorApp: App {

Text("Minimum: \(minNum)")
Slider(
$minNum.onChange { newValue in
value: $minNum.onChange { newValue in
if newValue > maxNum {
minNum = maxNum
}
},
minimum: 0,
maximum: 100
in: 0...100
)

Text("Maximum: \(maxNum)")
Slider(
$maxNum.onChange { newValue in
value: $maxNum.onChange { newValue in
if newValue < minNum {
maxNum = minNum
}
},
minimum: 0,
maximum: 100
in: 0...100
)

HStack {
Expand Down
73 changes: 18 additions & 55 deletions Sources/SwiftCrossUI/Views/Slider.swift
Original file line number Diff line number Diff line change
@@ -1,62 +1,27 @@
/// A value convertible to and from a ``Double``.`
public protocol DoubleConvertible {
/// Creates a value from a ``Double``.`
init(_ value: Double)

/// Converts the value to a ``Double``.`
var doubleRepresentation: Double { get }
}

/// A value represented by a ``BinaryFloatingPoint``.
struct FloatingPointValue<Value: BinaryFloatingPoint>: DoubleConvertible {
var value: Value

init(_ value: Value) {
self.value = value
}

init(_ value: Double) {
self.value = Value(value)
}

var doubleRepresentation: Double {
return Double(value)
}
}

/// A value represented by a ``BinaryInteger``.
struct IntegerValue<Value: BinaryInteger>: DoubleConvertible {
var value: Value

init(_ value: Value) {
self.value = value
}

init(_ value: Double) {
self.value = Value(value)
}

var doubleRepresentation: Double {
return Double(value)
}
}

/// A control for selecting a value from a bounded range of numerical values.
public struct Slider: ElementaryView, View {
/// The ideal width of a Slider.
private static let idealWidth: Double = 100

/// A binding to the current value.
private var value: Binding<Double>?
/// The slider's minimum value.
private var minimum: Double
/// The slider's maximum value.
private var maximum: Double
/// The slider's range of values.
private var range: ClosedRange<Double>
/// The number of decimal places used when displaying the value.
private var decimalPlaces: Int

/// Creates a slider to select a value between a minimum and maximum value.
@available(*, deprecated, renamed: "init(value:in:)")
public init<T: BinaryInteger>(_ value: Binding<T>? = nil, minimum: T, maximum: T) {
self.init(value: value, in: minimum...maximum)
}

@available(*, deprecated, renamed: "init(value:in:)")
public init<T: BinaryFloatingPoint>(_ value: Binding<T>? = nil, minimum: T, maximum: T) {
self.init(value: value, in: minimum...maximum)
}

/// Creates a slider to select a value between a minimum and maximum value.
public init<T: BinaryInteger>(value: Binding<T>? = nil, in range: ClosedRange<T>) {
if let value {
self.value = Binding<Double>(
get: {
Expand All @@ -67,13 +32,12 @@ public struct Slider: ElementaryView, View {
}
)
}
self.minimum = Double(minimum)
self.maximum = Double(maximum)
self.range = Double(range.lowerBound)...Double(range.upperBound)
decimalPlaces = 0
}

/// Creates a slider to select a value between a minimum and maximum value.
public init<T: BinaryFloatingPoint>(_ value: Binding<T>? = nil, minimum: T, maximum: T) {
public init<T: BinaryFloatingPoint>(value: Binding<T>? = nil, in range: ClosedRange<T>) {
if let value {
self.value = Binding<Double>(
get: {
Expand All @@ -84,8 +48,7 @@ public struct Slider: ElementaryView, View {
}
)
}
self.minimum = Double(minimum)
self.maximum = Double(maximum)
self.range = Double(range.lowerBound)...Double(range.upperBound)
decimalPlaces = 2
}

Expand Down Expand Up @@ -120,8 +83,8 @@ public struct Slider: ElementaryView, View {
) {
backend.updateSlider(
widget,
minimum: minimum,
maximum: maximum,
minimum: range.lowerBound,
maximum: range.upperBound,
decimalPlaces: decimalPlaces,
environment: environment
) { newValue in
Expand Down
Loading