StaveFocusable.kt
39 lines · 1.3 KB
Changed in: 0.5.0
| 1 | package dev.stave.ui.components |
| 2 | |
| 3 | import androidx.compose.foundation.border |
| 4 | import androidx.compose.foundation.interaction.MutableInteractionSource |
| 5 | import androidx.compose.foundation.interaction.collectIsFocusedAsState |
| 6 | import androidx.compose.foundation.shape.RoundedCornerShape |
| 7 | import androidx.compose.runtime.Composable |
| 8 | import androidx.compose.runtime.getValue |
| 9 | import androidx.compose.runtime.remember |
| 10 | import androidx.compose.ui.Modifier |
| 11 | import androidx.compose.ui.draw.clip |
| 12 | import androidx.compose.ui.graphics.Color |
| 13 | import androidx.compose.ui.unit.dp |
| 14 | import dev.stave.ui.theme.AccentAmber |
| 15 | import dev.stave.ui.theme.SurfaceRaised |
| 16 | |
| 17 | /** |
| 18 | * Adds the Earhorn focus visual: a 2dp amber border when focused, nothing |
| 19 | * otherwise (the caller paints its own background). Pair with a Modifier that |
| 20 | * declares focusability (e.g. clickable). |
| 21 | */ |
| 22 | @Composable |
| 23 | fun Modifier.staveFocusable( |
| 24 | interactionSource: MutableInteractionSource, |
| 25 | cornerRadiusDp: Int = 8, |
| 26 | ): Modifier { |
| 27 | val focused by interactionSource.collectIsFocusedAsState() |
| 28 | val shape = remember(cornerRadiusDp) { RoundedCornerShape(cornerRadiusDp.dp) } |
| 29 | return this |
| 30 | .clip(shape) |
| 31 | .then( |
| 32 | if (focused) { |
| 33 | Modifier |
| 34 | .border(2.dp, AccentAmber, shape) |
| 35 | } else { |
| 36 | Modifier |
| 37 | } |
| 38 | ) |
| 39 | } |


