Skip to content

Commit

Permalink
Invalidate the layout when the flex container's margin changes (#2539)
Browse files Browse the repository at this point in the history
* Invalidate the layout when the flex container's margin changes

Also when a spacer's width or height changes.

* Make the new function open, not abstract

* More test snapshots

* More missing snapshots
  • Loading branch information
squarejesse authored Jan 22, 2025
1 parent dc4fa22 commit f8f0f07
Show file tree
Hide file tree
Showing 45 changed files with 167 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ internal interface YogaFlexContainer<W : Any> :
marginBottom = margin.bottom.toPx().toFloat()
}
}
invalidateSize(true)
}

override fun horizontalAlignment(horizontalAlignment: MainAxisAlignment) {
Expand All @@ -71,10 +72,15 @@ internal interface YogaFlexContainer<W : Any> :

fun crossAxisAlignment(crossAxisAlignment: CrossAxisAlignment) {
rootNode.alignItems = crossAxisAlignment.toAlignItems()
invalidateSize(true)
}

fun mainAxisAlignment(mainAxisAlignment: MainAxisAlignment) {
rootNode.justifyContent = mainAxisAlignment.toJustifyContent()
invalidateSize(true)
}

open fun invalidateSize(nodeBecameDirty: Boolean = false) {
}
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,36 @@ abstract class AbstractFlexContainerTest<T : Any> {
snapshotter.snapshot("Empty")
}

@Test fun testRowMarginChanges() {
testContainerMarginChanges(FlexDirection.Row)
}

@Test fun testColumnMarginChanges() {
testContainerMarginChanges(FlexDirection.Column)
}

private fun testContainerMarginChanges(
flexDirection: FlexDirection = burstValues(FlexDirection.Column, FlexDirection.Row),
) {
val container = flexContainer(flexDirection)
val snapshotter = snapshotter(container.value)

container.width(Constraint.Fill)
container.height(Constraint.Fill)
container.crossAxisAlignment(CrossAxisAlignment.Stretch)
container.mainAxisAlignment(MainAxisAlignment.SpaceBetween)

container.add(widgetFactory.text(shortText(), backgroundColor = Red))
container.add(widgetFactory.text(shortText(), backgroundColor = Green))
container.margin(Margin.Zero)
container.onEndChanges()
snapshotter.snapshot("Zero")

container.margin(Margin(10.dp, 20.dp, 30.dp, 40.dp))
container.onEndChanges()
snapshotter.snapshot("Nonzero")
}

/** The view shouldn't crash if its displayed after being detached. */
@Test fun testLayoutAfterDetach() {
val container = flexContainer(FlexDirection.Column).apply {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ internal class UIViewFlexContainer(
invalidateSize()
}

internal fun invalidateSize(nodeBecameDirty: Boolean = false) {
override fun invalidateSize(nodeBecameDirty: Boolean) {
val sizeListener = this.sizeListener
if (sizeListener == null) {
// This is a top-level flex container. Tell the enclosing view to redo its layout.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,24 @@ import app.cash.redwood.layout.widget.Spacer
import app.cash.redwood.ui.Default
import app.cash.redwood.ui.Density
import app.cash.redwood.ui.Dp
import app.cash.redwood.widget.ResizableWidget
import app.cash.redwood.widget.ResizableWidget.SizeListener
import kotlinx.cinterop.CValue
import kotlinx.cinterop.cValue
import platform.CoreGraphics.CGRectZero
import platform.CoreGraphics.CGSize
import platform.CoreGraphics.CGSizeMake
import platform.UIKit.UIView

internal class UIViewSpacer : Spacer<UIView> {
internal class UIViewSpacer :
Spacer<UIView>,
ResizableWidget<UIView> {
private val view = SpacerUIView()
override val value: UIView get() = view
override var modifier: Modifier = Modifier

override var sizeListener: SizeListener? = null

override fun width(width: Dp) {
view.width = with(Density.Default) { width.toPx() }
invalidate()
Expand All @@ -44,6 +50,7 @@ internal class UIViewSpacer : Spacer<UIView> {

private fun invalidate() {
value.setNeedsLayout()
sizeListener?.invalidateSize()
}
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f8f0f07

Please sign in to comment.