Skip to content

Commit

Permalink
Merge pull request #61 from skydoves/preview/imagepicker
Browse files Browse the repository at this point in the history
Implement previewImagePainter for the ImageColorPicker
  • Loading branch information
skydoves authored Jun 22, 2024
2 parents ea5420b + 43f2641 commit b0b4272
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -70,6 +71,7 @@ fun ImageColorPickerScreen() {
hexCode = colorEnvelope.hexCode
textColor = colorEnvelope.color
},
previewImagePainter = painterResource(id = R.drawable.palettebar),
)

Spacer(modifier = Modifier.weight(5f))
Expand Down
2 changes: 1 addition & 1 deletion colorpicker-compose/api/colorpicker-compose.api
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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 ->
Expand Down

0 comments on commit b0b4272

Please sign in to comment.