Skip to content

Commit fab455d

Browse files
committed
Split SystemSlider in files
1 parent d405ed6 commit fab455d

File tree

5 files changed

+187
-80
lines changed

5 files changed

+187
-80
lines changed

Sources/CompactSlider/Treats/SystemSlider.swift renamed to Sources/CompactSlider/Treats/SystemSlider/SystemSlider.swift

Lines changed: 33 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -46,52 +46,14 @@ public struct SystemSlider<Value: BinaryFloatingPoint>: View {
4646
slider
4747
.compactSliderStyle(systemSliderStyle)
4848
.compactSliderBackground { configuration, _ in
49-
#if os(visionOS)
50-
Capsule()
51-
.fill(
52-
LinearGradient(
53-
colors: [.black.opacity(0.5), .black.opacity(0.1)],
54-
startPoint: configuration.type.isHorizontal ? .top : .leading,
55-
endPoint: configuration.type.isHorizontal ? .bottom : .trailing
56-
)
57-
)
58-
.background {
59-
Capsule()
60-
.strokeBorder(.white.opacity(0.2), lineWidth: 1)
61-
.padding(.top, 1)
62-
}
63-
#else
64-
Capsule().fill(
65-
Defaults.labelColor.opacity(colorScheme == .dark ? 0.15 : 0.07)
66-
)
67-
#endif
49+
SystemSliderBackgroundView(configuration: configuration)
6850
}
69-
.compactSliderProgress { _ in
70-
if #available(macOS 13.0, iOS 16.0, visionOS 1.0, watchOS 9.0, *) {
71-
Capsule().fill(Color.accentColor.gradient.opacity(0.8))
72-
} else {
73-
Capsule().fill(Color.accentColor.opacity(0.8))
74-
}
51+
.compactSliderProgress {
52+
SystemSliderProgressView(configuration: $0)
7553
}
7654
.compactSliderHandleStyle(handleStyle())
77-
.compactSliderHandle { configuration, handleStyle, _, _ in
78-
if systemSliderStyle.type.isScrollable {
79-
HandleView(configuration: configuration, style: handleStyle)
80-
} else {
81-
#if os(visionOS)
82-
handleView(configuration, handleStyle)
83-
.scaleEffect(visionOSHandleScaleEffect)
84-
.contentShape(.hoverEffect, Rectangle())
85-
.hoverEffect()
86-
.onChange(of: configuration.focusState.isFocused) { _, newValue in
87-
withAnimation(.bouncy(duration: 0.2, extraBounce: 0.25)) {
88-
visionOSHandleScaleEffect = newValue ? 0.5 : 0.8
89-
}
90-
}
91-
#else
92-
handleView(configuration, handleStyle)
93-
#endif
94-
}
55+
.compactSliderHandle { configuration, handleStyle, progress, _ in
56+
SystemSliderHandleView(configuration: configuration, handleStyle: handleStyle, progress: progress)
9557
}
9658
#if os(macOS)
9759
.frame(
@@ -137,19 +99,6 @@ public struct SystemSlider<Value: BinaryFloatingPoint>: View {
13799
return .circle(visibility: .always, progressAlignment: .inside, color: .white, radius: 13.5)
138100
#endif
139101
}
140-
141-
private func handleView(
142-
_ configuration: CompactSliderStyleConfiguration,
143-
_ handleStyle: HandleStyle
144-
) -> some View {
145-
#if os(macOS)
146-
HandleView(configuration: configuration, style: handleStyle)
147-
.shadow(radius: 1, y: 0.5)
148-
#else
149-
HandleView(configuration: configuration, style: handleStyle)
150-
.shadow(color: .black.opacity(0.25), radius: 4, y: 2)
151-
#endif
152-
}
153102
}
154103

155104
// MARK: - Constructors
@@ -216,30 +165,6 @@ extension SystemSlider {
216165
}
217166
}
218167

219-
// MARK: - System Style
220-
221-
/// A type of the "system" slider.
222-
public enum SystemSliderType {
223-
case horizontal(HorizontalAlignment)
224-
case vertical(VerticalAlignment)
225-
case scrollableHorizontal
226-
case scrollableVertical
227-
228-
/// A horizontal slider with the leading alignment.
229-
public static var horizontal: SystemSliderType { .horizontal(.leading) }
230-
/// A vertical slider with the bottom alignment.
231-
public static var vertical: SystemSliderType { .vertical(.bottom) }
232-
233-
var compactSliderType: CompactSliderType {
234-
switch self {
235-
case .horizontal(let alignment): .horizontal(alignment)
236-
case .vertical(let alignment): .vertical(alignment)
237-
case .scrollableHorizontal: .scrollableHorizontal
238-
case .scrollableVertical: .scrollableVertical
239-
}
240-
}
241-
}
242-
243168
// MARK: - Environment
244169

245170
struct SystemSliderStyleKey: EnvironmentKey {
@@ -269,3 +194,31 @@ extension View {
269194
)
270195
}
271196
}
197+
198+
#Preview {
199+
VStack(spacing: 20) {
200+
SystemSlider(value: .constant(0.2))
201+
SystemSlider(value: .constant(0.7), step: 0.1)
202+
203+
SystemSlider(value: .constant(0.2), step: 0.1)
204+
.systemSliderStyle(.scrollableHorizontal)
205+
206+
Divider()
207+
208+
HStack(spacing: 20) {
209+
Group {
210+
SystemSlider(value: .constant(0.2))
211+
SystemSlider(value: .constant(0.7), step: 0.1)
212+
}
213+
.systemSliderStyle(.vertical)
214+
215+
SystemSlider(value: .constant(0))
216+
.systemSliderStyle(.scrollableVertical)
217+
.compactSliderOptionsByAdding(.loopValues)
218+
}
219+
}
220+
.padding()
221+
#if os(macOS)
222+
.frame(width: 400, height: 400, alignment: .top)
223+
#endif
224+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// The MIT License (MIT)
2+
//
3+
// Copyright (c) 2025 Alexey Bukhtin (github.com/buh).
4+
//
5+
6+
import SwiftUI
7+
8+
/// A "system" slider background view.
9+
public struct SystemSliderBackgroundView: View {
10+
@Environment(\.colorScheme) var colorScheme
11+
let configuration: CompactSliderStyleConfiguration
12+
13+
/// Create a "system" slider background view.
14+
///
15+
/// - Parameter configuration: a configuration of the slider.
16+
public init(configuration: CompactSliderStyleConfiguration) {
17+
self.configuration = configuration
18+
}
19+
20+
public var body: some View {
21+
#if os(visionOS)
22+
Capsule()
23+
.fill(
24+
LinearGradient(
25+
colors: [.black.opacity(0.5), .black.opacity(0.1)],
26+
startPoint: configuration.type.isHorizontal ? .top : .leading,
27+
endPoint: configuration.type.isHorizontal ? .bottom : .trailing
28+
)
29+
)
30+
.background {
31+
Capsule()
32+
.strokeBorder(.white.opacity(0.2), lineWidth: 1)
33+
.padding(.top, 1)
34+
}
35+
#else
36+
Capsule().fill(
37+
Defaults.labelColor.opacity(colorScheme == .dark ? 0.15 : 0.07)
38+
)
39+
#endif
40+
}
41+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// The MIT License (MIT)
2+
//
3+
// Copyright (c) 2025 Alexey Bukhtin (github.com/buh).
4+
//
5+
6+
import SwiftUI
7+
8+
/// A "system" slider handle view.
9+
public struct SystemSliderHandleView: View {
10+
@Environment(\.systemSliderStyle) var systemSliderStyle
11+
let configuration: CompactSliderStyleConfiguration
12+
let handleStyle: HandleStyle
13+
let progress: Double
14+
15+
/// Create a "system" slider handle view.
16+
///
17+
/// - Parameters:
18+
/// - configuration: a configuration of the slider.
19+
/// - handleStyle: a style of the handle.
20+
/// - progress: a progress of the handle.
21+
public init(configuration: CompactSliderStyleConfiguration, handleStyle: HandleStyle, progress: Double) {
22+
self.configuration = configuration
23+
self.handleStyle = handleStyle
24+
self.progress = progress
25+
}
26+
27+
public var body: some View {
28+
if systemSliderStyle.type.isScrollable {
29+
HandleView(configuration: configuration, style: handleStyle)
30+
} else {
31+
#if os(visionOS)
32+
handleView(configuration, handleStyle)
33+
.scaleEffect(visionOSHandleScaleEffect)
34+
.contentShape(.hoverEffect, Rectangle())
35+
.hoverEffect()
36+
.onChange(of: configuration.focusState.isFocused) { _, newValue in
37+
withAnimation(.bouncy(duration: 0.2, extraBounce: 0.25)) {
38+
visionOSHandleScaleEffect = newValue ? 0.5 : 0.8
39+
}
40+
}
41+
#else
42+
handleView(configuration, handleStyle)
43+
#endif
44+
}
45+
}
46+
47+
private func handleView(
48+
_ configuration: CompactSliderStyleConfiguration,
49+
_ handleStyle: HandleStyle
50+
) -> some View {
51+
#if os(macOS)
52+
HandleView(configuration: configuration, style: handleStyle)
53+
.shadow(radius: 1, y: 0.5)
54+
#else
55+
HandleView(configuration: configuration, style: handleStyle)
56+
.shadow(color: .black.opacity(0.25), radius: 4, y: 2)
57+
#endif
58+
}
59+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// The MIT License (MIT)
2+
//
3+
// Copyright (c) 2025 Alexey Bukhtin (github.com/buh).
4+
//
5+
6+
import SwiftUI
7+
8+
/// A "system" slider progress view.
9+
public struct SystemSliderProgressView: View {
10+
let configuration: CompactSliderStyleConfiguration
11+
12+
/// Create a "system" slider progress view.
13+
///
14+
/// - Parameter configuration: a configuration of the slider.
15+
public init(configuration: CompactSliderStyleConfiguration) {
16+
self.configuration = configuration
17+
}
18+
19+
public var body: some View {
20+
if #available(macOS 13.0, iOS 16.0, visionOS 1.0, watchOS 9.0, *) {
21+
Capsule().fill(Color.accentColor.gradient.opacity(0.8))
22+
} else {
23+
Capsule().fill(Color.accentColor.opacity(0.8))
24+
}
25+
}
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// The MIT License (MIT)
2+
//
3+
// Copyright (c) 2025 Alexey Bukhtin (github.com/buh).
4+
//
5+
6+
import SwiftUI
7+
8+
/// A type of the "system" slider.
9+
public enum SystemSliderType {
10+
case horizontal(HorizontalAlignment)
11+
case vertical(VerticalAlignment)
12+
case scrollableHorizontal
13+
case scrollableVertical
14+
15+
/// A horizontal slider with the leading alignment.
16+
public static var horizontal: SystemSliderType { .horizontal(.leading) }
17+
/// A vertical slider with the bottom alignment.
18+
public static var vertical: SystemSliderType { .vertical(.bottom) }
19+
20+
var compactSliderType: CompactSliderType {
21+
switch self {
22+
case .horizontal(let alignment): .horizontal(alignment)
23+
case .vertical(let alignment): .vertical(alignment)
24+
case .scrollableHorizontal: .scrollableHorizontal
25+
case .scrollableVertical: .scrollableVertical
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)