Skip to content

Instantly share code, notes, and snippets.

@NikolaDespotoski
Created March 12, 2024 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NikolaDespotoski/327ec8e5c2076c196cf608a32804de62 to your computer and use it in GitHub Desktop.
Save NikolaDespotoski/327ec8e5c2076c196cf608a32804de62 to your computer and use it in GitHub Desktop.
Migrate composed 1
private fun Modifier.curve(
fillColor: Color,
lineColor: Color,
backgroundColor: Color,
stroke: Stroke,
points: SnapshotStateList<Point>,
thumbSize: Dp
) = this then CurveModifier(
fillColor = fillColor,
lineColor = lineColor,
backgroundColor = backgroundColor,
stroke = stroke,
points = points
)
@Immutable
private data class CurveModifier(
private val fillColor: Color,
private val lineColor: Color,
private val backgroundColor: Color,
private val stroke: Stroke,
private val points: SnapshotStateList<Point>
) : ModifierNodeElement<CurveNode>() {
override fun InspectorInfo.inspectableProperties() {
debugInspectorInfo {
properties["fillColor"] = fillColor
properties["lineColor"] = lineColor
properties["points"] = points.joinToString()
properties["stroke"] = stroke
}
}
override fun create() = CurveNode(
fillColor = fillColor,
lineColor = lineColor,
backgroundColor = backgroundColor,
stroke = stroke,
points = points
)
override fun update(node: CurveNode) {
node.fillColor = fillColor
node.lineColor = lineColor
node.backgroundColor = backgroundColor
node.stroke = stroke
node.points = points
}
}
private class CurveNode(
var fillColor: Color,
var lineColor: Color,
var backgroundColor: Color,
var stroke: Stroke,
var points: SnapshotStateList<Point>
) : DrawModifierNode, Modifier.Node() {
private val path = Path() // <--- remembered
override fun ContentDrawScope.draw() {
if (!path.isEmpty) {
path.reset()
}
path.drawSomething(
stroke = stroke, size = size, points = points.toList()
)
drawPath(path = path, color = lineColor, style = stroke)
drawPath(path = path, color = fillColor, alpha = 0.4f)
drawContent()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment