Skip to content

Commit

Permalink
Rename OudsContrastedSurface to OudsColoredBox
Browse files Browse the repository at this point in the history
  • Loading branch information
florentmaitre committed Dec 30, 2024
1 parent 881a926 commit 25e8d01
Show file tree
Hide file tree
Showing 29 changed files with 37 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@
* Software description: Android library of reusable graphical components
*/

package com.orange.ouds.core.component.contrastedsurface
package com.orange.ouds.core.component.coloredbox

import androidx.compose.foundation.background
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Surface
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.tooling.preview.PreviewParameter
import com.orange.ouds.core.component.button.OudsButton
import com.orange.ouds.core.theme.LocalContrastedSurface
import com.orange.ouds.core.theme.LocalColoredBox
import com.orange.ouds.core.theme.value
import com.orange.ouds.core.utilities.OudsPreview
import com.orange.ouds.foundation.utilities.BasicPreviewParameterProvider
Expand All @@ -33,29 +34,34 @@ import com.orange.ouds.theme.tokens.OudsColorKeyToken
import com.orange.ouds.theme.tokens.OudsSpaceKeyToken

/**
* An OUDS colored surface is a [Surface] where content color is automatically adjusted to maximize the contrast with the chosen background [color].
* An OUDS colored box is a [Box] where content color is automatically adjusted to maximize the contrast with the chosen background [color].
*
* Moreover, the colors of several OUDS components (for instance [OudsButton]) are also automatically adjusted.
* Some tokens associated with these colors can be customized and are identified with the `Mono` suffix (for instance `colorBgDefaultEnabledMono` in `OudsButtonTokens`).
* @param color The background color.
* @param modifier Modifier to be applied to the layout corresponding to the colored surface.
* @param shape Defines the surface's shape as well its shadow.
* @param content The content of this colored surface.
* @param modifier Modifier to be applied to the layout corresponding to the colored box.
* @param content The content of this colored box.
*/
@Composable
fun OudsContrastedSurface(
fun OudsColoredBox(
color: OudsColorKeyToken.Surface,
modifier: Modifier = Modifier,
shape: Shape = RectangleShape,
content: @Composable () -> Unit
content: @Composable BoxScope.() -> Unit
) {
CompositionLocalProvider(LocalContrastedSurface provides color) {
Surface(
modifier = modifier,
shape = shape,
color = color.value,
contentColor = contentColorFor(color),
CompositionLocalProvider(
LocalContentColor provides contentColorFor(color),
LocalColoredBox provides true
) {
// Filter the background modifiers in order to force the background color
// We could theoretically apply the background color after the modifier but in practise a hairline is still visible
val filteredModifier = modifier.foldIn<Modifier>(Modifier) { result, element ->
if (element::class.simpleName != "BackgroundElement") result.then(element) else result
}
Box(
modifier = Modifier
.background(color.value) // Set the background color first, otherwise padding (if any) is wrongly applied
.then(filteredModifier),
content = content
)
}
Expand Down Expand Up @@ -83,21 +89,21 @@ private fun contentColorFor(color: OudsColorKeyToken.Surface): Color {
@Suppress("PreviewShouldNotBeCalledRecursively")
@UiModePreviews.Default
@Composable
private fun PreviewOudsContrastedSurface(@PreviewParameter(OudsContrastedSurfacePreviewParameterProvider::class) parameter: OudsColorKeyToken.Surface) {
PreviewOudsContrastedSurface(darkThemeEnabled = isSystemInDarkTheme(), parameter = parameter)
private fun PreviewOudsColoredBox(@PreviewParameter(OudsColoredBoxPreviewParameterProvider::class) parameter: OudsColorKeyToken.Surface) {
PreviewOudsColoredBox(darkThemeEnabled = isSystemInDarkTheme(), parameter = parameter)
}

@Composable
internal fun PreviewOudsContrastedSurface(darkThemeEnabled: Boolean, parameter: OudsColorKeyToken.Surface) = OudsPreview(darkThemeEnabled = darkThemeEnabled) {
OudsContrastedSurface(color = parameter) {
internal fun PreviewOudsColoredBox(darkThemeEnabled: Boolean, parameter: OudsColorKeyToken.Surface) = OudsPreview(darkThemeEnabled = darkThemeEnabled) {
OudsColoredBox(color = parameter) {
Text(
modifier = Modifier.padding(all = OudsSpaceKeyToken.Fixed.Medium.value),
text = parameter.name.removePrefix("OudsColorKeyToken."),
)
}
}

internal class OudsContrastedSurfacePreviewParameterProvider : BasicPreviewParameterProvider<OudsColorKeyToken.Surface>(*previewParameterValues.toTypedArray())
internal class OudsColoredBoxPreviewParameterProvider : BasicPreviewParameterProvider<OudsColorKeyToken.Surface>(*previewParameterValues.toTypedArray())

private val previewParameterValues: List<OudsColorKeyToken.Surface>
get() = listOf(
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/com/orange/ouds/core/theme/OudsTheme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.staticCompositionLocalOf
import com.orange.ouds.theme.OudsThemeContract
import com.orange.ouds.theme.tokens.OudsColorKeyToken
import com.orange.ouds.theme.tokens.components.OudsComponentsTokens

private fun missingCompositionLocalError(compositionLocalName: String): Nothing =
Expand All @@ -41,7 +40,7 @@ private val LocalOpacities = staticCompositionLocalOf<OudsOpacities> { missingCo
private val LocalSizes = staticCompositionLocalOf<OudsSizes> { missingCompositionLocalError("LocalSizes") }
private val LocalSpaces = staticCompositionLocalOf<OudsSpaces> { missingCompositionLocalError("LocalSpaces") }
private val LocalComponentsTokens = staticCompositionLocalOf<OudsComponentsTokens> { missingCompositionLocalError("LocalComponentsTokens") }
internal val LocalContrastedSurface = staticCompositionLocalOf<OudsColorKeyToken.Surface?> { null }
internal val LocalColoredBox = staticCompositionLocalOf<Boolean> { false }

object OudsTheme {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Software description: Android library of reusable graphical components
*/

package com.orange.ouds.core.component.contrastedsurface
package com.orange.ouds.core.component.coloredbox

import app.cash.paparazzi.Paparazzi
import com.android.ide.common.rendering.api.SessionParams
Expand All @@ -21,31 +21,31 @@ import org.junit.runner.RunWith
import org.junit.runners.Parameterized

@RunWith(Parameterized::class)
internal class OudsContrastedSurfaceTest(private val parameter: OudsColorKeyToken.Surface) {
internal class OudsColoredBoxTest(private val parameter: OudsColorKeyToken.Surface) {

companion object {
@JvmStatic
@Parameterized.Parameters
internal fun data() = OudsContrastedSurfacePreviewParameterProvider().values.toList()
internal fun data() = OudsColoredBoxPreviewParameterProvider().values.toList()
}

@get:Rule
val paparazzi = Paparazzi(renderingMode = SessionParams.RenderingMode.SHRINK, maxPercentDifference = 0.0)

@Test
fun takeOudsContrastedSurfaceLightThemeSnapshot() {
fun takeOudsColoredBoxLightThemeSnapshot() {
paparazzi.snapshot {
PreviewOudsContrastedSurface(
PreviewOudsColoredBox(
darkThemeEnabled = false,
parameter = parameter
)
}
}

@Test
fun takeOudsContrastedSurfaceDarkThemeSnapshot() {
fun takeOudsColoredBoxDarkThemeSnapshot() {
paparazzi.snapshot {
PreviewOudsContrastedSurface(
PreviewOudsColoredBox(
darkThemeEnabled = true,
parameter = parameter
)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 25e8d01

Please sign in to comment.