diff --git a/compose-stylesheet-core/src/androidUnitTest/kotlin/com/moriatsushi/compose/stylesheet/component/padding/ComponentPaddingTest.kt b/compose-stylesheet-core/src/androidUnitTest/kotlin/com/moriatsushi/compose/stylesheet/component/padding/ComponentPaddingTest.kt index cac649e..150ef33 100644 --- a/compose-stylesheet-core/src/androidUnitTest/kotlin/com/moriatsushi/compose/stylesheet/component/padding/ComponentPaddingTest.kt +++ b/compose-stylesheet-core/src/androidUnitTest/kotlin/com/moriatsushi/compose/stylesheet/component/padding/ComponentPaddingTest.kt @@ -7,14 +7,12 @@ import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.unit.LayoutDirection import androidx.compose.ui.unit.dp import androidx.test.ext.junit.runners.AndroidJUnit4 -import com.moriatsushi.compose.stylesheet.component.StyleSheetComponentApi import com.moriatsushi.compose.stylesheet.token.Token import kotlin.test.assertEquals import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith -@OptIn(StyleSheetComponentApi::class) @RunWith(AndroidJUnit4::class) class ComponentPaddingTest { @get:Rule @@ -165,4 +163,54 @@ class ComponentPaddingTest { } } } + + @Test + fun testAsPaddingValues_merged() { + val paddingValues = PaddingValues( + start = 10.dp, + top = 20.dp, + end = 30.dp, + bottom = 40.dp, + ) + val padding1 = ComponentPadding( + PaddingValues( + start = 10.dp, + top = 20.dp, + end = 30.dp, + bottom = 40.dp, + ), + ) + val padding2 = ComponentPadding( + start = Token(50.dp), + top = Token(60.dp), + ) + val merged1 = padding1.merge(padding2) + val merged2 = padding2.merge(padding1) + + composeTestRule.setContent { + CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Ltr) { + assertEquals( + PaddingValues.Absolute( + left = 50.dp, + top = 60.dp, + right = 30.dp, + bottom = 40.dp, + ), + merged1.asPaddingValues(), + ) + } + + CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Ltr) { + assertEquals( + PaddingValues.Absolute( + left = 10.dp, + top = 20.dp, + right = 30.dp, + bottom = 40.dp, + ), + merged2.asPaddingValues(), + ) + } + } + } } 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 4ee9269..67290df 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 @@ -15,6 +15,9 @@ import com.moriatsushi.compose.stylesheet.token.value @Immutable sealed class ComponentPadding { + internal abstract val values: List>> + internal abstract val paddingValues: PaddingValues? + @Composable internal abstract fun asPaddingValues(): PaddingValues @@ -26,6 +29,16 @@ sealed class ComponentPadding { right: Token? = null, left: Token? = null, ): ComponentPadding + + internal fun merge(other: ComponentPadding): ComponentPadding = + if (other.paddingValues != null) { + other + } else { + ComponentPaddingImpl( + values = other.values + values, + paddingValues = paddingValues, + ) + } } internal fun ComponentPadding(paddingValues: PaddingValues): ComponentPadding = @@ -50,8 +63,8 @@ internal fun ComponentPadding( ) private data class ComponentPaddingImpl( - private val values: List>> = emptyList(), - private val paddingValues: PaddingValues? = null, + override val values: List>> = emptyList(), + override val paddingValues: PaddingValues? = null, ) : ComponentPadding() { @Composable override fun asPaddingValues(): PaddingValues { @@ -125,6 +138,6 @@ fun Modifier.componentPadding(padding: ComponentPadding?): Modifier = this.then( if (padding != null) Modifier.padding(padding.asPaddingValues()) else Modifier, ) -private enum class PaddingSide { +internal enum class PaddingSide { Left, Top, Right, Bottom, Start, End, } 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 4355368..f7fdf3d 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 @@ -35,7 +35,7 @@ class PaddingSetter internal constructor() { operator fun plusAssign(padding: ComponentPadding?) { if (padding != null) { - value = padding + value = value?.merge(padding) ?: padding } }