[go: nahoru, domu]

Updates M3 Divider to use token values

Relnote: Add default divider for menu
Bug: 221292344
Test: Screenshot tests added for updated color scheme
Change-Id: I013746cec4c0d9b24ee72a70aaca04d70259d2ab
diff --git a/compose/material3/material3/api/current.txt b/compose/material3/material3/api/current.txt
index b8743d9..6f38450 100644
--- a/compose/material3/material3/api/current.txt
+++ b/compose/material3/material3/api/current.txt
@@ -235,6 +235,7 @@
   }
 
   public final class MenuDefaults {
+    method @androidx.compose.runtime.Composable public void Divider(optional androidx.compose.ui.Modifier modifier, optional long color, optional float thickness);
     method public androidx.compose.foundation.layout.PaddingValues getDropdownMenuItemContentPadding();
     method @androidx.compose.runtime.Composable public androidx.compose.material3.MenuItemColors itemColors(optional long textColor, optional long leadingIconColor, optional long trailingIconColor, optional long disabledTextColor, optional long disabledLeadingIconColor, optional long disabledTrailingIconColor);
     property public final androidx.compose.foundation.layout.PaddingValues DropdownMenuItemContentPadding;
diff --git a/compose/material3/material3/api/public_plus_experimental_current.txt b/compose/material3/material3/api/public_plus_experimental_current.txt
index 7cbe48a..75c529d 100644
--- a/compose/material3/material3/api/public_plus_experimental_current.txt
+++ b/compose/material3/material3/api/public_plus_experimental_current.txt
@@ -298,6 +298,7 @@
   }
 
   public final class MenuDefaults {
+    method @androidx.compose.runtime.Composable public void Divider(optional androidx.compose.ui.Modifier modifier, optional long color, optional float thickness);
     method public androidx.compose.foundation.layout.PaddingValues getDropdownMenuItemContentPadding();
     method @androidx.compose.runtime.Composable public androidx.compose.material3.MenuItemColors itemColors(optional long textColor, optional long leadingIconColor, optional long trailingIconColor, optional long disabledTextColor, optional long disabledLeadingIconColor, optional long disabledTrailingIconColor);
     property public final androidx.compose.foundation.layout.PaddingValues DropdownMenuItemContentPadding;
diff --git a/compose/material3/material3/api/restricted_current.txt b/compose/material3/material3/api/restricted_current.txt
index b8743d9..6f38450 100644
--- a/compose/material3/material3/api/restricted_current.txt
+++ b/compose/material3/material3/api/restricted_current.txt
@@ -235,6 +235,7 @@
   }
 
   public final class MenuDefaults {
+    method @androidx.compose.runtime.Composable public void Divider(optional androidx.compose.ui.Modifier modifier, optional long color, optional float thickness);
     method public androidx.compose.foundation.layout.PaddingValues getDropdownMenuItemContentPadding();
     method @androidx.compose.runtime.Composable public androidx.compose.material3.MenuItemColors itemColors(optional long textColor, optional long leadingIconColor, optional long trailingIconColor, optional long disabledTextColor, optional long disabledLeadingIconColor, optional long disabledTrailingIconColor);
     property public final androidx.compose.foundation.layout.PaddingValues DropdownMenuItemContentPadding;
diff --git a/compose/material3/material3/samples/src/main/java/androidx/compose/material3/samples/MenuSamples.kt b/compose/material3/material3/samples/src/main/java/androidx/compose/material3/samples/MenuSamples.kt
index 2455922..03fa547 100644
--- a/compose/material3/material3/samples/src/main/java/androidx/compose/material3/samples/MenuSamples.kt
+++ b/compose/material3/material3/samples/src/main/java/androidx/compose/material3/samples/MenuSamples.kt
@@ -25,11 +25,11 @@
 import androidx.compose.material.icons.outlined.Edit
 import androidx.compose.material.icons.outlined.Email
 import androidx.compose.material.icons.outlined.Settings
-import androidx.compose.material3.Divider
 import androidx.compose.material3.DropdownMenu
 import androidx.compose.material3.DropdownMenuItem
 import androidx.compose.material3.Icon
 import androidx.compose.material3.IconButton
+import androidx.compose.material3.MenuDefaults
 import androidx.compose.material3.Text
 import androidx.compose.runtime.Composable
 import androidx.compose.runtime.getValue
@@ -71,7 +71,7 @@
                         contentDescription = null
                     )
                 })
-            Divider()
+            MenuDefaults.Divider()
             DropdownMenuItem(
                 text = { Text("Send Feedback") },
                  /* Handle send feedback! */ },
diff --git a/compose/material3/material3/src/androidAndroidTest/kotlin/androidx/compose/material3/DividerScreenshotTest.kt b/compose/material3/material3/src/androidAndroidTest/kotlin/androidx/compose/material3/DividerScreenshotTest.kt
new file mode 100644
index 0000000..4e232bf
--- /dev/null
+++ b/compose/material3/material3/src/androidAndroidTest/kotlin/androidx/compose/material3/DividerScreenshotTest.kt
@@ -0,0 +1,84 @@
+/*
+ * Copyright 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package androidx.compose.material3
+
+import android.os.Build
+import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.layout.Spacer
+import androidx.compose.foundation.layout.size
+import androidx.compose.testutils.assertAgainstGolden
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.platform.testTag
+import androidx.compose.ui.test.ExperimentalTestApi
+import androidx.compose.ui.test.captureToImage
+import androidx.compose.ui.test.junit4.createComposeRule
+import androidx.compose.ui.test.onNodeWithTag
+import androidx.compose.ui.unit.dp
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import androidx.test.filters.MediumTest
+import androidx.test.filters.SdkSuppress
+import androidx.test.screenshot.AndroidXScreenshotTestRule
+import org.junit.Rule
+import org.junit.Test
+import org.junit.runner.RunWith
+
+@MediumTest
+@RunWith(AndroidJUnit4::class)
+@SdkSuppress(minSdkVersion = Build.VERSION_CODES.O)
+@OptIn(ExperimentalTestApi::class)
+class DividerScreenshotTest {
+
+    @get:Rule
+    val composeTestRule = createComposeRule()
+
+    @get:Rule
+    val screenshotRule = AndroidXScreenshotTestRule(GOLDEN_MATERIAL3)
+
+    private val Tag = "Divider"
+
+    @Test
+    fun lightTheme() {
+        composeTestRule.setContent {
+            MaterialTheme(lightColorScheme()) {
+                Column(Modifier.testTag(Tag)) {
+                    Spacer(Modifier.size(10.dp))
+                    Divider()
+                    Spacer(Modifier.size(10.dp))
+                }
+            }
+        }
+        composeTestRule.onNodeWithTag(Tag)
+            .captureToImage()
+            .assertAgainstGolden(screenshotRule, "divider_lightTheme")
+    }
+
+    @Test
+    fun darkTheme() {
+        composeTestRule.setContent {
+            MaterialTheme(darkColorScheme()) {
+                Column(Modifier.testTag(Tag)) {
+                    Spacer(Modifier.size(10.dp))
+                    Divider()
+                    Spacer(Modifier.size(10.dp))
+                }
+            }
+        }
+        composeTestRule.onNodeWithTag(Tag)
+            .captureToImage()
+            .assertAgainstGolden(screenshotRule, "divider_darkTheme")
+    }
+}
diff --git a/compose/material3/material3/src/androidAndroidTest/kotlin/androidx/compose/material3/DividerUiTest.kt b/compose/material3/material3/src/androidAndroidTest/kotlin/androidx/compose/material3/DividerUiTest.kt
index 84d67c1..71c734b 100644
--- a/compose/material3/material3/src/androidAndroidTest/kotlin/androidx/compose/material3/DividerUiTest.kt
+++ b/compose/material3/material3/src/androidAndroidTest/kotlin/androidx/compose/material3/DividerUiTest.kt
@@ -95,7 +95,7 @@
         rule.setContent {
             sizePx = with(LocalDensity.current) { size.toPx().roundToInt() }
             dividerColor =
-                MaterialTheme.colorScheme.surfaceVariant
+                MaterialTheme.colorScheme.onSurfaceVariant
             Box(modifier = Modifier.size(size).background(Color.Black)) {
                 Divider(
                     modifier = Modifier.testTag(testTag).fillMaxWidth(),
diff --git a/compose/material3/material3/src/androidAndroidTest/kotlin/androidx/compose/material3/MenuScreenshotTest.kt b/compose/material3/material3/src/androidAndroidTest/kotlin/androidx/compose/material3/MenuScreenshotTest.kt
index ea3f79f..210c05a 100644
--- a/compose/material3/material3/src/androidAndroidTest/kotlin/androidx/compose/material3/MenuScreenshotTest.kt
+++ b/compose/material3/material3/src/androidAndroidTest/kotlin/androidx/compose/material3/MenuScreenshotTest.kt
@@ -125,7 +125,7 @@
                         )
                     },
                     trailingIcon = { Text("F11", textAlign = TextAlign.Center) })
-                Divider()
+                MenuDefaults.Divider()
                 DropdownMenuItem(
                     text = { Text("Send Feedback") },
                      },
diff --git a/compose/material3/material3/src/commonMain/kotlin/androidx/compose/material3/Divider.kt b/compose/material3/material3/src/commonMain/kotlin/androidx/compose/material3/Divider.kt
index f87e227..7b58f66 100644
--- a/compose/material3/material3/src/commonMain/kotlin/androidx/compose/material3/Divider.kt
+++ b/compose/material3/material3/src/commonMain/kotlin/androidx/compose/material3/Divider.kt
@@ -21,6 +21,7 @@
 import androidx.compose.foundation.layout.fillMaxWidth
 import androidx.compose.foundation.layout.padding
 import androidx.compose.foundation.layout.height
+import androidx.compose.material3.tokens.DividerTokens
 import androidx.compose.runtime.Composable
 import androidx.compose.ui.Modifier
 import androidx.compose.ui.graphics.Color
@@ -29,22 +30,21 @@
 import androidx.compose.ui.unit.dp
 
 // TODO: Provide M3 divider image when asset is available.
-// TODO(b/210996136): Update to use standalone divider token.
 /**
  * <a href="https://material.io/components/dividers" class="external" target="_blank">Material Design divider</a>.
  *
  * A divider is a thin line that groups content in lists and layouts.
  *
  * @param color color of the divider line
- * @param thickness thickness of the divider line, 1 dp is used by default. Using [Dp.Hairline]
- * will produce a single pixel divider regardless of screen density.
+ * @param thickness thickness of the divider line
+ * Using [Dp.Hairline] will produce a single pixel divider regardless of screen density
  * @param startIndent start offset of this line, no offset by default
  */
 @Composable
 fun Divider(
     modifier: Modifier = Modifier,
-    color: Color = MaterialTheme.colorScheme.surfaceVariant,
-    thickness: Dp = 1.dp,
+    color: Color = DividerTokens.Color.toColor(),
+    thickness: Dp = DividerTokens.Thickness,
     startIndent: Dp = 0.dp
 ) {
     val indentMod = if (startIndent.value != 0f) {
diff --git a/compose/material3/material3/src/commonMain/kotlin/androidx/compose/material3/Menu.kt b/compose/material3/material3/src/commonMain/kotlin/androidx/compose/material3/Menu.kt
index 4a357c8..d6e0581 100644
--- a/compose/material3/material3/src/commonMain/kotlin/androidx/compose/material3/Menu.kt
+++ b/compose/material3/material3/src/commonMain/kotlin/androidx/compose/material3/Menu.kt
@@ -52,6 +52,7 @@
 import androidx.compose.ui.graphics.TransformOrigin
 import androidx.compose.ui.graphics.graphicsLayer
 import androidx.compose.ui.unit.Density
+import androidx.compose.ui.unit.Dp
 import androidx.compose.ui.unit.DpOffset
 import androidx.compose.ui.unit.IntOffset
 import androidx.compose.ui.unit.IntRect
@@ -260,6 +261,27 @@
         horizontal = DropdownMenuItemHorizontalPadding,
         vertical = 0.dp
     )
+
+    /**
+     * Default [Divider], which can be optionally positioned at the bottom of the
+     * [DropdownMenuItemContent].
+     *
+     * @param modifier modifier for the divider's layout
+     * @param color color of the divider
+     * @param thickness thickness of the divider
+     */
+    @Composable
+    fun Divider(
+        modifier: Modifier = Modifier,
+        color: Color = MenuTokens.DividerColor.toColor(),
+        thickness: Dp = MenuTokens.DividerHeight,
+    ) {
+        androidx.compose.material3.Divider(
+            modifier = modifier,
+            color = color,
+            thickness = thickness,
+        )
+    }
 }
 
 /**
diff --git a/compose/material3/material3/src/commonMain/kotlin/androidx/compose/material3/tokens/DividerTokens.kt b/compose/material3/material3/src/commonMain/kotlin/androidx/compose/material3/tokens/DividerTokens.kt
new file mode 100644
index 0000000..9dd3deb
--- /dev/null
+++ b/compose/material3/material3/src/commonMain/kotlin/androidx/compose/material3/tokens/DividerTokens.kt
@@ -0,0 +1,26 @@
+/*
+ * Copyright 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+// VERSION: v0_86
+// GENERATED CODE - DO NOT MODIFY BY HAND
+
+package androidx.compose.material3.tokens
+
+import androidx.compose.ui.unit.dp
+
+internal object DividerTokens {
+    val Color = ColorSchemeKeyTokens.OnSurfaceVariant
+    val Thickness = 1.0.dp
+}
\ No newline at end of file