Skip to content

Commit

Permalink
docs(Slider): Add some more kdoc comments
Browse files Browse the repository at this point in the history
  • Loading branch information
levinzonr committed Mar 19, 2024
1 parent 5b96b5d commit 0d870bb
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 25 deletions.
27 changes: 27 additions & 0 deletions slider/src/main/java/io/monstarlab/mosaic/slider/SliderColors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,41 @@ import androidx.compose.runtime.Immutable
import androidx.compose.runtime.Stable
import androidx.compose.ui.graphics.Color

/**
* Colors of the slider
*/
@Immutable
public class SliderColors(
/**
* Color of the active track representing the track to the start of the thumb
*/
public val activeTrackColor: Color,
/**
* Represents the color of the disabled range,
* where user is not able to put a thumb to
*/
public val disabledRangeTrackColor: Color = activeTrackColor,
/**
* Color of the Inactive track, to the end of the thumb
*/
public val inactiveTrackColor: Color = activeTrackColor.copy(alpha = 0.5f),
/**
* Active track color when the Slider is disabled and user can't interact with it
*/
public val disabledActiveTrackColor: Color = activeTrackColor.copy(alpha = 0.2f),
/**
* Color of the Inactive track when the Slider is disabled
*/
public val disabledInactiveTrackColor: Color = activeTrackColor.copy(alpha = 0.2f),
/**
* Color of the thumb
* only applied when the default thumb is used
*/
public val thumbColor: Color = activeTrackColor,
/**
* Color of the thumb when the Slider is disabled
* Only applied when the default thumb is used
*/
public val disabledThumbColor: Color = disabledRangeTrackColor,
) {
@Stable
Expand Down
56 changes: 31 additions & 25 deletions slider/src/main/java/io/monstarlab/mosaic/slider/SliderState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public class SliderState(
*
*/
public var value: Float
get() = valueState.coerceIntoDisabledRange()
get() = valueState
set(value) {
valueState = value.coerceIntoDisabledRange()
valueState = coerceValue(value)
}

/**
Expand Down Expand Up @@ -110,35 +110,18 @@ public class SliderState(
}
}

internal fun coerceValue(value: Float): Float {
return value
.coerceIn(range)
.coerceIntoDisabledRange()
}

private fun coerceRangeIntoFractions(
subrange: ClosedFloatingPointRange<Float>,
): ClosedFloatingPointRange<Float> {
if (subrange.isEmpty()) return subrange
val interpolatedRange = valueDistribution.interpolate(range)
val interpolatedSubrange = valueDistribution.interpolate(subrange)
return calcFraction(
interpolatedRange.start,
interpolatedRange.endInclusive,
interpolatedSubrange.start,
)..calcFraction(
interpolatedRange.start,
interpolatedRange.endInclusive,
interpolatedSubrange.endInclusive,
)
}

/**
* Scales offset in to the value that user should see
*/
private fun scaleToUserValue(offset: Float): Float {
val range = valueDistribution.interpolate(range)
val scaledUserValue = scale(0f, totalWidth, offset, range.start, range.endInclusive)
return coerceValue(valueDistribution.inverse(scaledUserValue))
}

/**
* Converts value of the user into the raw offset on the track
*/
private fun scaleToOffset(value: Float): Float {
val coerced = coerceValue(value)
val interpolatedRange = valueDistribution.interpolate(range)
Expand All @@ -152,6 +135,12 @@ public class SliderState(
)
}

internal fun coerceValue(value: Float): Float {
return value
.coerceIn(range)
.coerceIntoDisabledRange()
}

private fun Float.coerceIntoDisabledRange(): Float {
if (disabledRange.isEmpty()) return this
// check if disabled range is on the left or right
Expand All @@ -161,6 +150,23 @@ public class SliderState(
coerceAtMost(disabledRange.start)
}
}

private fun coerceRangeIntoFractions(
subrange: ClosedFloatingPointRange<Float>,
): ClosedFloatingPointRange<Float> {
if (subrange.isEmpty()) return subrange
val interpolatedRange = valueDistribution.interpolate(range)
val interpolatedSubrange = valueDistribution.interpolate(subrange)
return calcFraction(
interpolatedRange.start,
interpolatedRange.endInclusive,
interpolatedSubrange.start,
)..calcFraction(
interpolatedRange.start,
interpolatedRange.endInclusive,
interpolatedSubrange.endInclusive,
)
}
}

@Composable
Expand Down

0 comments on commit 0d870bb

Please sign in to comment.