Skip to content

Commit

Permalink
migrate Modifier.size(TextSize) to Modifier.Node
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed May 26, 2023
1 parent 0497b76 commit 2f83029
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package kiwi.orbit.compose.ui.layout

import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.Measurable
import androidx.compose.ui.layout.MeasureResult
import androidx.compose.ui.layout.MeasureScope
import androidx.compose.ui.node.LayoutModifierNode
import androidx.compose.ui.node.ModifierNodeElement
import androidx.compose.ui.platform.InspectorInfo
import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.TextUnit

public fun Modifier.size(size: TextUnit): Modifier =
this then SizeByTextSizeModifierElementNode(size, size)

public fun Modifier.size(width: TextUnit, height: TextUnit): Modifier =
this then SizeByTextSizeModifierElementNode(width, height)

private data class SizeByTextSizeModifierElementNode(
val width: TextUnit,
val height: TextUnit,
) : ModifierNodeElement<SizeByTextSizeModifierNode>() {
override fun InspectorInfo.inspectableProperties() {
name = "sizeByTextSize"
properties["width"] = width
properties["height"] = height
}

override fun create() = SizeByTextSizeModifierNode(width, height)

override fun update(node: SizeByTextSizeModifierNode) {
node.width = width
node.height = height
}
}

private class SizeByTextSizeModifierNode(
var width: TextUnit,
var height: TextUnit,
) : Modifier.Node(), LayoutModifierNode {
override fun MeasureScope.measure(measurable: Measurable, constraints: Constraints): MeasureResult {
val newConstraints = Constraints.fixed(width.roundToPx(), height.roundToPx())
val placeable = measurable.measure(newConstraints)
return layout(placeable.width, placeable.height) {
placeable.placeRelative(0, 0)
}
}
}

This file was deleted.

0 comments on commit 2f83029

Please sign in to comment.