From 34f9dffac644de87c44af3ff51731f571405f943 Mon Sep 17 00:00:00 2001 From: kitakkun <48154936+kitakkun@users.noreply.github.com> Date: Tue, 4 Jul 2023 17:11:56 +0900 Subject: [PATCH] fix: dragging on BrightnessSlider doesn't work properly when parent is scrollable --- .../colorpicker/compose/BrightnessSlider.kt | 66 +++++++++++-------- 1 file changed, 40 insertions(+), 26 deletions(-) diff --git a/colorpicker-compose/src/main/kotlin/com/github/skydoves/colorpicker/compose/BrightnessSlider.kt b/colorpicker-compose/src/main/kotlin/com/github/skydoves/colorpicker/compose/BrightnessSlider.kt index e05366c..3d47917 100644 --- a/colorpicker-compose/src/main/kotlin/com/github/skydoves/colorpicker/compose/BrightnessSlider.kt +++ b/colorpicker-compose/src/main/kotlin/com/github/skydoves/colorpicker/compose/BrightnessSlider.kt @@ -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 @@ -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 @@ -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) } } ) {