8000 Fix Dragging Issue on HsvColorPicker, AlphaSlider, and BrightnessSlider by kitakkun · Pull Request #29 · skydoves/colorpicker-compose · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix Dragging Issue on HsvColorPicker, AlphaSlider, and BrightnessSlider #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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
Expand Down Expand Up @@ -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)
}
}
) {
Expand Down
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.toF 8000 loat()
)
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
}
) {
Expand Down
0