From 11653cb396d97b70b41adbd971f5da27398ce2d5 Mon Sep 17 00:00:00 2001 From: kitakkun <48154936+kitakkun@users.noreply.github.com> Date: Tue, 4 Jul 2023 16:44:02 +0900 Subject: [PATCH 1/3] fix: HsvColorPicker is not draggable when parent is scrollable --- .../colorpicker/compose/HsvColorPicker.kt | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/colorpicker-compose/src/main/kotlin/com/github/skydoves/colorpicker/compose/HsvColorPicker.kt b/colorpicker-compose/src/main/kotlin/com/github/skydoves/colorpicker/compose/HsvColorPicker.kt index a853f65..7f77f54 100644 --- a/colorpicker-compose/src/main/kotlin/com/github/skydoves/colorpicker/compose/HsvColorPicker.kt +++ b/colorpicker-compose/src/main/kotlin/com/github/skydoves/colorpicker/compose/HsvColorPicker.kt @@ -18,8 +18,9 @@ package com.github.skydoves.colorpicker.compose import android.graphics.Matrix import android.graphics.RectF -import android.view.MotionEvent import androidx.compose.foundation.Canvas +import androidx.compose.foundation.gestures.detectDragGestures +import androidx.compose.foundation.gestures.detectTapGestures import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.runtime.Composable import androidx.compose.runtime.DisposableEffect @@ -37,7 +38,7 @@ import androidx.compose.ui.graphics.Paint import androidx.compose.ui.graphics.asAndroidBitmap import androidx.compose.ui.graphics.drawscope.drawIntoCanvas import androidx.compose.ui.graphics.nativeCanvas -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.LocalContext import kotlinx.coroutines.Dispatchers @@ -133,16 +134,14 @@ public fun HsvColorPicker( controller.imageBitmapMatrix.value = shaderMatrix } } - .pointerInteropFilter { event -> - when (event.action) { - MotionEvent.ACTION_DOWN, - MotionEvent.ACTION_MOVE, - MotionEvent.ACTION_UP -> { - controller.selectByCoordinate(event.x, event.y, true) - true - } - - else -> false + .pointerInput(Unit) { + detectTapGestures { offset -> + controller.selectByCoordinate(offset.x, offset.y, true) + } + } + .pointerInput(Unit) { + detectDragGestures { change, _ -> + controller.selectByCoordinate(change.position.x, change.position.y, true) } } ) { From 5300117500e96382127b9848e74bb0b3cf4b6296 Mon Sep 17 00:00:00 2001 From: kitakkun <48154936+kitakkun@users.noreply.github.com> Date: Tue, 4 Jul 2023 17:11:01 +0900 Subject: [PATCH 2/3] fix: dragging on AlphaSlider doesn't work properly when parent is scrollable --- .../colorpicker/compose/AlphaSlider.kt | 64 +++++++++++-------- 1 file changed, 38 insertions(+), 26 deletions(-) diff --git a/colorpicker-compose/src/main/kotlin/com/github/skydoves/colorpicker/compose/AlphaSlider.kt b/colorpicker-compose/src/main/kotlin/com/github/skydoves/colorpicker/compose/AlphaSlider.kt index 10f5149..28b7ef0 100644 --- a/colorpicker-compose/src/main/kotlin/com/github/skydoves/colorpicker/compose/AlphaSlider.kt +++ b/colorpicker-compose/src/main/kotlin/com/github/skydoves/colorpicker/compose/AlphaSlider.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 @@ -37,7 +38,7 @@ import androidx.compose.ui.graphics.TileMode import androidx.compose.ui.graphics.asAndroidBitmap import androidx.compose.ui.graphics.drawscope.drawIntoCanvas import androidx.compose.ui.graphics.nativeCanvas -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 @@ -132,31 +133,42 @@ public fun AlphaSlider( } 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.setAlpha(position.coerceIn(0f, 1f), fromUser = true) - true + .pointerInput(Unit) { + detectHorizontalDragGestures { change, _ -> + 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 } - - else -> false + controller.setAlpha(position.coerceIn(0f, 1f), fromUser = true) + } + } + .pointerInput(Unit) { + detectTapGestures { offset -> + 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 + } + controller.setAlpha(position.coerceIn(0f, 1f), fromUser = true) } } ) { 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 3/3] 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) } } ) {