Skip to content

Commit

Permalink
Add padding parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
mori-atsushi committed Nov 24, 2023
1 parent 2ac5379 commit 62e1660
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,28 @@ class ComponentPaddingTest {
.assertHeightIsEqualTo(50.dp)
}

@Test
fun testPadding_vertical() {
val style = DummyComponentStyle {
padding.vertical += 10.dp
}

composeTestRule.setContent {
DummyComponent(
modifier = Modifier.testTag("dummyComponent"),
style = style,
)
}

composeTestRule.onNodeWithTag("dummyComponent")
.assertWidthIsEqualTo(10.dp)
.assertHeightIsEqualTo(30.dp)
}

@Test
fun testPadding_horizontal() {
val style = DummyComponentStyle {
padding += padding(horizontal = 10.dp)
padding.horizontal += 10.dp
}

composeTestRule.setContent {
Expand All @@ -97,7 +115,7 @@ class ComponentPaddingTest {
@Test
fun testPadding_top() {
val style = DummyComponentStyle {
padding += padding(top = 10.dp)
padding.top += 10.dp
}

composeTestRule.setContent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class ComponentPaddingTest {

@Test
fun testAsPaddingValues_absolute() {
val componentPadding = ComponentPadding.absolute(
val componentPadding = ComponentPadding(
left = Token(10.dp),
top = Token(20.dp),
right = Token(30.dp),
Expand Down Expand Up @@ -125,4 +125,44 @@ class ComponentPaddingTest {
}
}
}

@Test
fun testAsPaddingValues_copied() {
val paddingValues = PaddingValues(
start = 10.dp,
top = 20.dp,
end = 30.dp,
bottom = 40.dp,
)
val componentPadding = ComponentPadding(paddingValues)
.copy(left = Token(50.dp))
.copy(bottom = Token(60.dp))
.copy(start = Token(70.dp))

composeTestRule.setContent {
CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Ltr) {
assertEquals(
PaddingValues.Absolute(
left = 70.dp,
top = 20.dp,
right = 30.dp,
bottom = 60.dp,
),
componentPadding.asPaddingValues(),
)
}

CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl) {
assertEquals(
PaddingValues.Absolute(
left = 50.dp,
top = 20.dp,
right = 70.dp,
bottom = 60.dp,
),
componentPadding.asPaddingValues(),
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,14 @@ abstract class ComponentStyleBuilder<T : ComponentStyle> : StyleBuilder<T> {
* Example:
* ```
* padding += 10.dp
* padding += sizeToken // Token<Dp>
* padding += padding(horizontal = 10.dp, vertical = 20.dp)
* padding += padding(horizontal = horizontalToken, vertical = verticalToken)
* padding += dpToken
* padding += padding(start = 10.dp, top = 20.dp, end = 30.dp, bottom = 40.dp)
* padding += absolutePadding(left = 10.dp, top = 20.dp, right = 30.dp, bottom = 40.dp)
* padding += PaddingValues(10.dp)
* padding.top += 10.dp
* padding.top += dpToken
* padding.vertical += 10.dp
* padding.horizontal += 10.dp
* ```
*/
val padding: PaddingSetter = PaddingSetter()
Expand Down Expand Up @@ -248,7 +250,7 @@ abstract class ComponentStyleBuilder<T : ComponentStyle> : StyleBuilder<T> {
top: Token<Dp> = Token(0.dp),
right: Token<Dp> = Token(0.dp),
bottom: Token<Dp> = Token(0.dp),
): ComponentPadding = ComponentPadding.absolute(
): ComponentPadding = ComponentPadding(
left = left,
top = top,
right = right,
Expand All @@ -260,7 +262,7 @@ abstract class ComponentStyleBuilder<T : ComponentStyle> : StyleBuilder<T> {
top: Dp = 0.dp,
right: Dp = 0.dp,
bottom: Dp = 0.dp,
): ComponentPadding = ComponentPadding.absolute(
): ComponentPadding = ComponentPadding(
left = Token(left),
top = Token(top),
right = Token(right),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,38 @@ sealed interface ComponentPadding {
@Composable
fun asPaddingValues(): PaddingValues

companion object {
internal fun absolute(
left: Token<Dp> = Token(0.dp),
top: Token<Dp> = Token(0.dp),
right: Token<Dp> = Token(0.dp),
bottom: Token<Dp> = Token(0.dp),
): ComponentPadding = ComponentPadding(
PaddingSide.Left to left,
PaddingSide.Top to top,
PaddingSide.Right to right,
PaddingSide.Bottom to bottom,
)
}
@StyleSheetComponentApi
fun copy(
start: Token<Dp>? = null,
top: Token<Dp>? = null,
end: Token<Dp>? = null,
bottom: Token<Dp>? = null,
right: Token<Dp>? = null,
left: Token<Dp>? = null,
): ComponentPadding
}

internal fun ComponentPadding(paddingValues: PaddingValues): ComponentPadding =
ComponentPaddingImpl(paddingValues = paddingValues)

internal fun ComponentPadding(
start: Token<Dp> = Token(0.dp),
top: Token<Dp> = Token(0.dp),
end: Token<Dp> = Token(0.dp),
bottom: Token<Dp> = Token(0.dp),
): ComponentPadding = ComponentPadding(
PaddingSide.Start to start,
PaddingSide.Top to top,
PaddingSide.End to end,
PaddingSide.Bottom to bottom,
start: Token<Dp>? = null,
top: Token<Dp>? = null,
end: Token<Dp>? = null,
bottom: Token<Dp>? = null,
right: Token<Dp>? = null,
left: Token<Dp>? = null,
): ComponentPadding = ComponentPaddingImpl(
listOfNotNull(
start?.let { PaddingSide.Start to it },
top?.let { PaddingSide.Top to it },
end?.let { PaddingSide.End to it },
bottom?.let { PaddingSide.Bottom to it },
right?.let { PaddingSide.Right to it },
left?.let { PaddingSide.Left to it },
),
)

private fun ComponentPadding(vararg values: Pair<PaddingSide, Token<Dp>>): ComponentPadding =
ComponentPaddingImpl(values = values.toList())

private data class ComponentPaddingImpl(
private val values: List<Pair<PaddingSide, Token<Dp>>> = emptyList(),
private val paddingValues: PaddingValues? = null,
Expand All @@ -78,6 +77,26 @@ private data class ComponentPaddingImpl(

return PaddingValues.Absolute(left = left, top = top, right = right, bottom = bottom)
}

override fun copy(
start: Token<Dp>?,
top: Token<Dp>?,
end: Token<Dp>?,
bottom: Token<Dp>?,
right: Token<Dp>?,
left: Token<Dp>?,
): ComponentPadding {
val newList = listOfNotNull(
start?.let { PaddingSide.Start to it },
top?.let { PaddingSide.Top to it },
end?.let { PaddingSide.End to it },
bottom?.let { PaddingSide.Bottom to it },
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) })
}
}

private fun PaddingSide.isLeft(layoutDirection: LayoutDirection): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,36 @@ package com.moriatsushi.compose.stylesheet.component.padding
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.ui.unit.Dp
import com.moriatsushi.compose.stylesheet.token.Token
import com.moriatsushi.compose.stylesheet.token.TokenSetterCallback

class PaddingSetter internal constructor() {
internal var value: ComponentPadding? = null

val start: TokenSetterCallback<Dp> = TokenSetterCallback {
value = value?.copy(start = it) ?: ComponentPadding(start = it)
}
val top: TokenSetterCallback<Dp> = TokenSetterCallback {
value = value?.copy(top = it) ?: ComponentPadding(top = it)
}
val end: TokenSetterCallback<Dp> = TokenSetterCallback {
value = value?.copy(end = it) ?: ComponentPadding(end = it)
}
val bottom: TokenSetterCallback<Dp> = TokenSetterCallback {
value = value?.copy(bottom = it) ?: ComponentPadding(bottom = it)
}
val right: TokenSetterCallback<Dp> = TokenSetterCallback {
value = value?.copy(right = it) ?: ComponentPadding(right = it)
}
val left: TokenSetterCallback<Dp> = TokenSetterCallback {
value = value?.copy(left = it) ?: ComponentPadding(left = it)
}
val vertical: TokenSetterCallback<Dp> = TokenSetterCallback {
value = value?.copy(top = it, bottom = it) ?: ComponentPadding(top = it, bottom = it)
}
val horizontal: TokenSetterCallback<Dp> = TokenSetterCallback {
value = value?.copy(start = it, end = it) ?: ComponentPadding(start = it, end = it)
}

operator fun plusAssign(padding: ComponentPadding?) {
if (padding != null) {
value = padding
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ val topAppBarStyleSheet = StyleSheet {
topAppBar {
width += fill
minHeight += 64.dp
padding += padding(vertical = 2.dp)
padding.vertical += 2.dp
background += Colors.semantic.background

layout {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal val buttonStyleSheet = StyleSheet {
background += Colors.semantic.inverseSurface
shape += RoundedCornerShape(50)
minSize += 40.dp
padding += padding(vertical = 2.dp)
padding.vertical += 2.dp

layout {
spaceBetweenContentAndContainer += 24.dp
Expand Down

0 comments on commit 62e1660

Please sign in to comment.