Skip to content

Commit

Permalink
fix: dragging on BrightnessSlider doesn't work properly when parent i…
Browse files Browse the repository at this point in the history
…s scrollable
  • Loading branch information
kitakkun committed Jul 4, 2023
1 parent 5300117 commit 34f9dff
Showing 1 changed file with 40 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@

package com.github.skydoves.colorpicker.compose

import android.view.MotionEvent
import androidx.annotation.FloatRange
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.gestures.detectHorizontalDragGestures
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
Expand All @@ -40,7 +41,7 @@ import androidx.compose.ui.graphics.PaintingStyle
import androidx.compose.ui.graphics.TileMode
import androidx.compose.ui.graphics.asAndroidBitmap
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
import androidx.compose.ui.input.pointer.pointerInteropFilter
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Dp
Expand Down Expand Up @@ -121,31 +122,44 @@ public fun BrightnessSlider(
}
bitmapSize = size
}
.pointerInteropFilter { event ->
when (event.action) {
MotionEvent.ACTION_DOWN,
MotionEvent.ACTION_MOVE,
MotionEvent.ACTION_UP -> {
// calculate wheel position.
val wheelPoint = event.x
val position: Float = if (wheelImageBitmap == null) {
val point = wheelPoint.coerceIn(
minimumValue = 0f,
maximumValue = bitmapSize.width.toFloat()
)
point / bitmapSize.width
} else {
val point = wheelPoint.coerceIn(
minimumValue = 0f,
maximumValue = bitmapSize.width.toFloat()
)
point / bitmapSize.width
}
controller.setBrightness(position.coerceIn(0f, 1f), fromUser = true)
true
.pointerInput(Unit) {
detectTapGestures { offset ->
// calculate wheel position.
val wheelPoint = offset.x
val position: Float = if (wheelImageBitmap == null) {
val point = wheelPoint.coerceIn(
minimumValue = 0f,
maximumValue = bitmapSize.width.toFloat()
)
point / bitmapSize.width
} else {
val point = wheelPoint.coerceIn(
minimumValue = 0f,
maximumValue = bitmapSize.width.toFloat()
)
point / bitmapSize.width
}

else -> false
controller.setBrightness(position.coerceIn(0f, 1f), fromUser = true)
}
}
.pointerInput(Unit) {
detectHorizontalDragGestures { change, _ ->
// calculate wheel position.
val wheelPoint = change.position.x
val position: Float = if (wheelImageBitmap == null) {
val point = wheelPoint.coerceIn(
minimumValue = 0f,
maximumValue = bitmapSize.width.toFloat()
)
point / bitmapSize.width
} else {
val point = wheelPoint.coerceIn(
minimumValue = 0f,
maximumValue = bitmapSize.width.toFloat()
)
point / bitmapSize.width
}
controller.setBrightness(position.coerceIn(0f, 1f), fromUser = true)
}
}
) {
Expand Down

0 comments on commit 34f9dff

Please sign in to comment.