package dev.darkpyonix.composerust.design
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Checkbox
import androidx.compose.material3.CheckboxDefaults
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.ui.unit.Dp
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.ProgressIndicatorDefaults
import androidx.compose.material3.RadioButton
import androidx.compose.material3.RadioButtonDefaults
import androidx.compose.material3.Slider
import androidx.compose.material3.SliderDefaults
import androidx.compose.material3.Switch
import androidx.compose.material3.SwitchDefaults
import androidx.compose.material3.VerticalDivider
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.Alignment
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.requiredSize
import androidx.compose.foundation.layout.size
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import dev.darkpyonix.composerust.protocol.ColorRole
/**
* Material 3's controls, drawn by `androidx.compose.material3`.
*
* This is the one design system that does not use [DrawnControlWidgets]. The library is
* already a dependency of every Renderer target, and it draws the ripples, the state
* layers, the accessibility semantics, the minimum touch targets and the motion that a
* hand-written copy would have to chase and would eventually get wrong.
*
* What still comes from [ComponentRules.controls] is the colour. The Host's theme decides
* the palette, and these components would otherwise read `MaterialTheme.colorScheme`,
* which nothing here sets, so a Host running a dark theme would get light controls. Every
* colour below is therefore passed in from the resolved theme rather than defaulted.
*
* Dimensions are not passed in: those are the part the library is authoritative about, and
* naming them here would be a second opinion about Material's own metrics.
*/
internal object Material3ControlWidgets : ControlWidgets {
@Composable
override fun Toggle(
role: ToggleRole,
checked: Boolean,
enabled: Boolean,
onChange: ((Boolean) -> Unit)?,
modifier: Modifier,
theme: ResolvedTheme,
look: ControlLook,
) {
// The library's Switch and Checkbox have fixed metrics, so a size or a colour the
// application named for them is drawn by the shared control drawing instead, which
// also owns the transition and the toggle semantics.
if (look.appliesTo(role)) {
DrawnControlWidgets.Toggle(role, checked, enabled, onChange, modifier, theme, look)
return
}
val primary = theme.color(ColorRole.Primary)
val onPrimary = theme.color(ColorRole.OnPrimary)
val outline = theme.color(ColorRole.Outline)
when (role) {
ToggleRole.Checkbox -> Checkbox(
checked = checked,
// A control with no handler is read-only, which material3 spells as a null
// callback rather than as a disabled control: disabled would also grey it.
onCheckedChange = onChange,
modifier = modifier,
enabled = enabled,
colors = CheckboxDefaults.colors(
checkedColor = primary,
uncheckedColor = outline,
checkmarkColor = onPrimary,
),
)
ToggleRole.RadioButton -> RadioButton(
selected = checked,
onClick = onChange?.let { report -> { report(!checked) } },
modifier = modifier,
enabled = enabled,
colors = RadioButtonDefaults.colors(
selectedColor = primary,
unselectedColor = outline,
),
)
ToggleRole.Switch -> Switch(
checked = checked,
onCheckedChange = onChange,
modifier = modifier,
enabled = enabled,
colors = SwitchDefaults.colors(
checkedThumbColor = onPrimary,
checkedTrackColor = primary,
uncheckedThumbColor = outline,
uncheckedTrackColor = theme.color(ColorRole.SurfaceVariant),
uncheckedBorderColor = outline,
),
)
}
}
// The slider with a thumb slot is experimental in the Material 3 that Android builds
// against, though not in the desktop one; the opt-in is needed there to draw a sized thumb.
@OptIn(ExperimentalMaterial3Api::class)
@Composable
override fun Slider(
value: Float,
range: ClosedFloatingPointRange<Float>,
steps: Int,
enabled: Boolean,
named: Color?,
onChange: (Float) -> Unit,
modifier: Modifier,
theme: ResolvedTheme,
look: ControlLook,
) {
val primary = named ?: theme.color(ColorRole.Primary)
val colors = SliderDefaults.colors(
thumbColor = look.thumb ?: primary,
activeTrackColor = look.activeTrack ?: primary,
inactiveTrackColor = look.track ?: theme.color(ColorRole.SurfaceVariant),
)
val interactions = remember { MutableInteractionSource() }
// A thumb the application sized is the library's own thumb at that size, round. It is
// drawn inside a box the size of Material's own handle, because the library lays the
// slider out around its thumb: the slider keeps its height and the area that takes a
// press, and the track keeps its gap, whatever size the thumb is drawn at.
val thumbSize = look.thumbSize
Slider(
value = value,
onValueChange = onChange,
modifier = modifier,
enabled = enabled,
valueRange = range,
steps = steps,
colors = colors,
interactionSource = interactions,
thumb = {
if (thumbSize == null) {
SliderDefaults.Thumb(interactionSource = interactions, colors = colors, enabled = enabled)
} else {
Box(Modifier.size(MATERIAL_HANDLE), contentAlignment = Alignment.Center) {
SliderDefaults.Thumb(
interactionSource = interactions,
modifier = Modifier.requiredSize(thumbSize),
colors = colors,
enabled = enabled,
thumbSize = DpSize(thumbSize, thumbSize),
)
}
}
},
)
}
@Composable
override fun ProgressIndicator(
determinate: Boolean,
value: Float,
circular: Boolean,
modifier: Modifier,
theme: ResolvedTheme,
diameter: Dp?,
color: Color?,
trackColor: Color?,
) {
val indicator = color ?: theme.color(ColorRole.Primary)
val track = trackColor ?: theme.color(ColorRole.SurfaceVariant)
val stroke = ringThickness(ProgressIndicatorDefaults.CircularStrokeWidth, diameter)
when {
circular && determinate -> CircularProgressIndicator(
progress = { value },
modifier = modifier,
color = indicator,
strokeWidth = stroke,
trackColor = track,
)
circular -> CircularProgressIndicator(
modifier = modifier,
color = indicator,
strokeWidth = stroke,
trackColor = track,
)
determinate -> LinearProgressIndicator(
progress = { value },
modifier = modifier,
color = indicator,
trackColor = track,
)
else -> LinearProgressIndicator(
modifier = modifier,
color = indicator,
trackColor = track,
)
}
}
@Composable
override fun Divider(vertical: Boolean, modifier: Modifier, theme: ResolvedTheme) {
val style = theme.rules.controls(theme).divider
if (vertical) {
VerticalDivider(modifier = modifier, thickness = style.thickness, color = style.color)
} else {
HorizontalDivider(modifier = modifier, thickness = style.thickness, color = style.color)
}
}
}
/** The size Material 3 lays its slider handle out at: a bar 4 dp wide and 44 dp tall. */
private val MATERIAL_HANDLE = DpSize(4.dp, 44.dp)