|
import androidx.compose.foundation.BorderStroke |
|
import androidx.compose.foundation.ExperimentalFoundationApi |
|
import androidx.compose.foundation.LocalIndication |
|
import androidx.compose.foundation.combinedClickable |
|
import androidx.compose.foundation.interaction.MutableInteractionSource |
|
import androidx.compose.foundation.layout.Arrangement |
|
import androidx.compose.foundation.layout.PaddingValues |
|
import androidx.compose.foundation.layout.Row |
|
import androidx.compose.foundation.layout.RowScope |
|
import androidx.compose.foundation.layout.defaultMinSize |
|
import androidx.compose.foundation.layout.padding |
|
import androidx.compose.material3.ButtonColors |
|
import androidx.compose.material3.ButtonDefaults |
|
import androidx.compose.material3.ButtonElevation |
|
import androidx.compose.material3.ExperimentalMaterial3Api |
|
import androidx.compose.material3.LocalContentColor |
|
import androidx.compose.material3.MaterialTheme |
|
import androidx.compose.material3.ProvideTextStyle |
|
import androidx.compose.material3.Surface |
|
import androidx.compose.runtime.Composable |
|
import androidx.compose.runtime.CompositionLocalProvider |
|
import androidx.compose.runtime.remember |
|
import androidx.compose.ui.Alignment |
|
import androidx.compose.ui.Modifier |
|
import androidx.compose.ui.graphics.Shape |
|
import androidx.compose.ui.unit.dp |
|
|
|
/** |
|
* A button that handles normal, double and long clicks. |
|
* @author Arnau Mora |
|
* @since 20220824 |
|
* @author Arnau Mora, adneal |
|
* @see <a href="https://stackoverflow.com/a/65850523/5717211">Stack Overflow</a> |
|
*/ |
|
@Composable |
|
@ExperimentalMaterial3Api |
|
@ExperimentalFoundationApi |
|
fun Button( |
|
modifier: Modifier = Modifier, |
|
onClick: () -> Unit, |
|
onLongClick: (() -> Unit)? = null, |
|
onDoubleClick: (() -> Unit)? = null, |
|
enabled: Boolean = true, |
|
shape: Shape = ButtonDefaults.shape, |
|
colors: ButtonColors = ButtonDefaults.buttonColors(), |
|
elevation: ButtonElevation? = ButtonDefaults.buttonElevation(), |
|
border: BorderStroke? = null, |
|
contentPadding: PaddingValues = ButtonDefaults.ContentPadding, |
|
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, |
|
content: @Composable RowScope.() -> Unit |
|
) { |
|
val containerColor = colors.containerColor(enabled).value |
|
val contentColor = colors.contentColor(enabled).value |
|
val shadowElevation = elevation?.shadowElevation(enabled, interactionSource)?.value ?: 0.dp |
|
val tonalElevation = elevation?.tonalElevation(enabled, interactionSource)?.value ?: 0.dp |
|
Surface( |
|
modifier = modifier |
|
.clip(shape) |
|
.combinedClickable( |
|
enabled = enabled, |
|
indication = LocalIndication.current, |
|
interactionSource = interactionSource, |
|
onLongClick = onLongClick, |
|
onClick = onClick, |
|
onDoubleClick = onDoubleClick, |
|
), |
|
shape = shape, |
|
color = containerColor, |
|
contentColor = contentColor, |
|
tonalElevation = tonalElevation, |
|
shadowElevation = shadowElevation, |
|
border = border, |
|
) { |
|
CompositionLocalProvider(LocalContentColor provides contentColor) { |
|
ProvideTextStyle(value = MaterialTheme.typography.labelLarge) { |
|
Row( |
|
Modifier |
|
.defaultMinSize( |
|
minWidth = ButtonDefaults.MinWidth, |
|
minHeight = ButtonDefaults.MinHeight |
|
) |
|
.padding(contentPadding), |
|
horizontalArrangement = Arrangement.Center, |
|
verticalAlignment = Alignment.CenterVertically, |
|
content = content |
|
) |
|
} |
|
} |
|
} |
|
} |