-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrate Modifier.size(TextSize) to Modifier.Node
- Loading branch information
Showing
2 changed files
with
48 additions
and
36 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
ui/src/main/java/kiwi/orbit/compose/ui/layout/SizeByTextSizeModifierNode.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
36 changes: 0 additions & 36 deletions
36
ui/src/main/java/kiwi/orbit/compose/ui/layout/TextScalingSizeModifier.kt
This file was deleted.
Oops, something went wrong.