Skip to content

Commit a1f3eeb

Browse files
committed
Add isWheelScrolling for the focus state. Animated
1 parent 952e3ee commit a1f3eeb

File tree

6 files changed

+58
-23
lines changed

6 files changed

+58
-23
lines changed

Demo/Sources/Demo/Demo.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ struct CompactSliderDemo: View {
111111
private func baseHorizontalSliders() -> some View {
112112
Group {
113113
CompactSlider(value: $progress).compactSliderStyle(default: .horizontal(.leading))
114+
.compactSliderOnChange { print("CompactSlider onChange:", $0.focusState) }
114115
CompactSlider(value: $progress).compactSliderStyle(default: .horizontal(.center))
115116
CompactSlider(value: $progress).compactSliderStyle(default: .horizontal(.trailing))
116117

Sources/CompactSlider/CompactSlider+Dragging.swift

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ extension CompactSlider {
1111
startDragTime = CFAbsoluteTimeGetCurrent()
1212
}
1313

14-
if let animation = animations[.dragging] {
15-
withAnimation(animation) {
14+
if !isDragging {
15+
if let animation = animations[.dragging] {
16+
withAnimation(animation) {
17+
isDragging = true
18+
}
19+
} else {
1620
isDragging = true
1721
}
18-
} else {
19-
isDragging = true
2022
}
2123

2224
if startDragLocation == nil {
@@ -66,12 +68,14 @@ extension CompactSlider {
6668
)
6769
}
6870

69-
if let animation = animations[.dragging] {
70-
withAnimation(animation) {
71+
if isDragging {
72+
if let animation = animations[.dragging] {
73+
withAnimation(animation) {
74+
isDragging = false
75+
}
76+
} else {
7177
isDragging = false
7278
}
73-
} else {
74-
isDragging = false
7579
}
7680
}
7781

Sources/CompactSlider/CompactSlider+ScrollWheel.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,29 @@ extension CompactSlider {
1010
func scrollWheelOnChange(_ event: ScrollWheelEvent, size: CGSize, location: CGPoint) {
1111
guard isHovering else { return }
1212

13-
if !event.isEnded {
13+
if event.isEnded {
14+
defer {
15+
if isWheelScrolling {
16+
if let animation = animations[.wheelScrolling] {
17+
withAnimation(animation) {
18+
isWheelScrolling = false
19+
}
20+
} else {
21+
isWheelScrolling = false
22+
}
23+
}
24+
}
25+
} else {
26+
if !isWheelScrolling {
27+
if let animation = animations[.wheelScrolling] {
28+
withAnimation(animation) {
29+
isWheelScrolling = true
30+
}
31+
} else {
32+
isWheelScrolling = true
33+
}
34+
}
35+
1436
if style.type.isHorizontal, !event.isHorizontalDelta {
1537
return
1638
}

Sources/CompactSlider/CompactSlider.swift

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ public struct CompactSlider<Value: BinaryFloatingPoint, Point: CompactSliderPoin
151151

152152
@State var isHovering = false
153153
@State var isDragging = false
154+
@State var isWheelScrolling = false
154155
@State var startDragTime: CFAbsoluteTime?
155156
@State var startDragLocation: CGPoint?
156157
@State var scrollWheelEvent = ScrollWheelEvent.zero
@@ -180,24 +181,27 @@ public struct CompactSlider<Value: BinaryFloatingPoint, Point: CompactSliderPoin
180181
height: proxy.size.height - style.padding.vertical
181182
)
182183

183-
style.makeBody(
184-
configuration: CompactSliderStyleConfiguration(
184+
style.makeBody(configuration: {
185+
let configuration = CompactSliderStyleConfiguration(
185186
type: style.type,
186187
size: size,
187-
focusState: .init(isHovering: isHovering, isDragging: isDragging),
188+
focusState: .init(
189+
isHovering: isHovering,
190+
isDragging: isDragging,
191+
isWheelScrolling: isWheelScrolling
192+
),
188193
progress: progress,
189194
step: step,
190195
colorScheme: colorScheme
191196
)
192-
)
197+
198+
onChangeAction?(configuration)
199+
return configuration
200+
}())
193201
.dragGesture(
194202
options: options,
195-
onChanged: {
196-
dragGestureOnChange($0, size: size)
197-
},
198-
onEnded: {
199-
dragGestureOnEnded($0, size: size)
200-
}
203+
onChanged: { dragGestureOnChange($0, size: size) },
204+
onEnded: { dragGestureOnEnded($0, size: size) }
201205
)
202206
#if os(macOS)
203207
.onScrollWheel(isEnabled: options.contains(.scrollWheel)) {

Sources/CompactSlider/CompactSliderAnimations.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public enum CompactSliderAnimationEvent {
1010
case hovering
1111
case dragging
1212
case tapping
13+
case wheelScrolling
1314
}
1415

1516
// MARK: - Environment

Sources/CompactSlider/CompactSliderStyleConfiguration.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,19 @@ extension CompactSliderStyleConfiguration {
5757
public var isHovering: Bool
5858
/// True, when dragging the slider.
5959
public var isDragging: Bool
60+
/// True, when wheel-scrolling the slider.
61+
public var isWheelScrolling: Bool
6062
/// True, when dragging or hovering the slider.
61-
public var isFocused: Bool { isHovering || isDragging }
63+
public var isFocused: Bool { isHovering || isDragging || isWheelScrolling }
6264

6365
/// Create a focus state.
64-
init(isHovering: Bool, isDragging: Bool) {
66+
init(isHovering: Bool, isDragging: Bool, isWheelScrolling: Bool) {
6567
self.isHovering = isHovering
6668
self.isDragging = isDragging
69+
self.isWheelScrolling = isWheelScrolling
6770
}
6871

69-
static var none = FocusState(isHovering: false, isDragging: false)
72+
static var none = FocusState(isHovering: false, isDragging: false, isWheelScrolling: false)
7073
}
7174
}
7275

@@ -390,7 +393,7 @@ struct CompactSliderStyleConfigurationKey: EnvironmentKey {
390393
static var defaultValue: CompactSliderStyleConfiguration = CompactSliderStyleConfiguration(
391394
type: .horizontal(.leading),
392395
size: .zero,
393-
focusState: .init(isHovering: false, isDragging: false),
396+
focusState: .none,
394397
progress: Progress(),
395398
step: nil,
396399
colorScheme: .light

0 commit comments

Comments
 (0)