From 43f2641ee430cc2a4395fd0be6565f9733f3786d Mon Sep 17 00:00:00 2001 From: skydoves Date: Sat, 22 Jun 2024 10:49:49 +0900 Subject: [PATCH] Implement previewImagePainter for the ImageColorPicker --- .../screens/ImageColorPickerScreen.kt | 2 ++ colorpicker-compose/api/colorpicker-compose.api | 2 +- .../colorpicker/compose/ImageColorPicker.kt | 15 +++++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/src/main/kotlin/com/github/skydoves/colorpickercomposedemo/screens/ImageColorPickerScreen.kt b/app/src/main/kotlin/com/github/skydoves/colorpickercomposedemo/screens/ImageColorPickerScreen.kt index fbf4476..d9f7ad8 100644 --- a/app/src/main/kotlin/com/github/skydoves/colorpickercomposedemo/screens/ImageColorPickerScreen.kt +++ b/app/src/main/kotlin/com/github/skydoves/colorpickercomposedemo/screens/ImageColorPickerScreen.kt @@ -34,6 +34,7 @@ import androidx.compose.ui.draw.clip import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.ImageBitmap import androidx.compose.ui.res.imageResource +import androidx.compose.ui.res.painterResource import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp @@ -70,6 +71,7 @@ fun ImageColorPickerScreen() { hexCode = colorEnvelope.hexCode textColor = colorEnvelope.color }, + previewImagePainter = painterResource(id = R.drawable.palettebar), ) Spacer(modifier = Modifier.weight(5f)) diff --git a/colorpicker-compose/api/colorpicker-compose.api b/colorpicker-compose/api/colorpicker-compose.api index f236bfb..e080859 100644 --- a/colorpicker-compose/api/colorpicker-compose.api +++ b/colorpicker-compose/api/colorpicker-compose.api @@ -70,7 +70,7 @@ public final class com/github/skydoves/colorpicker/compose/HsvColorPickerKt { } public final class com/github/skydoves/colorpicker/compose/ImageColorPickerKt { - public static final fun ImageColorPicker (Landroidx/compose/ui/Modifier;Lcom/github/skydoves/colorpicker/compose/ColorPickerController;Landroidx/compose/ui/graphics/ImageBitmap;Landroidx/compose/ui/graphics/ImageBitmap;Lkotlin/jvm/functions/Function1;ZLcom/github/skydoves/colorpicker/compose/PaletteContentScale;Lkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;II)V + public static final fun ImageColorPicker (Landroidx/compose/ui/Modifier;Lcom/github/skydoves/colorpicker/compose/ColorPickerController;Landroidx/compose/ui/graphics/ImageBitmap;Landroidx/compose/ui/graphics/ImageBitmap;Lkotlin/jvm/functions/Function1;ZLcom/github/skydoves/colorpicker/compose/PaletteContentScale;Landroidx/compose/ui/graphics/painter/Painter;Lkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;II)V } public final class com/github/skydoves/colorpicker/compose/PaletteContentScale : java/lang/Enum { diff --git a/colorpicker-compose/src/main/kotlin/com/github/skydoves/colorpicker/compose/ImageColorPicker.kt b/colorpicker-compose/src/main/kotlin/com/github/skydoves/colorpicker/compose/ImageColorPicker.kt index 3976964..226ab67 100644 --- a/colorpicker-compose/src/main/kotlin/com/github/skydoves/colorpicker/compose/ImageColorPicker.kt +++ b/colorpicker-compose/src/main/kotlin/com/github/skydoves/colorpicker/compose/ImageColorPicker.kt @@ -21,6 +21,8 @@ import android.graphics.Matrix import android.graphics.RectF import android.view.MotionEvent import androidx.compose.foundation.Canvas +import androidx.compose.foundation.Image +import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.runtime.Composable import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.rememberCoroutineScope @@ -36,8 +38,10 @@ import androidx.compose.ui.graphics.TileMode import androidx.compose.ui.graphics.asAndroidBitmap import androidx.compose.ui.graphics.drawscope.DrawScope import androidx.compose.ui.graphics.drawscope.drawIntoCanvas +import androidx.compose.ui.graphics.painter.Painter import androidx.compose.ui.input.pointer.pointerInteropFilter import androidx.compose.ui.layout.onSizeChanged +import androidx.compose.ui.platform.LocalInspectionMode import androidx.compose.ui.unit.toSize import androidx.core.util.Pools import kotlinx.coroutines.Dispatchers @@ -54,6 +58,7 @@ import kotlinx.coroutines.launch * @param drawOnPosSelected to draw anything on the canvas when [ColorPickerController.selectedPoint] changes * @param drawDefaultWheelIndicator should the indicator be drawn on the canvas. Defaults to false if either [wheelImageBitmap] or [drawOnPosSelected] are not null. * @param paletteContentScale Represents a rule to apply to scale a source rectangle to be inscribed into a destination. + * @param previewImagePainter Display an image instead of the palette on the inspection preview mode on Android Studio. * @param onColorChanged Color changed listener. */ @Composable @@ -65,6 +70,7 @@ public fun ImageColorPicker( drawOnPosSelected: (DrawScope.() -> Unit)? = null, drawDefaultWheelIndicator: Boolean = wheelImageBitmap == null && drawOnPosSelected == null, paletteContentScale: PaletteContentScale = PaletteContentScale.FIT, + previewImagePainter: Painter? = null, onColorChanged: ((colorEnvelope: ColorEnvelope) -> Unit)? = null, ) { val coroutineScope = rememberCoroutineScope() @@ -85,6 +91,15 @@ public fun ImageColorPicker( } } + if (LocalInspectionMode.current && previewImagePainter != null) { + Image( + modifier = modifier.fillMaxSize(), + painter = previewImagePainter, + contentDescription = null, + ) + return + } + Canvas( modifier = modifier .onSizeChanged { size ->