Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,39 @@ class GoogleMapViewClusteringTests {
assertThat(marker.rotation).isEqualTo(180f)
}
}

@OptIn(MapsComposeExperimentalApi::class)
@Test
fun testClusterItemContentUsingRememberComposeBitmapDescriptorDoesNotCrash() {
val clusterManagerHolder = arrayOfNulls<ClusterManager<MyItem>>(1)
val items = listOf(MyItem(startingPosition, "Item", "Snippet", 0f))

// Regression test for https://github.com/googlemaps/android-maps-compose/issues/694:
// rememberComposeBitmapDescriptor used to throw "measured to have a width or height of
// zero" when called from content composed inside Clustering's clusterItemContent,
// because its parent (InvalidatingComposeView) hadn't been through an Android layout
// pass yet at that point.
val marker = initMapAndGetMarker(clusterManagerHolder) {
Clustering(
items = items,
clusterItemContent = {
rememberComposeBitmapDescriptor {
Surface(modifier = Modifier.size(20.dp)) {
Text("X")
}
}
Surface(modifier = Modifier.size(20.dp)) {
Text("X")
}
},
onClusterManager = { cm ->
clusterManagerHolder[0] = cm
}
)
}

composeTestRule.runOnUiThread {
assertThat(marker.isVisible).isTrue()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.maps.android.compose

import android.graphics.Canvas
import android.view.View
import android.view.ViewGroup
import androidx.compose.runtime.Composable
Expand Down Expand Up @@ -47,14 +48,24 @@ public fun rememberComposeBitmapDescriptor(
}

private val measureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
private val fakeCanvas = Canvas()

private fun renderComposableToBitmapDescriptor(
parent: ViewGroup,
compositionContext: CompositionContext,
content: @Composable () -> Unit,
): BitmapDescriptor {
// Host the throwaway rendering ComposeView on the window's root view rather than on `parent`
// directly. `parent` (LocalView.current) can itself be mid-attach with no Android layout pass
// performed on it yet -- e.g. when this is called from content composed inside a Clustering
// item, which is first composed while its own hosting view is still being attached to its
// parent. `setParentCompositionContext` keeps this composition correctly scoped to the
// surrounding composition regardless of which Android View it's physically parented under, so
// it's safe to use a different, already-laid-out ViewGroup as the Android host.
val host = parent.rootView as? ViewGroup ?: parent

val composeView =
ComposeView(parent.context)
ComposeView(host.context)
.apply {
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
Expand All @@ -63,7 +74,11 @@ private fun renderComposableToBitmapDescriptor(
setParentCompositionContext(compositionContext)
setContent(content)
}
.also(parent::addView)
.also(host::addView)

// AndroidComposeView triggers LayoutNode's layout phase in the View draw phase, so trigger a
// draw to an empty canvas to force that.
composeView.draw(fakeCanvas)

composeView.measure(measureSpec, measureSpec)

Expand All @@ -79,7 +94,7 @@ private fun renderComposableToBitmapDescriptor(

bitmap.applyCanvas { composeView.draw(this) }

parent.removeView(composeView)
host.removeView(composeView)

return BitmapDescriptorFactory.fromBitmap(bitmap)
}