Skip to content

Replace stub intrinsic measurement samples with working implementations - #1218

Open
hamen wants to merge 2 commits into
android:mainfrom
hamen:intrinsic-measurements-samples
Open

hamen wants to merge 2 commits into
android:mainfrom
hamen:intrinsic-measurements-samples

Conversation

@hamen

@hamen hamen commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

Code snippets for:

Replaces the // Measure and layout here, // Logic here and TODO() stubs in android_compose_intrinsics_custom_layout and android_compose_intrinsics_custom_modifier with working implementations. Today both bodies throw NotImplementedError inside excluded blocks and the rendered page shows only stub comments. The guide page is being updated separately to explain both overrides.

  • android_compose_intrinsics_custom_layout: MyCustomComposable is implemented as a basic column that relaxes minHeight to 0, measures each child, reports the widest child's width (or 0 when empty) and the sum of the heights coerced into the incoming constraints, and places the children vertically with placeRelative. It overrides minIntrinsicHeight(width) instead of minIntrinsicWidth(height): for a column where each child gets the full width, the minimum intrinsic height is the sum of the children's minIntrinsicHeight(width).
  • android_compose_intrinsics_custom_modifier: Modifier.myCustomModifier(padding: Dp = 8.dp) adds fixed padding on all sides. measure converts padding to pixels, measures the child with constraints.offset(-horizontal, -vertical), reports the child size plus the horizontal and vertical padding coerced into constraints, and places the child at (paddingPx, paddingPx). minIntrinsicWidth(height) subtracts the vertical padding from height (keeping Constraints.Infinity as Constraints.Infinity and clamping finite values to at least 0), queries measurable.minIntrinsicWidth(childHeight), and adds the horizontal padding.
  • IntrinsicsCustomLayoutPreview: a private @Preview composable outside the snippet regions that calls IntrinsicsSnippet3.MyCustomComposable with Modifier.height(IntrinsicSize.Min) and two Text children, applying Modifier.width(IntrinsicSize.Min).myCustomModifier(padding = 8.dp) to the second child.
Region Page section Change Formatter changes
android_compose_intrinsics_custom_layout Intrinsics in your custom layouts Replace measure and minIntrinsicWidth TODO() stubs with a working column measure and minIntrinsicHeight(width) override (46 rendered lines vs 23 before) 0 lines changed by spotlessApply
android_compose_intrinsics_custom_modifier Intrinsics in your custom layouts Replace /* ... */ parameter stub and measure / minIntrinsicWidth TODO() stubs with padding: Dp = 8.dp and working implementations (35 rendered lines vs 22 before) 0 lines changed by spotlessApply

The two existing TwoTexts regions (android_compose_intrinsics_twotexts and android_compose_intrinsics_twotexts2) in IntrinsicSnippets.kt are unchanged. Every block on the page already comes from this file; nothing stays hardcoded.

@hamen
hamen requested a review from a team as a code owner October 23, 2026 15:47
@hamen
hamen requested a review from prateekbatra-g October 23, 2026 15:47
@hamen

hamen commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

cc @riggaroo

val childHeight = if (height == Constraints.Infinity) {
Constraints.Infinity
} else {
(height - paddingPx * 2).coerceAtLeast(0)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest replacing paddingPx * 2 with variables horizontal and vertical to make it easier for understanding

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 077f5b2. Added horizontal and vertical in minIntrinsicWidth to match measure.

} else {
(height - paddingPx * 2).coerceAtLeast(0)
}
return measurable.minIntrinsicWidth(childHeight) + paddingPx * 2

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 077f5b2 (+ horizontal).

@hamen

hamen commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

@dturner if you get a chance: the last round of feedback is in 077f5b2, which pulls horizontal and vertical out of minIntrinsicWidth.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants