Skip to content

Commit

Permalink
fix: Return early in rememberComposeBitmapDescriptor for invalid view…
Browse files Browse the repository at this point in the history
… size (#533)

* fix: Don't throw IllegalArgumentException when parent of marker composable is zero sized

* refactor: Extracted measure specification

* refactor: Changed exception wording
  • Loading branch information
ln-12 authored Jun 18, 2024
1 parent 603dd17 commit db97e65
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ public fun Marker(
/**
* Composable rendering the content passed as a marker.
*
* This composable must have a non-zero size in both dimensions
*
* @param keys unique keys representing the state of this Marker. Any changes to one of the key will
* trigger a rendering of the content composable and thus the rendering of an updated marker.
* @param state the [MarkerState] to be used to control or observe the marker
Expand All @@ -272,6 +274,9 @@ public fun Marker(
* @param onInfoWindowClose a lambda invoked when the marker's info window is closed
* @param onInfoWindowLongClick a lambda invoked when the marker's info window is long clicked
* @param content composable lambda expression used to customize the marker's content
*
* @throws IllegalStateException if the composable is measured to have a size of zero in either
* dimension
*/
@Composable
@GoogleMapComposable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ internal fun rememberComposeBitmapDescriptor(
}
}

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

private fun renderComposableToBitmapDescriptor(
parent: ViewGroup,
compositionContext: CompositionContext,
Expand All @@ -39,17 +41,23 @@ private fun renderComposableToBitmapDescriptor(
val composeView =
ComposeView(parent.context)
.apply {
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
)
setParentCompositionContext(compositionContext)
setContent(content)
}
.also(parent::addView)

composeView.draw(fakeCanvas)

composeView.measure(
View.MeasureSpec.makeMeasureSpec(parent.width, View.MeasureSpec.AT_MOST),
View.MeasureSpec.makeMeasureSpec(parent.height, View.MeasureSpec.AT_MOST),
)
composeView.measure(measureSpec, measureSpec)

if (composeView.measuredWidth == 0 || composeView.measuredHeight == 0) {
throw IllegalStateException("The ComposeView was measured to have a width or height of " +
"zero. Make sure that the content has a non-zero size.")
}

composeView.layout(0, 0, composeView.measuredWidth, composeView.measuredHeight)

Expand Down

0 comments on commit db97e65

Please sign in to comment.