Skip to content
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

Implement previewImagePainter for the ImageColorPicker #61

Merged
merged 1 commit into from
Jun 22, 2024
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 @@ -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
Loading