[go: nahoru, domu]

Adds default values to Material ambients

Also removes unecessary toInt() for colors

Test: Manually verified
Change-Id: Id517cc2cb8a3b1f2b7b14263d5ed969ddaa218f0
diff --git a/ui/ui-foundation/integration-tests/foundation-demos/src/main/java/androidx/ui/foundation/demos/PopupActivity.kt b/ui/ui-foundation/integration-tests/foundation-demos/src/main/java/androidx/ui/foundation/demos/PopupActivity.kt
index e94497c..84d0564 100644
--- a/ui/ui-foundation/integration-tests/foundation-demos/src/main/java/androidx/ui/foundation/demos/PopupActivity.kt
+++ b/ui/ui-foundation/integration-tests/foundation-demos/src/main/java/androidx/ui/foundation/demos/PopupActivity.kt
@@ -342,7 +342,7 @@
             ColoredContainer(
                 height = heightSize,
                 width = widthSize,
-                color = Color(0xFFFF0000.toInt()),
+                color = Color(0xFFFF0000),
                 alignment = Alignment.BottomCenter
             ) {
                 Popup(popupAlignment.value) {
@@ -489,7 +489,7 @@
                 ColoredContainer(
                     width = 80.dp,
                     height = 160.dp,
-                    color = Color(0xFF00FF00.toInt())
+                    color = Color(0xFF00FF00)
                 ) {
                     Popup(alignment = Alignment.Center) {
                         ClickableTextWithBackground(text = "Centered", color = Color.Cyan)
diff --git a/ui/ui-material/integration-tests/material-demos/src/main/java/androidx/ui/material/demos/DynamicThemeActivity.kt b/ui/ui-material/integration-tests/material-demos/src/main/java/androidx/ui/material/demos/DynamicThemeActivity.kt
index 7348013..ea4ef71 100644
--- a/ui/ui-material/integration-tests/material-demos/src/main/java/androidx/ui/material/demos/DynamicThemeActivity.kt
+++ b/ui/ui-material/integration-tests/material-demos/src/main/java/androidx/ui/material/demos/DynamicThemeActivity.kt
@@ -146,8 +146,8 @@
 private fun Cards() {
     Column {
         repeat(20) { index ->
-            val shapeColor = lerp(Color(0xFF303030.toInt()), Color.White, index / 19f)
-            val textColor = lerp(Color.White, Color(0xFF303030.toInt()), index / 19f)
+            val shapeColor = lerp(Color(0xFF303030), Color.White, index / 19f)
+            val textColor = lerp(Color.White, Color(0xFF303030), index / 19f)
             Padding(25.dp) {
                 Container(ExpandedWidth, height = 150.dp) {
                     // TODO: ideally this would be a Card but currently Surface consumes every
@@ -165,9 +165,9 @@
 private fun interpolateTheme(fraction: Float): ColorPalette {
     val interpolatedFraction = FastOutSlowInEasing(fraction)
 
-    val primary = lerp(Color(0xFF6200EE.toInt()), Color(0xFF303030.toInt()), interpolatedFraction)
-    val secondary = lerp(Color(0xFF03DAC6.toInt()), Color(0xFFBB86FC.toInt()), interpolatedFraction)
-    val background = lerp(Color.White, Color(0xFF121212.toInt()), interpolatedFraction)
+    val primary = lerp(Color(0xFF6200EE), Color(0xFF303030), interpolatedFraction)
+    val secondary = lerp(Color(0xFF03DAC6), Color(0xFFBB86FC), interpolatedFraction)
+    val background = lerp(Color.White, Color(0xFF121212), interpolatedFraction)
 
     return ColorPalette(
         primary = primary,
diff --git a/ui/ui-material/integration-tests/material-demos/src/main/java/androidx/ui/material/demos/MaterialDemoActivity.kt b/ui/ui-material/integration-tests/material-demos/src/main/java/androidx/ui/material/demos/MaterialDemoActivity.kt
index 89680ea..98ccd23 100644
--- a/ui/ui-material/integration-tests/material-demos/src/main/java/androidx/ui/material/demos/MaterialDemoActivity.kt
+++ b/ui/ui-material/integration-tests/material-demos/src/main/java/androidx/ui/material/demos/MaterialDemoActivity.kt
@@ -130,7 +130,7 @@
             if (savedValue.isNullOrBlank()) {
                 null
             } else {
-                val parsedColor = Color(java.lang.Long.parseLong(savedValue, 16).toInt())
+                val parsedColor = Color(java.lang.Long.parseLong(savedValue, 16))
                 parameter to parsedColor
             }
         }.toMap()
@@ -179,8 +179,8 @@
      * or light-on-dark color pair.
      */
     private fun generateColorPair(): Pair<Color, Color> {
-        val darkColor = Color(DARK_PALETTE_COLORS.random().toInt())
-        val lightColor = Color(LIGHT_PALETTE_COLORS.random().toInt())
+        val darkColor = Color(DARK_PALETTE_COLORS.random())
+        val lightColor = Color(LIGHT_PALETTE_COLORS.random())
         val isMainColorLight = Random.nextBoolean()
         return if (isMainColorLight) {
             (lightColor to darkColor)
diff --git a/ui/ui-material/integration-tests/material-studies/src/main/java/androidx/ui/material/studies/rally/RallyTheme.kt b/ui/ui-material/integration-tests/material-studies/src/main/java/androidx/ui/material/studies/rally/RallyTheme.kt
index c8cc995..8ba5984 100644
--- a/ui/ui-material/integration-tests/material-studies/src/main/java/androidx/ui/material/studies/rally/RallyTheme.kt
+++ b/ui/ui-material/integration-tests/material-studies/src/main/java/androidx/ui/material/studies/rally/RallyTheme.kt
@@ -41,7 +41,7 @@
 fun RallyTheme(children: @Composable() () -> Unit) {
     val colors = ColorPalette(
         primary = rallyGreen,
-        surface = Color(0xFF26282F.toInt()),
+        surface = Color(0xFF26282F),
         >
         background = Color(0xFF26282F),
         >
diff --git a/ui/ui-material/integration-tests/samples/src/main/java/androidx/ui/material/samples/ThemeSamples.kt b/ui/ui-material/integration-tests/samples/src/main/java/androidx/ui/material/samples/ThemeSamples.kt
index d94ef22..ccf1c58 100644
--- a/ui/ui-material/integration-tests/samples/src/main/java/androidx/ui/material/samples/ThemeSamples.kt
+++ b/ui/ui-material/integration-tests/samples/src/main/java/androidx/ui/material/samples/ThemeSamples.kt
@@ -37,8 +37,8 @@
 @Composable
 fun MaterialThemeSample() {
     val colors = ColorPalette(
-        primary = Color(0xFF1EB980.toInt()),
-        surface = Color(0xFF26282F.toInt()),
+        primary = Color(0xFF1EB980),
+        surface = Color(0xFF26282F),
         >
     )
 
diff --git a/ui/ui-material/src/main/java/androidx/ui/material/Color.kt b/ui/ui-material/src/main/java/androidx/ui/material/Color.kt
index 63c98ae..463b591 100644
--- a/ui/ui-material/src/main/java/androidx/ui/material/Color.kt
+++ b/ui/ui-material/src/main/java/androidx/ui/material/Color.kt
@@ -93,13 +93,13 @@
  * [https://material.io/design/color/the-color-system.html#color-theme-creation].
  */
 fun ColorPalette(
-    primary: Color = Color(0xFF6200EE.toInt()),
-    primaryVariant: Color = Color(0xFF3700B3.toInt()),
-    secondary: Color = Color(0xFF03DAC6.toInt()),
-    secondaryVariant: Color = Color(0xFF018786.toInt()),
+    primary: Color = Color(0xFF6200EE),
+    primaryVariant: Color = Color(0xFF3700B3),
+    secondary: Color = Color(0xFF03DAC6),
+    secondaryVariant: Color = Color(0xFF018786),
     background: Color = Color.White,
     surface: Color = Color.White,
-    error: Color = Color(0xFFB00020.toInt()),
+    error: Color = Color(0xFFB00020),
     onPrimary: Color = Color.White,
     onSecondary: Color = Color.Black,
     onBackground: Color = Color.Black,
diff --git a/ui/ui-material/src/main/java/androidx/ui/material/DataTable.kt b/ui/ui-material/src/main/java/androidx/ui/material/DataTable.kt
index 5fa07d9..c1b33c1 100644
--- a/ui/ui-material/src/main/java/androidx/ui/material/DataTable.kt
+++ b/ui/ui-material/src/main/java/androidx/ui/material/DataTable.kt
@@ -536,5 +536,5 @@
 private val DataRowHeight = 52.dp
 private val HeaderRowHeight = 56.dp
 private val CellSpacing = EdgeInsets(left = 16.dp, right = 16.dp)
-private val BorderColor = Color(0xFFC6C6C6.toInt())
+private val BorderColor = Color(0xFFC6C6C6)
 private val BorderWidth = 1.dp
diff --git a/ui/ui-material/src/main/java/androidx/ui/material/MaterialTheme.kt b/ui/ui-material/src/main/java/androidx/ui/material/MaterialTheme.kt
index fc52852..8ac5b6e 100644
--- a/ui/ui-material/src/main/java/androidx/ui/material/MaterialTheme.kt
+++ b/ui/ui-material/src/main/java/androidx/ui/material/MaterialTheme.kt
@@ -21,25 +21,15 @@
 import androidx.compose.Composable
 import androidx.compose.ambient
 import androidx.compose.effectOf
-import androidx.compose.memo
-import androidx.compose.unaryPlus
 import androidx.ui.core.CurrentTextStyleProvider
-import androidx.ui.core.ambientDensity
 import androidx.ui.core.dp
 import androidx.ui.core.sp
-import androidx.ui.core.withDensity
 import androidx.ui.engine.geometry.Shape
 import androidx.ui.foundation.shape.RectangleShape
 import androidx.ui.foundation.shape.corner.RoundedCornerShape
-import androidx.ui.graphics.Color
-import androidx.ui.graphics.luminance
-import androidx.ui.material.ripple.CurrentRippleTheme
-import androidx.ui.material.ripple.DefaultRippleEffectFactory
-import androidx.ui.material.ripple.RippleTheme
-import androidx.ui.material.surface.CurrentBackground
-import androidx.ui.text.font.FontWeight
-import androidx.ui.text.font.FontFamily
 import androidx.ui.text.TextStyle
+import androidx.ui.text.font.FontFamily
+import androidx.ui.text.font.FontWeight
 
 /**
  * This component defines the styling principles from the Material design specification. It must be
@@ -68,11 +58,7 @@
 ) {
     ProvideColorPalette(colors) {
         Typography.Provider(value = typography) {
-            CurrentTextStyleProvider(value = typography.body1) {
-                MaterialRippleTheme {
-                    MaterialShapeTheme(children = children)
-                }
-            }
+            CurrentTextStyleProvider(value = typography.body1, children = children)
         }
     }
 }
@@ -95,7 +81,7 @@
  *
  * To access values within this ambient, use [themeTextStyle].
  */
-val Typography = Ambient.of<MaterialTypography> { error("No typography found!") }
+val Typography = Ambient.of { MaterialTypography() }
 
 /**
  * Data class holding typography definitions as defined by the [Material typography specification](https://material.io/design/typography/the-type-system.html#type-scale).
@@ -160,33 +146,6 @@
 )
 
 /**
- * Applies the default [RippleTheme] parameters based on the Material Design
- * guidelines for descendants
- */
-@Composable
-private fun MaterialRippleTheme(children: @Composable() () -> Unit) {
-    val defaultTheme = +memo {
-        RippleTheme(
-            factory = DefaultRippleEffectFactory,
-            defaultColor = effectOf {
-                val background = +ambient(CurrentBackground)
-                val textColor = +textColorForBackground(background)
-                when {
-                    textColor != null -> textColor
-                    background.alpha == 0f || background.luminance() >= 0.5 -> Color.Black
-                    else -> Color.White
-                }
-            },
-            opacity = effectOf {
-                val isDarkTheme = (+MaterialTheme.colors()).surface.luminance() < 0.5f
-                if (isDarkTheme) 0.24f else 0.12f
-            }
-        )
-    }
-    CurrentRippleTheme.Provider(value = defaultTheme, children = children)
-}
-
-/**
  * Helper effect that resolves [TextStyle]s from the [Typography] ambient by applying
  * [choosingBlock].
  *
@@ -210,36 +169,18 @@
     /**
      * Shape used for [Button]
      */
-    val button: Shape,
+    val button: Shape = RoundedCornerShape(4.dp),
     /**
      * Shape used for [androidx.ui.material.surface.Card]
      */
-    val card: Shape
+    val card: Shape = RectangleShape
     // TODO(Andrey): Add shapes for other surfaces? will see what we need.
 )
 
 /**
  * Ambient used to specify the default shapes for the surfaces.
- *
- * @see [MaterialShapeTheme] for the default Material Design value
  */
-val CurrentShapeAmbient = Ambient.of<Shapes> {
-    throw IllegalStateException("No default shapes provided.")
-}
-
-/**
- * Applies the default [Shape]s for all the surfaces.
- */
-@Composable
-fun MaterialShapeTheme(children: @Composable() () -> Unit) {
-    val value = withDensity(+ambientDensity()) {
-        Shapes(
-            button = RoundedCornerShape(4.dp),
-            card = RectangleShape
-        )
-    }
-    CurrentShapeAmbient.Provider(value = value, children = children)
-}
+val CurrentShapeAmbient = Ambient.of { Shapes() }
 
 /**
  * Helper effect to resolve [Shape]s from the [CurrentShapeAmbient] ambient by applying
@@ -248,4 +189,4 @@
 @CheckResult(suggest = "+")
 fun themeShape(
     choosingBlock: Shapes.() -> Shape
-) = effectOf<Shape> { (+ambient(CurrentShapeAmbient)).choosingBlock() }
\ No newline at end of file
+) = effectOf<Shape> { (+ambient(CurrentShapeAmbient)).choosingBlock() }
diff --git a/ui/ui-material/src/main/java/androidx/ui/material/ripple/RippleTheme.kt b/ui/ui-material/src/main/java/androidx/ui/material/ripple/RippleTheme.kt
index 8531af0..5097d4d 100644
--- a/ui/ui-material/src/main/java/androidx/ui/material/ripple/RippleTheme.kt
+++ b/ui/ui-material/src/main/java/androidx/ui/material/ripple/RippleTheme.kt
@@ -16,10 +16,15 @@
 
 package androidx.ui.material.ripple
 
-import androidx.ui.graphics.Color
 import androidx.compose.Ambient
 import androidx.compose.Effect
+import androidx.compose.ambient
+import androidx.compose.effectOf
+import androidx.ui.graphics.Color
+import androidx.ui.graphics.luminance
 import androidx.ui.material.MaterialTheme
+import androidx.ui.material.surface.CurrentBackground
+import androidx.ui.material.textColorForBackground
 
 /**
  * Defines the appearance and the behavior for [Ripple]s.
@@ -45,6 +50,21 @@
     val opacity: Effect<Float>
 )
 
-val CurrentRippleTheme = Ambient.of<RippleTheme> {
-    error("No RippleTheme provided. Please add MaterialTheme as an ancestor.")
-}
+val CurrentRippleTheme = Ambient.of { DefaultRippleTheme }
+
+private val DefaultRippleTheme = RippleTheme(
+        factory = DefaultRippleEffectFactory,
+        defaultColor = effectOf {
+            val background = +ambient(CurrentBackground)
+            val textColor = +textColorForBackground(background)
+            when {
+                textColor != null -> textColor
+                background.alpha == 0f || background.luminance() >= 0.5 -> Color.Black
+                else -> Color.White
+            }
+        },
+        opacity = effectOf {
+            val isDarkTheme = (+MaterialTheme.colors()).surface.luminance() < 0.5f
+            if (isDarkTheme) 0.24f else 0.12f
+        }
+    )
\ No newline at end of file