From 0a7ceab24a69bdd107b6e6f998789f52cc7ee336 Mon Sep 17 00:00:00 2001 From: Jake Wharton Date: Thu, 24 Oct 2024 07:08:11 -0700 Subject: [PATCH] Migrate View Box away from subtype (#2408) When widgets switch to abstract classes, this will no longer be allowed. --- .../app/cash/redwood/layout/view/ViewBox.kt | 34 ++++++++++++------- .../cash/redwood/layout/view/ViewBoxTest.kt | 2 +- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/redwood-layout-view/src/main/kotlin/app/cash/redwood/layout/view/ViewBox.kt b/redwood-layout-view/src/main/kotlin/app/cash/redwood/layout/view/ViewBox.kt index 564deae129..9317ba5404 100644 --- a/redwood-layout-view/src/main/kotlin/app/cash/redwood/layout/view/ViewBox.kt +++ b/redwood-layout-view/src/main/kotlin/app/cash/redwood/layout/view/ViewBox.kt @@ -36,10 +36,22 @@ import app.cash.redwood.ui.Margin import app.cash.redwood.widget.ViewGroupChildren import app.cash.redwood.widget.Widget -internal class ViewBox( +internal class ViewBox(context: Context) : Box { + private val delegate = BoxViewGroup(context) + override val value: View get() = delegate + override var modifier by delegate::modifier + override val children get() = delegate.children + + override fun width(width: Constraint) = delegate.width(width) + override fun height(height: Constraint) = delegate.height(height) + override fun margin(margin: Margin) = delegate.margin(margin) + override fun horizontalAlignment(horizontalAlignment: CrossAxisAlignment) = delegate.horizontalAlignment(horizontalAlignment) + override fun verticalAlignment(verticalAlignment: CrossAxisAlignment) = delegate.verticalAlignment(verticalAlignment) +} + +private class BoxViewGroup( context: Context, -) : ViewGroup(context), - Box { +) : ViewGroup(context) { private val density = Density(context.resources) private var horizontalAlignment = CrossAxisAlignment.Start private var verticalAlignment = CrossAxisAlignment.Start @@ -47,35 +59,33 @@ internal class ViewBox( private var heightConstraint = Constraint.Wrap private var margin: Margin = Margin.Zero - override var modifier: Modifier = Modifier - - override val value get() = this + var modifier: Modifier = Modifier - override val children = ViewGroupChildren(this) + val children = ViewGroupChildren(this) private val measurer = Measurer() - override fun width(width: Constraint) { + fun width(width: Constraint) { this.widthConstraint = width requestLayout() } - override fun height(height: Constraint) { + fun height(height: Constraint) { this.heightConstraint = height requestLayout() } - override fun margin(margin: Margin) { + fun margin(margin: Margin) { this.margin = margin requestLayout() } - override fun horizontalAlignment(horizontalAlignment: CrossAxisAlignment) { + fun horizontalAlignment(horizontalAlignment: CrossAxisAlignment) { this.horizontalAlignment = horizontalAlignment requestLayout() } - override fun verticalAlignment(verticalAlignment: CrossAxisAlignment) { + fun verticalAlignment(verticalAlignment: CrossAxisAlignment) { this.verticalAlignment = verticalAlignment requestLayout() } diff --git a/redwood-layout-view/src/test/kotlin/app/cash/redwood/layout/view/ViewBoxTest.kt b/redwood-layout-view/src/test/kotlin/app/cash/redwood/layout/view/ViewBoxTest.kt index 693790b084..b0b8f48060 100644 --- a/redwood-layout-view/src/test/kotlin/app/cash/redwood/layout/view/ViewBoxTest.kt +++ b/redwood-layout-view/src/test/kotlin/app/cash/redwood/layout/view/ViewBoxTest.kt @@ -45,7 +45,7 @@ class ViewBoxTest( override fun box(): Box { return ViewBox(paparazzi.context).apply { - background = ColorDrawable(0x88000000.toInt()) + value.background = ColorDrawable(0x88000000.toInt()) applyDefaults() } }