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

[DrawablePainter] Fix reacting to intrinsic size changes #1124

Merged
merged 8 commits into from Jun 28, 2022
Expand Up @@ -58,21 +58,16 @@ private val MAIN_HANDLER by lazy(LazyThreadSafetyMode.NONE) {
class DrawablePainter(
val drawable: Drawable
) : Painter(), RememberObserver {
private var invalidateTick by mutableStateOf(0)
private var layoutTick by mutableStateOf(0)

private var lastIntrinsicSize: Size = Size.Unspecified
private var drawInvalidateTick by mutableStateOf(0)
private var drawableIntrinsicSize by mutableStateOf(Size.Unspecified)
chrisbanes marked this conversation as resolved.
Show resolved Hide resolved

private val callback: Drawable.Callback by lazy {
object : Drawable.Callback {
override fun invalidateDrawable(d: Drawable) {
// Update the tick so that we get re-drawn
invalidateTick++

if (lastIntrinsicSize != drawable.intrinsicSize) {
// If the intrinsic size has changed, update the layout tick
layoutTick++
}
drawInvalidateTick++
// Update our intrinsic size too
drawableIntrinsicSize = drawable.intrinsicSize
}

override fun scheduleDrawable(d: Drawable, what: Runnable, time: Long) {
Expand Down Expand Up @@ -128,17 +123,12 @@ class DrawablePainter(
return false
}

override val intrinsicSize: Size
get() {
// Reading this ensures that we re-layout when invalidateDrawable() is called
layoutTick
return drawable.intrinsicSize.also { lastIntrinsicSize = it }
}
override val intrinsicSize: Size get() = drawableIntrinsicSize

override fun DrawScope.onDraw() {
drawIntoCanvas { canvas ->
// Reading this ensures that we invalidate when invalidateDrawable() is called
invalidateTick
drawInvalidateTick

// Update the Drawable's bounds
drawable.setBounds(0, 0, size.width.roundToInt(), size.height.roundToInt())
Expand Down