Skip to content

Commit

Permalink
Add a default value for ComponentPadding
Browse files Browse the repository at this point in the history
  • Loading branch information
mori-atsushi committed Nov 24, 2023
1 parent dce3a7c commit 41c0935
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Color>? = null,
internal val shape: Token<Shape?>? = null,
internal val border: BorderStyle? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Dp>? = null,
Expand All @@ -51,7 +55,7 @@ internal fun ComponentPadding(
bottom: Token<Dp>? = null,
right: Token<Dp>? = null,
left: Token<Dp>? = null,
): ComponentPadding = ComponentPaddingImpl(
): ComponentPadding = ComponentPadding(
listOfNotNull(
start?.let { PaddingSide.Start to it },
top?.let { PaddingSide.Top to it },
Expand All @@ -62,9 +66,17 @@ internal fun ComponentPadding(
),
)

private fun ComponentPadding(
values: List<Pair<PaddingSide, Token<Dp>>> = emptyList(),
paddingValues: PaddingValues? = null,
): ComponentPadding = ComponentPaddingImpl(
values = values.distinctBy { it.first },
paddingValues = paddingValues,
)

private data class ComponentPaddingImpl(
override val values: List<Pair<PaddingSide, Token<Dp>>> = emptyList(),
override val paddingValues: PaddingValues? = null,
override val values: List<Pair<PaddingSide, Token<Dp>>>,
override val paddingValues: PaddingValues?,
) : ComponentPadding() {
@Composable
override fun asPaddingValues(): PaddingValues {
Expand Down Expand Up @@ -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 })
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Dp> = TokenSetterCallback {
value = value?.copy(start = it) ?: ComponentPadding(start = it)
value = value.copy(start = it)
}
val top: TokenSetterCallback<Dp> = TokenSetterCallback {
value = value?.copy(top = it) ?: ComponentPadding(top = it)
value = value.copy(top = it)
}
val end: TokenSetterCallback<Dp> = TokenSetterCallback {
value = value?.copy(end = it) ?: ComponentPadding(end = it)
value = value.copy(end = it)
}
val bottom: TokenSetterCallback<Dp> = TokenSetterCallback {
value = value?.copy(bottom = it) ?: ComponentPadding(bottom = it)
value = value.copy(bottom = it)
}
val right: TokenSetterCallback<Dp> = TokenSetterCallback {
value = value?.copy(right = it) ?: ComponentPadding(right = it)
value = value.copy(right = it)
}
val left: TokenSetterCallback<Dp> = TokenSetterCallback {
value = value?.copy(left = it) ?: ComponentPadding(left = it)
value = value.copy(left = it)
}
val vertical: TokenSetterCallback<Dp> = TokenSetterCallback {
value = value?.copy(top = it, bottom = it) ?: ComponentPadding(top = it, bottom = it)
value = value.copy(top = it, bottom = it)
}
val horizontal: TokenSetterCallback<Dp> = 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<Dp>) {
Expand Down

0 comments on commit 41c0935

Please sign in to comment.