@@ -14,17 +14,22 @@ import androidx.compose.runtime.remember
1414import androidx.compose.ui.ExperimentalComposeUiApi
1515import androidx.compose.ui.Modifier
1616import androidx.compose.ui.composed
17+ import androidx.compose.ui.draw.drawBehind
18+ import androidx.compose.ui.draw.drawWithContent
19+ import androidx.compose.ui.geometry.Offset
20+ import androidx.compose.ui.graphics.BlendMode
1721import androidx.compose.ui.graphics.Color
1822import androidx.compose.ui.graphics.RectangleShape
1923import androidx.compose.ui.graphics.Shape
20- import androidx.compose.ui.input.pointer.pointerInput
2124import androidx.compose.ui.input.pointer.pointerInteropFilter
25+ import androidx.compose.ui.layout.layout
2226import androidx.compose.ui.layout.onPlaced
2327import androidx.compose.ui.platform.LocalDensity
2428import androidx.compose.ui.semantics.Role
2529import androidx.compose.ui.unit.Dp
2630import androidx.compose.ui.unit.DpSize
2731import androidx.compose.ui.unit.dp
32+ import androidx.compose.ui.unit.offset
2833
2934inline fun Modifier.addIf (
3035 predicate : Boolean ,
@@ -56,7 +61,8 @@ fun Modifier.unboundedClickable(
5661}
5762
5863
59- fun Modifier.debugBounds (color : Color = Color .Magenta , shape : Shape = RectangleShape ) = this .border(1 .dp, color, shape)
64+ fun Modifier.debugBounds (color : Color = Color .Magenta , shape : Shape = RectangleShape ) =
65+ this .border(1 .dp, color, shape)
6066
6167fun Modifier.rememberedClickable (
6268 enabled : Boolean = true,
@@ -122,4 +128,61 @@ fun Modifier.swallowClicks(onClick: () -> Unit = { }): Modifier =
122128 onClick()
123129 }
124130 true
125- }
131+ }
132+
133+ /* *
134+ * Draws circle with a solid [color] behind the content.
135+ *
136+ * @param color The color of the circle.
137+ * @param padding The padding to be applied externally to the circular shape. It determines the spacing between
138+ * the edge of the circle and the content inside.
139+ *
140+ * @return Combined [Modifier] that first draws the background circle and then centers the layout.
141+ */
142+ fun Modifier.circleBackground (color : Color , padding : Dp ): Modifier {
143+ val backgroundModifier = drawBehind {
144+ drawCircle(color, size.width / 2f , center = Offset (size.width / 2f , size.height / 2f ))
145+ }
146+
147+ val layoutModifier = layout { measurable, constraints ->
148+ // Adjust the constraints by the padding amount
149+ val adjustedConstraints = constraints.offset(- padding.roundToPx())
150+
151+ // Measure the composable with the adjusted constraints
152+ val placeable = measurable.measure(adjustedConstraints)
153+
154+ // Get the current max dimension to assign width=height
155+ val currentHeight = placeable.height
156+ val currentWidth = placeable.width
157+ val newDiameter = maxOf(currentHeight, currentWidth) + padding.roundToPx() * 2
158+
159+ // Assign the dimension and the center position
160+ layout(newDiameter, newDiameter) {
161+ // Place the composable at the calculated position
162+ placeable.placeRelative(
163+ (newDiameter - currentWidth) / 2 ,
164+ (newDiameter - currentHeight) / 2
165+ )
166+ }
167+ }
168+
169+ return this then backgroundModifier then layoutModifier
170+ }
171+
172+ fun Modifier.punchRectangle (color : Color ) = this .drawWithContent {
173+ drawRect(
174+ color,
175+ blendMode = BlendMode .Src
176+ )
177+
178+ drawContent()
179+ }
180+
181+ fun Modifier.punchCircle (color : Color ) = this .drawWithContent {
182+ drawCircle(
183+ color,
184+ blendMode = BlendMode .Src
185+ )
186+
187+ drawContent()
188+ }
0 commit comments