From 41c09355af6131a07d07d187a89ea1778db27b7a Mon Sep 17 00:00:00 2001 From: Mori-Atsushi Date: Sat, 25 Nov 2023 00:16:34 +0900 Subject: [PATCH] Add a default value for ComponentPadding --- .../component/ComponentCommonStyle.kt | 2 +- .../component/padding/ComponentPadding.kt | 26 ++++++++++++++----- .../component/padding/PaddingSetter.kt | 24 ++++++++--------- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/compose-stylesheet-core/src/commonMain/kotlin/com/moriatsushi/compose/stylesheet/component/ComponentCommonStyle.kt b/compose-stylesheet-core/src/commonMain/kotlin/com/moriatsushi/compose/stylesheet/component/ComponentCommonStyle.kt index abad28b..823b460 100644 --- a/compose-stylesheet-core/src/commonMain/kotlin/com/moriatsushi/compose/stylesheet/component/ComponentCommonStyle.kt +++ b/compose-stylesheet-core/src/commonMain/kotlin/com/moriatsushi/compose/stylesheet/component/ComponentCommonStyle.kt @@ -29,7 +29,7 @@ import com.moriatsushi.compose.stylesheet.token.value @StyleSheetComponentApi data class ComponentCommonStyle internal constructor( internal val size: ComponentSize = ComponentSize.Default, - val padding: ComponentPadding? = null, + val padding: ComponentPadding = ComponentPadding.Default, internal val background: Token? = null, internal val shape: Token? = null, internal val border: BorderStyle? = null, diff --git a/compose-stylesheet-core/src/commonMain/kotlin/com/moriatsushi/compose/stylesheet/component/padding/ComponentPadding.kt b/compose-stylesheet-core/src/commonMain/kotlin/com/moriatsushi/compose/stylesheet/component/padding/ComponentPadding.kt index 67290df..2d509d4 100644 --- a/compose-stylesheet-core/src/commonMain/kotlin/com/moriatsushi/compose/stylesheet/component/padding/ComponentPadding.kt +++ b/compose-stylesheet-core/src/commonMain/kotlin/com/moriatsushi/compose/stylesheet/component/padding/ComponentPadding.kt @@ -34,15 +34,19 @@ sealed class ComponentPadding { if (other.paddingValues != null) { other } else { - ComponentPaddingImpl( + ComponentPadding( values = other.values + values, paddingValues = paddingValues, ) } + + companion object { + internal val Default: ComponentPadding = ComponentPadding(emptyList()) + } } internal fun ComponentPadding(paddingValues: PaddingValues): ComponentPadding = - ComponentPaddingImpl(paddingValues = paddingValues) + ComponentPadding(emptyList(), paddingValues = paddingValues) internal fun ComponentPadding( start: Token? = null, @@ -51,7 +55,7 @@ internal fun ComponentPadding( bottom: Token? = null, right: Token? = null, left: Token? = null, -): ComponentPadding = ComponentPaddingImpl( +): ComponentPadding = ComponentPadding( listOfNotNull( start?.let { PaddingSide.Start to it }, top?.let { PaddingSide.Top to it }, @@ -62,9 +66,17 @@ internal fun ComponentPadding( ), ) +private fun ComponentPadding( + values: List>> = emptyList(), + paddingValues: PaddingValues? = null, +): ComponentPadding = ComponentPaddingImpl( + values = values.distinctBy { it.first }, + paddingValues = paddingValues, +) + private data class ComponentPaddingImpl( - override val values: List>> = emptyList(), - override val paddingValues: PaddingValues? = null, + override val values: List>>, + override val paddingValues: PaddingValues?, ) : ComponentPadding() { @Composable override fun asPaddingValues(): PaddingValues { @@ -105,8 +117,8 @@ private data class ComponentPaddingImpl( right?.let { PaddingSide.Right to it }, left?.let { PaddingSide.Left to it }, ) - val newSides = newList.map { it.first }.toSet() - return copy(values = newList + values.filterNot { newSides.contains(it.first) }) + + return copy(values = (newList + values).distinctBy { it.first }) } } diff --git a/compose-stylesheet-core/src/commonMain/kotlin/com/moriatsushi/compose/stylesheet/component/padding/PaddingSetter.kt b/compose-stylesheet-core/src/commonMain/kotlin/com/moriatsushi/compose/stylesheet/component/padding/PaddingSetter.kt index f7fdf3d..9b83ca7 100644 --- a/compose-stylesheet-core/src/commonMain/kotlin/com/moriatsushi/compose/stylesheet/component/padding/PaddingSetter.kt +++ b/compose-stylesheet-core/src/commonMain/kotlin/com/moriatsushi/compose/stylesheet/component/padding/PaddingSetter.kt @@ -6,37 +6,35 @@ import com.moriatsushi.compose.stylesheet.token.Token import com.moriatsushi.compose.stylesheet.token.TokenSetterCallback class PaddingSetter internal constructor() { - internal var value: ComponentPadding? = null + internal var value: ComponentPadding = ComponentPadding.Default val start: TokenSetterCallback = TokenSetterCallback { - value = value?.copy(start = it) ?: ComponentPadding(start = it) + value = value.copy(start = it) } val top: TokenSetterCallback = TokenSetterCallback { - value = value?.copy(top = it) ?: ComponentPadding(top = it) + value = value.copy(top = it) } val end: TokenSetterCallback = TokenSetterCallback { - value = value?.copy(end = it) ?: ComponentPadding(end = it) + value = value.copy(end = it) } val bottom: TokenSetterCallback = TokenSetterCallback { - value = value?.copy(bottom = it) ?: ComponentPadding(bottom = it) + value = value.copy(bottom = it) } val right: TokenSetterCallback = TokenSetterCallback { - value = value?.copy(right = it) ?: ComponentPadding(right = it) + value = value.copy(right = it) } val left: TokenSetterCallback = TokenSetterCallback { - value = value?.copy(left = it) ?: ComponentPadding(left = it) + value = value.copy(left = it) } val vertical: TokenSetterCallback = TokenSetterCallback { - value = value?.copy(top = it, bottom = it) ?: ComponentPadding(top = it, bottom = it) + value = value.copy(top = it, bottom = it) } val horizontal: TokenSetterCallback = TokenSetterCallback { - value = value?.copy(start = it, end = it) ?: ComponentPadding(start = it, end = it) + value = value.copy(start = it, end = it) } - operator fun plusAssign(padding: ComponentPadding?) { - if (padding != null) { - value = value?.merge(padding) ?: padding - } + operator fun plusAssign(padding: ComponentPadding) { + value = value.merge(padding) } operator fun plusAssign(all: Token) {