mirror of
https://github.com/BetterAndroid/FlexiUI.git
synced 2025-09-07 19:14:12 +08:00
refactor: move padding and the other components properties to style
This commit is contained in:
@@ -35,6 +35,7 @@ import androidx.compose.foundation.layout.RowScope
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.CompositionLocalProvider
|
||||
import androidx.compose.runtime.Immutable
|
||||
import androidx.compose.runtime.ReadOnlyComposable
|
||||
import androidx.compose.runtime.compositionLocalOf
|
||||
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.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
|
||||
fun AreaBox(
|
||||
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,
|
||||
style: AreaBoxStyle = AreaBox.style,
|
||||
contentAlignment: Alignment = Alignment.TopStart,
|
||||
propagateMinConstraints: Boolean = false,
|
||||
content: @Composable BoxScope.() -> Unit
|
||||
) {
|
||||
CompositionLocalProvider(
|
||||
LocalInAreaBox provides true,
|
||||
LocalAreaBoxShape provides shape
|
||||
LocalAreaBoxShape provides style.shape
|
||||
) {
|
||||
Box(
|
||||
modifier = modifier.box(padding, topPadding, startPadding, bottomPadding, endPadding, shape, border, color),
|
||||
modifier = modifier.box(style, color),
|
||||
contentAlignment = contentAlignment,
|
||||
propagateMinConstraints = propagateMinConstraints,
|
||||
content = content
|
||||
@@ -81,24 +87,18 @@ fun AreaBox(
|
||||
@Composable
|
||||
fun AreaRow(
|
||||
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,
|
||||
style: AreaBoxStyle = AreaBox.style,
|
||||
horizontalArrangement: Arrangement.Horizontal = Arrangement.Start,
|
||||
verticalAlignment: Alignment.Vertical = Alignment.Top,
|
||||
content: @Composable RowScope.() -> Unit
|
||||
) {
|
||||
CompositionLocalProvider(
|
||||
LocalInAreaBox provides true,
|
||||
LocalAreaBoxShape provides shape
|
||||
LocalAreaBoxShape provides style.shape
|
||||
) {
|
||||
Row(
|
||||
modifier = modifier.box(padding, topPadding, startPadding, bottomPadding, endPadding, shape, border, color),
|
||||
modifier = modifier.box(style, color),
|
||||
horizontalArrangement = horizontalArrangement,
|
||||
verticalAlignment = verticalAlignment,
|
||||
content = content
|
||||
@@ -109,24 +109,18 @@ fun AreaRow(
|
||||
@Composable
|
||||
fun AreaColumn(
|
||||
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,
|
||||
style: AreaBoxStyle = AreaBox.style,
|
||||
verticalArrangement: Arrangement.Vertical = Arrangement.Top,
|
||||
horizontalAlignment: Alignment.Horizontal = Alignment.Start,
|
||||
content: @Composable ColumnScope.() -> Unit
|
||||
) {
|
||||
CompositionLocalProvider(
|
||||
LocalInAreaBox provides true,
|
||||
LocalAreaBoxShape provides shape
|
||||
LocalAreaBoxShape provides style.shape
|
||||
) {
|
||||
Column(
|
||||
modifier = modifier.box(padding, topPadding, startPadding, bottomPadding, endPadding, shape, border, color),
|
||||
modifier = modifier.box(style, color),
|
||||
verticalArrangement = verticalArrangement,
|
||||
horizontalAlignment = horizontalAlignment,
|
||||
content = content
|
||||
@@ -134,42 +128,26 @@ fun AreaColumn(
|
||||
}
|
||||
}
|
||||
|
||||
private fun Modifier.box(
|
||||
padding: Dp,
|
||||
topPadding: Dp,
|
||||
startPadding: Dp,
|
||||
bottomPadding: Dp,
|
||||
endPadding: Dp,
|
||||
shape: Shape,
|
||||
border: BorderStroke,
|
||||
color: Color
|
||||
) = 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
|
||||
)
|
||||
private fun Modifier.box(style: AreaBoxStyle, color: Color) =
|
||||
clip(style.shape)
|
||||
.background(color, style.shape)
|
||||
.borderOrNot(style.border, style.shape)
|
||||
.padding(
|
||||
top = style.topPadding.orElse() ?: style.padding,
|
||||
start = style.startPadding.orElse() ?: style.padding,
|
||||
bottom = style.bottomPadding.orElse() ?: style.padding,
|
||||
end = style.endPadding.orElse() ?: style.padding
|
||||
)
|
||||
|
||||
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
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
get() = defaultAreaBoxColor()
|
||||
val style: AreaBoxStyle
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
get() = defaultAreaBoxStyle()
|
||||
}
|
||||
|
||||
internal val LocalInAreaBox = compositionLocalOf { false }
|
||||
@@ -180,16 +158,20 @@ internal val DefaultAreaBoxShape: Shape = DefaultShapes.primary
|
||||
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
private fun defaultAreaBoxPadding() = LocalSizes.current.spacingPrimary
|
||||
private fun defaultAreaBoxStyle() = AreaBoxStyle(
|
||||
padding = LocalSizes.current.spacingPrimary,
|
||||
topPadding = Dp.Unspecified,
|
||||
startPadding = Dp.Unspecified,
|
||||
bottomPadding = Dp.Unspecified,
|
||||
endPadding = Dp.Unspecified,
|
||||
shape = LocalShapes.current.primary,
|
||||
border = defaultAreaBoxBorder()
|
||||
)
|
||||
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
private fun defaultAreaBoxShape() = LocalShapes.current.primary
|
||||
private fun defaultAreaBoxColor() = LocalColors.current.foregroundPrimary
|
||||
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
private fun defaultAreaBoxBorder() = BorderStroke(LocalSizes.current.borderSizeTertiary, LocalColors.current.textPrimary)
|
||||
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
private fun defaultAreaBoxColor() = LocalColors.current.foregroundPrimary
|
||||
private fun defaultAreaBoxBorder() = BorderStroke(LocalSizes.current.borderSizeTertiary, LocalColors.current.textPrimary)
|
@@ -58,25 +58,30 @@ data class ButtonColors(
|
||||
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
|
||||
fun Button(
|
||||
onClick: () -> Unit,
|
||||
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,
|
||||
style: ButtonStyle = Button.style,
|
||||
enabled: Boolean = true,
|
||||
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
|
||||
header: @Composable () -> Unit = {},
|
||||
footer: @Composable () -> Unit = {},
|
||||
content: @Composable RowScope.() -> Unit
|
||||
) {
|
||||
var sModifier = modifier.clip(shape = shape)
|
||||
var sModifier = modifier.clip(style.shape)
|
||||
sModifier = if (enabled) sModifier.rippleClickable(
|
||||
enabled = enabled,
|
||||
role = Role.Button,
|
||||
@@ -84,8 +89,8 @@ fun Button(
|
||||
interactionSource = interactionSource,
|
||||
onClick = onClick
|
||||
) else sModifier.alpha(0.5f)
|
||||
sModifier = sModifier.background(color = colors.backgroundColor, shape = shape)
|
||||
sModifier = sModifier.borderOrNot(border = border, shape = shape)
|
||||
sModifier = sModifier.background(colors.backgroundColor, style.shape)
|
||||
sModifier = sModifier.borderOrNot(style.border, style.shape)
|
||||
val localTextStyle = LocalTextStyle.current.copy(color = colors.contentColor)
|
||||
val localProgressIndicatorColors = LocalProgressIndicatorColors.current.copy(
|
||||
foregroundColor = colors.contentColor,
|
||||
@@ -98,10 +103,10 @@ fun Button(
|
||||
) {
|
||||
Row(
|
||||
Modifier.padding(
|
||||
top = topPadding.orElse() ?: padding,
|
||||
start = startPadding.orElse() ?: padding,
|
||||
bottom = bottomPadding.orElse() ?: padding,
|
||||
end = endPadding.orElse() ?: padding
|
||||
top = style.topPadding.orElse() ?: style.padding,
|
||||
start = style.startPadding.orElse() ?: style.padding,
|
||||
bottom = style.bottomPadding.orElse() ?: style.padding,
|
||||
end = style.endPadding.orElse() ?: style.padding
|
||||
)
|
||||
) {
|
||||
header()
|
||||
@@ -159,33 +164,6 @@ fun IconToggleButton(
|
||||
}
|
||||
|
||||
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
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
@@ -193,25 +171,12 @@ object Button {
|
||||
true -> defaultButtonInBoxColors()
|
||||
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
|
||||
@ReadOnlyComposable
|
||||
private fun defaultButtonInBoxColors() = ButtonColors(
|
||||
@@ -226,4 +191,23 @@ private fun defaultButtonOutBoxColors() = ButtonColors(
|
||||
rippleColor = LocalColors.current.foregroundSecondary,
|
||||
contentColor = Color.White,
|
||||
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)
|
@@ -29,6 +29,7 @@ import androidx.compose.foundation.layout.BoxScope
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.CompositionLocalProvider
|
||||
import androidx.compose.runtime.Immutable
|
||||
import androidx.compose.runtime.ReadOnlyComposable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
@@ -39,64 +40,68 @@ import com.highcapable.flexiui.utils.orElse
|
||||
|
||||
// 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
|
||||
fun Surface(
|
||||
modifier: Modifier = Modifier,
|
||||
padding: Dp = Surface.padding,
|
||||
topPadding: Dp = Dp.Unspecified,
|
||||
startPadding: Dp = Dp.Unspecified,
|
||||
bottomPadding: Dp = Dp.Unspecified,
|
||||
endPadding: Dp = Dp.Unspecified,
|
||||
color: Color = Surface.color,
|
||||
contentColor: Color = Surface.contentColor,
|
||||
colors: SurfaceColors = Surface.colors,
|
||||
style: SurfaceStyle = Surface.style,
|
||||
content: @Composable BoxScope.() -> Unit
|
||||
) {
|
||||
CompositionLocalProvider(
|
||||
LocalColors provides LocalColors.current.copy(
|
||||
backgroundPrimary = color,
|
||||
textPrimary = contentColor
|
||||
backgroundPrimary = colors.backgroundColor,
|
||||
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(
|
||||
padding: Dp,
|
||||
topPadding: Dp,
|
||||
startPadding: Dp,
|
||||
bottomPadding: Dp,
|
||||
endPadding: Dp,
|
||||
color: Color
|
||||
) = background(color = color)
|
||||
.padding(
|
||||
top = topPadding.orElse() ?: padding,
|
||||
start = startPadding.orElse() ?: padding,
|
||||
bottom = bottomPadding.orElse() ?: padding,
|
||||
end = endPadding.orElse() ?: padding
|
||||
private fun Modifier.surface(colors: SurfaceColors, style: SurfaceStyle) =
|
||||
background(colors.backgroundColor).padding(
|
||||
top = style.topPadding.orElse() ?: style.padding,
|
||||
start = style.startPadding.orElse() ?: style.padding,
|
||||
bottom = style.bottomPadding.orElse() ?: style.padding,
|
||||
end = style.endPadding.orElse() ?: style.padding
|
||||
)
|
||||
|
||||
object Surface {
|
||||
val color: Color
|
||||
val colors: SurfaceColors
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
get() = defaultSurfaceColor()
|
||||
val contentColor: Color
|
||||
get() = defaultSurfaceColors()
|
||||
val style: SurfaceStyle
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
get() = defaultSurfaceContentColor()
|
||||
val padding: Dp
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
get() = defaultSurfacePadding()
|
||||
get() = defaultSurfaceStyle()
|
||||
}
|
||||
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
private fun defaultSurfacePadding() = LocalSizes.current.spacingPrimary
|
||||
private fun defaultSurfaceColors() = SurfaceColors(
|
||||
contentColor = LocalColors.current.textPrimary,
|
||||
backgroundColor = LocalColors.current.backgroundPrimary
|
||||
)
|
||||
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
private fun defaultSurfaceColor() = LocalColors.current.backgroundPrimary
|
||||
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
private fun defaultSurfaceContentColor() = LocalColors.current.textPrimary
|
||||
private fun defaultSurfaceStyle() = SurfaceStyle(
|
||||
padding = LocalSizes.current.spacingPrimary,
|
||||
topPadding = Dp.Unspecified,
|
||||
startPadding = Dp.Unspecified,
|
||||
bottomPadding = Dp.Unspecified,
|
||||
endPadding = Dp.Unspecified
|
||||
)
|
@@ -75,6 +75,7 @@ data class SwitchColors(
|
||||
|
||||
@Immutable
|
||||
data class SwitchStyle(
|
||||
val padding: Dp,
|
||||
val thumbDiameter: Dp,
|
||||
val thumbGain: Float,
|
||||
val thumbShadowSize: Dp,
|
||||
@@ -91,7 +92,6 @@ fun Switch(
|
||||
checked: Boolean,
|
||||
onCheckedChange: (Boolean) -> Unit,
|
||||
modifier: Modifier = Modifier,
|
||||
padding: Dp = Switch.padding,
|
||||
colors: SwitchColors = Switch.colors,
|
||||
style: SwitchStyle = Switch.style,
|
||||
enabled: Boolean = true,
|
||||
@@ -99,7 +99,7 @@ fun Switch(
|
||||
contentSpacing: Dp = Switch.contentSpacing,
|
||||
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 hovered by interactionSource.collectIsHoveredAsState()
|
||||
var dragging by remember { mutableStateOf(false) }
|
||||
@@ -131,7 +131,7 @@ fun Switch(
|
||||
}.background(if (efficientDragging) trackColor else animatedTrackColor, style.trackShape)
|
||||
.borderOrNot(style.trackBorder, style.trackShape)
|
||||
.size(style.trackWidth, style.trackHeight)
|
||||
.padding(start = padding, end = padding),
|
||||
.padding(start = style.padding, end = style.padding),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
content = content
|
||||
)
|
||||
@@ -180,10 +180,6 @@ fun Switch(
|
||||
}
|
||||
|
||||
object Switch {
|
||||
val padding: Dp
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
get() = DefaultSwitchPadding
|
||||
val colors: SwitchColors
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
@@ -209,6 +205,7 @@ private fun defaultSwitchColors() = SwitchColors(
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
private fun defaultSwitchStyle() = SwitchStyle(
|
||||
padding = DefaultSwitchPadding,
|
||||
thumbDiameter = DefaultThumbDiameter,
|
||||
thumbGain = DefaultThumbGain,
|
||||
thumbShadowSize = DefaultThumbShadowSize,
|
||||
|
@@ -70,20 +70,25 @@ data class TextFieldColors(
|
||||
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
|
||||
fun TextField(
|
||||
value: String,
|
||||
onValueChange: (String) -> Unit,
|
||||
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,
|
||||
style: TextFieldStyle = TextField.style,
|
||||
enabled: Boolean = true,
|
||||
readOnly: Boolean = false,
|
||||
keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
|
||||
@@ -97,7 +102,7 @@ fun TextField(
|
||||
header: @Composable () -> Unit = {},
|
||||
placeholder: @Composable () -> Unit = {},
|
||||
footer: @Composable () -> Unit = {},
|
||||
style: TextStyle = TextField.style
|
||||
textStyle: TextStyle = TextField.textStyle
|
||||
) {
|
||||
TextFieldStyle(colors) {
|
||||
BasicTextField(
|
||||
@@ -106,7 +111,7 @@ fun TextField(
|
||||
modifier = modifier,
|
||||
enabled = enabled,
|
||||
readOnly = readOnly,
|
||||
textStyle = style,
|
||||
textStyle = textStyle,
|
||||
keyboardOptions = keyboardOptions,
|
||||
keyboardActions = keyboardActions,
|
||||
singleLine = singleLine,
|
||||
@@ -120,15 +125,8 @@ fun TextField(
|
||||
TextFieldDecorationBox(
|
||||
value = value,
|
||||
modifier = modifier,
|
||||
padding = padding,
|
||||
topPadding = topPadding,
|
||||
startPadding = startPadding,
|
||||
bottomPadding = bottomPadding,
|
||||
endPadding = endPadding,
|
||||
shape = shape,
|
||||
borderInactive = borderInactive,
|
||||
borderActive = borderActive,
|
||||
colors = colors,
|
||||
style = style,
|
||||
enabled = enabled,
|
||||
interactionSource = interactionSource,
|
||||
header = header,
|
||||
@@ -146,15 +144,8 @@ fun TextField(
|
||||
value: TextFieldValue,
|
||||
onValueChange: (TextFieldValue) -> Unit,
|
||||
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,
|
||||
style: TextFieldStyle = TextField.style,
|
||||
enabled: Boolean = true,
|
||||
readOnly: Boolean = false,
|
||||
keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
|
||||
@@ -168,7 +159,7 @@ fun TextField(
|
||||
header: @Composable () -> Unit = {},
|
||||
placeholder: @Composable () -> Unit = {},
|
||||
footer: @Composable () -> Unit = {},
|
||||
style: TextStyle = TextField.style
|
||||
textStyle: TextStyle = TextField.textStyle
|
||||
) {
|
||||
TextFieldStyle(colors) {
|
||||
BasicTextField(
|
||||
@@ -177,7 +168,7 @@ fun TextField(
|
||||
modifier = modifier,
|
||||
enabled = enabled,
|
||||
readOnly = readOnly,
|
||||
textStyle = style,
|
||||
textStyle = textStyle,
|
||||
keyboardOptions = keyboardOptions,
|
||||
keyboardActions = keyboardActions,
|
||||
singleLine = singleLine,
|
||||
@@ -191,15 +182,8 @@ fun TextField(
|
||||
TextFieldDecorationBox(
|
||||
value = value.text,
|
||||
modifier = modifier,
|
||||
padding = padding,
|
||||
topPadding = topPadding,
|
||||
startPadding = startPadding,
|
||||
bottomPadding = bottomPadding,
|
||||
endPadding = endPadding,
|
||||
shape = shape,
|
||||
borderInactive = borderInactive,
|
||||
borderActive = borderActive,
|
||||
colors = colors,
|
||||
style = style,
|
||||
enabled = enabled,
|
||||
interactionSource = interactionSource,
|
||||
header = header,
|
||||
@@ -223,15 +207,8 @@ private fun TextFieldStyle(colors: TextFieldColors, content: @Composable () -> U
|
||||
private fun TextFieldDecorationBox(
|
||||
value: String,
|
||||
modifier: Modifier,
|
||||
padding: Dp,
|
||||
topPadding: Dp,
|
||||
startPadding: Dp,
|
||||
bottomPadding: Dp,
|
||||
endPadding: Dp,
|
||||
shape: Shape,
|
||||
borderInactive: BorderStroke,
|
||||
borderActive: BorderStroke,
|
||||
colors: TextFieldColors,
|
||||
style: TextFieldStyle,
|
||||
enabled: Boolean,
|
||||
interactionSource: MutableInteractionSource,
|
||||
header: @Composable () -> Unit,
|
||||
@@ -240,17 +217,12 @@ private fun TextFieldDecorationBox(
|
||||
innerTextField: @Composable () -> Unit
|
||||
) {
|
||||
val focused by interactionSource.collectIsFocusedAsState()
|
||||
val border = if (focused) borderActive else borderInactive
|
||||
val border = if (focused) style.borderActive else style.borderInactive
|
||||
Box(
|
||||
modifier.textField(
|
||||
padding = padding,
|
||||
topPadding = topPadding,
|
||||
startPadding = startPadding,
|
||||
bottomPadding = bottomPadding,
|
||||
endPadding = endPadding,
|
||||
shape = shape,
|
||||
border = border,
|
||||
colors = colors,
|
||||
style = style,
|
||||
border = border,
|
||||
enabled = enabled
|
||||
)
|
||||
) {
|
||||
@@ -271,54 +243,34 @@ private fun TextFieldDecorationBox(
|
||||
}
|
||||
|
||||
private fun Modifier.textField(
|
||||
padding: Dp,
|
||||
topPadding: Dp,
|
||||
startPadding: Dp,
|
||||
bottomPadding: Dp,
|
||||
endPadding: Dp,
|
||||
shape: Shape,
|
||||
border: BorderStroke,
|
||||
colors: TextFieldColors,
|
||||
style: TextFieldStyle,
|
||||
border: BorderStroke,
|
||||
enabled: Boolean
|
||||
): Modifier {
|
||||
var sModifier = clip(shape = shape)
|
||||
.background(color = colors.backgroundColor, shape = shape)
|
||||
.borderOrNot(border, shape)
|
||||
var sModifier = clip(style.shape)
|
||||
.background(colors.backgroundColor, style.shape)
|
||||
.borderOrNot(border, style.shape)
|
||||
.padding(
|
||||
top = topPadding.orElse() ?: padding,
|
||||
start = startPadding.orElse() ?: padding,
|
||||
bottom = bottomPadding.orElse() ?: padding,
|
||||
end = endPadding.orElse() ?: padding
|
||||
top = style.topPadding.orElse() ?: style.padding,
|
||||
start = style.startPadding.orElse() ?: style.padding,
|
||||
bottom = style.bottomPadding.orElse() ?: style.padding,
|
||||
end = style.endPadding.orElse() ?: style.padding
|
||||
)
|
||||
if (!enabled) sModifier = sModifier.alpha(0.5f)
|
||||
return sModifier
|
||||
}
|
||||
|
||||
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
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
get() = defaultTextFieldColors()
|
||||
val style: TextStyle
|
||||
val style: TextFieldStyle
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
get() = defaultTextFieldStyle()
|
||||
val textStyle: TextStyle
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
get() = LocalTextStyle.current.default(LocalColors.current.textPrimary)
|
||||
@@ -339,7 +291,23 @@ private fun defaultTextFieldColors() = TextFieldColors(
|
||||
|
||||
@Composable
|
||||
@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
|
||||
@ReadOnlyComposable
|
||||
|
Reference in New Issue
Block a user