Status Update
Comments
cl...@google.com <cl...@google.com>
po...@google.com <po...@google.com> #2
Thanks for reporting this!
The intention here was for parentWidth
to be hidden and it will be fixed soon. I see the potential convenience of exposing parentWidth
and parentHeight
, but in general it leads to a not very clean data flow: it would mean that after telling the layout system my size I receive the size back although my initial calculation is still in scope. I am afraid this would bring more confusion than convenience longer term.
Just as a background, the reason we have parentWidth
and not parentHeight
is that it is needed to calculate horizontally mirrored positioning of children for layout direction RTL when placeRelative
is used.
ap...@google.com <ap...@google.com> #3
Branch: androidx-master-dev
commit 36b705d1dd148fc57e8089a94d485e0bfcd05d0f
Author: Mihai Popa <popam@google.com>
Date: Mon Sep 14 11:59:30 2020
Hide parentWidth and parentLD in PlacementScope
When writing a custom layout, parentWidth and parentLayoutDirection
should not be read from the placement block as they are available or
calculated in the measure block. This CL is making them protected in
PlacementScope. The CL also merges the PlacementScope implementations
used for modifiers and layout nodes, which is now possible after the
modules' merging.
Relnote: PlacementScope#parentWidth and PlacementScope#parentLayoutDirection can no longer be read from the placement block of a custom layout.
Fixes: 168295623
Test: tests in foundation-layout
Change-Id: Icc1ae00d774147c5fa7006c4bb408c99c7731690
M compose/ui/ui/api/current.txt
M compose/ui/ui/api/public_plus_experimental_current.txt
M compose/ui/ui/api/restricted_current.txt
M compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/MeasureScope.kt
M compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/Placeable.kt
M compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/node/DelegatingLayoutNodeWrapper.kt
M compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/node/LayoutNode.kt
M compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/node/OuterMeasurablePlaceable.kt
[Deleted User] <[Deleted User]> #4
I actually found a reason for exposing both parentWidth
and parentHeight
in PlacementScope
and that is modifiers. I wanted to write a modifier that places a composable using a percent which requires access to the parent's size. This currently does not seem possible using modifiers.
po...@google.com <po...@google.com> #5
Note that parentWidth
was referring to the size of the current modifier (the same one returned when calling layout
), not to the size of the parent.
In general there would be a way to extend the layout system to provide the info you are looking for (the size of our parent when placing our children). However, I do not think this would be a good idea because modifiers relying on the parent size would not be nicely composable. I can write a modifier that positions children at a percentage of the parent but what if before me there is a modifier that offsets me? The percentage will not work anymore for my content.
[Deleted User] <[Deleted User]> #6
Hmm. I see that as completely valid though. You might want to place it in the center but offset X a number of DP.
But I understand there might be other complexities. I was just surprised there was no way to write such a modifier today.
Description
PlacementScope
already has aparentWidth
which is convenient but it does not have aparentHeight
. For consistency it should probably have both or neither. I would prefer both as it would allow for something like this: