mirror of
https://github.com/BetterAndroid/FlexiUI.git
synced 2025-09-07 19:14:12 +08:00
refactor: merge all padding to paddings data class
This commit is contained in:
@@ -51,20 +51,18 @@ import com.highcapable.flexiui.LocalColors
|
||||
import com.highcapable.flexiui.LocalShapes
|
||||
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 paddings: AreaBoxPaddings,
|
||||
val shape: Shape,
|
||||
val border: BorderStroke,
|
||||
val shadowSize: Dp
|
||||
)
|
||||
|
||||
@Immutable
|
||||
data class AreaBoxPaddings(val start: Dp, val top: Dp, val end: Dp, val bottom: Dp)
|
||||
|
||||
@Composable
|
||||
fun AreaBox(
|
||||
modifier: Modifier = Modifier,
|
||||
@@ -146,10 +144,10 @@ private fun Modifier.box(
|
||||
.borderOrNot(style.border, style.shape)
|
||||
.then(modifier)
|
||||
.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
|
||||
start = style.paddings.start,
|
||||
top = style.paddings.top,
|
||||
end = style.paddings.end,
|
||||
bottom = style.paddings.bottom
|
||||
)
|
||||
|
||||
object AreaBox {
|
||||
@@ -172,11 +170,12 @@ internal val DefaultAreaBoxShape: Shape = DefaultShapes.primary
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
private fun defaultAreaBoxStyle() = AreaBoxStyle(
|
||||
padding = LocalSizes.current.spacingPrimary,
|
||||
topPadding = Dp.Unspecified,
|
||||
startPadding = Dp.Unspecified,
|
||||
bottomPadding = Dp.Unspecified,
|
||||
endPadding = Dp.Unspecified,
|
||||
paddings = AreaBoxPaddings(
|
||||
start = defaultAreaBoxPadding(),
|
||||
top = defaultAreaBoxPadding(),
|
||||
end = defaultAreaBoxPadding(),
|
||||
bottom = defaultAreaBoxPadding()
|
||||
),
|
||||
shape = LocalShapes.current.primary,
|
||||
border = defaultAreaBoxBorder(),
|
||||
shadowSize = DefaultAreaBoxShadowSize
|
||||
@@ -190,4 +189,8 @@ private fun defaultAreaBoxColor() = LocalColors.current.foregroundPrimary
|
||||
@ReadOnlyComposable
|
||||
private fun defaultAreaBoxBorder() = BorderStroke(LocalSizes.current.borderSizeTertiary, LocalColors.current.textPrimary)
|
||||
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
private fun defaultAreaBoxPadding() = LocalSizes.current.spacingPrimary
|
||||
|
||||
private val DefaultAreaBoxShadowSize = 0.dp
|
@@ -62,15 +62,14 @@ data class ButtonColors(
|
||||
|
||||
@Immutable
|
||||
data class ButtonStyle(
|
||||
val padding: Dp,
|
||||
val topPadding: Dp,
|
||||
val startPadding: Dp,
|
||||
val bottomPadding: Dp,
|
||||
val endPadding: Dp,
|
||||
val paddings: ButtonPaddings,
|
||||
val shape: Shape,
|
||||
val border: BorderStroke
|
||||
)
|
||||
|
||||
@Immutable
|
||||
data class ButtonPaddings(val start: Dp, val top: Dp, val end: Dp, val bottom: Dp)
|
||||
|
||||
@Composable
|
||||
fun Button(
|
||||
onClick: () -> Unit,
|
||||
@@ -189,10 +188,10 @@ private fun Modifier.button(
|
||||
.then(modifier)
|
||||
|
||||
private fun Modifier.buttonPadding(style: ButtonStyle) = 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
|
||||
start = style.paddings.start,
|
||||
top = style.paddings.top,
|
||||
end = style.paddings.end,
|
||||
bottom = style.paddings.bottom
|
||||
)
|
||||
|
||||
object Button {
|
||||
@@ -247,11 +246,12 @@ private fun defaultIconButtonColors() = ButtonColors(
|
||||
@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,
|
||||
paddings = ButtonPaddings(
|
||||
start = LocalSizes.current.spacingPrimary,
|
||||
top = LocalSizes.current.spacingSecondary,
|
||||
end = LocalSizes.current.spacingPrimary,
|
||||
bottom = LocalSizes.current.spacingSecondary
|
||||
),
|
||||
shape = when (LocalInAreaBox.current) {
|
||||
true -> LocalAreaBoxShape.current
|
||||
else -> LocalShapes.current.tertiary
|
||||
@@ -262,11 +262,7 @@ private fun defaultButtonStyle() = ButtonStyle(
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
private fun defaultIconButtonStyle() = ButtonStyle(
|
||||
padding = 0.dp,
|
||||
topPadding = Dp.Unspecified,
|
||||
startPadding = Dp.Unspecified,
|
||||
bottomPadding = Dp.Unspecified,
|
||||
endPadding = Dp.Unspecified,
|
||||
paddings = ButtonPaddings(0.dp, 0.dp, 0.dp, 0.dp),
|
||||
shape = CircleShape,
|
||||
border = defaultButtonBorder()
|
||||
)
|
||||
|
@@ -131,11 +131,7 @@ data class DropdownMenuColors(
|
||||
|
||||
@Immutable
|
||||
data class DropdownListStyle(
|
||||
val padding: Dp,
|
||||
val topPadding: Dp,
|
||||
val startPadding: Dp,
|
||||
val bottomPadding: Dp,
|
||||
val endPadding: Dp,
|
||||
val paddings: DropdownListPaddings,
|
||||
val shape: Shape,
|
||||
val endIconSize: Dp,
|
||||
val borderInactive: BorderStroke,
|
||||
@@ -150,6 +146,9 @@ data class DropdownMenuStyle(
|
||||
val borderStyle: AreaBoxStyle
|
||||
)
|
||||
|
||||
@Immutable
|
||||
data class DropdownListPaddings(val start: Dp, val top: Dp, val end: Dp, val bottom: Dp)
|
||||
|
||||
@Composable
|
||||
fun DropdownList(
|
||||
expanded: Boolean,
|
||||
@@ -169,8 +168,6 @@ fun DropdownList(
|
||||
val focused by interactionSource.collectIsFocusedAsState()
|
||||
val hovered by interactionSource.collectIsHoveredAsState()
|
||||
val focusRequester = remember { FocusRequester() }
|
||||
val startPadding = style.startPadding.orElse() ?: style.padding
|
||||
val endPadding = style.endPadding.orElse() ?: style.padding
|
||||
val animatedEndIconTint by animateColorAsState(when {
|
||||
focused || hovered -> colors.endIconActiveTint
|
||||
else -> colors.endIconInactiveTint
|
||||
@@ -206,7 +203,7 @@ fun DropdownList(
|
||||
}
|
||||
)
|
||||
) {
|
||||
val menuMaxWidth = maxWidth + startPadding + endPadding
|
||||
val menuMaxWidth = maxWidth + style.paddings.start + style.paddings.end
|
||||
// Note: If minWidth is not 0, a constant width is currently set.
|
||||
// At this time, the child layout must be completely filled into the parent layout.
|
||||
val needInflatable = minWidth > 0.dp
|
||||
@@ -429,10 +426,10 @@ private fun Modifier.dropdownList(
|
||||
.borderOrNot(border, style.shape)
|
||||
.then(modifier)
|
||||
.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
|
||||
start = style.paddings.start,
|
||||
top = style.paddings.top,
|
||||
end = style.paddings.end,
|
||||
bottom = style.paddings.bottom
|
||||
)
|
||||
|
||||
private fun calculateTransformOrigin(parentBounds: IntRect, menuBounds: IntRect): TransformOrigin {
|
||||
@@ -563,11 +560,12 @@ private fun defaultDropdownMenuColors() = DropdownMenuColors(
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
private fun defaultDropdownListStyle() = DropdownListStyle(
|
||||
padding = LocalSizes.current.spacingSecondary,
|
||||
topPadding = Dp.Unspecified,
|
||||
startPadding = Dp.Unspecified,
|
||||
bottomPadding = Dp.Unspecified,
|
||||
endPadding = Dp.Unspecified,
|
||||
paddings = DropdownListPaddings(
|
||||
start = defaultDropdownListPadding(),
|
||||
top = defaultDropdownListPadding(),
|
||||
end = defaultDropdownListPadding(),
|
||||
bottom = defaultDropdownListPadding()
|
||||
),
|
||||
shape = when (LocalInAreaBox.current) {
|
||||
true -> LocalAreaBoxShape.current
|
||||
else -> LocalShapes.current.secondary
|
||||
@@ -583,13 +581,21 @@ private fun defaultDropdownMenuStyle() = DropdownMenuStyle(
|
||||
inTransitionDuration = DefaultInTransitionDuration,
|
||||
outTransitionDuration = DefaultOutTransitionDuration,
|
||||
contentStyle = AreaBox.style.copy(
|
||||
padding = 0.dp,
|
||||
startPadding = DefaultMenuContentPadding,
|
||||
endPadding = DefaultMenuContentPadding,
|
||||
paddings = AreaBoxPaddings(
|
||||
start = DefaultMenuContentPadding,
|
||||
top = 0.dp,
|
||||
end = DefaultMenuContentPadding,
|
||||
bottom = 0.dp
|
||||
),
|
||||
shape = LocalShapes.current.secondary
|
||||
),
|
||||
borderStyle = AreaBox.style.copy(
|
||||
padding = LocalSizes.current.spacingTertiary,
|
||||
paddings = AreaBoxPaddings(
|
||||
start = defaultDropdownListBorderPadding(),
|
||||
top = defaultDropdownListBorderPadding(),
|
||||
end = defaultDropdownListBorderPadding(),
|
||||
bottom = defaultDropdownListBorderPadding()
|
||||
),
|
||||
shadowSize = LocalSizes.current.zoomSizeTertiary,
|
||||
shape = LocalShapes.current.primary
|
||||
)
|
||||
@@ -603,6 +609,14 @@ private fun defaultDropdownListInactiveBorder() = BorderStroke(LocalSizes.curren
|
||||
@ReadOnlyComposable
|
||||
private fun defaultDropdownListActiveBorder() = BorderStroke(LocalSizes.current.borderSizePrimary, LocalColors.current.themePrimary)
|
||||
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
private fun defaultDropdownListPadding() = LocalSizes.current.spacingSecondary
|
||||
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
private fun defaultDropdownListBorderPadding() = LocalSizes.current.spacingTertiary
|
||||
|
||||
private val DefaultDropdownListMenuOffset = DpOffset((-10).dp, 10.dp)
|
||||
|
||||
private val DefaultMenuContentPadding = 16.dp
|
||||
|
@@ -36,7 +36,6 @@ import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.unit.Dp
|
||||
import com.highcapable.flexiui.LocalColors
|
||||
import com.highcapable.flexiui.LocalSizes
|
||||
import com.highcapable.flexiui.utils.orElse
|
||||
|
||||
// TODO: Linkage BetterAndroid SafeArea (SystemBarsController)
|
||||
|
||||
@@ -48,13 +47,12 @@ data class SurfaceColors(
|
||||
|
||||
@Immutable
|
||||
data class SurfaceStyle(
|
||||
val padding: Dp,
|
||||
val topPadding: Dp,
|
||||
val startPadding: Dp,
|
||||
val bottomPadding: Dp,
|
||||
val endPadding: Dp
|
||||
val paddings: SurfacePaddings
|
||||
)
|
||||
|
||||
@Immutable
|
||||
data class SurfacePaddings(val start: Dp, val top: Dp, val end: Dp, val bottom: Dp)
|
||||
|
||||
@Composable
|
||||
fun Surface(
|
||||
modifier: Modifier = Modifier,
|
||||
@@ -80,10 +78,10 @@ private fun Modifier.surface(
|
||||
.background(colors.backgroundColor)
|
||||
.then(modifier)
|
||||
.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
|
||||
start = style.paddings.start,
|
||||
top = style.paddings.top,
|
||||
end = style.paddings.end,
|
||||
bottom = style.paddings.bottom
|
||||
)
|
||||
|
||||
object Surface {
|
||||
@@ -107,9 +105,14 @@ private fun defaultSurfaceColors() = SurfaceColors(
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
private fun defaultSurfaceStyle() = SurfaceStyle(
|
||||
padding = LocalSizes.current.spacingPrimary,
|
||||
topPadding = Dp.Unspecified,
|
||||
startPadding = Dp.Unspecified,
|
||||
bottomPadding = Dp.Unspecified,
|
||||
endPadding = Dp.Unspecified
|
||||
)
|
||||
paddings = SurfacePaddings(
|
||||
start = defaultSurfacePaddings(),
|
||||
top = defaultSurfacePaddings(),
|
||||
end = defaultSurfacePaddings(),
|
||||
bottom = defaultSurfacePaddings()
|
||||
)
|
||||
)
|
||||
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
private fun defaultSurfacePaddings() = LocalSizes.current.spacingPrimary
|
@@ -115,11 +115,7 @@ data class AutoCompleteBoxColors(
|
||||
|
||||
@Immutable
|
||||
data class TextFieldStyle(
|
||||
val padding: Dp,
|
||||
val topPadding: Dp,
|
||||
val startPadding: Dp,
|
||||
val bottomPadding: Dp,
|
||||
val endPadding: Dp,
|
||||
val paddings: TextFieldPaddings,
|
||||
val shape: Shape,
|
||||
val borderInactive: BorderStroke,
|
||||
val borderActive: BorderStroke,
|
||||
@@ -134,6 +130,9 @@ data class AutoCompleteOptions(
|
||||
val threshold: Int = 2
|
||||
)
|
||||
|
||||
@Immutable
|
||||
data class TextFieldPaddings(val start: Dp, val top: Dp, val end: Dp, val bottom: Dp)
|
||||
|
||||
@Composable
|
||||
fun TextField(
|
||||
value: TextFieldValue,
|
||||
@@ -365,7 +364,7 @@ fun PasswordTextField(
|
||||
if (value.text.isEmpty() && animatedSize == 0.dp) passwordVisible = defaultPasswordVisible
|
||||
IconToggleButton(
|
||||
modifier = Modifier.size(animatedSize).pointerHoverState(TextFieldPointerState.NORMAL),
|
||||
style = IconButton.style.copy(padding = DefaultDecorIconPadding),
|
||||
style = IconButton.style.copy(paddings = DefaultDecorIconPaddings),
|
||||
checked = passwordVisible,
|
||||
onCheckedChange = {
|
||||
passwordVisible = it
|
||||
@@ -492,7 +491,7 @@ fun BackspaceTextField(
|
||||
focusRequester.requestFocus()
|
||||
},
|
||||
modifier = Modifier.width(animatedSize).pointerHoverState(TextFieldPointerState.NORMAL),
|
||||
style = IconButton.style.copy(padding = DefaultDecorIconPadding),
|
||||
style = IconButton.style.copy(paddings = DefaultDecorIconPaddings),
|
||||
enabled = enabled,
|
||||
interactionSource = cInteractionSource
|
||||
) { Icon(imageVector = Icons.Backspace) }
|
||||
@@ -732,13 +731,13 @@ private fun Modifier.textFieldPadding(
|
||||
fitEnd: Boolean = false
|
||||
) = when {
|
||||
!fitStart && !fitEnd -> 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
|
||||
start = style.paddings.start,
|
||||
top = style.paddings.top,
|
||||
end = style.paddings.end,
|
||||
bottom = style.paddings.bottom
|
||||
)
|
||||
fitStart -> padding(start = style.startPadding.orElse() ?: style.padding)
|
||||
fitEnd -> padding(end = style.endPadding.orElse() ?: style.padding)
|
||||
fitStart -> padding(start = style.paddings.start)
|
||||
fitEnd -> padding(end = style.paddings.end)
|
||||
else -> this
|
||||
}
|
||||
|
||||
@@ -779,11 +778,12 @@ private fun defaultTextFieldColors() = TextFieldColors(
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
private fun defaultTextFieldStyle() = TextFieldStyle(
|
||||
padding = LocalSizes.current.spacingSecondary,
|
||||
topPadding = Dp.Unspecified,
|
||||
startPadding = Dp.Unspecified,
|
||||
bottomPadding = Dp.Unspecified,
|
||||
endPadding = Dp.Unspecified,
|
||||
paddings = TextFieldPaddings(
|
||||
start = defaultTextFieldPadding(),
|
||||
top = defaultTextFieldPadding(),
|
||||
end = defaultTextFieldPadding(),
|
||||
bottom = defaultTextFieldPadding()
|
||||
),
|
||||
shape = when (LocalInAreaBox.current) {
|
||||
true -> LocalAreaBoxShape.current
|
||||
else -> LocalShapes.current.secondary
|
||||
@@ -801,5 +801,9 @@ private fun defaultTextFieldInactiveBorder() = BorderStroke(LocalSizes.current.b
|
||||
@ReadOnlyComposable
|
||||
private fun defaultTextFieldActiveBorder() = BorderStroke(LocalSizes.current.borderSizePrimary, LocalColors.current.themePrimary)
|
||||
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
private fun defaultTextFieldPadding() = LocalSizes.current.spacingSecondary
|
||||
|
||||
private val DefaultDecorIconSize = 24.dp
|
||||
private val DefaultDecorIconPadding = 2.dp
|
||||
private val DefaultDecorIconPaddings = ButtonPaddings(2.dp, 2.dp, 2.dp, 2.dp)
|
@@ -207,18 +207,30 @@ internal fun defaultContextMenuColors() = ContextMenuColors(
|
||||
@ReadOnlyComposable
|
||||
internal fun defaultContextMenuStyle() = ContextMenuStyle(
|
||||
contentStyle = LocalContextMenuStyle.current.contentStyle ?: AreaBox.style.copy(
|
||||
padding = 0.dp,
|
||||
startPadding = DefaultMenuContentPadding,
|
||||
endPadding = DefaultMenuContentPadding,
|
||||
paddings = AreaBoxPaddings(
|
||||
start = DefaultMenuContentPadding,
|
||||
top = 0.dp,
|
||||
end = DefaultMenuContentPadding,
|
||||
bottom = 0.dp
|
||||
),
|
||||
shape = LocalShapes.current.secondary
|
||||
),
|
||||
borderStyle = LocalContextMenuStyle.current.borderStyle ?: AreaBox.style.copy(
|
||||
padding = LocalSizes.current.spacingTertiary,
|
||||
paddings = AreaBoxPaddings(
|
||||
start = defaultContextMenuBorderPadding(),
|
||||
top = defaultContextMenuBorderPadding(),
|
||||
end = defaultContextMenuBorderPadding(),
|
||||
bottom = defaultContextMenuBorderPadding()
|
||||
),
|
||||
shadowSize = LocalSizes.current.zoomSizeTertiary,
|
||||
shape = LocalShapes.current.primary
|
||||
)
|
||||
)
|
||||
|
||||
@Composable
|
||||
@ReadOnlyComposable
|
||||
private fun defaultContextMenuBorderPadding() = LocalSizes.current.spacingTertiary
|
||||
|
||||
private val DefaultMenuContentMinWidth = 112.dp
|
||||
private val DefaultMenuContentMaxWidth = 280.dp
|
||||
private val DefaultMenuContentMinHeight = 32.dp
|
||||
|
Reference in New Issue
Block a user