Skip to content

Commit daa2a4f

Browse files
change slider max height constraint
1 parent bf95786 commit daa2a4f

File tree

3 files changed

+46
-53
lines changed

3 files changed

+46
-53
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ track with varying sizes, borders with solid or gradient colors. And Sliders wit
77
Composables like **Icon**.
88

99

10-
1110
<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%"/>
1211

1312
## ColorfulSlider
@@ -201,6 +200,7 @@ fun ColorfulIconSlider(
201200
Usage
202201

203202
Emojis are transparent by default in Compose, you might want to set non-transparent color for `Text`
203+
204204
```
205205
ColorfulIconSlider(
206206
value = progress,
@@ -244,13 +244,13 @@ ColorfulIconSlider(
244244
}
245245
```
246246

247-
248247
## Gradle Setup
249248

250249
To get a Git project into your build:
251250

252251
* Step 1. Add the JitPack repository to your build file Add it in your root build.gradle at the end
253252
of repositories:
253+
254254
```
255255
allprojects {
256256
repositories {

slider/src/main/java/com/smarttoolfactory/slider/ColorfulIconSlider.kt

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ package com.smarttoolfactory.slider
22

33
import androidx.compose.foundation.BorderStroke
44
import androidx.compose.foundation.Canvas
5-
import androidx.compose.foundation.layout.Box
6-
import androidx.compose.foundation.layout.fillMaxSize
7-
import androidx.compose.foundation.layout.offset
8-
import androidx.compose.foundation.layout.requiredSizeIn
5+
import androidx.compose.foundation.layout.*
96
import androidx.compose.runtime.Composable
107
import androidx.compose.runtime.mutableStateOf
118
import androidx.compose.runtime.remember
@@ -262,24 +259,30 @@ private fun IconSliderImpl(
262259
modifier: Modifier,
263260
) {
264261

265-
Box(
266-
// DefaultSliderConstraints constrains this Box with min width and max height
267-
modifier.then(DefaultSliderConstraints),
268-
contentAlignment = Alignment.CenterStart
269-
) {
262+
val trackStrokeWidth: Float
270263

271-
val trackStrokeWidth: Float
264+
var borderWidth = 0f
265+
val borderBrush: Brush? = borderStroke?.brush
266+
val thumbSize: Dp
272267

273-
var borderWidth = 0f
274-
val borderBrush: Brush? = borderStroke?.brush
268+
with(LocalDensity.current) {
269+
trackStrokeWidth = trackHeight.toPx()
270+
thumbSize = (2 * thumbRadius).toDp()
275271

276-
277-
with(LocalDensity.current) {
278-
trackStrokeWidth = trackHeight.toPx()
279-
if (borderStroke != null) {
280-
borderWidth = borderStroke.width.toPx()
281-
}
272+
if (borderStroke != null) {
273+
borderWidth = borderStroke.width.toPx()
282274
}
275+
}
276+
277+
Box(
278+
// Constraint max height of Slider to max of thumb or track or minimum touch 48.dp
279+
modifier.heightIn(
280+
max = trackHeight
281+
.coerceAtLeast(thumbSize)
282+
.coerceAtLeast(TrackHeight)
283+
),
284+
contentAlignment = Alignment.CenterStart
285+
) {
283286

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

312-
313315
/**
314316
* Draws active and if [drawInactiveTrack] is set to true inactive tracks on Canvas.
315317
* If inactive track is to be drawn it's drawn between start and end of canvas. Active track

slider/src/main/java/com/smarttoolfactory/slider/ColorfulSlider.kt

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,7 @@ fun ColorfulSlider(
169169
.minimumTouchTargetSize()
170170
.requiredSizeIn(
171171
minWidth = ThumbRadius * 2,
172-
minHeight = ThumbRadius * 2,
173-
maxHeight = 48.dp
172+
minHeight = ThumbRadius * 2
174173
),
175174
contentAlignment = Alignment.CenterStart
176175
) {
@@ -281,35 +280,36 @@ private fun SliderImpl(
281280
modifier: Modifier,
282281
) {
283282

284-
Box(
285-
// DefaultSliderConstraints constrains this Box with min width and max height
286-
modifier.then(DefaultSliderConstraints)
287-
) {
288-
289-
val trackStrokeWidth: Float
290-
val thumbSize: Dp
291-
292-
var borderWidth = 0f
293-
val borderBrush: Brush? = borderStroke?.brush
283+
val trackStrokeWidth: Float
284+
val thumbSize: Dp
294285

286+
var borderWidth = 0f
287+
val borderBrush: Brush? = borderStroke?.brush
295288

296-
with(LocalDensity.current) {
297-
trackStrokeWidth = trackHeight.toPx()
298-
thumbSize = (2 * thumbRadius).toDp()
299-
300-
if (borderStroke != null) {
301-
borderWidth = borderStroke.width.toPx()
302-
}
289+
with(LocalDensity.current) {
290+
trackStrokeWidth = trackHeight.toPx()
291+
thumbSize = (2 * thumbRadius).toDp()
303292

293+
if (borderStroke != null) {
294+
borderWidth = borderStroke.width.toPx()
304295
}
296+
}
297+
298+
Box(
299+
// Constraint max height of Slider to max of thumb or track or minimum touch 48.dp
300+
modifier
301+
.heightIn(
302+
max = trackHeight
303+
.coerceAtLeast(thumbSize)
304+
.coerceAtLeast(TrackHeight)
305+
)
306+
) {
305307

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

309311
Track(
310-
modifier = Modifier
311-
.align(Alignment.CenterStart)
312-
.fillMaxSize(),
312+
modifier = Modifier.fillMaxSize(),
313313
fraction = fraction,
314314
tickFractions = tickFractions,
315315
thumbRadius = thumbRadius,
@@ -499,15 +499,6 @@ internal fun stepsToTickFractions(steps: Int): List<Float> {
499499
return if (steps == 0) emptyList() else List(steps + 2) { it.toFloat() / (steps + 1) }
500500
}
501501

502-
// Internal to be referred to in tests
503502
internal val ThumbRadius = 10.dp
504-
505-
// Internal to be referred to in tests
506503
internal val TrackHeight = 4.dp
507-
private val SliderHeight = 48.dp
508-
private val SliderMinWidth = 144.dp
509-
510-
internal val DefaultSliderConstraints =
511-
Modifier
512-
.widthIn(min = SliderMinWidth)
513-
.heightIn(min = SliderHeight)
504+
internal val SliderHeight = 48.dp

0 commit comments

Comments
 (0)