Skip to content

Commit 5ba8614

Browse files
authored
Merge pull request #53 from jacobp100/master
Call editing changed callback before updating binding values
2 parents 538e16b + b1fcb5f commit 5ba8614

File tree

11 files changed

+73
-70
lines changed

11 files changed

+73
-70
lines changed

Sources/Sliders/Base/DefaultHorizontalValueView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import SwiftUI
33
public struct DefaultHorizontalValueView: View {
44
public var body: some View {
55
Capsule()
6-
.foregroundColor(.accentColor)
6+
.foregroundColor(Color.accentColor)
77
.frame(height: 3)
88
}
99
}

Sources/Sliders/Base/DefaultVerticalValueView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import SwiftUI
33
public struct DefaultVerticalValueView: View {
44
public var body: some View {
55
Capsule()
6-
.foregroundColor(.accentColor)
6+
.foregroundColor(Color.accentColor)
77
.frame(width: 3)
88
}
99
}

Sources/Sliders/PointSlider/Styles/Rectangular/RectangularPointSliderStyle.swift

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ public struct RectangularPointSliderStyle<Track: View, Thumb: View>: PointSlider
1717
yBounds: configuration.yBounds,
1818
yStep: configuration.yStep
1919
))
20-
.accentColor(.accentColor)
21-
20+
.accentColor(Color.accentColor)
21+
2222
return GeometryReader { geometry in
2323
ZStack {
2424
if self.options.contains(.interactiveTrack) {
2525
track.gesture(
2626
DragGesture(minimumDistance: 0)
2727
.onChanged { gestureValue in
28+
configuration.onEditingChanged(true)
29+
2830
let computedValueX = valueFrom(
2931
distance: gestureValue.location.x,
3032
availableDistance: geometry.size.width,
@@ -33,7 +35,7 @@ public struct RectangularPointSliderStyle<Track: View, Thumb: View>: PointSlider
3335
leadingOffset: self.thumbSize.width / 2,
3436
trailingOffset: self.thumbSize.width / 2
3537
)
36-
38+
3739
let computedValueY = configuration.yBounds.upperBound - valueFrom(
3840
distance: gestureValue.location.y,
3941
availableDistance: geometry.size.height,
@@ -42,10 +44,9 @@ public struct RectangularPointSliderStyle<Track: View, Thumb: View>: PointSlider
4244
leadingOffset: self.thumbSize.height / 2,
4345
trailingOffset: self.thumbSize.height / 2
4446
)
45-
47+
4648
configuration.x.wrappedValue = computedValueX
4749
configuration.y.wrappedValue = computedValueY
48-
configuration.onEditingChanged(true)
4950
}
5051
.onEnded { _ in
5152
configuration.onEditingChanged(false)
@@ -54,7 +55,7 @@ public struct RectangularPointSliderStyle<Track: View, Thumb: View>: PointSlider
5455
} else {
5556
track
5657
}
57-
58+
5859
ZStack {
5960
self.thumb
6061
.frame(width: self.thumbSize.width, height: self.thumbSize.height)
@@ -80,6 +81,8 @@ public struct RectangularPointSliderStyle<Track: View, Thumb: View>: PointSlider
8081
.gesture(
8182
DragGesture()
8283
.onChanged { gestureValue in
84+
configuration.onEditingChanged(true)
85+
8386
if configuration.dragOffset.wrappedValue == nil {
8487
let gragOffsetX = gestureValue.startLocation.x - distanceFrom(
8588
value: configuration.x.wrappedValue,
@@ -88,18 +91,18 @@ public struct RectangularPointSliderStyle<Track: View, Thumb: View>: PointSlider
8891
leadingOffset: self.thumbSize.width / 2,
8992
trailingOffset: self.thumbSize.width / 2
9093
)
91-
94+
9295
let dragOffsetY = gestureValue.startLocation.y - (geometry.size.height - distanceFrom(
9396
value: configuration.y.wrappedValue,
9497
availableDistance: geometry.size.height,
9598
bounds: configuration.yBounds,
9699
leadingOffset: self.thumbSize.height / 2,
97100
trailingOffset: self.thumbSize.height / 2
98101
))
99-
102+
100103
configuration.dragOffset.wrappedValue = CGPoint(x: gragOffsetX, y: dragOffsetY)
101104
}
102-
105+
103106
let computedValueX = valueFrom(
104107
distance: gestureValue.location.x - (configuration.dragOffset.wrappedValue?.x ?? 0),
105108
availableDistance: geometry.size.width,
@@ -108,7 +111,7 @@ public struct RectangularPointSliderStyle<Track: View, Thumb: View>: PointSlider
108111
leadingOffset: self.thumbSize.width / 2,
109112
trailingOffset: self.thumbSize.width / 2
110113
)
111-
114+
112115
let computedValueY = valueFrom(
113116
distance: geometry.size.height - (gestureValue.location.y - (configuration.dragOffset.wrappedValue?.y ?? 0)),
114117
availableDistance: geometry.size.height,
@@ -117,23 +120,22 @@ public struct RectangularPointSliderStyle<Track: View, Thumb: View>: PointSlider
117120
leadingOffset: self.thumbSize.height / 2,
118121
trailingOffset: self.thumbSize.height / 2
119122
)
120-
123+
121124
configuration.x.wrappedValue = computedValueX
122125
configuration.y.wrappedValue = computedValueY
123-
configuration.onEditingChanged(true)
124126
}
125127
.onEnded { _ in
126128
configuration.dragOffset.wrappedValue = nil
127129
configuration.onEditingChanged(false)
128130
}
129131
)
130-
132+
131133
}
132134
.frame(width: geometry.size.width, height: geometry.size.height)
133135
}
134136
.frame(minWidth: self.thumbInteractiveSize.width, minHeight: self.thumbInteractiveSize.height)
135137
}
136-
138+
137139
public init(track: Track, thumb: Thumb, thumbSize: CGSize = CGSize(width: 27, height: 27), thumbInteractiveSize: CGSize = CGSize(width: 44, height: 44), options: PointSliderOptions = .defaultOptions) {
138140
self.track = track
139141
self.thumb = thumb
@@ -176,6 +178,6 @@ extension RectangularPointSliderStyle where Thumb == DefaultThumb, Track == Defa
176178
public struct DefaultRectangularPointTrack: View {
177179
public var body: some View {
178180
Rectangle()
179-
.foregroundColor(.accentColor)
181+
.foregroundColor(Color.accentColor)
180182
}
181183
}

Sources/Sliders/RangeSlider/Styles/Horizontal/HorizontalRangeSliderStyle.swift

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ public struct HorizontalRangeSliderStyle<Track: View, LowerThumb: View, UpperThu
77

88
let lowerThumbSize: CGSize
99
let upperThumbSize: CGSize
10-
10+
1111
let lowerThumbInteractiveSize: CGSize
1212
let upperThumbInteractiveSize: CGSize
13-
13+
1414
private let options: RangeSliderOptions
15-
15+
1616
let onSelectLower: () -> Void
1717
let onSelectUpper: () -> Void
1818

@@ -28,8 +28,8 @@ public struct HorizontalRangeSliderStyle<Track: View, LowerThumb: View, UpperThu
2828
upperLeadingOffset: self.lowerThumbSize.width + self.upperThumbSize.width / 2,
2929
upperTrailingOffset: self.upperThumbSize.width / 2
3030
))
31-
.accentColor(.accentColor)
32-
31+
.accentColor(Color.accentColor)
32+
3333
ZStack {
3434
self.lowerThumb
3535
.frame(width: self.lowerThumbSize.width, height: self.lowerThumbSize.height)
@@ -51,9 +51,10 @@ public struct HorizontalRangeSliderStyle<Track: View, LowerThumb: View, UpperThu
5151
.gesture(
5252
DragGesture()
5353
.onChanged { gestureValue in
54-
54+
configuration.onEditingChanged(true)
55+
5556
self.onSelectLower()
56-
57+
5758
if configuration.dragOffset.wrappedValue == nil {
5859
configuration.dragOffset.wrappedValue = gestureValue.startLocation.x - distanceFrom(
5960
value: configuration.range.wrappedValue.lowerBound,
@@ -63,7 +64,7 @@ public struct HorizontalRangeSliderStyle<Track: View, LowerThumb: View, UpperThu
6364
trailingOffset: self.lowerThumbSize.width / 2
6465
)
6566
}
66-
67+
6768
let computedLowerBound = valueFrom(
6869
distance: gestureValue.location.x - (configuration.dragOffset.wrappedValue ?? 0),
6970
availableDistance: geometry.size.width - self.upperThumbSize.width,
@@ -72,23 +73,21 @@ public struct HorizontalRangeSliderStyle<Track: View, LowerThumb: View, UpperThu
7273
leadingOffset: self.lowerThumbSize.width / 2,
7374
trailingOffset: self.lowerThumbSize.width / 2
7475
)
75-
76+
7677
if self.options.contains(.forceAdjacentValue) {
7778
let computedUpperBound = max(computedLowerBound, configuration.range.wrappedValue.upperBound)
7879
configuration.range.wrappedValue = computedLowerBound...computedUpperBound
7980
} else {
8081
let computedLowerBound = min(computedLowerBound, configuration.range.wrappedValue.upperBound)
8182
configuration.range.wrappedValue = computedLowerBound...configuration.range.wrappedValue.upperBound
8283
}
83-
84-
configuration.onEditingChanged(true)
8584
}
8685
.onEnded { _ in
8786
configuration.dragOffset.wrappedValue = nil
8887
configuration.onEditingChanged(false)
8988
}
9089
)
91-
90+
9291
ZStack {
9392
self.upperThumb
9493
.frame(width: self.upperThumbSize.width, height: self.upperThumbSize.height)
@@ -110,7 +109,8 @@ public struct HorizontalRangeSliderStyle<Track: View, LowerThumb: View, UpperThu
110109
.gesture(
111110
DragGesture()
112111
.onChanged { gestureValue in
113-
112+
configuration.onEditingChanged(true)
113+
114114
self.onSelectUpper()
115115

116116
if configuration.dragOffset.wrappedValue == nil {
@@ -122,7 +122,7 @@ public struct HorizontalRangeSliderStyle<Track: View, LowerThumb: View, UpperThu
122122
trailingOffset: self.upperThumbSize.width / 2
123123
)
124124
}
125-
125+
126126
let computedUpperBound = valueFrom(
127127
distance: gestureValue.location.x - (configuration.dragOffset.wrappedValue ?? 0),
128128
availableDistance: geometry.size.width,
@@ -131,7 +131,7 @@ public struct HorizontalRangeSliderStyle<Track: View, LowerThumb: View, UpperThu
131131
leadingOffset: self.lowerThumbSize.width + self.upperThumbSize.width / 2,
132132
trailingOffset: self.upperThumbSize.width / 2
133133
)
134-
134+
135135
if self.options.contains(.forceAdjacentValue) {
136136
let computedLowerBound = min(computedUpperBound, configuration.range.wrappedValue.lowerBound)
137137
configuration.range.wrappedValue = computedLowerBound...computedUpperBound
@@ -140,7 +140,6 @@ public struct HorizontalRangeSliderStyle<Track: View, LowerThumb: View, UpperThu
140140
configuration.range.wrappedValue = configuration.range.wrappedValue.lowerBound...computedUpperBound
141141
}
142142

143-
configuration.onEditingChanged(true)
144143
}
145144
.onEnded { _ in
146145
configuration.dragOffset.wrappedValue = nil
@@ -153,7 +152,7 @@ public struct HorizontalRangeSliderStyle<Track: View, LowerThumb: View, UpperThu
153152
}
154153
.frame(minHeight: max(self.lowerThumbInteractiveSize.height, self.upperThumbInteractiveSize.height))
155154
}
156-
155+
157156
public init(track: Track, lowerThumb: LowerThumb, upperThumb: UpperThumb, lowerThumbSize: CGSize = CGSize(width: 27, height: 27), upperThumbSize: CGSize = CGSize(width: 27, height: 27), lowerThumbInteractiveSize: CGSize = CGSize(width: 44, height: 44), upperThumbInteractiveSize: CGSize = CGSize(width: 44, height: 44), options: RangeSliderOptions = .defaultOptions,
158157
onSelectLower: @escaping () -> Void = {},
159158
onSelectUpper: @escaping () -> Void = {}) {

Sources/Sliders/RangeSlider/Styles/Vertical/VerticalRangeSliderStyle.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ public struct VerticalRangeSliderStyle<Track: View, LowerThumb: View, UpperThumb
77

88
let lowerThumbSize: CGSize
99
let upperThumbSize: CGSize
10-
10+
1111
let lowerThumbInteractiveSize: CGSize
1212
let upperThumbInteractiveSize: CGSize
13-
13+
1414
private let options: RangeSliderOptions
1515

1616
public func makeBody(configuration: Self.Configuration) -> some View {
@@ -25,8 +25,8 @@ public struct VerticalRangeSliderStyle<Track: View, LowerThumb: View, UpperThumb
2525
upperLeadingOffset: self.lowerThumbSize.height + self.upperThumbSize.height / 2,
2626
upperTrailingOffset: self.upperThumbSize.height / 2
2727
))
28-
.accentColor(.accentColor)
29-
28+
.accentColor(Color.accentColor)
29+
3030
ZStack {
3131
self.lowerThumb
3232
.frame(width: self.lowerThumbSize.width, height: self.lowerThumbSize.height)
@@ -45,6 +45,8 @@ public struct VerticalRangeSliderStyle<Track: View, LowerThumb: View, UpperThumb
4545
.gesture(
4646
DragGesture()
4747
.onChanged { gestureValue in
48+
configuration.onEditingChanged(true)
49+
4850
if configuration.dragOffset.wrappedValue == nil {
4951
configuration.dragOffset.wrappedValue = gestureValue.startLocation.y - (geometry.size.height - distanceFrom(
5052
value: configuration.range.wrappedValue.lowerBound,
@@ -54,7 +56,7 @@ public struct VerticalRangeSliderStyle<Track: View, LowerThumb: View, UpperThumb
5456
trailingOffset: self.lowerThumbSize.height / 2
5557
))
5658
}
57-
59+
5860
let computedLowerBound = valueFrom(
5961
distance: geometry.size.height - (gestureValue.location.y - (configuration.dragOffset.wrappedValue ?? 0)),
6062
availableDistance: geometry.size.height - self.upperThumbSize.height,
@@ -63,16 +65,14 @@ public struct VerticalRangeSliderStyle<Track: View, LowerThumb: View, UpperThumb
6365
leadingOffset: self.lowerThumbSize.height / 2,
6466
trailingOffset: self.lowerThumbSize.height / 2
6567
)
66-
68+
6769
if self.options.contains(.forceAdjacentValue) {
6870
let computedUpperBound = max(computedLowerBound, configuration.range.wrappedValue.upperBound)
6971
configuration.range.wrappedValue = computedLowerBound...computedUpperBound
7072
} else {
7173
let computedLowerBound = min(computedLowerBound, configuration.range.wrappedValue.upperBound)
7274
configuration.range.wrappedValue = computedLowerBound...configuration.range.wrappedValue.upperBound
7375
}
74-
75-
configuration.onEditingChanged(true)
7676
}
7777
.onEnded { _ in
7878
configuration.dragOffset.wrappedValue = nil
@@ -98,6 +98,8 @@ public struct VerticalRangeSliderStyle<Track: View, LowerThumb: View, UpperThumb
9898
.gesture(
9999
DragGesture()
100100
.onChanged { gestureValue in
101+
configuration.onEditingChanged(true)
102+
101103
if configuration.dragOffset.wrappedValue == nil {
102104
configuration.dragOffset.wrappedValue = gestureValue.startLocation.y - (geometry.size.height - distanceFrom(
103105
value: configuration.range.wrappedValue.upperBound,
@@ -107,7 +109,7 @@ public struct VerticalRangeSliderStyle<Track: View, LowerThumb: View, UpperThumb
107109
trailingOffset: self.upperThumbSize.height / 2
108110
))
109111
}
110-
112+
111113
let computedUpperBound = valueFrom(
112114
distance: geometry.size.height - (gestureValue.location.y - (configuration.dragOffset.wrappedValue ?? 0)),
113115
availableDistance: geometry.size.height,
@@ -116,29 +118,27 @@ public struct VerticalRangeSliderStyle<Track: View, LowerThumb: View, UpperThumb
116118
leadingOffset: self.lowerThumbSize.height + self.upperThumbSize.height / 2,
117119
trailingOffset: self.upperThumbSize.height / 2
118120
)
119-
121+
120122
if self.options.contains(.forceAdjacentValue) {
121123
let computedLowerBound = min(computedUpperBound, configuration.range.wrappedValue.lowerBound)
122124
configuration.range.wrappedValue = computedLowerBound...computedUpperBound
123125
} else {
124126
let computedUpperBound = max(computedUpperBound, configuration.range.wrappedValue.lowerBound)
125127
configuration.range.wrappedValue = configuration.range.wrappedValue.lowerBound...computedUpperBound
126128
}
127-
128-
configuration.onEditingChanged(true)
129129
}
130130
.onEnded { _ in
131131
configuration.dragOffset.wrappedValue = nil
132132
configuration.onEditingChanged(false)
133133
}
134134
)
135-
135+
136136
}
137137
.frame(width: geometry.size.width)
138138
}
139139
.frame(minWidth: max(self.lowerThumbInteractiveSize.width, self.upperThumbInteractiveSize.width))
140140
}
141-
141+
142142
public init(track: Track, lowerThumb: LowerThumb, upperThumb: UpperThumb, lowerThumbSize: CGSize = CGSize(width: 27, height: 27), upperThumbSize: CGSize = CGSize(width: 27, height: 27), lowerThumbInteractiveSize: CGSize = CGSize(width: 44, height: 44), upperThumbInteractiveSize: CGSize = CGSize(width: 44, height: 44), options: RangeSliderOptions = .defaultOptions) {
143143
self.track = track
144144
self.lowerThumb = lowerThumb

0 commit comments

Comments
 (0)