Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package io.github.lyxnx.compose.compodals.popups
import androidx.compose.animation.animateColorAsState
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsFocusedAsState
import androidx.compose.foundation.interaction.collectIsPressedAsState
import androidx.compose.foundation.layout.Box
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.NonRestartableComposable
import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
/**
* Container for the default values used by a [PopupDialogButton]
*/
public object PopupDialogButtonDefaults {
/**
* The default text style for an opinionated popup button
*/
public val DialogButtonStyle: TextStyle
@Composable
get() = MaterialTheme.typography.bodyLarge.copy(fontWeight = FontWeight.Medium)
/**
* Creates a [PopupDialogColors] instance with the given colors
*
* @param defaultColor the default color used for the button
* @param pressedColor the color used for the button when pressed
* @param focusedColor the color used for the button when focused
* @param disabledColor the color used for the button when disabled
*/
@Composable
public fun buttonColors(
defaultColor: Color = MaterialTheme.colorScheme.primary,
pressedColor: Color = MaterialTheme.colorScheme.onPrimaryContainer,
focusedColor: Color = MaterialTheme.colorScheme.inversePrimary,
disabledColor: Color = MaterialTheme.colorScheme.inverseOnSurface
): PopupDialogButtonColors = PopupDialogButtonColors(
default = defaultColor,
pressed = pressedColor,
focused = focusedColor,
disabled = disabledColor
)
}
/**
* Represents the colors used by a [PopupDialogButton]
*/
@Immutable
public data class PopupDialogButtonColors(
private val default: Color,
private val pressed: Color,
private val focused: Color,
private val disabled: Color
) {
@Composable
internal fun color(enabled: Boolean, pressed: Boolean, focused: Boolean): State =
animateColorAsState(
when {
!enabled -> this.disabled
pressed -> this.pressed
focused -> this.focused
else -> this.default
}
)
}
/**
* This is a convenience function for a [PopupDialogButton] that accepts a small number of the most useful parameters
*
* To use a customised [PopupDialogButton], use the function itself in place of this
*
* @param text button text
* @param modifier modifier to apply to the button
* @param enabled whether the button is enabled
* @param onClick optional action to perform when clicking on the button
*/
@Composable
public fun popupDialogButton(
text: String,
modifier: Modifier = Modifier,
enabled: Boolean = true,
onClick: () -> Unit = {},
): @Composable () -> Unit = popupDialogButton(
text = AnnotatedString(text),
modifier = modifier,
enabled = enabled,
onClick = onClick
)
/**
* This is a convenience function for a [PopupDialogButton] that accepts a small number of the most useful parameters
*
* To use a customised [PopupDialogButton], use the function itself in place of this
*
* @param text button text
* @param modifier modifier to apply to the button
* @param enabled whether the button is enabled
* @param onClick optional action to perform when clicking on the button
*/
@Composable
public fun popupDialogButton(
text: AnnotatedString,
modifier: Modifier = Modifier,
enabled: Boolean = true,
onClick: () -> Unit = {},
): @Composable () -> Unit = {
PopupDialogButton(
text = text,
modifier = modifier,
enabled = enabled,
onClick = onClick
)
}
/**
* A popup dialog button that can be used in a [PopupDialog]
*
* This is an optional, however useful and good default button, as a [PopupDialog] allows custom composables to be specified
* for the button slots
*
* @param text button text
* @param modifier modifier to apply to the button
* @param colors colors to apply to the button in various states
* @param style style of the button text
* @param interactionSource mutable interaction source to emit interaction events to
* @param enabled whether the button is enabled. Specifying false for this will result in the button not responding to
* [onClick]
* @param onClick action to perform when clicking on this button, if [enabled] is true
*/
@Composable
@NonRestartableComposable
public fun PopupDialogButton(
text: String,
modifier: Modifier = Modifier,
colors: PopupDialogButtonColors = PopupDialogButtonDefaults.buttonColors(),
style: TextStyle = PopupDialogButtonDefaults.DialogButtonStyle,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
enabled: Boolean = true,
onClick: () -> Unit = {}
) {
PopupDialogButton(
text = AnnotatedString(text),
modifier = modifier,
colors = colors,
style = style,
interactionSource = interactionSource,
enabled = enabled,
onClick = onClick
)
}
/**
* A popup dialog button that can be used in a [PopupDialog]
*
* This is an optional, however useful and good default button, as a [PopupDialog] allows custom composables to be specified
* for the button slots
*
* @param text button text
* @param modifier modifier to apply to the button
* @param colors colors to apply to the button in various states
* @param style style of the button text
* @param interactionSource mutable interaction source to emit interaction events to
* @param enabled whether the button is enabled. Specifying false for this will result in the button not responding to
* [onClick]
* @param onClick action to perform when clicking on this button, if [enabled] is true
*/
@Composable
public fun PopupDialogButton(
text: AnnotatedString,
modifier: Modifier = Modifier,
colors: PopupDialogButtonColors = PopupDialogButtonDefaults.buttonColors(),
style: TextStyle = PopupDialogButtonDefaults.DialogButtonStyle,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
enabled: Boolean = true,
onClick: () -> Unit = {}
) {
val isPressed by interactionSource.collectIsPressedAsState()
val isFocused by interactionSource.collectIsFocusedAsState()
val color by colors.color(enabled = enabled, pressed = isPressed, focused = isFocused)
Box(
modifier = modifier
.clickable(
enabled = enabled,
interactionSource = interactionSource,
indication = null,
onClick = onClick
),
contentAlignment = Alignment.Center
) {
Text(
text = text,
textAlign = TextAlign.Center,
maxLines = 1,
style = style,
color = color
)
}
}