Hello, will the parent size react to change in a c...
# doodle
c
Hello, will the parent size react to change in a child's size? Meaning will the parent re-calculate its size based on new change?
n
The parent will be asked to do layout if it has a
Layout
installed and that
Layout
indicates it needs updating from that child’s size change. The parent’s
layout
is then free to recalculate the parent’s size. Otherwise, the parent won’t automatically update its size on such a change.
c
We have the following hierarchy:
display
->
container(Size: display size)
->
container(Size: max with of buttons, sum of 3button's height
->
3 buttons
if i change one of the button's height or width (making it bigger than parent) parent doesn't recalculate because the parent's layout is:
Copy code
view.bounds = Rectangle(0.0, y, view.width, view.height)
instead of:
Copy code
view.bounds = Rectangle(0.0,y, postionable.width, postionable.height)
1. Is this intended? 2. We have to manually recalculate size in this scenario right?
n
Are you saying your layout for the container with 3 buttons is something like:
Copy code
simpleLayout { container ->
    var y = …
    container.children.forEach { child ->
        child.bounds = Rectangle(0.0, y, child.width, child.height)
        y += …
    }
}
If so, how do you enforce that the container is sized based on the buttons? Are you also setting the container size in that same layout? That is how you could force the container to resize as the buttons do. The thing is that Layouts don’t get a direct reference to a View. So they can’t change the container’s bounds unless you keep a reference to the actual container around for the layout to use. This is by design since Layouts work for the Display as well as Containers; and they don’t have a common interface. So you could do this as follows:
Copy code
val containerWithButtons: Container
…

containerWithButtons.layout = simpleLayout { container ->
    var y = …
    var maxWidth = 0.0
    container.children.forEach { child ->
        child.bounds = Rectangle(0.0, y, child.width, child.height)
        y += …
        maxWidth = …
    }

    containerWithButtons.size = Size(maxWidth, y) // or container.children.last().bounds.bottom, if y isn’t equal to the last child’s bottom
}
c
Yeah. This is what we are doing basically. We thought maybe there's a better way.
More question though. Sorry for too much. Can we have a view with a defined size, but its widths are anchored to the parent so that when the display width changes it (child) reacts
n
Not sure I follow. Do you mean the container would stay the same size, but it’s child would change as the Display size changes? If so, you can do this a few ways. The first is to directly observe the Display’s boundsChange and update the child’s bounds. You could also have a Layout on the Display the changes the child’s bounds instead of the container’s. You’d need a reference to the child in either case of course. But this is doable.