Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement custom thumbOffset #73

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ public struct HorizontalValueSliderStyle<Track: View, Thumb: View>: ValueSliderS
private let thumbSize: CGSize
private let thumbInteractiveSize: CGSize
private let options: ValueSliderOptions
private let thumbOffsets: CGFloat?

public func makeBody(configuration: Self.Configuration) -> some View {
let track = self.track
.environment(\.trackValue, configuration.value.wrappedValue)
.environment(\.valueTrackConfiguration, ValueTrackConfiguration(
bounds: configuration.bounds,
leadingOffset: self.thumbSize.width / 2,
trailingOffset: self.thumbSize.width / 2)
leadingOffset: thumbOffsets ?? self.thumbSize.width / 2,
trailingOffset: thumbOffsets ?? self.thumbSize.width / 2)
)
.accentColor(Color.accentColor)

Expand All @@ -29,8 +30,8 @@ public struct HorizontalValueSliderStyle<Track: View, Thumb: View>: ValueSliderS
availableDistance: geometry.size.width,
bounds: configuration.bounds,
step: configuration.step,
leadingOffset: self.thumbSize.width / 2,
trailingOffset: self.thumbSize.width / 2
leadingOffset: thumbOffsets ?? self.thumbSize.width / 2,
trailingOffset: thumbOffsets ?? self.thumbSize.width / 2
)
configuration.value.wrappedValue = computedValue
}
Expand All @@ -52,8 +53,8 @@ public struct HorizontalValueSliderStyle<Track: View, Thumb: View>: ValueSliderS
value: configuration.value.wrappedValue,
availableDistance: geometry.size.width,
bounds: configuration.bounds,
leadingOffset: self.thumbSize.width / 2,
trailingOffset: self.thumbSize.width / 2
leadingOffset: thumbOffsets ?? self.thumbSize.width / 2,
trailingOffset: thumbOffsets ?? self.thumbSize.width / 2
),
y: geometry.size.height / 2
)
Expand All @@ -67,8 +68,8 @@ public struct HorizontalValueSliderStyle<Track: View, Thumb: View>: ValueSliderS
value: configuration.value.wrappedValue,
availableDistance: geometry.size.width,
bounds: configuration.bounds,
leadingOffset: self.thumbSize.width / 2,
trailingOffset: self.thumbSize.width / 2
leadingOffset: thumbOffsets ?? self.thumbSize.width / 2,
trailingOffset: thumbOffsets ?? self.thumbSize.width / 2
)
}

Expand All @@ -77,8 +78,8 @@ public struct HorizontalValueSliderStyle<Track: View, Thumb: View>: ValueSliderS
availableDistance: geometry.size.width,
bounds: configuration.bounds,
step: configuration.step,
leadingOffset: self.thumbSize.width / 2,
trailingOffset: self.thumbSize.width / 2
leadingOffset: thumbOffsets ?? self.thumbSize.width / 2,
trailingOffset: thumbOffsets ?? self.thumbSize.width / 2
)

configuration.value.wrappedValue = computedValue
Expand All @@ -94,42 +95,62 @@ public struct HorizontalValueSliderStyle<Track: View, Thumb: View>: ValueSliderS
.frame(minHeight: self.thumbInteractiveSize.height)
}

public init(track: Track, thumb: Thumb, thumbSize: CGSize = CGSize(width: 27, height: 27), thumbInteractiveSize: CGSize = CGSize(width: 44, height: 44), options: ValueSliderOptions = .defaultOptions) {
public init(track: Track,
thumb: Thumb,
thumbSize: CGSize = CGSize(width: 27, height: 27),
thumbInteractiveSize: CGSize = CGSize(width: 44, height: 44),
options: ValueSliderOptions = .defaultOptions,
thumbOffsets: CGFloat? = nil) {
self.track = track
self.thumb = thumb
self.thumbSize = thumbSize
self.thumbInteractiveSize = thumbInteractiveSize
self.options = options
self.thumbOffsets = thumbOffsets
}
}

extension HorizontalValueSliderStyle where Track == DefaultHorizontalValueTrack {
public init(thumb: Thumb, thumbSize: CGSize = CGSize(width: 27, height: 27), thumbInteractiveSize: CGSize = CGSize(width: 44, height: 44), options: ValueSliderOptions = .defaultOptions) {
public init(thumb: Thumb,
thumbSize: CGSize = CGSize(width: 27, height: 27),
thumbInteractiveSize: CGSize = CGSize(width: 44, height: 44),
options: ValueSliderOptions = .defaultOptions,
thumbOffsets: CGFloat? = nil) {
self.track = DefaultHorizontalValueTrack()
self.thumb = thumb
self.thumbSize = thumbSize
self.thumbInteractiveSize = thumbInteractiveSize
self.options = options
self.thumbOffsets = thumbOffsets
}
}

extension HorizontalValueSliderStyle where Thumb == DefaultThumb {
public init(track: Track, thumbSize: CGSize = CGSize(width: 27, height: 27), thumbInteractiveSize: CGSize = CGSize(width: 44, height: 44), options: ValueSliderOptions = .defaultOptions) {
public init(track: Track,
thumbSize: CGSize = CGSize(width: 27, height: 27),
thumbInteractiveSize: CGSize = CGSize(width: 44, height: 44),
options: ValueSliderOptions = .defaultOptions,
thumbOffsets: CGFloat? = nil) {
self.track = track
self.thumb = DefaultThumb()
self.thumbSize = thumbSize
self.thumbInteractiveSize = thumbInteractiveSize
self.options = options
self.thumbOffsets = thumbOffsets
}
}

extension HorizontalValueSliderStyle where Thumb == DefaultThumb, Track == DefaultHorizontalValueTrack {
public init(thumbSize: CGSize = CGSize(width: 27, height: 27), thumbInteractiveSize: CGSize = CGSize(width: 44, height: 44), options: ValueSliderOptions = .defaultOptions) {
public init(thumbSize: CGSize = CGSize(width: 27, height: 27),
thumbInteractiveSize: CGSize = CGSize(width: 44, height: 44),
options: ValueSliderOptions = .defaultOptions,
thumbOffsets: CGFloat? = nil) {
self.track = DefaultHorizontalValueTrack()
self.thumb = DefaultThumb()
self.thumbSize = thumbSize
self.thumbInteractiveSize = thumbInteractiveSize
self.options = options
self.thumbOffsets = thumbOffsets
}
}

Expand Down