|
| 1 | +package com.smarttoolfactory.colorpicker.widget |
| 2 | + |
| 3 | +import androidx.compose.foundation.clickable |
| 4 | +import androidx.compose.foundation.interaction.MutableInteractionSource |
| 5 | +import androidx.compose.foundation.layout.Arrangement |
| 6 | +import androidx.compose.foundation.layout.Box |
| 7 | +import androidx.compose.foundation.layout.Row |
| 8 | +import androidx.compose.foundation.layout.padding |
| 9 | +import androidx.compose.material.Text |
| 10 | +import androidx.compose.runtime.* |
| 11 | +import androidx.compose.ui.Modifier |
| 12 | +import androidx.compose.ui.text.font.FontWeight |
| 13 | +import androidx.compose.ui.unit.dp |
| 14 | +import androidx.compose.ui.unit.sp |
| 15 | +import com.smarttoolfactory.colorpicker.model.ColorModel |
| 16 | +import com.smarttoolfactory.colorpicker.ui.Grey400 |
| 17 | +import com.smarttoolfactory.colorpicker.ui.Grey600 |
| 18 | + |
| 19 | +/** |
| 20 | + * Tab Section for changing between HSL, HSV and RGB color models. |
| 21 | + * @param colorModel current color model. |
| 22 | + * @param onColorModelChange callback that is triggered when user touches buttons. |
| 23 | + */ |
| 24 | +@Composable |
| 25 | +fun ColorModelChangeTabRow( |
| 26 | + modifier: Modifier, |
| 27 | + colorModel: ColorModel, |
| 28 | + onColorModelChange: (ColorModel) -> Unit |
| 29 | +) { |
| 30 | + var selectedIndex by remember { |
| 31 | + mutableStateOf( |
| 32 | + when (colorModel) { |
| 33 | + ColorModel.HSL -> 0 |
| 34 | + ColorModel.HSV -> 1 |
| 35 | + ColorModel.RGB -> 2 |
| 36 | + } |
| 37 | + ) |
| 38 | + } |
| 39 | + |
| 40 | + Row( |
| 41 | + modifier = modifier, |
| 42 | + horizontalArrangement = Arrangement.SpaceEvenly |
| 43 | + ) { |
| 44 | + modelList.forEachIndexed { index, s -> |
| 45 | + Box(modifier = Modifier |
| 46 | + .clickable( |
| 47 | + interactionSource = MutableInteractionSource(), |
| 48 | + indication = null, |
| 49 | + onClick = { |
| 50 | + selectedIndex = index |
| 51 | + onColorModelChange(ColorModel.values()[index]) |
| 52 | + } |
| 53 | + ) |
| 54 | + .padding(10.dp) |
| 55 | + ) { |
| 56 | + Text( |
| 57 | + text = s, |
| 58 | + fontWeight = FontWeight.Bold, |
| 59 | + fontSize = 16.sp, |
| 60 | + color = if (index == selectedIndex) Grey400 else Grey600 |
| 61 | + ) |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +private val modelList = listOf("HSL", "HSV", "RGB") |
0 commit comments