Skip to content

Commit

Permalink
change slider max height constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
SmartToolFactory committed Apr 11, 2022
1 parent bf95786 commit daa2a4f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 53 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ track with varying sizes, borders with solid or gradient colors. And Sliders wit
Composables like **Icon**.



<img src="./screenshots/slider_icons.png" width="19%"/> <img src="./screenshots/slider_gradient.png" width="19%"/> <img src="./screenshots/slider_properties.png" width="19%"/> <img src="./screenshots/slider_labels.png" width="19%"/> <img src="./screenshots/slider_dimensions.png" width="19%"/>

## ColorfulSlider
Expand Down Expand Up @@ -201,6 +200,7 @@ fun ColorfulIconSlider(
Usage

Emojis are transparent by default in Compose, you might want to set non-transparent color for `Text`

```
ColorfulIconSlider(
value = progress,
Expand Down Expand Up @@ -244,13 +244,13 @@ ColorfulIconSlider(
}
```


## Gradle Setup

To get a Git project into your build:

* Step 1. Add the JitPack repository to your build file Add it in your root build.gradle at the end
of repositories:

```
allprojects {
repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package com.smarttoolfactory.slider

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.requiredSizeIn
import androidx.compose.foundation.layout.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand Down Expand Up @@ -262,24 +259,30 @@ private fun IconSliderImpl(
modifier: Modifier,
) {

Box(
// DefaultSliderConstraints constrains this Box with min width and max height
modifier.then(DefaultSliderConstraints),
contentAlignment = Alignment.CenterStart
) {
val trackStrokeWidth: Float

val trackStrokeWidth: Float
var borderWidth = 0f
val borderBrush: Brush? = borderStroke?.brush
val thumbSize: Dp

var borderWidth = 0f
val borderBrush: Brush? = borderStroke?.brush
with(LocalDensity.current) {
trackStrokeWidth = trackHeight.toPx()
thumbSize = (2 * thumbRadius).toDp()


with(LocalDensity.current) {
trackStrokeWidth = trackHeight.toPx()
if (borderStroke != null) {
borderWidth = borderStroke.width.toPx()
}
if (borderStroke != null) {
borderWidth = borderStroke.width.toPx()
}
}

Box(
// Constraint max height of Slider to max of thumb or track or minimum touch 48.dp
modifier.heightIn(
max = trackHeight
.coerceAtLeast(thumbSize)
.coerceAtLeast(TrackHeight)
),
contentAlignment = Alignment.CenterStart
) {

// Position that corresponds to center of this slider's thumb
val thumbCenterPos = (trackStart + (trackEnd - trackStart) * fraction)
Expand Down Expand Up @@ -309,7 +312,6 @@ private fun IconSliderImpl(
}
}


/**
* Draws active and if [drawInactiveTrack] is set to true inactive tracks on Canvas.
* If inactive track is to be drawn it's drawn between start and end of canvas. Active track
Expand Down
55 changes: 23 additions & 32 deletions slider/src/main/java/com/smarttoolfactory/slider/ColorfulSlider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@ fun ColorfulSlider(
.minimumTouchTargetSize()
.requiredSizeIn(
minWidth = ThumbRadius * 2,
minHeight = ThumbRadius * 2,
maxHeight = 48.dp
minHeight = ThumbRadius * 2
),
contentAlignment = Alignment.CenterStart
) {
Expand Down Expand Up @@ -281,35 +280,36 @@ private fun SliderImpl(
modifier: Modifier,
) {

Box(
// DefaultSliderConstraints constrains this Box with min width and max height
modifier.then(DefaultSliderConstraints)
) {

val trackStrokeWidth: Float
val thumbSize: Dp

var borderWidth = 0f
val borderBrush: Brush? = borderStroke?.brush
val trackStrokeWidth: Float
val thumbSize: Dp

var borderWidth = 0f
val borderBrush: Brush? = borderStroke?.brush

with(LocalDensity.current) {
trackStrokeWidth = trackHeight.toPx()
thumbSize = (2 * thumbRadius).toDp()

if (borderStroke != null) {
borderWidth = borderStroke.width.toPx()
}
with(LocalDensity.current) {
trackStrokeWidth = trackHeight.toPx()
thumbSize = (2 * thumbRadius).toDp()

if (borderStroke != null) {
borderWidth = borderStroke.width.toPx()
}
}

Box(
// Constraint max height of Slider to max of thumb or track or minimum touch 48.dp
modifier
.heightIn(
max = trackHeight
.coerceAtLeast(thumbSize)
.coerceAtLeast(TrackHeight)
)
) {

// Position that corresponds to center of this slider's thumb
val thumbCenterPos = (trackStart + (trackEnd - trackStart) * fraction)

Track(
modifier = Modifier
.align(Alignment.CenterStart)
.fillMaxSize(),
modifier = Modifier.fillMaxSize(),
fraction = fraction,
tickFractions = tickFractions,
thumbRadius = thumbRadius,
Expand Down Expand Up @@ -499,15 +499,6 @@ internal fun stepsToTickFractions(steps: Int): List<Float> {
return if (steps == 0) emptyList() else List(steps + 2) { it.toFloat() / (steps + 1) }
}

// Internal to be referred to in tests
internal val ThumbRadius = 10.dp

// Internal to be referred to in tests
internal val TrackHeight = 4.dp
private val SliderHeight = 48.dp
private val SliderMinWidth = 144.dp

internal val DefaultSliderConstraints =
Modifier
.widthIn(min = SliderMinWidth)
.heightIn(min = SliderHeight)
internal val SliderHeight = 48.dp

0 comments on commit daa2a4f

Please sign in to comment.