Skip to content

Commit

Permalink
Add threshold to ProminentCompactSliderStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
buh committed Apr 24, 2024
1 parent 31a4db8 commit abe4d1d
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions Sources/CompactSlider/ProminentCompactSliderStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public struct ProminentCompactSliderStyle: CompactSliderStyle {
public let upperColor: Color
/// Flag which allows the specified colours (`lowerColor`, `upperColor`) to be used for the gradient background.
public var useGradientBackground: Bool = false
/// The threshold to switch from `lowerColor` to `upperColor`.
public var threshold: CGFloat = 0.5

public func makeBody(configuration: Configuration) -> some View {
configuration.label
Expand All @@ -36,8 +38,9 @@ public struct ProminentCompactSliderStyle: CompactSliderStyle {
)
.accentColor(
configuration.isRangeValues
? abs(configuration.upperProgress - 0.5) > abs(configuration.lowerProgress - 0.5) ? upperColor : lowerColor
: configuration.lowerProgress > 0.5 ? upperColor : lowerColor
? (abs(configuration.upperProgress - threshold) > abs(configuration.lowerProgress - threshold)
? upperColor : lowerColor)
: configuration.lowerProgress > threshold ? upperColor : lowerColor
)
.clipShape(RoundedRectangle(cornerRadius: .cornerRadius))
}
Expand All @@ -48,12 +51,14 @@ public extension CompactSliderStyle where Self == ProminentCompactSliderStyle {
static func prominent(
lowerColor: Color,
upperColor: Color,
useGradientBackground: Bool = false
useGradientBackground: Bool = false,
threshold: CGFloat = 0.5
) -> Self {
ProminentCompactSliderStyle(
lowerColor: lowerColor,
upperColor: upperColor,
useGradientBackground: useGradientBackground
useGradientBackground: useGradientBackground,
threshold: threshold
)
}
}

0 comments on commit abe4d1d

Please sign in to comment.