diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2023-06-08 21:37:00 +0000 |
---|---|---|
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2023-06-08 21:37:00 +0000 |
commit | d7a3ca2e3a6cdd0651074235b1e6406d3caa73f3 (patch) | |
tree | 605c8080986607c9cce3e5e230b7f3a59a535ca8 | |
parent | 79791f1dc63889351eb70fabb4d047d74b75c909 (diff) | |
parent | 863d4c5606d027fe3ac73bbd0e01c97b8658b961 (diff) | |
download | support-d7a3ca2e3a6cdd0651074235b1e6406d3caa73f3.tar.gz |
Merge cherrypicks of ['android-review.googlesource.com/2616362'] into androidx-compose-beta-release.
Change-Id: I9914843ec2642c4e36b334492827cad1864e09d6
5 files changed, 142 insertions, 11 deletions
diff --git a/compose/ui/ui/integration-tests/ui-demos/src/main/java/androidx/compose/ui/demos/DialogDemo.kt b/compose/ui/ui/integration-tests/ui-demos/src/main/java/androidx/compose/ui/demos/DialogDemo.kt index 442665e7990..c3878af1c98 100644 --- a/compose/ui/ui/integration-tests/ui-demos/src/main/java/androidx/compose/ui/demos/DialogDemo.kt +++ b/compose/ui/ui/integration-tests/ui-demos/src/main/java/androidx/compose/ui/demos/DialogDemo.kt @@ -35,6 +35,11 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.RectangleShape import androidx.compose.ui.unit.dp import androidx.compose.ui.window.Dialog +import androidx.compose.ui.window.DialogProperties + +private var expandedWidthInit = false +private var expandedHeightInit = false +private var usePlatformDefaultWidthInit = false @Composable fun DialogDemo() { @@ -42,11 +47,22 @@ fun DialogDemo() { var shape by remember { mutableStateOf(RectangleShape) } var elevation by remember { mutableStateOf(8.dp) } var openDialog by remember { mutableStateOf(true) } + var expandedWidth by remember { mutableStateOf(expandedWidthInit) } + var expandedHeight by remember { mutableStateOf(expandedHeightInit) } + var usePlatformDefaultWidth by remember { mutableStateOf(usePlatformDefaultWidthInit) } + TextButton(onClick = { openDialog = !openDialog }) { + Text("Tap anywhere to reopen dialog") + } if (openDialog) { - Dialog(onDismissRequest = { openDialog = false }) { + Dialog( + onDismissRequest = { openDialog = false }, + properties = DialogProperties(usePlatformDefaultWidth = usePlatformDefaultWidth) + ) { + val width = if (expandedWidth) { 1500.dp } else { 300.dp } + val height = if (expandedHeight) { 600.dp } else { 400.dp } Card( - modifier = Modifier.size(350.dp, 200.dp).padding(10.dp), + modifier = Modifier.size(width, height).padding(10.dp), elevation = elevation, shape = shape ) { @@ -64,7 +80,31 @@ fun DialogDemo() { } } ) { - Text("Toggle shape") + Text("Toggle corners") + } + TextButton( + onClick = { + expandedWidth = !expandedWidth + expandedWidthInit = expandedWidth + } + ) { + Text("Toggle width") + } + TextButton( + onClick = { + expandedHeight = !expandedHeight + expandedHeightInit = expandedHeight + } + ) { + Text("Toggle height") + } + TextButton( + onClick = { + usePlatformDefaultWidth = !usePlatformDefaultWidth + usePlatformDefaultWidthInit = usePlatformDefaultWidthInit + } + ) { + Text("Toggle widthlock") } Row(verticalAlignment = Alignment.CenterVertically) { TextButton(onClick = { elevation -= 1.dp }) { @@ -75,6 +115,8 @@ fun DialogDemo() { Text("+1") } } + Text("Current size: [$width, $height]") + Text("usePlatformDefaultWidth = $usePlatformDefaultWidth") } } } diff --git a/compose/ui/ui/src/androidAndroidTest/kotlin/androidx/compose/ui/window/DialogTest.kt b/compose/ui/ui/src/androidAndroidTest/kotlin/androidx/compose/ui/window/DialogTest.kt index 1dec8eac0bb..d98d8077c8f 100644 --- a/compose/ui/ui/src/androidAndroidTest/kotlin/androidx/compose/ui/window/DialogTest.kt +++ b/compose/ui/ui/src/androidAndroidTest/kotlin/androidx/compose/ui/window/DialogTest.kt @@ -20,12 +20,14 @@ import androidx.activity.compose.BackHandler import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.size import androidx.compose.foundation.text.BasicText import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.compositionLocalOf +import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember -import androidx.compose.ui.ExperimentalComposeUiApi +import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.layout.onSizeChanged import androidx.compose.ui.test.TestActivity @@ -36,6 +38,7 @@ import androidx.compose.ui.test.junit4.createAndroidComposeRule import androidx.compose.ui.test.onFirst import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick +import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.height import androidx.test.espresso.Espresso import androidx.test.ext.junit.runners.AndroidJUnit4 @@ -315,7 +318,6 @@ class DialogTest { } } - @OptIn(ExperimentalComposeUiApi::class) @Test fun canFillScreenWidth_dependingOnProperty() { var box1Width = 0 @@ -339,4 +341,38 @@ class DialogTest { Truth.assertThat(box2Width).isLessThan(box1Width) } } + + @Test + fun canChangeSize() { + var width by mutableStateOf(10.dp) + var usePlatformDefaultWidth by mutableStateOf(false) + var actualWidth = 0 + + rule.setContent { + Dialog( + onDismissRequest = {}, + properties = DialogProperties(usePlatformDefaultWidth = usePlatformDefaultWidth) + ) { + Box(Modifier.size(width, 150.dp).onSizeChanged { actualWidth = it.width }) + } + } + rule.runOnIdle { + Truth.assertThat(actualWidth).isEqualTo((10 * rule.density.density).roundToInt()) + } + width = 20.dp + rule.runOnIdle { + Truth.assertThat(actualWidth).isEqualTo((20 * rule.density.density).roundToInt()) + } + + usePlatformDefaultWidth = true + + width = 30.dp + rule.runOnIdle { + Truth.assertThat(actualWidth).isEqualTo((30 * rule.density.density).roundToInt()) + } + width = 40.dp + rule.runOnIdle { + Truth.assertThat(actualWidth).isEqualTo((40 * rule.density.density).roundToInt()) + } + } }
\ No newline at end of file diff --git a/compose/ui/ui/src/androidAndroidTest/kotlin/androidx/compose/ui/window/PopupTest.kt b/compose/ui/ui/src/androidAndroidTest/kotlin/androidx/compose/ui/window/PopupTest.kt index 36d5ef192b9..df8fecb1840 100644 --- a/compose/ui/ui/src/androidAndroidTest/kotlin/androidx/compose/ui/window/PopupTest.kt +++ b/compose/ui/ui/src/androidAndroidTest/kotlin/androidx/compose/ui/window/PopupTest.kt @@ -385,6 +385,40 @@ class PopupTest { } } + @OptIn(ExperimentalComposeUiApi::class) + @Test + fun canChangeSize() { + var width by mutableStateOf(10.dp) + var usePlatformDefaultWidth by mutableStateOf(false) + var actualWidth = 0 + + rule.setContent { + Popup( + properties = PopupProperties(usePlatformDefaultWidth = usePlatformDefaultWidth) + ) { + Box(Modifier.size(width, 150.dp).onSizeChanged { actualWidth = it.width }) + } + } + rule.runOnIdle { + assertThat(actualWidth).isEqualTo((10 * rule.density.density).roundToInt()) + } + width = 20.dp + rule.runOnIdle { + assertThat(actualWidth).isEqualTo((20 * rule.density.density).roundToInt()) + } + + usePlatformDefaultWidth = true + + width = 30.dp + rule.runOnIdle { + assertThat(actualWidth).isEqualTo((30 * rule.density.density).roundToInt()) + } + width = 40.dp + rule.runOnIdle { + assertThat(actualWidth).isEqualTo((40 * rule.density.density).roundToInt()) + } + } + @Test fun didNotMeasureTooSmallLast() { rule.setContent { diff --git a/compose/ui/ui/src/androidMain/kotlin/androidx/compose/ui/window/AndroidDialog.android.kt b/compose/ui/ui/src/androidMain/kotlin/androidx/compose/ui/window/AndroidDialog.android.kt index 346e6f3732d..1ecf799da5b 100644 --- a/compose/ui/ui/src/androidMain/kotlin/androidx/compose/ui/window/AndroidDialog.android.kt +++ b/compose/ui/ui/src/androidMain/kotlin/androidx/compose/ui/window/AndroidDialog.android.kt @@ -247,8 +247,10 @@ private class DialogLayout( super.internalOnLayout(changed, left, top, right, bottom) // Now set the content size as fixed layout params, such that ViewRootImpl knows // the exact window size. - val child = getChildAt(0) ?: return - window.setLayout(child.measuredWidth, child.measuredHeight) + if (!usePlatformDefaultWidth) { + val child = getChildAt(0) ?: return + window.setLayout(child.measuredWidth, child.measuredHeight) + } } private val displayWidth: Int @@ -405,6 +407,14 @@ private class DialogWrapper( this.properties = properties setSecurePolicy(properties.securePolicy) setLayoutDirection(layoutDirection) + if (properties.usePlatformDefaultWidth && !dialogLayout.usePlatformDefaultWidth) { + // Undo fixed size in internalOnLayout, which would suppress size changes when + // usePlatformDefaultWidth is true. + window?.setLayout( + WindowManager.LayoutParams.WRAP_CONTENT, + WindowManager.LayoutParams.WRAP_CONTENT + ) + } dialogLayout.usePlatformDefaultWidth = properties.usePlatformDefaultWidth if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) { @OptIn(ExperimentalComposeUiApi::class) diff --git a/compose/ui/ui/src/androidMain/kotlin/androidx/compose/ui/window/AndroidPopup.android.kt b/compose/ui/ui/src/androidMain/kotlin/androidx/compose/ui/window/AndroidPopup.android.kt index 086117875d8..c0784081cbc 100644 --- a/compose/ui/ui/src/androidMain/kotlin/androidx/compose/ui/window/AndroidPopup.android.kt +++ b/compose/ui/ui/src/androidMain/kotlin/androidx/compose/ui/window/AndroidPopup.android.kt @@ -483,10 +483,12 @@ internal class PopupLayout( super.internalOnLayout(changed, left, top, right, bottom) // Now set the content size as fixed layout params, such that ViewRootImpl knows // the exact window size. - val child = getChildAt(0) ?: return - params.width = child.measuredWidth - params.height = child.measuredHeight - popupLayoutHelper.updateViewLayout(windowManager, this, params) + if (!properties.usePlatformDefaultWidth) { + val child = getChildAt(0) ?: return + params.width = child.measuredWidth + params.height = child.measuredHeight + popupLayoutHelper.updateViewLayout(windowManager, this, params) + } } private val displayWidth: Int @@ -562,6 +564,13 @@ internal class PopupLayout( layoutDirection: LayoutDirection ) { this.onDismissRequest = onDismissRequest + if (properties.usePlatformDefaultWidth && !this.properties.usePlatformDefaultWidth) { + // Undo fixed size in internalOnLayout, which would suppress size changes when + // usePlatformDefaultWidth is true. + params.width = WindowManager.LayoutParams.WRAP_CONTENT + params.height = WindowManager.LayoutParams.WRAP_CONTENT + popupLayoutHelper.updateViewLayout(windowManager, this, params) + } this.properties = properties this.testTag = testTag setIsFocusable(properties.focusable) |