Skip to content

Commit 829913e

Browse files
committed
chore: migrate modifier to Modifier.Node
1 parent 7f3ac02 commit 829913e

File tree

1 file changed

+50
-5
lines changed

1 file changed

+50
-5
lines changed

app/src/main/java/to/bitkit/ui/shared/util/Modifiers.kt

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import androidx.compose.runtime.remember
1818
import androidx.compose.ui.Modifier
1919
import androidx.compose.ui.composed
2020
import androidx.compose.ui.draw.clip
21-
import androidx.compose.ui.draw.drawBehind
2221
import androidx.compose.ui.draw.drawWithContent
2322
import androidx.compose.ui.draw.shadow
2423
import androidx.compose.ui.geometry.Offset
@@ -31,7 +30,9 @@ import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
3130
import androidx.compose.ui.graphics.graphicsLayer
3231
import androidx.compose.ui.graphics.toArgb
3332
import androidx.compose.ui.input.pointer.pointerInput
34-
import androidx.compose.ui.platform.LocalDensity
33+
import androidx.compose.ui.node.DrawModifierNode
34+
import androidx.compose.ui.node.ModifierNodeElement
35+
import androidx.compose.ui.platform.InspectorInfo
3536
import androidx.compose.ui.unit.Dp
3637
import androidx.compose.ui.unit.dp
3738
import to.bitkit.ui.theme.Colors
@@ -130,10 +131,51 @@ fun Modifier.outerGlow(
130131
glowOpacity: Float,
131132
glowRadius: Dp = 12.dp,
132133
cornerRadius: Dp = 16.dp,
133-
): Modifier = composed {
134-
val density = LocalDensity.current.density
134+
): Modifier = this.then(
135+
OuterGlowElement(
136+
glowColor = glowColor,
137+
glowOpacity = glowOpacity,
138+
glowRadius = glowRadius,
139+
cornerRadius = cornerRadius
140+
)
141+
)
142+
143+
private data class OuterGlowElement(
144+
val glowColor: Color,
145+
val glowOpacity: Float,
146+
val glowRadius: Dp,
147+
val cornerRadius: Dp,
148+
) : ModifierNodeElement<OuterGlowNode>() {
149+
override fun create(): OuterGlowNode = OuterGlowNode(
150+
glowColor = glowColor,
151+
glowOpacity = glowOpacity,
152+
glowRadius = glowRadius,
153+
cornerRadius = cornerRadius
154+
)
155+
156+
override fun update(node: OuterGlowNode) {
157+
node.glowColor = glowColor
158+
node.glowOpacity = glowOpacity
159+
node.glowRadius = glowRadius
160+
node.cornerRadius = cornerRadius
161+
}
135162

136-
this.drawBehind {
163+
override fun InspectorInfo.inspectableProperties() {
164+
name = "outerGlow"
165+
properties["glowColor"] = glowColor
166+
properties["glowOpacity"] = glowOpacity
167+
properties["glowRadius"] = glowRadius
168+
properties["cornerRadius"] = cornerRadius
169+
}
170+
}
171+
172+
private class OuterGlowNode(
173+
var glowColor: Color,
174+
var glowOpacity: Float,
175+
var glowRadius: Dp,
176+
var cornerRadius: Dp,
177+
) : DrawModifierNode, Modifier.Node() {
178+
override fun androidx.compose.ui.graphics.drawscope.ContentDrawScope.draw() {
137179
val glowRadiusPx = glowRadius.toPx()
138180
val cornerRadiusPx = cornerRadius.toPx()
139181

@@ -163,6 +205,9 @@ fun Modifier.outerGlow(
163205
paint = paint
164206
)
165207
}
208+
209+
// Draw the actual content
210+
drawContent()
166211
}
167212
}
168213

0 commit comments

Comments
 (0)