Skip to content

Commit 90e0f3b

Browse files
Merge pull request #47 from nisrulz/fix/slider-thumb-track-position-and-shadow
Fix: Slider thumb track position and shadow
2 parents e7b1f37 + 976c7c0 commit 90e0f3b

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

beforeafter/src/main/java/com/smarttoolfactory/beforeafter/DefaultOverlay.kt

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ import androidx.compose.foundation.shape.CircleShape
1313
import androidx.compose.material.Icon
1414
import androidx.compose.runtime.Composable
1515
import androidx.compose.runtime.Immutable
16+
import androidx.compose.runtime.derivedStateOf
17+
import androidx.compose.runtime.getValue
18+
import androidx.compose.runtime.remember
1619
import androidx.compose.ui.Alignment
1720
import androidx.compose.ui.Modifier
21+
import androidx.compose.ui.draw.clip
1822
import androidx.compose.ui.draw.shadow
1923
import androidx.compose.ui.geometry.Offset
2024
import androidx.compose.ui.graphics.Brush
@@ -56,6 +60,15 @@ internal fun DefaultOverlay(
5660
val thumbSize = overlayStyle.thumbSize
5761
val thumbPositionPercent = overlayStyle.thumbPositionPercent
5862

63+
val shadow by remember {
64+
derivedStateOf {
65+
if (thumbBackgroundColor == Color.Transparent) {
66+
0.dp
67+
} else {
68+
thumbElevation
69+
}
70+
}
71+
}
5972

6073
var thumbPosX = position.x
6174
var thumbPosY = position.y
@@ -72,17 +85,16 @@ internal fun DefaultOverlay(
7285
val horizontalOffset = imageWidthInPx / 2
7386
val verticalOffset = imageHeightInPx / 2
7487

88+
val thumbMinY = -verticalOffset + thumbRadius
89+
val thumbMaxY = verticalOffset - thumbRadius
90+
7591
linePosition = thumbPosX.coerceIn(0f, imageWidthInPx)
7692
thumbPosX -= horizontalOffset
7793

7894
thumbPosY = if (verticalThumbMove) {
79-
(thumbPosY - verticalOffset)
80-
.coerceIn(
81-
-verticalOffset + thumbRadius,
82-
verticalOffset - thumbRadius
83-
)
95+
(thumbPosY - verticalOffset).coerceIn(thumbMinY, thumbMaxY)
8496
} else {
85-
((imageHeightInPx * thumbPositionPercent / 100f - thumbRadius) - verticalOffset)
97+
(imageHeightInPx * thumbPositionPercent / 100f) - verticalOffset
8698
}
8799
}
88100

@@ -115,7 +127,8 @@ internal fun DefaultOverlay(
115127
.offset {
116128
IntOffset(thumbPosX.toInt(), thumbPosY.toInt())
117129
}
118-
.shadow(thumbElevation, thumbShape)
130+
.clip(thumbShape)
131+
.shadow(shadow)
119132
.background(thumbBackgroundColor)
120133
.size(thumbSize)
121134
.padding(4.dp)
@@ -150,5 +163,5 @@ class OverlayStyle(
150163
val thumbElevation: Dp = 2.dp,
151164
@DrawableRes val thumbResource: Int = R.drawable.baseline_swap_horiz_24,
152165
val thumbSize: Dp = 36.dp,
153-
@FloatRange(from = 0.0, to = 100.0) val thumbPositionPercent: Float = 85f,
166+
@FloatRange(from = 0.0, to = 100.0) val thumbPositionPercent: Float = 50f,
154167
)

0 commit comments

Comments
 (0)