Alessandro Marcolini
06/26/2023, 10:40 PMNick
06/27/2023, 1:06 AMAlessandro Marcolini
06/27/2023, 1:33 AMdata:text/html,<html><head></head><body style="width: 100vw;height: 100vh;padding: 0;margin: 0;display: flex;flex-direction: column;"><div style="width: 100%;background-color: red;height: 100%;"></div><div style="width: 100%;background-color: blue;min-height: 100px;"></div></body></html>
. I'm looking for a horizontal version of this as well.Alessandro Marcolini
06/27/2023, 1:39 AMNick
06/27/2023, 1:43 AMAlessandro Marcolini
06/27/2023, 1:44 AMNick
06/27/2023, 3:52 PMOrientation
.
class FlexColumn: Layout {
override fun layout(container: PositionableContainer) {
var y = <http://container.insets.top|container.insets.top>
val x = container.insets.left
val w = container.width - container.insets.run { left + right }
var remainingHeight = container.height - container.insets.run { top + bottom }
val visibleChildren = container.children.filter { it.visible }
val numChildrenCanIncrease = visibleChildren.size
var minDecreaseAvailable = -1.0
var numChildrenCanDecrease = 0
val updateMinDecreaseInfo: (child: Positionable) -> Unit = { child ->
if (child.height > child.minimumSize.height) {
val decreaseAvailable = child.height - child.minimumSize.height
minDecreaseAvailable = if (minDecreaseAvailable < 0.0) decreaseAvailable else min(minDecreaseAvailable, decreaseAvailable)
++numChildrenCanDecrease
}
}
visibleChildren.forEach { child ->
val h = child.idealSize?.height ?: child.minimumSize.height
child.bounds = Rectangle(x, y, w, h)
y += h
remainingHeight -= h
updateMinDecreaseInfo(child)
}
while (remainingHeight != 0.0) {
y = <http://container.insets.top|container.insets.top>
when {
remainingHeight > 0.0 -> {
val proportionalIncrease = remainingHeight / numChildrenCanIncrease
visibleChildren.forEach { child ->
child.bounds = Rectangle(child.x, y, child.width, child.height + proportionalIncrease)
y += child.height
}
remainingHeight = 0.0
}
remainingHeight < 0.0 -> {
when {
numChildrenCanDecrease > 0 -> {
val proportionalDecrease = min(minDecreaseAvailable, -remainingHeight / numChildrenCanDecrease)
remainingHeight += proportionalDecrease * numChildrenCanDecrease
numChildrenCanDecrease = 0
minDecreaseAvailable = -1.0
visibleChildren.forEach { child ->
when {
child.height > child.minimumSize.height -> {
child.bounds = Rectangle(child.x, y, child.width, child.height - proportionalDecrease)
y += child.height
updateMinDecreaseInfo(child)
}
else -> child.bounds = Rectangle(child.x, y, child.width, child.height)
}
}
}
else -> break
}
}
}
}
}
}
Nick
06/27/2023, 3:52 PMAlessandro Marcolini
06/27/2023, 10:46 PMy += child.height
to after the when
when doing the proportional decrease (because otherwise children that didn't decrease wouldn't change y
). I haven't seen this issue because it wouldn't apply in my use case, but it looks like the proportional decrease could still violate views' minimumSize
(e.g., one view can decrease 30 and another can decrease 10, but both are decreased 20, violating the view that could only decrease 10).Nick
06/28/2023, 1:14 AM