Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate View Box away from subtype #2408

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,46 +36,56 @@ 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<View> {
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<View> {
) : ViewGroup(context) {
private val density = Density(context.resources)
private var horizontalAlignment = CrossAxisAlignment.Start
private var verticalAlignment = CrossAxisAlignment.Start
private var widthConstraint = Constraint.Wrap
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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class ViewBoxTest(

override fun box(): Box<View> {
return ViewBox(paparazzi.context).apply {
background = ColorDrawable(0x88000000.toInt())
value.background = ColorDrawable(0x88000000.toInt())
applyDefaults()
}
}
Expand Down