Skip to content

Commit

Permalink
Add OudsContrastedSurface
Browse files Browse the repository at this point in the history
  • Loading branch information
florentmaitre committed Dec 18, 2024
1 parent ad71e17 commit c69e968
Show file tree
Hide file tree
Showing 38 changed files with 201 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Software Name: OUDS Android
* SPDX-FileCopyrightText: Copyright (c) Orange SA
* SPDX-License-Identifier: MIT
*
* This software is distributed under the MIT license,
* the text of which is available at https://opensource.org/license/MIT/
* or see the "LICENSE" file for more details.
*
* Software description: Android library of reusable graphical components
*/

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

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
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 androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.orange.ouds.core.theme.value
import com.orange.ouds.core.utilities.OudsPreview
import com.orange.ouds.foundation.utilities.BasicPreviewParameterProvider
import com.orange.ouds.foundation.utilities.UiModePreviews
import com.orange.ouds.theme.tokens.OudsColorKeyToken
import com.orange.ouds.theme.tokens.OudsSpaceKeyToken

@Composable
fun OudsContrastedSurface(
color: OudsColorKeyToken.Surface,
modifier: Modifier = Modifier,
shape: Shape = RectangleShape,
tonalElevation: Dp = 0.dp,
shadowElevation: Dp = 0.dp,
border: BorderStroke? = null,
content: @Composable () -> Unit
) {
Surface(
modifier = modifier,
shape = shape,
color = color.value,
contentColor = contentColorFor(color),
tonalElevation = tonalElevation,
shadowElevation = shadowElevation,
border = border,
content = content
)
}

@Composable
private fun contentColorFor(color: OudsColorKeyToken.Surface): Color {
return when (color) {
OudsColorKeyToken.Surface.Brand.Primary -> OudsColorKeyToken.Content.OnBrand.Primary
OudsColorKeyToken.Surface.Status.Accent.Emphasized,
OudsColorKeyToken.Surface.Status.Info.Emphasized,
OudsColorKeyToken.Surface.Status.Negative.Emphasized,
OudsColorKeyToken.Surface.Status.Positive.Emphasized,
OudsColorKeyToken.Surface.Status.Warning.Emphasized -> OudsColorKeyToken.Content.OnStatus.Emphasized
OudsColorKeyToken.Surface.Status.Neutral.Emphasized -> OudsColorKeyToken.Content.OnStatus.EmphasizedNeutral
OudsColorKeyToken.Surface.Status.Accent.Muted,
OudsColorKeyToken.Surface.Status.Info.Muted,
OudsColorKeyToken.Surface.Status.Negative.Muted,
OudsColorKeyToken.Surface.Status.Positive.Muted,
OudsColorKeyToken.Surface.Status.Neutral.Muted,
OudsColorKeyToken.Surface.Status.Warning.Muted -> OudsColorKeyToken.Content.OnStatus.Muted
}.value
}

@Suppress("PreviewShouldNotBeCalledRecursively")
@UiModePreviews.Default
@Composable
private fun PreviewOudsContrastedSurface(@PreviewParameter(OudsContrastedSurfacePreviewParameterProvider::class) parameter: OudsColorKeyToken.Surface) {
PreviewOudsContrastedSurface(darkThemeEnabled = isSystemInDarkTheme(), parameter = parameter)
}

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

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

private val previewParameterValues: List<OudsColorKeyToken.Surface>
get() = listOf(
OudsColorKeyToken.Surface.Brand.Primary,
OudsColorKeyToken.Surface.Status.Accent.Emphasized,
OudsColorKeyToken.Surface.Status.Accent.Muted,
OudsColorKeyToken.Surface.Status.Info.Emphasized,
OudsColorKeyToken.Surface.Status.Info.Muted,
OudsColorKeyToken.Surface.Status.Negative.Emphasized,
OudsColorKeyToken.Surface.Status.Negative.Muted,
OudsColorKeyToken.Surface.Status.Neutral.Emphasized,
OudsColorKeyToken.Surface.Status.Neutral.Muted,
OudsColorKeyToken.Surface.Status.Positive.Emphasized,
OudsColorKeyToken.Surface.Status.Positive.Muted,
OudsColorKeyToken.Surface.Status.Warning.Emphasized,
OudsColorKeyToken.Surface.Status.Warning.Muted,
)
8 changes: 7 additions & 1 deletion core/src/main/java/com/orange/ouds/core/theme/OudsTheme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ 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 @@ -40,6 +41,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") }
private val LocalContrastedSurface = staticCompositionLocalOf<OudsColorKeyToken.Surface?> { null }

object OudsTheme {

Expand Down Expand Up @@ -96,6 +98,11 @@ object OudsTheme {
@Composable
@ReadOnlyComposable
get() = LocalComponentsTokens.current

internal val contrastedSurface: OudsColorKeyToken.Surface?
@Composable
@ReadOnlyComposable
get() = LocalContrastedSurface.current
}

/**
Expand All @@ -111,7 +118,6 @@ fun OudsTheme(
darkThemeEnabled: Boolean = isSystemInDarkTheme(),
content: @Composable () -> Unit
) {

val colorScheme = if (darkThemeEnabled) themeContract.colorTokens.darkColorScheme else themeContract.colorTokens.lightColorScheme
val materialColorScheme = if (darkThemeEnabled) materialDarkColorScheme else materialLightColorScheme

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Software Name: OUDS Android
* SPDX-FileCopyrightText: Copyright (c) Orange SA
* SPDX-License-Identifier: MIT
*
* This software is distributed under the MIT license,
* the text of which is available at https://opensource.org/license/MIT/
* or see the "LICENSE" file for more details.
*
* Software description: Android library of reusable graphical components
*/

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

import app.cash.paparazzi.Paparazzi
import com.android.ide.common.rendering.api.SessionParams
import com.orange.ouds.theme.tokens.OudsColorKeyToken
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized

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

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

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

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

@Test
fun takeOudsContrastedSurfaceDarkThemeSnapshot() {
paparazzi.snapshot {
PreviewOudsContrastedSurface(
darkThemeEnabled = true,
parameter = parameter
)
}
}
}
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.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

package com.orange.ouds.theme.tokens

sealed interface OudsBorderKeyToken {
sealed interface OudsBorderKeyToken : OudsKeyToken {
sealed interface Radius : OudsBorderKeyToken {
data object Default : Radius
data object Medium : Radius
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

package com.orange.ouds.theme.tokens

sealed interface OudsColorKeyToken {
sealed interface OudsColorKeyToken : OudsKeyToken {
sealed interface Opacity : OudsColorKeyToken {
data object Lower : Opacity
data object Lowest : Opacity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

package com.orange.ouds.theme.tokens

sealed interface OudsElevationKeyToken {
sealed interface OudsElevationKeyToken : OudsKeyToken {

data object None : OudsElevationKeyToken
data object Raised : OudsElevationKeyToken
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

package com.orange.ouds.theme.tokens

sealed interface OudsGridKeyToken {
sealed interface OudsGridKeyToken : OudsKeyToken {
data object ColumnGap : OudsGridKeyToken
data object Margin : OudsGridKeyToken
data object MaxWidth : OudsGridKeyToken
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Software Name: OUDS Android
* SPDX-FileCopyrightText: Copyright (c) Orange SA
* SPDX-License-Identifier: MIT
*
* This software is distributed under the MIT license,
* the text of which is available at https://opensource.org/license/MIT/
* or see the "LICENSE" file for more details.
*
* Software description: Android library of reusable graphical components
*/

package com.orange.ouds.theme.tokens

sealed interface OudsKeyToken {

val name: String
get() {
val packageName = this::class.java.`package`?.name.orEmpty()
return this::class.qualifiedName.orEmpty().removePrefix("$packageName.")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

package com.orange.ouds.theme.tokens

sealed interface OudsOpacityKeyToken {
sealed interface OudsOpacityKeyToken : OudsKeyToken {
data object Invisible : OudsOpacityKeyToken
data object Medium : OudsOpacityKeyToken
data object Opaque : OudsOpacityKeyToken
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

package com.orange.ouds.theme.tokens

sealed interface OudsSizeKeyToken {
sealed interface OudsSizeKeyToken : OudsKeyToken {
sealed interface Icon : OudsSizeKeyToken {
sealed interface Decorative : Icon {
data object ExtraExtraLarge : Decorative
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

package com.orange.ouds.theme.tokens

sealed interface OudsSpaceKeyToken {
sealed interface OudsSpaceKeyToken : OudsKeyToken {
sealed interface ColumnGap : OudsSpaceKeyToken {
data object Medium : ColumnGap
data object None : ColumnGap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

package com.orange.ouds.theme.tokens

sealed interface OudsTypographyKeyToken {
sealed interface OudsTypographyKeyToken : OudsKeyToken {

sealed interface Display : OudsTypographyKeyToken {
data object Large : Display
Expand Down

0 comments on commit c69e968

Please sign in to comment.