refactor: move padding and the other components properties to style

This commit is contained in:
2023-11-11 21:36:58 +08:00
parent 2c8baf0423
commit 846f5b1b28
5 changed files with 190 additions and 254 deletions

View File

@@ -35,6 +35,7 @@ import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.ReadOnlyComposable import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.runtime.compositionLocalOf import androidx.compose.runtime.compositionLocalOf
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
@@ -50,27 +51,32 @@ import com.highcapable.flexiui.LocalSizes
import com.highcapable.flexiui.utils.borderOrNot import com.highcapable.flexiui.utils.borderOrNot
import com.highcapable.flexiui.utils.orElse import com.highcapable.flexiui.utils.orElse
@Immutable
data class AreaBoxStyle(
val padding: Dp,
val topPadding: Dp,
val startPadding: Dp,
val bottomPadding: Dp,
val endPadding: Dp,
val shape: Shape,
val border: BorderStroke
)
@Composable @Composable
fun AreaBox( fun AreaBox(
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
padding: Dp = AreaBox.padding,
topPadding: Dp = Dp.Unspecified,
startPadding: Dp = Dp.Unspecified,
bottomPadding: Dp = Dp.Unspecified,
endPadding: Dp = Dp.Unspecified,
shape: Shape = AreaBox.shape,
border: BorderStroke = AreaBox.border,
color: Color = AreaBox.color, color: Color = AreaBox.color,
style: AreaBoxStyle = AreaBox.style,
contentAlignment: Alignment = Alignment.TopStart, contentAlignment: Alignment = Alignment.TopStart,
propagateMinConstraints: Boolean = false, propagateMinConstraints: Boolean = false,
content: @Composable BoxScope.() -> Unit content: @Composable BoxScope.() -> Unit
) { ) {
CompositionLocalProvider( CompositionLocalProvider(
LocalInAreaBox provides true, LocalInAreaBox provides true,
LocalAreaBoxShape provides shape LocalAreaBoxShape provides style.shape
) { ) {
Box( Box(
modifier = modifier.box(padding, topPadding, startPadding, bottomPadding, endPadding, shape, border, color), modifier = modifier.box(style, color),
contentAlignment = contentAlignment, contentAlignment = contentAlignment,
propagateMinConstraints = propagateMinConstraints, propagateMinConstraints = propagateMinConstraints,
content = content content = content
@@ -81,24 +87,18 @@ fun AreaBox(
@Composable @Composable
fun AreaRow( fun AreaRow(
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
padding: Dp = AreaBox.padding,
topPadding: Dp = Dp.Unspecified,
startPadding: Dp = Dp.Unspecified,
bottomPadding: Dp = Dp.Unspecified,
endPadding: Dp = Dp.Unspecified,
shape: Shape = AreaBox.shape,
border: BorderStroke = AreaBox.border,
color: Color = AreaBox.color, color: Color = AreaBox.color,
style: AreaBoxStyle = AreaBox.style,
horizontalArrangement: Arrangement.Horizontal = Arrangement.Start, horizontalArrangement: Arrangement.Horizontal = Arrangement.Start,
verticalAlignment: Alignment.Vertical = Alignment.Top, verticalAlignment: Alignment.Vertical = Alignment.Top,
content: @Composable RowScope.() -> Unit content: @Composable RowScope.() -> Unit
) { ) {
CompositionLocalProvider( CompositionLocalProvider(
LocalInAreaBox provides true, LocalInAreaBox provides true,
LocalAreaBoxShape provides shape LocalAreaBoxShape provides style.shape
) { ) {
Row( Row(
modifier = modifier.box(padding, topPadding, startPadding, bottomPadding, endPadding, shape, border, color), modifier = modifier.box(style, color),
horizontalArrangement = horizontalArrangement, horizontalArrangement = horizontalArrangement,
verticalAlignment = verticalAlignment, verticalAlignment = verticalAlignment,
content = content content = content
@@ -109,24 +109,18 @@ fun AreaRow(
@Composable @Composable
fun AreaColumn( fun AreaColumn(
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
padding: Dp = AreaBox.padding,
topPadding: Dp = Dp.Unspecified,
startPadding: Dp = Dp.Unspecified,
bottomPadding: Dp = Dp.Unspecified,
endPadding: Dp = Dp.Unspecified,
shape: Shape = AreaBox.shape,
border: BorderStroke = AreaBox.border,
color: Color = AreaBox.color, color: Color = AreaBox.color,
style: AreaBoxStyle = AreaBox.style,
verticalArrangement: Arrangement.Vertical = Arrangement.Top, verticalArrangement: Arrangement.Vertical = Arrangement.Top,
horizontalAlignment: Alignment.Horizontal = Alignment.Start, horizontalAlignment: Alignment.Horizontal = Alignment.Start,
content: @Composable ColumnScope.() -> Unit content: @Composable ColumnScope.() -> Unit
) { ) {
CompositionLocalProvider( CompositionLocalProvider(
LocalInAreaBox provides true, LocalInAreaBox provides true,
LocalAreaBoxShape provides shape LocalAreaBoxShape provides style.shape
) { ) {
Column( Column(
modifier = modifier.box(padding, topPadding, startPadding, bottomPadding, endPadding, shape, border, color), modifier = modifier.box(style, color),
verticalArrangement = verticalArrangement, verticalArrangement = verticalArrangement,
horizontalAlignment = horizontalAlignment, horizontalAlignment = horizontalAlignment,
content = content content = content
@@ -134,42 +128,26 @@ fun AreaColumn(
} }
} }
private fun Modifier.box( private fun Modifier.box(style: AreaBoxStyle, color: Color) =
padding: Dp, clip(style.shape)
topPadding: Dp, .background(color, style.shape)
startPadding: Dp, .borderOrNot(style.border, style.shape)
bottomPadding: Dp, .padding(
endPadding: Dp, top = style.topPadding.orElse() ?: style.padding,
shape: Shape, start = style.startPadding.orElse() ?: style.padding,
border: BorderStroke, bottom = style.bottomPadding.orElse() ?: style.padding,
color: Color end = style.endPadding.orElse() ?: style.padding
) = clip(shape = shape) )
.background(color = color, shape = shape)
.borderOrNot(border, shape)
.padding(
top = topPadding.orElse() ?: padding,
start = startPadding.orElse() ?: padding,
bottom = bottomPadding.orElse() ?: padding,
end = endPadding.orElse() ?: padding
)
object AreaBox { object AreaBox {
val padding: Dp
@Composable
@ReadOnlyComposable
get() = defaultAreaBoxPadding()
val shape: Shape
@Composable
@ReadOnlyComposable
get() = defaultAreaBoxShape()
val border: BorderStroke
@Composable
@ReadOnlyComposable
get() = defaultAreaBoxBorder()
val color: Color val color: Color
@Composable @Composable
@ReadOnlyComposable @ReadOnlyComposable
get() = defaultAreaBoxColor() get() = defaultAreaBoxColor()
val style: AreaBoxStyle
@Composable
@ReadOnlyComposable
get() = defaultAreaBoxStyle()
} }
internal val LocalInAreaBox = compositionLocalOf { false } internal val LocalInAreaBox = compositionLocalOf { false }
@@ -180,16 +158,20 @@ internal val DefaultAreaBoxShape: Shape = DefaultShapes.primary
@Composable @Composable
@ReadOnlyComposable @ReadOnlyComposable
private fun defaultAreaBoxPadding() = LocalSizes.current.spacingPrimary private fun defaultAreaBoxStyle() = AreaBoxStyle(
padding = LocalSizes.current.spacingPrimary,
@Composable topPadding = Dp.Unspecified,
@ReadOnlyComposable startPadding = Dp.Unspecified,
private fun defaultAreaBoxShape() = LocalShapes.current.primary bottomPadding = Dp.Unspecified,
endPadding = Dp.Unspecified,
@Composable shape = LocalShapes.current.primary,
@ReadOnlyComposable border = defaultAreaBoxBorder()
private fun defaultAreaBoxBorder() = BorderStroke(LocalSizes.current.borderSizeTertiary, LocalColors.current.textPrimary) )
@Composable @Composable
@ReadOnlyComposable @ReadOnlyComposable
private fun defaultAreaBoxColor() = LocalColors.current.foregroundPrimary private fun defaultAreaBoxColor() = LocalColors.current.foregroundPrimary
@Composable
@ReadOnlyComposable
private fun defaultAreaBoxBorder() = BorderStroke(LocalSizes.current.borderSizeTertiary, LocalColors.current.textPrimary)

View File

@@ -58,25 +58,30 @@ data class ButtonColors(
val backgroundColor: Color val backgroundColor: Color
) )
@Immutable
data class ButtonStyle(
val padding: Dp,
val topPadding: Dp,
val startPadding: Dp,
val bottomPadding: Dp,
val endPadding: Dp,
val shape: Shape,
val border: BorderStroke
)
@Composable @Composable
fun Button( fun Button(
onClick: () -> Unit, onClick: () -> Unit,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
padding: Dp = Dp.Unspecified,
topPadding: Dp = Button.topPadding,
startPadding: Dp = Button.startPadding,
bottomPadding: Dp = Button.bottomPadding,
endPadding: Dp = Button.endPadding,
shape: Shape = Button.shape,
border: BorderStroke = Button.border,
colors: ButtonColors = Button.colors, colors: ButtonColors = Button.colors,
style: ButtonStyle = Button.style,
enabled: Boolean = true, enabled: Boolean = true,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
header: @Composable () -> Unit = {}, header: @Composable () -> Unit = {},
footer: @Composable () -> Unit = {}, footer: @Composable () -> Unit = {},
content: @Composable RowScope.() -> Unit content: @Composable RowScope.() -> Unit
) { ) {
var sModifier = modifier.clip(shape = shape) var sModifier = modifier.clip(style.shape)
sModifier = if (enabled) sModifier.rippleClickable( sModifier = if (enabled) sModifier.rippleClickable(
enabled = enabled, enabled = enabled,
role = Role.Button, role = Role.Button,
@@ -84,8 +89,8 @@ fun Button(
interactionSource = interactionSource, interactionSource = interactionSource,
onClick = onClick onClick = onClick
) else sModifier.alpha(0.5f) ) else sModifier.alpha(0.5f)
sModifier = sModifier.background(color = colors.backgroundColor, shape = shape) sModifier = sModifier.background(colors.backgroundColor, style.shape)
sModifier = sModifier.borderOrNot(border = border, shape = shape) sModifier = sModifier.borderOrNot(style.border, style.shape)
val localTextStyle = LocalTextStyle.current.copy(color = colors.contentColor) val localTextStyle = LocalTextStyle.current.copy(color = colors.contentColor)
val localProgressIndicatorColors = LocalProgressIndicatorColors.current.copy( val localProgressIndicatorColors = LocalProgressIndicatorColors.current.copy(
foregroundColor = colors.contentColor, foregroundColor = colors.contentColor,
@@ -98,10 +103,10 @@ fun Button(
) { ) {
Row( Row(
Modifier.padding( Modifier.padding(
top = topPadding.orElse() ?: padding, top = style.topPadding.orElse() ?: style.padding,
start = startPadding.orElse() ?: padding, start = style.startPadding.orElse() ?: style.padding,
bottom = bottomPadding.orElse() ?: padding, bottom = style.bottomPadding.orElse() ?: style.padding,
end = endPadding.orElse() ?: padding end = style.endPadding.orElse() ?: style.padding
) )
) { ) {
header() header()
@@ -159,33 +164,6 @@ fun IconToggleButton(
} }
object Button { object Button {
val topPadding: Dp
@Composable
@ReadOnlyComposable
get() = defalutButtonPaddings()[0]
val startPadding: Dp
@Composable
@ReadOnlyComposable
get() = defalutButtonPaddings()[1]
val bottomPadding: Dp
@Composable
@ReadOnlyComposable
get() = defalutButtonPaddings()[2]
val endPadding: Dp
@Composable
@ReadOnlyComposable
get() = defalutButtonPaddings()[3]
val shape: Shape
@Composable
@ReadOnlyComposable
get() = when (LocalInAreaBox.current) {
true -> LocalAreaBoxShape.current
else -> defaultButtonShape()
}
val border: BorderStroke
@Composable
@ReadOnlyComposable
get() = defaultButtonBorder()
val colors: ButtonColors val colors: ButtonColors
@Composable @Composable
@ReadOnlyComposable @ReadOnlyComposable
@@ -193,25 +171,12 @@ object Button {
true -> defaultButtonInBoxColors() true -> defaultButtonInBoxColors()
else -> defaultButtonOutBoxColors() else -> defaultButtonOutBoxColors()
} }
val style: ButtonStyle
@Composable
@ReadOnlyComposable
get() = defaultButtonStyle()
} }
@Composable
@ReadOnlyComposable
private fun defalutButtonPaddings() = arrayOf(
LocalSizes.current.spacingSecondary,
LocalSizes.current.spacingPrimary,
LocalSizes.current.spacingSecondary,
LocalSizes.current.spacingPrimary
)
@Composable
@ReadOnlyComposable
private fun defaultButtonBorder() = BorderStroke(LocalSizes.current.borderSizeTertiary, LocalColors.current.textPrimary)
@Composable
@ReadOnlyComposable
private fun defaultButtonShape() = LocalShapes.current.tertiary
@Composable @Composable
@ReadOnlyComposable @ReadOnlyComposable
private fun defaultButtonInBoxColors() = ButtonColors( private fun defaultButtonInBoxColors() = ButtonColors(
@@ -227,3 +192,22 @@ private fun defaultButtonOutBoxColors() = ButtonColors(
contentColor = Color.White, contentColor = Color.White,
backgroundColor = LocalColors.current.themePrimary backgroundColor = LocalColors.current.themePrimary
) )
@Composable
@ReadOnlyComposable
private fun defaultButtonStyle() = ButtonStyle(
padding = Dp.Unspecified,
topPadding = LocalSizes.current.spacingSecondary,
startPadding = LocalSizes.current.spacingPrimary,
bottomPadding = LocalSizes.current.spacingSecondary,
endPadding = LocalSizes.current.spacingPrimary,
shape = when (LocalInAreaBox.current) {
true -> LocalAreaBoxShape.current
else -> LocalShapes.current.tertiary
},
border = defaultButtonBorder()
)
@Composable
@ReadOnlyComposable
private fun defaultButtonBorder() = BorderStroke(LocalSizes.current.borderSizeTertiary, LocalColors.current.textPrimary)

View File

@@ -29,6 +29,7 @@ import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.ReadOnlyComposable import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
@@ -39,64 +40,68 @@ import com.highcapable.flexiui.utils.orElse
// TODO: Linkage BetterAndroid SafeArea (SystemBarsController) // TODO: Linkage BetterAndroid SafeArea (SystemBarsController)
@Immutable
data class SurfaceColors(
val contentColor: Color,
val backgroundColor: Color
)
@Immutable
data class SurfaceStyle(
val padding: Dp,
val topPadding: Dp,
val startPadding: Dp,
val bottomPadding: Dp,
val endPadding: Dp
)
@Composable @Composable
fun Surface( fun Surface(
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
padding: Dp = Surface.padding, colors: SurfaceColors = Surface.colors,
topPadding: Dp = Dp.Unspecified, style: SurfaceStyle = Surface.style,
startPadding: Dp = Dp.Unspecified,
bottomPadding: Dp = Dp.Unspecified,
endPadding: Dp = Dp.Unspecified,
color: Color = Surface.color,
contentColor: Color = Surface.contentColor,
content: @Composable BoxScope.() -> Unit content: @Composable BoxScope.() -> Unit
) { ) {
CompositionLocalProvider( CompositionLocalProvider(
LocalColors provides LocalColors.current.copy( LocalColors provides LocalColors.current.copy(
backgroundPrimary = color, backgroundPrimary = colors.backgroundColor,
textPrimary = contentColor textPrimary = colors.contentColor
) )
) { Box(modifier.surface(padding, topPadding, startPadding, bottomPadding, endPadding, color), content = content) } ) { Box(modifier.surface(colors, style), content = content) }
} }
private fun Modifier.surface( private fun Modifier.surface(colors: SurfaceColors, style: SurfaceStyle) =
padding: Dp, background(colors.backgroundColor).padding(
topPadding: Dp, top = style.topPadding.orElse() ?: style.padding,
startPadding: Dp, start = style.startPadding.orElse() ?: style.padding,
bottomPadding: Dp, bottom = style.bottomPadding.orElse() ?: style.padding,
endPadding: Dp, end = style.endPadding.orElse() ?: style.padding
color: Color
) = background(color = color)
.padding(
top = topPadding.orElse() ?: padding,
start = startPadding.orElse() ?: padding,
bottom = bottomPadding.orElse() ?: padding,
end = endPadding.orElse() ?: padding
) )
object Surface { object Surface {
val color: Color val colors: SurfaceColors
@Composable @Composable
@ReadOnlyComposable @ReadOnlyComposable
get() = defaultSurfaceColor() get() = defaultSurfaceColors()
val contentColor: Color val style: SurfaceStyle
@Composable @Composable
@ReadOnlyComposable @ReadOnlyComposable
get() = defaultSurfaceContentColor() get() = defaultSurfaceStyle()
val padding: Dp
@Composable
@ReadOnlyComposable
get() = defaultSurfacePadding()
} }
@Composable @Composable
@ReadOnlyComposable @ReadOnlyComposable
private fun defaultSurfacePadding() = LocalSizes.current.spacingPrimary private fun defaultSurfaceColors() = SurfaceColors(
contentColor = LocalColors.current.textPrimary,
backgroundColor = LocalColors.current.backgroundPrimary
)
@Composable @Composable
@ReadOnlyComposable @ReadOnlyComposable
private fun defaultSurfaceColor() = LocalColors.current.backgroundPrimary private fun defaultSurfaceStyle() = SurfaceStyle(
padding = LocalSizes.current.spacingPrimary,
@Composable topPadding = Dp.Unspecified,
@ReadOnlyComposable startPadding = Dp.Unspecified,
private fun defaultSurfaceContentColor() = LocalColors.current.textPrimary bottomPadding = Dp.Unspecified,
endPadding = Dp.Unspecified
)

View File

@@ -75,6 +75,7 @@ data class SwitchColors(
@Immutable @Immutable
data class SwitchStyle( data class SwitchStyle(
val padding: Dp,
val thumbDiameter: Dp, val thumbDiameter: Dp,
val thumbGain: Float, val thumbGain: Float,
val thumbShadowSize: Dp, val thumbShadowSize: Dp,
@@ -91,7 +92,6 @@ fun Switch(
checked: Boolean, checked: Boolean,
onCheckedChange: (Boolean) -> Unit, onCheckedChange: (Boolean) -> Unit,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
padding: Dp = Switch.padding,
colors: SwitchColors = Switch.colors, colors: SwitchColors = Switch.colors,
style: SwitchStyle = Switch.style, style: SwitchStyle = Switch.style,
enabled: Boolean = true, enabled: Boolean = true,
@@ -99,7 +99,7 @@ fun Switch(
contentSpacing: Dp = Switch.contentSpacing, contentSpacing: Dp = Switch.contentSpacing,
content: @Composable () -> Unit = {} content: @Composable () -> Unit = {}
) { ) {
val maxOffset = with(LocalDensity.current) { (style.trackWidth - style.thumbDiameter - padding * 2).toPx() } val maxOffset = with(LocalDensity.current) { (style.trackWidth - style.thumbDiameter - style.padding * 2).toPx() }
val halfWidth = maxOffset / 2 val halfWidth = maxOffset / 2
val hovered by interactionSource.collectIsHoveredAsState() val hovered by interactionSource.collectIsHoveredAsState()
var dragging by remember { mutableStateOf(false) } var dragging by remember { mutableStateOf(false) }
@@ -131,7 +131,7 @@ fun Switch(
}.background(if (efficientDragging) trackColor else animatedTrackColor, style.trackShape) }.background(if (efficientDragging) trackColor else animatedTrackColor, style.trackShape)
.borderOrNot(style.trackBorder, style.trackShape) .borderOrNot(style.trackBorder, style.trackShape)
.size(style.trackWidth, style.trackHeight) .size(style.trackWidth, style.trackHeight)
.padding(start = padding, end = padding), .padding(start = style.padding, end = style.padding),
verticalAlignment = Alignment.CenterVertically, verticalAlignment = Alignment.CenterVertically,
content = content content = content
) )
@@ -180,10 +180,6 @@ fun Switch(
} }
object Switch { object Switch {
val padding: Dp
@Composable
@ReadOnlyComposable
get() = DefaultSwitchPadding
val colors: SwitchColors val colors: SwitchColors
@Composable @Composable
@ReadOnlyComposable @ReadOnlyComposable
@@ -209,6 +205,7 @@ private fun defaultSwitchColors() = SwitchColors(
@Composable @Composable
@ReadOnlyComposable @ReadOnlyComposable
private fun defaultSwitchStyle() = SwitchStyle( private fun defaultSwitchStyle() = SwitchStyle(
padding = DefaultSwitchPadding,
thumbDiameter = DefaultThumbDiameter, thumbDiameter = DefaultThumbDiameter,
thumbGain = DefaultThumbGain, thumbGain = DefaultThumbGain,
thumbShadowSize = DefaultThumbShadowSize, thumbShadowSize = DefaultThumbShadowSize,

View File

@@ -70,20 +70,25 @@ data class TextFieldColors(
val backgroundColor: Color val backgroundColor: Color
) )
@Immutable
data class TextFieldStyle(
val padding: Dp,
val topPadding: Dp,
val startPadding: Dp,
val bottomPadding: Dp,
val endPadding: Dp,
val shape: Shape,
val borderInactive: BorderStroke,
val borderActive: BorderStroke
)
@Composable @Composable
fun TextField( fun TextField(
value: String, value: String,
onValueChange: (String) -> Unit, onValueChange: (String) -> Unit,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
padding: Dp = TextField.padding,
topPadding: Dp = Dp.Unspecified,
startPadding: Dp = Dp.Unspecified,
bottomPadding: Dp = Dp.Unspecified,
endPadding: Dp = Dp.Unspecified,
shape: Shape = TextField.shape,
borderInactive: BorderStroke = TextField.borderInactive,
borderActive: BorderStroke = TextField.borderActive,
colors: TextFieldColors = TextField.colors, colors: TextFieldColors = TextField.colors,
style: TextFieldStyle = TextField.style,
enabled: Boolean = true, enabled: Boolean = true,
readOnly: Boolean = false, readOnly: Boolean = false,
keyboardOptions: KeyboardOptions = KeyboardOptions.Default, keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
@@ -97,7 +102,7 @@ fun TextField(
header: @Composable () -> Unit = {}, header: @Composable () -> Unit = {},
placeholder: @Composable () -> Unit = {}, placeholder: @Composable () -> Unit = {},
footer: @Composable () -> Unit = {}, footer: @Composable () -> Unit = {},
style: TextStyle = TextField.style textStyle: TextStyle = TextField.textStyle
) { ) {
TextFieldStyle(colors) { TextFieldStyle(colors) {
BasicTextField( BasicTextField(
@@ -106,7 +111,7 @@ fun TextField(
modifier = modifier, modifier = modifier,
enabled = enabled, enabled = enabled,
readOnly = readOnly, readOnly = readOnly,
textStyle = style, textStyle = textStyle,
keyboardOptions = keyboardOptions, keyboardOptions = keyboardOptions,
keyboardActions = keyboardActions, keyboardActions = keyboardActions,
singleLine = singleLine, singleLine = singleLine,
@@ -120,15 +125,8 @@ fun TextField(
TextFieldDecorationBox( TextFieldDecorationBox(
value = value, value = value,
modifier = modifier, modifier = modifier,
padding = padding,
topPadding = topPadding,
startPadding = startPadding,
bottomPadding = bottomPadding,
endPadding = endPadding,
shape = shape,
borderInactive = borderInactive,
borderActive = borderActive,
colors = colors, colors = colors,
style = style,
enabled = enabled, enabled = enabled,
interactionSource = interactionSource, interactionSource = interactionSource,
header = header, header = header,
@@ -146,15 +144,8 @@ fun TextField(
value: TextFieldValue, value: TextFieldValue,
onValueChange: (TextFieldValue) -> Unit, onValueChange: (TextFieldValue) -> Unit,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
padding: Dp = TextField.padding,
topPadding: Dp = Dp.Unspecified,
startPadding: Dp = Dp.Unspecified,
bottomPadding: Dp = Dp.Unspecified,
endPadding: Dp = Dp.Unspecified,
shape: Shape = TextField.shape,
borderInactive: BorderStroke = TextField.borderInactive,
borderActive: BorderStroke = TextField.borderActive,
colors: TextFieldColors = TextField.colors, colors: TextFieldColors = TextField.colors,
style: TextFieldStyle = TextField.style,
enabled: Boolean = true, enabled: Boolean = true,
readOnly: Boolean = false, readOnly: Boolean = false,
keyboardOptions: KeyboardOptions = KeyboardOptions.Default, keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
@@ -168,7 +159,7 @@ fun TextField(
header: @Composable () -> Unit = {}, header: @Composable () -> Unit = {},
placeholder: @Composable () -> Unit = {}, placeholder: @Composable () -> Unit = {},
footer: @Composable () -> Unit = {}, footer: @Composable () -> Unit = {},
style: TextStyle = TextField.style textStyle: TextStyle = TextField.textStyle
) { ) {
TextFieldStyle(colors) { TextFieldStyle(colors) {
BasicTextField( BasicTextField(
@@ -177,7 +168,7 @@ fun TextField(
modifier = modifier, modifier = modifier,
enabled = enabled, enabled = enabled,
readOnly = readOnly, readOnly = readOnly,
textStyle = style, textStyle = textStyle,
keyboardOptions = keyboardOptions, keyboardOptions = keyboardOptions,
keyboardActions = keyboardActions, keyboardActions = keyboardActions,
singleLine = singleLine, singleLine = singleLine,
@@ -191,15 +182,8 @@ fun TextField(
TextFieldDecorationBox( TextFieldDecorationBox(
value = value.text, value = value.text,
modifier = modifier, modifier = modifier,
padding = padding,
topPadding = topPadding,
startPadding = startPadding,
bottomPadding = bottomPadding,
endPadding = endPadding,
shape = shape,
borderInactive = borderInactive,
borderActive = borderActive,
colors = colors, colors = colors,
style = style,
enabled = enabled, enabled = enabled,
interactionSource = interactionSource, interactionSource = interactionSource,
header = header, header = header,
@@ -223,15 +207,8 @@ private fun TextFieldStyle(colors: TextFieldColors, content: @Composable () -> U
private fun TextFieldDecorationBox( private fun TextFieldDecorationBox(
value: String, value: String,
modifier: Modifier, modifier: Modifier,
padding: Dp,
topPadding: Dp,
startPadding: Dp,
bottomPadding: Dp,
endPadding: Dp,
shape: Shape,
borderInactive: BorderStroke,
borderActive: BorderStroke,
colors: TextFieldColors, colors: TextFieldColors,
style: TextFieldStyle,
enabled: Boolean, enabled: Boolean,
interactionSource: MutableInteractionSource, interactionSource: MutableInteractionSource,
header: @Composable () -> Unit, header: @Composable () -> Unit,
@@ -240,17 +217,12 @@ private fun TextFieldDecorationBox(
innerTextField: @Composable () -> Unit innerTextField: @Composable () -> Unit
) { ) {
val focused by interactionSource.collectIsFocusedAsState() val focused by interactionSource.collectIsFocusedAsState()
val border = if (focused) borderActive else borderInactive val border = if (focused) style.borderActive else style.borderInactive
Box( Box(
modifier.textField( modifier.textField(
padding = padding,
topPadding = topPadding,
startPadding = startPadding,
bottomPadding = bottomPadding,
endPadding = endPadding,
shape = shape,
border = border,
colors = colors, colors = colors,
style = style,
border = border,
enabled = enabled enabled = enabled
) )
) { ) {
@@ -271,54 +243,34 @@ private fun TextFieldDecorationBox(
} }
private fun Modifier.textField( private fun Modifier.textField(
padding: Dp,
topPadding: Dp,
startPadding: Dp,
bottomPadding: Dp,
endPadding: Dp,
shape: Shape,
border: BorderStroke,
colors: TextFieldColors, colors: TextFieldColors,
style: TextFieldStyle,
border: BorderStroke,
enabled: Boolean enabled: Boolean
): Modifier { ): Modifier {
var sModifier = clip(shape = shape) var sModifier = clip(style.shape)
.background(color = colors.backgroundColor, shape = shape) .background(colors.backgroundColor, style.shape)
.borderOrNot(border, shape) .borderOrNot(border, style.shape)
.padding( .padding(
top = topPadding.orElse() ?: padding, top = style.topPadding.orElse() ?: style.padding,
start = startPadding.orElse() ?: padding, start = style.startPadding.orElse() ?: style.padding,
bottom = bottomPadding.orElse() ?: padding, bottom = style.bottomPadding.orElse() ?: style.padding,
end = endPadding.orElse() ?: padding end = style.endPadding.orElse() ?: style.padding
) )
if (!enabled) sModifier = sModifier.alpha(0.5f) if (!enabled) sModifier = sModifier.alpha(0.5f)
return sModifier return sModifier
} }
object TextField { object TextField {
val padding: Dp
@Composable
@ReadOnlyComposable
get() = LocalSizes.current.spacingSecondary
val shape: Shape
@Composable
@ReadOnlyComposable
get() = when (LocalInAreaBox.current) {
true -> LocalAreaBoxShape.current
else -> LocalShapes.current.primary
}
val borderInactive: BorderStroke
@Composable
@ReadOnlyComposable
get() = defaultTextFieldInActiveBorder()
val borderActive: BorderStroke
@Composable
@ReadOnlyComposable
get() = defaultTextFieldActiveBorder()
val colors: TextFieldColors val colors: TextFieldColors
@Composable @Composable
@ReadOnlyComposable @ReadOnlyComposable
get() = defaultTextFieldColors() get() = defaultTextFieldColors()
val style: TextStyle val style: TextFieldStyle
@Composable
@ReadOnlyComposable
get() = defaultTextFieldStyle()
val textStyle: TextStyle
@Composable @Composable
@ReadOnlyComposable @ReadOnlyComposable
get() = LocalTextStyle.current.default(LocalColors.current.textPrimary) get() = LocalTextStyle.current.default(LocalColors.current.textPrimary)
@@ -339,7 +291,23 @@ private fun defaultTextFieldColors() = TextFieldColors(
@Composable @Composable
@ReadOnlyComposable @ReadOnlyComposable
private fun defaultTextFieldInActiveBorder() = BorderStroke(LocalSizes.current.borderSizeSecondary, LocalColors.current.themeSecondary) private fun defaultTextFieldStyle() = TextFieldStyle(
padding = LocalSizes.current.spacingSecondary,
topPadding = Dp.Unspecified,
startPadding = Dp.Unspecified,
bottomPadding = Dp.Unspecified,
endPadding = Dp.Unspecified,
shape = when (LocalInAreaBox.current) {
true -> LocalAreaBoxShape.current
else -> LocalShapes.current.secondary
},
borderInactive = defaultTextFieldInactiveBorder(),
borderActive = defaultTextFieldActiveBorder()
)
@Composable
@ReadOnlyComposable
private fun defaultTextFieldInactiveBorder() = BorderStroke(LocalSizes.current.borderSizeSecondary, LocalColors.current.themeSecondary)
@Composable @Composable
@ReadOnlyComposable @ReadOnlyComposable