Alex Styl
04/27/2025, 3:25 AM@Composable
function for CMP
I needed a common way to access the window container size but it is not yet available on Android, so here is currentWindowContainerSize
until it LocalWindowInfo.current.containerSize
reaches common:
@Composable
fun App() {
val containerSize = currentWindowContainerSize()
if (containerSize.width >= 480.dp) {
TabletLayout()
} else {
PhoneLayout()
}
}
https://github.com/alexstyl/cmp-windowContainerSizeMichael Paus
04/27/2025, 7:12 AMAlex Styl
04/27/2025, 8:03 AMseb
04/27/2025, 1:12 PMseb
04/27/2025, 1:14 PMAlex Styl
04/27/2025, 1:43 PMWhy not using window size classes on AndroidYou mean why use the function instead of window size classes? because I needed a way to be able to calculate my own window size classes / breakpoints, that is not tied to a specific framework
I'm fairly sure the logic in the snippet above is "wrong"you are right. it's a random value i put for this example
seb
04/27/2025, 1:46 PMseb
04/27/2025, 1:47 PMAlex Styl
04/27/2025, 1:50 PMseb
04/27/2025, 1:50 PMAlex Styl
04/27/2025, 6:51 PMseb
04/27/2025, 6:56 PMAlex Styl
04/27/2025, 6:59 PMAlex Styl
04/27/2025, 6:59 PM// new
val sizeClass = WindowSizeClass.BREAKPOINTS_V1
.computeWindowSizeClass(widthDp, heightDp)
when {
sizeClass.isWidthAtLeastBreakpoint(WIDTH_DP_EXPANDED_LOWER_BOUND) -> {
doExpanded()
}
sizeClass.isWidthAtLeastBreakpoint(WIDTH_DP_MEDIUM_LOWER_BOUND) -> {
doMedium()
}
else -> {
doCompact()
}
}
I dont get what i am reading. looking into itAlex Styl
04/27/2025, 7:12 PMAlex Vanyo
04/28/2025, 3:37 AMLocalWindowInfo.current.containerSize
just became part of 1.8.0 on the Android side:
https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/WindowInfo#containerSize()Yang
04/28/2025, 5:45 AMAlex Vanyo
04/28/2025, 5:05 PMLocalWindowInfo.current.containerSize
’s implementation should match WindowMetricsCalculator.computeCurrentWindowMetrics
- are you seeing a case where it doesn’t?Yang
04/29/2025, 12:04 AMval screenWidth = LocalConfiguration.current.screenWidth.dp
after upgrading to 1.8:
Using Configuration.screenWidthDp instead of LocalWindowInfo.current.containerSize
is this a false positive?Alex Vanyo
04/29/2025, 12:10 AMLocalConfiguration.current.screenWidth.dp
.
Now there’s a better built-in way with LocalWindowInfo.current.containerSize
Yang
04/29/2025, 12:22 AMYang
05/02/2025, 5:18 AMLocalWindowInfo.current.containerSize
works as expected 👍Alex Vanyo
05/02/2025, 5:44 PMYang
05/02/2025, 5:57 PMscreenWidthDp.dp
directly to containerSize.width.dp
, forgetting that the unit has changed so it should be with(LocalDensity.current) { LocalWindowInfo.current.containerSize.width.toDp() }
instead 😅Yang
05/02/2025, 5:57 PMAlex Vanyo
05/02/2025, 5:58 PM