Skip to content

Commit

Permalink
Add a merge function for ComponentPadding
Browse files Browse the repository at this point in the history
  • Loading branch information
mori-atsushi committed Nov 24, 2023
1 parent 6f0bf96 commit dce3a7c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import com.moriatsushi.compose.stylesheet.token.value

@Immutable
sealed class ComponentPadding {
internal abstract val values: List<Pair<PaddingSide, Token<Dp>>>
internal abstract val paddingValues: PaddingValues?

@Composable
internal abstract fun asPaddingValues(): PaddingValues

Expand All @@ -26,6 +29,16 @@ sealed class ComponentPadding {
right: Token<Dp>? = null,
left: Token<Dp>? = 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 =
Expand All @@ -50,8 +63,8 @@ internal fun ComponentPadding(
)

private data class ComponentPaddingImpl(
private val values: List<Pair<PaddingSide, Token<Dp>>> = emptyList(),
private val paddingValues: PaddingValues? = null,
override val values: List<Pair<PaddingSide, Token<Dp>>> = emptyList(),
override val paddingValues: PaddingValues? = null,
) : ComponentPadding() {
@Composable
override fun asPaddingValues(): PaddingValues {
Expand Down Expand Up @@ -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,
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class PaddingSetter internal constructor() {

operator fun plusAssign(padding: ComponentPadding?) {
if (padding != null) {
value = padding
value = value?.merge(padding) ?: padding
}
}

Expand Down

0 comments on commit dce3a7c

Please sign in to comment.