|
| 1 | +package com.example.jetsnack.ui.components |
| 2 | + |
| 3 | +import androidx.compose.Composable |
| 4 | +import androidx.compose.Providers |
| 5 | +import androidx.ui.core.Modifier |
| 6 | +import androidx.ui.core.clip |
| 7 | +import androidx.ui.core.drawShadow |
| 8 | +import androidx.ui.core.zIndex |
| 9 | +import androidx.ui.foundation.Border |
| 10 | +import androidx.ui.foundation.Box |
| 11 | +import androidx.ui.foundation.ContentColorAmbient |
| 12 | +import androidx.ui.foundation.drawBackground |
| 13 | +import androidx.ui.foundation.drawBorder |
| 14 | +import androidx.ui.graphics.Color |
| 15 | +import androidx.ui.graphics.RectangleShape |
| 16 | +import androidx.ui.graphics.Shape |
| 17 | +import androidx.ui.graphics.compositeOver |
| 18 | +import androidx.ui.unit.Dp |
| 19 | +import androidx.ui.unit.dp |
| 20 | +import com.example.jetsnack.ui.theme.JetsnackTheme |
| 21 | +import kotlin.math.ln |
| 22 | + |
| 23 | +/** |
| 24 | + * An alternative to [androidx.ui.material.Surface] utilizing [com.example.jetsnack.ui.theme.JetsnackColorPalette] |
| 25 | + */ |
| 26 | +@Composable |
| 27 | +fun JetsnackSurface( |
| 28 | + modifier: Modifier = Modifier, |
| 29 | + shape: Shape = RectangleShape, |
| 30 | + color: Color = JetsnackTheme.colors.uiBackground, |
| 31 | + contentColor: Color = JetsnackTheme.colors.textPrimary, |
| 32 | + border: Border? = null, |
| 33 | + elevation: Dp = 0.dp, |
| 34 | + content: @Composable () -> Unit |
| 35 | +) { |
| 36 | + Box( |
| 37 | + modifier = modifier.drawShadow(elevation = elevation, shape = shape, clip = false) |
| 38 | + .zIndex(elevation.value) |
| 39 | + + (if (border != null) Modifier.drawBorder(border, shape) else Modifier) |
| 40 | + .drawBackground( |
| 41 | + color = getBackgroundColorForElevation(color, elevation), |
| 42 | + shape = shape |
| 43 | + ) |
| 44 | + .clip(shape) |
| 45 | + ) { |
| 46 | + Providers(ContentColorAmbient provides contentColor, children = content) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +@Composable |
| 51 | +private fun getBackgroundColorForElevation(color: Color, elevation: Dp): Color { |
| 52 | + return if (elevation > 0.dp //&& https://issuetracker.google.com/issues/161429530 |
| 53 | + //JetsnackTheme.colors.isDark //&& |
| 54 | + //color == JetsnackTheme.colors.uiBackground |
| 55 | + ) { |
| 56 | + color.withElevation(elevation) |
| 57 | + } else { |
| 58 | + color |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Applies a [Color.White] overlay to this color based on the [elevation]. This increases visibility |
| 64 | + * of elevation for surfaces in a dark theme. |
| 65 | + */ |
| 66 | +private fun Color.withElevation(elevation: Dp): Color { |
| 67 | + val foreground = calculateForeground(elevation) |
| 68 | + return foreground.compositeOver(this) |
| 69 | +} |
| 70 | + |
| 71 | +// TODO: b/145802792 - clarify this algorithm |
| 72 | +/** |
| 73 | + * @return the alpha-modified [Color.White] to overlay on top of the surface color to produce |
| 74 | + * the resultant color. |
| 75 | + */ |
| 76 | +private fun calculateForeground(elevation: Dp): Color { |
| 77 | + val alpha = ((4.5f * ln(elevation.value + 1)) + 2f) / 100f |
| 78 | + return Color.White.copy(alpha = alpha) |
| 79 | +} |
0 commit comments