diff --git a/Examples/Sources/ControlsExample/ControlsApp.swift b/Examples/Sources/ControlsExample/ControlsApp.swift index fe94e13ee87..535ff4fb314 100644 --- a/Examples/Sources/ControlsExample/ControlsApp.swift +++ b/Examples/Sources/ControlsExample/ControlsApp.swift @@ -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))") } diff --git a/Examples/Sources/RandomNumberGeneratorExample/RandomNumberGeneratorApp.swift b/Examples/Sources/RandomNumberGeneratorExample/RandomNumberGeneratorApp.swift index 1442aa3d45b..f09cabe9aa8 100644 --- a/Examples/Sources/RandomNumberGeneratorExample/RandomNumberGeneratorApp.swift +++ b/Examples/Sources/RandomNumberGeneratorExample/RandomNumberGeneratorApp.swift @@ -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 { diff --git a/Sources/SwiftCrossUI/Views/Slider.swift b/Sources/SwiftCrossUI/Views/Slider.swift index 26567b35241..6b930d720a0 100644 --- a/Sources/SwiftCrossUI/Views/Slider.swift +++ b/Sources/SwiftCrossUI/Views/Slider.swift @@ -1,46 +1,3 @@ -/// 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: 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: 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. @@ -48,15 +5,23 @@ public struct Slider: ElementaryView, View { /// A binding to the current value. private var value: Binding? - /// 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 /// 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(_ value: Binding? = nil, minimum: T, maximum: T) { + self.init(value: value, in: minimum...maximum) + } + + @available(*, deprecated, renamed: "init(value:in:)") + public init(_ value: Binding? = 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(value: Binding? = nil, in range: ClosedRange) { if let value { self.value = Binding( get: { @@ -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(_ value: Binding? = nil, minimum: T, maximum: T) { + public init(value: Binding? = nil, in range: ClosedRange) { if let value { self.value = Binding( get: { @@ -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 } @@ -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