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

Replace FakeImage with ColorImage. #2697

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion coil-compose/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ You can override the preview behaviour like so:

```kotlin
val previewHandler = AsyncImagePreviewHandler {
FakeImage(color = 0xFFFF0000) // Available in `io.coil-kt.coil3:coil-test`.
ColorImage(Color.Red.toArgb())
}

CompositionLocalProvider(LocalAsyncImagePreviewHandler provides previewHandler) {
Expand Down
15 changes: 15 additions & 0 deletions coil-core/api/android/coil-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ public final class coil3/BitmapImage : coil3/Image {
public fun toString ()Ljava/lang/String;
}

public final class coil3/ColorImage : coil3/Image {
public fun <init> ()V
public fun <init> (IIIJZ)V
public synthetic fun <init> (IIIJZILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun draw (Landroid/graphics/Canvas;)V
public fun equals (Ljava/lang/Object;)Z
public final fun getColor ()I
public fun getHeight ()I
public fun getShareable ()Z
public fun getSize ()J
public fun getWidth ()I
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public final class coil3/ComponentRegistry {
public fun <init> ()V
public synthetic fun <init> (Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
Expand Down
20 changes: 20 additions & 0 deletions coil-core/api/coil-core.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,26 @@ final class coil3/BitmapImage : coil3/Image { // coil3/BitmapImage|null[0]
final fun toString(): kotlin/String // coil3/BitmapImage.toString|toString(){}[0]
}

final class coil3/ColorImage : coil3/Image { // coil3/ColorImage|null[0]
constructor <init>(kotlin/Int = ..., kotlin/Int = ..., kotlin/Int = ..., kotlin/Long = ..., kotlin/Boolean = ...) // coil3/ColorImage.<init>|<init>(kotlin.Int;kotlin.Int;kotlin.Int;kotlin.Long;kotlin.Boolean){}[0]

final val color // coil3/ColorImage.color|{}color[0]
final fun <get-color>(): kotlin/Int // coil3/ColorImage.color.<get-color>|<get-color>(){}[0]
final val height // coil3/ColorImage.height|{}height[0]
final fun <get-height>(): kotlin/Int // coil3/ColorImage.height.<get-height>|<get-height>(){}[0]
final val shareable // coil3/ColorImage.shareable|{}shareable[0]
final fun <get-shareable>(): kotlin/Boolean // coil3/ColorImage.shareable.<get-shareable>|<get-shareable>(){}[0]
final val size // coil3/ColorImage.size|{}size[0]
final fun <get-size>(): kotlin/Long // coil3/ColorImage.size.<get-size>|<get-size>(){}[0]
final val width // coil3/ColorImage.width|{}width[0]
final fun <get-width>(): kotlin/Int // coil3/ColorImage.width.<get-width>|<get-width>(){}[0]

final fun draw(org.jetbrains.skia/Canvas) // coil3/ColorImage.draw|draw(org.jetbrains.skia.Canvas){}[0]
final fun equals(kotlin/Any?): kotlin/Boolean // coil3/ColorImage.equals|equals(kotlin.Any?){}[0]
final fun hashCode(): kotlin/Int // coil3/ColorImage.hashCode|hashCode(){}[0]
final fun toString(): kotlin/String // coil3/ColorImage.toString|toString(){}[0]
}

final class coil3/ComponentRegistry { // coil3/ComponentRegistry|null[0]
constructor <init>() // coil3/ComponentRegistry.<init>|<init>(){}[0]

Expand Down
15 changes: 15 additions & 0 deletions coil-core/api/jvm/coil-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ public final class coil3/BitmapImage : coil3/Image {
public fun toString ()Ljava/lang/String;
}

public final class coil3/ColorImage : coil3/Image {
public fun <init> ()V
public fun <init> (IIIJZ)V
public synthetic fun <init> (IIIJZILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun draw (Lorg/jetbrains/skia/Canvas;)V
public fun equals (Ljava/lang/Object;)Z
public final fun getColor ()I
public fun getHeight ()I
public fun getShareable ()Z
public fun getSize ()J
public fun getWidth ()I
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public final class coil3/ComponentRegistry {
public fun <init> ()V
public synthetic fun <init> (Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
Expand Down
30 changes: 30 additions & 0 deletions coil-core/src/androidMain/kotlin/coil3/ColorImage.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package coil3

import android.graphics.Paint
import coil3.annotation.ExperimentalCoilApi
import coil3.annotation.Poko

@ExperimentalCoilApi
@Poko
actual class ColorImage actual constructor(
actual val color: Int,
actual override val width: Int,
actual override val height: Int,
actual override val size: Long,
actual override val shareable: Boolean,
) : Image {
private var lazyPaint: Paint? = null

actual override fun draw(canvas: Canvas) {
val paint = lazyPaint ?: run {
Paint()
.apply { color = [email protected] }
.also { lazyPaint = it }
}
if (width >= 0 && height >= 0) {
canvas.drawRect(0f, 0f, width.toFloat(), height.toFloat(), paint)
} else {
canvas.drawPaint(paint)
}
}
}
3 changes: 0 additions & 3 deletions coil-core/src/androidMain/kotlin/coil3/Image.android.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ fun Image.toBitmap(
return createBitmap(width, height, config).applyCanvas(::draw)
}

/**
* An [Image] backed by an Android [Bitmap].
*/
@Poko
actual class BitmapImage internal constructor(
actual val bitmap: Bitmap,
Expand Down
32 changes: 32 additions & 0 deletions coil-core/src/commonMain/kotlin/coil3/ColorImage.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package coil3

import coil3.annotation.ExperimentalCoilApi

/**
* An image that draws a [color].
*
* By default the image has no intrinsic size and will fill its canvas. Set [width] and [height]
* to a positive value to draw a square with those dimensions.
*
* @param color The ARGB hex color to draw with the format `0xAARRGGBB.toInt()`.
* Tip: Use Compose's color class: `ColorImage(Color.Black.toArgb())`.
* @param width See [Image.width].
* @param height See [Image.height].
* @param size See [Image.size].
* @param shareable See [Image.shareable].
*/
@ExperimentalCoilApi
expect class ColorImage(
color: Int = 0xFF000000.toInt(),
width: Int = -1,
height: Int = -1,
size: Long = 0,
shareable: Boolean = true,
) : Image {
val color: Int
override val size: Long
override val width: Int
override val height: Int
override val shareable: Boolean
override fun draw(canvas: Canvas)
}
2 changes: 1 addition & 1 deletion coil-core/src/commonMain/kotlin/coil3/Image.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ interface Image {
}

/**
* A special implementation of [Image] that's backed by a [Bitmap].
* An [Image] that's backed by a [Bitmap].
*/
expect class BitmapImage : Image {
val bitmap: Bitmap
Expand Down
10 changes: 2 additions & 8 deletions coil-core/src/commonMain/kotlin/coil3/decode/BlackholeDecoder.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package coil3.decode

import coil3.Canvas
import coil3.ColorImage
import coil3.Image
import coil3.ImageLoader
import coil3.annotation.ExperimentalCoilApi
Expand Down Expand Up @@ -38,13 +38,7 @@ class BlackholeDecoder(
) = BlackholeDecoder(imageFactory)

companion object {
@JvmField val EMPTY_IMAGE = object : Image {
override val size get() = 0L
override val width get() = -1
override val height get() = -1
override val shareable get() = true
override fun draw(canvas: Canvas) { /* Draw nothing. */ }
}
@JvmField val EMPTY_IMAGE: Image = ColorImage(0x00000000)
}
}
}
36 changes: 36 additions & 0 deletions coil-core/src/nonAndroidMain/kotlin/coil3/ColorImage.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package coil3

import coil3.annotation.ExperimentalCoilApi
import coil3.annotation.Poko
import org.jetbrains.skia.Paint
import org.jetbrains.skia.Rect

@ExperimentalCoilApi
@Poko
actual class ColorImage actual constructor(
actual val color: Int,
actual override val width: Int,
actual override val height: Int,
actual override val size: Long,
actual override val shareable: Boolean,
) : Image {
private var lazyPaint: Paint? = null
private var lazyRect: Rect? = null

actual override fun draw(canvas: Canvas) {
val paint = lazyPaint ?: run {
Paint()
.apply { color = [email protected] }
.also { lazyPaint = it }
}
if (width >= 0 && height >= 0) {
val rect = lazyRect ?: run {
Rect.makeWH(width.toFloat(), height.toFloat())
.also { lazyRect = it }
}
canvas.drawRect(rect, paint)
} else {
canvas.drawPaint(paint)
}
}
}
3 changes: 0 additions & 3 deletions coil-core/src/nonAndroidMain/kotlin/coil3/Image.nonAndroid.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ fun Image.toBitmap(
return bitmap
}

/**
* An [Image] backed by a Skia [Bitmap].
*/
@Poko
actual class BitmapImage internal constructor(
actual val bitmap: Bitmap,
Expand Down
12 changes: 6 additions & 6 deletions coil-test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ testImplementation("io.coil-kt.coil3:coil-test:3.0.4")

```kotlin
val engine = FakeImageLoaderEngine.Builder()
.intercept("https://example.com/image.jpg", FakeImage(color = 0xF00)) // Red
.intercept({ it is String && it.endsWith("test.png") }, FakeImage(color = 0x0F0)) // Green
.default(FakeImage(color = 0x00F)) // Blue
.intercept("https://example.com/image.jpg", ColorImage(Color.Red.toArgb()))
.intercept({ it is String && it.endsWith("test.png") }, ColorImage(Color.Green.toArgb()))
.default(ColorImage(Color.Blue.toArgb()))
.build()
val imageLoader = ImageLoader.Builder(context)
.components { add(engine) }
Expand All @@ -31,9 +31,9 @@ class PaparazziTest {
@Before
fun before() {
val engine = FakeImageLoaderEngine.Builder()
.intercept("https://example.com/image.jpg", FakeImage(color = 0xF00)) // Red
.intercept({ it is String && it.endsWith("test.png") }, FakeImage(color = 0x0F0)) // Green
.default(FakeImage(color = 0x00F)) // Blue
.intercept("https://example.com/image.jpg", ColorImage(Color.Red.toArgb()))
.intercept({ it is String && it.endsWith("test.png") }, ColorImage(Color.Green.toArgb()))
.default(ColorImage(Color.Blue.toArgb()))
.build()
val imageLoader = ImageLoader.Builder(paparazzi.context)
.components { add(engine) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import coil3.Canvas
import coil3.Image
import coil3.annotation.Poko

@Deprecated(
message = "This use case is better fulfilled by ColorImage.",
replaceWith = ReplaceWith("ColorImage", "coil3.ColorImage"),
level = DeprecationLevel.WARNING,
)
@Poko
actual class FakeImage actual constructor(
actual override val width: Int,
Expand All @@ -21,7 +26,6 @@ actual class FakeImage actual constructor(
.apply { color = [email protected] }
.also { lazyPaint = it }
}

canvas.drawRect(0f, 0f, width.toFloat(), height.toFloat(), paint)
}
}
8 changes: 6 additions & 2 deletions coil-test/src/commonMain/kotlin/coil3/test/FakeImage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ package coil3.test

import coil3.Canvas
import coil3.Image
import coil3.test.internal.Black

/**
* A simple [Image] that draws a 100x100 black square by default.
*/
@Deprecated(
message = "This use case is better fulfilled by ColorImage.",
replaceWith = ReplaceWith("ColorImage", "coil3.ColorImage"),
level = DeprecationLevel.WARNING,
)
expect class FakeImage(
width: Int = 100,
height: Int = 100,
size: Long = 4L * width * height,
shareable: Boolean = true,
color: Int = Black,
color: Int = 0xFF000000.toInt(),
) : Image {
override val width: Int
override val height: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ fun FakeImageLoaderEngine(image: Image): FakeImageLoaderEngine {
*
* ```
* val engine = FakeImageLoaderEngine.Builder()
* .intercept("https://www.example.com/image.jpg", FakeImage())
* .intercept({ it is String && it.endsWith("test.png") }, FakeImage())
* .default(FakeImage(color = 0x0000FF))
* .intercept("https://www.example.com/image.jpg", ColorImage(Color.Red))
* .intercept({ it is String && it.endsWith("test.png") }, ColorImage(Color.Green))
* .default(ColorImage(Color.Blue))
* .build()
* val imageLoader = ImageLoader.Builder(context)
* .components { add(engine) }
Expand Down
3 changes: 0 additions & 3 deletions coil-test/src/commonMain/kotlin/coil3/test/internal/utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,3 @@ internal fun imageResultOf(
request = request,
dataSource = DataSource.MEMORY,
)

// Format: 0xALPHA_RED_GREEN_BLUE
internal const val Black = 0xFF_00_00_00.toInt()
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package coil3.test

import coil3.ColorImage
import coil3.ImageLoader
import coil3.decode.DataSource
import coil3.request.ErrorResult
Expand All @@ -18,7 +19,7 @@ class FakeImageLoaderEngineTest : RobolectricTest() {
@Test
fun extraData() = runTest {
val url = "https://www.example.com/image.jpg"
val image = FakeImage()
val image = ColorImage()
val engine = FakeImageLoaderEngine.Builder()
.intercept(url, image)
.build()
Expand All @@ -39,9 +40,9 @@ class FakeImageLoaderEngineTest : RobolectricTest() {
@Test
fun predicateData() = runTest {
val url = "https://www.example.com/image.jpg"
val image = FakeImage()
val image = ColorImage()
val engine = FakeImageLoaderEngine.Builder()
.intercept("different_string", FakeImage())
.intercept("different_string", ColorImage())
.intercept({ it is String && it == url }, image)
.build()
val imageLoader = ImageLoader.Builder(context)
Expand All @@ -61,9 +62,9 @@ class FakeImageLoaderEngineTest : RobolectricTest() {
@Test
fun defaultDrawable() = runTest {
val url = "https://www.example.com/image.jpg"
val image = FakeImage()
val image = ColorImage()
val engine = FakeImageLoaderEngine.Builder()
.intercept("different_string", FakeImage())
.intercept("different_string", ColorImage())
.default(image)
.build()
val imageLoader = ImageLoader.Builder(context)
Expand All @@ -84,7 +85,7 @@ class FakeImageLoaderEngineTest : RobolectricTest() {
fun optionalInterceptor() = runTest {
var currentIndex = -1
val url = "https://www.example.com/image.jpg"
val images = listOf(FakeImage(), FakeImage(), FakeImage())
val images = listOf(ColorImage(), ColorImage(), ColorImage())
fun testInterceptor(index: Int) = OptionalInterceptor { chain ->
if (currentIndex == index) {
SuccessResult(images[index], chain.request, DataSource.MEMORY)
Expand Down Expand Up @@ -119,7 +120,7 @@ class FakeImageLoaderEngineTest : RobolectricTest() {
fun requestTransformerEnforcesInvariants() = runTest {
val url = "https://www.example.com/image.jpg"
val engine = FakeImageLoaderEngine.Builder()
.intercept(url, FakeImage())
.intercept(url, ColorImage())
.requestTransformer { request ->
request.newBuilder()
.data(null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import coil3.annotation.Poko
import org.jetbrains.skia.Paint
import org.jetbrains.skia.Rect

@Deprecated(
message = "This use case is better fulfilled by ColorImage.",
replaceWith = ReplaceWith("ColorImage", "coil3.ColorImage"),
level = DeprecationLevel.WARNING,
)
@Poko
actual class FakeImage actual constructor(
actual override val width: Int,
Expand Down
Loading
Loading