mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 12:28:40 +00:00
fix(ui): show dialogs on the second-screen menu without a separate platform window (#2452)
* fix(ui): show dialogs on the second-screen menu without a separate platform window * fix(ui): track presentation alert window for controller events * test(ui): assert presentation and platform dialog window semantics * test(ui): target the presentation popup for outside taps * test(ui): keep presentation popup tap tests off the auto-advancing idle loop The outside-tap dialog tests hang in the shared unit-test JVM when the compose clock auto-advances; step frames explicitly instead.
This commit is contained in:
1 parent
fbb705cf6f
commit
75c321123d
3 files changed
+292
-5
No files matched your search
@@ -1,17 +1,39 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import androidx.compose.foundation.ScrollState
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.ColumnScope
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.widthIn
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.material3.AlertDialog
|
||||
import androidx.compose.material3.AlertDialogDefaults
|
||||
import androidx.compose.material3.DropdownMenu
|
||||
import androidx.compose.material3.LocalContentColor
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.ProvideTextStyle
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.CompositionLocalProvider
|
||||
import androidx.compose.runtime.staticCompositionLocalOf
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.Shape
|
||||
import androidx.compose.ui.unit.Dp
|
||||
import androidx.compose.ui.unit.DpOffset
|
||||
import androidx.compose.ui.unit.IntOffset
|
||||
import androidx.compose.ui.unit.IntRect
|
||||
import androidx.compose.ui.unit.IntSize
|
||||
import androidx.compose.ui.unit.LayoutDirection
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.window.Dialog
|
||||
import androidx.compose.ui.window.DialogProperties
|
||||
import androidx.compose.ui.window.Popup
|
||||
@@ -41,13 +63,82 @@ internal fun TrackKeyWindow() {
|
||||
}
|
||||
}
|
||||
|
||||
internal val LocalPresentationWindow = staticCompositionLocalOf { false }
|
||||
|
||||
private val OriginPosition = object : PopupPositionProvider {
|
||||
override fun calculatePosition(anchorBounds: IntRect, windowSize: IntSize, layoutDirection: LayoutDirection, popupContentSize: IntSize) = IntOffset.Zero
|
||||
}
|
||||
|
||||
@Composable
|
||||
internal fun WindowDialog(
|
||||
onDismissRequest: () -> Unit,
|
||||
properties: DialogProperties = DialogProperties(),
|
||||
content: @Composable () -> Unit,
|
||||
) {
|
||||
if (!LocalPresentationWindow.current) {
|
||||
Dialog(onDismissRequest, properties, content)
|
||||
return
|
||||
}
|
||||
Popup(
|
||||
OriginPosition, onDismissRequest,
|
||||
PopupProperties(focusable = true, dismissOnBackPress = properties.dismissOnBackPress, dismissOnClickOutside = false, clippingEnabled = false),
|
||||
) {
|
||||
Box(
|
||||
Modifier.fillMaxSize().background(androidx.compose.ui.graphics.Color.Black.copy(alpha = 0.6f))
|
||||
.tapOutside { if (properties.dismissOnClickOutside) onDismissRequest() },
|
||||
contentAlignment = Alignment.Center,
|
||||
) { content() }
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
internal fun GlideDialog(
|
||||
onDismissRequest: () -> Unit,
|
||||
properties: DialogProperties = DialogProperties(),
|
||||
content: @Composable () -> Unit,
|
||||
) {
|
||||
Dialog(onDismissRequest, properties) { TrackKeyWindow(); FocusGlideHost(content = content) }
|
||||
WindowDialog(onDismissRequest, properties) { TrackKeyWindow(); FocusGlideHost(content = content) }
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun PresentationAlert(
|
||||
confirmButton: @Composable () -> Unit,
|
||||
modifier: Modifier,
|
||||
dismissButton: (@Composable () -> Unit)?,
|
||||
icon: (@Composable () -> Unit)?,
|
||||
title: (@Composable () -> Unit)?,
|
||||
text: (@Composable () -> Unit)?,
|
||||
shape: Shape,
|
||||
containerColor: Color,
|
||||
iconContentColor: Color,
|
||||
titleContentColor: Color,
|
||||
textContentColor: Color,
|
||||
tonalElevation: Dp,
|
||||
) {
|
||||
Surface(modifier.blockTouches().padding(24.dp).widthIn(min = 280.dp, max = 560.dp), shape, containerColor, tonalElevation = tonalElevation) {
|
||||
Column(Modifier.padding(24.dp)) {
|
||||
icon?.let {
|
||||
Box(Modifier.align(Alignment.CenterHorizontally)) { CompositionLocalProvider(LocalContentColor provides iconContentColor, content = it) }
|
||||
Spacer(Modifier.height(16.dp))
|
||||
}
|
||||
title?.let {
|
||||
ProvideTextStyle(MaterialTheme.typography.headlineSmall) {
|
||||
CompositionLocalProvider(LocalContentColor provides titleContentColor, content = it)
|
||||
}
|
||||
Spacer(Modifier.height(16.dp))
|
||||
}
|
||||
text?.let {
|
||||
ProvideTextStyle(MaterialTheme.typography.bodyMedium) {
|
||||
CompositionLocalProvider(LocalContentColor provides textContentColor, content = it)
|
||||
}
|
||||
Spacer(Modifier.height(24.dp))
|
||||
}
|
||||
Row(Modifier.align(Alignment.End), horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
dismissButton?.invoke()
|
||||
confirmButton()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
@@ -81,7 +172,13 @@ internal fun GlideAlertDialog(
|
||||
properties: DialogProperties = DialogProperties(),
|
||||
) {
|
||||
FocusGlideWindow { overlay ->
|
||||
AlertDialog(
|
||||
if (LocalPresentationWindow.current) WindowDialog(onDismissRequest, properties) {
|
||||
TrackKeyWindow()
|
||||
PresentationAlert(
|
||||
confirmButton, modifier.then(overlay), dismissButton, icon, title, text,
|
||||
shape, containerColor, iconContentColor, titleContentColor, textContentColor, tonalElevation,
|
||||
)
|
||||
} else AlertDialog(
|
||||
onDismissRequest, confirmButton, modifier.then(overlay), dismissButton, icon, title, text,
|
||||
shape, containerColor, iconContentColor, titleContentColor, textContentColor, tonalElevation, properties,
|
||||
)
|
||||
|
||||
@@ -143,7 +143,7 @@ class SecondScreenPresentation(
|
||||
val view = androidx.compose.ui.platform.ComposeView(context).apply {
|
||||
isFocusable = true
|
||||
isFocusableInTouchMode = true
|
||||
setContent { content() }
|
||||
setContent { PresentationWindows(content) }
|
||||
}
|
||||
chatView = view
|
||||
root.addView(view, 1.coerceAtMost(root.childCount), LinearLayout.LayoutParams(-1, -2))
|
||||
@@ -179,7 +179,7 @@ class SecondScreenPresentation(
|
||||
isFocusable = true
|
||||
isFocusableInTouchMode = true
|
||||
defaultFocusHighlightEnabled = false
|
||||
setContent { content() }
|
||||
setContent { PresentationWindows(content) }
|
||||
frame.addView(this, FrameLayout.LayoutParams(-1, -1))
|
||||
menuView = this
|
||||
}
|
||||
@@ -340,7 +340,7 @@ class SecondScreenPresentation(
|
||||
private fun renderDashboard() {
|
||||
val content = dashboard ?: return
|
||||
bar(SecondScreenMode.DASHBOARD)
|
||||
val view = androidx.compose.ui.platform.ComposeView(context).apply { setContent { content() } }
|
||||
val view = androidx.compose.ui.platform.ComposeView(context).apply { setContent { PresentationWindows(content) } }
|
||||
dashboardView = view
|
||||
root.addView(view, weightParams())
|
||||
}
|
||||
@@ -388,3 +388,8 @@ class SecondScreenPresentation(
|
||||
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION)
|
||||
}
|
||||
}
|
||||
|
||||
@androidx.compose.runtime.Composable
|
||||
private fun PresentationWindows(content: @androidx.compose.runtime.Composable () -> Unit) {
|
||||
androidx.compose.runtime.CompositionLocalProvider(LocalPresentationWindow provides true) { content() }
|
||||
}
|
||||
@@ -0,0 +1,185 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import android.app.Presentation
|
||||
import android.hardware.display.DisplayManager
|
||||
import android.os.Looper
|
||||
import android.view.Display
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.setViewTreeOnBackPressedDispatcherOwner
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.CompositionLocalProvider
|
||||
import androidx.compose.runtime.SideEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.platform.ComposeView
|
||||
import androidx.compose.ui.platform.LocalView
|
||||
import androidx.compose.ui.test.assert
|
||||
import androidx.compose.ui.test.assertIsDisplayed
|
||||
import androidx.compose.ui.test.click
|
||||
import androidx.compose.ui.test.hasAnyAncestor
|
||||
import androidx.compose.ui.test.isDialog
|
||||
import androidx.compose.ui.test.isPopup
|
||||
import androidx.compose.ui.test.onNodeWithText
|
||||
import androidx.compose.ui.test.percentOffset
|
||||
import androidx.compose.ui.test.performClick
|
||||
import androidx.compose.ui.test.performTouchInput
|
||||
import androidx.compose.ui.window.DialogProperties
|
||||
import androidx.lifecycle.setViewTreeLifecycleOwner
|
||||
import androidx.lifecycle.setViewTreeViewModelStoreOwner
|
||||
import androidx.savedstate.setViewTreeSavedStateRegistryOwner
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.Robolectric
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.Shadows.shadowOf
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.shadows.ShadowDisplayManager
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], qualifiers = "w640dp-h480dp-land", application = android.app.Application::class)
|
||||
class PresentationWindowTest {
|
||||
private companion object { const val FRAMES = 5 }
|
||||
|
||||
@get:Rule val compose = createRobolectricComposeRule()
|
||||
|
||||
private fun showOutsideTapPopup(content: @Composable () -> Unit) {
|
||||
compose.mainClock.autoAdvance = false
|
||||
compose.setContent { CompositionLocalProvider(LocalPresentationWindow provides true, content = content) }
|
||||
repeat(FRAMES) { compose.mainClock.advanceTimeByFrame() }
|
||||
}
|
||||
|
||||
private fun tapOutsidePopup() {
|
||||
compose.onNode(isPopup()).performTouchInput { click(percentOffset(.99f, .99f)) }
|
||||
repeat(FRAMES) { compose.mainClock.advanceTimeByFrame() }
|
||||
}
|
||||
|
||||
@Test fun alertInsidePresentationRendersDismissesAndTracksItsWindow() {
|
||||
var shown by mutableStateOf(true)
|
||||
var displayId = Display.DEFAULT_DISPLAY
|
||||
compose.setContent {
|
||||
CompositionLocalProvider(LocalPresentationWindow provides true) {
|
||||
val view = LocalView.current
|
||||
SideEffect { displayId = view.display?.displayId ?: Display.DEFAULT_DISPLAY }
|
||||
if (shown) GlideAlertDialog(
|
||||
onDismissRequest = { shown = false },
|
||||
confirmButton = { TextButton(onClick = { shown = false }) { Text("Confirm") } },
|
||||
title = { Text("Title") },
|
||||
text = { Text("Body") },
|
||||
)
|
||||
}
|
||||
}
|
||||
compose.onNodeWithText("Title").assertIsDisplayed()
|
||||
.assert(hasAnyAncestor(isPopup()))
|
||||
compose.onNodeWithText("Body").assertIsDisplayed()
|
||||
compose.onNode(isDialog()).assertDoesNotExist()
|
||||
compose.runOnIdle { assertNotNull(KeyWindows.topOn(displayId)) }
|
||||
compose.onNodeWithText("Confirm").performClick()
|
||||
compose.onNodeWithText("Title").assertDoesNotExist()
|
||||
}
|
||||
|
||||
@Test fun dialogInsidePresentationRendersItsContent() {
|
||||
var shown by mutableStateOf(true)
|
||||
compose.setContent {
|
||||
CompositionLocalProvider(LocalPresentationWindow provides true) {
|
||||
if (shown) GlideDialog(onDismissRequest = { shown = false }) {
|
||||
TextButton(onClick = { shown = false }) { Text("Close") }
|
||||
}
|
||||
}
|
||||
}
|
||||
compose.onNodeWithText("Close").assertIsDisplayed()
|
||||
.assert(hasAnyAncestor(isPopup()))
|
||||
compose.onNode(isDialog()).assertDoesNotExist()
|
||||
compose.onNodeWithText("Close").performClick()
|
||||
compose.onNodeWithText("Close").assertDoesNotExist()
|
||||
}
|
||||
|
||||
@Test fun dialogInsidePresentationDismissesOnOutsideTapByDefault() {
|
||||
var shown by mutableStateOf(true)
|
||||
showOutsideTapPopup { if (shown) GlideDialog(onDismissRequest = { shown = false }) { Text("Panel") } }
|
||||
tapOutsidePopup()
|
||||
compose.onNodeWithText("Panel").assertDoesNotExist()
|
||||
}
|
||||
|
||||
@Test fun dialogInsidePresentationHonorsDisabledOutsideDismissal() {
|
||||
var dismissals = 0
|
||||
showOutsideTapPopup {
|
||||
GlideDialog(
|
||||
onDismissRequest = { dismissals++ },
|
||||
properties = DialogProperties(dismissOnClickOutside = false),
|
||||
) { Text("Panel") }
|
||||
}
|
||||
tapOutsidePopup()
|
||||
compose.onNodeWithText("Panel").assertIsDisplayed()
|
||||
assertEquals(0, dismissals)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(sdk = [33])
|
||||
fun dialogUsesThePresentationWindowOnAndroid13() {
|
||||
val activityController = Robolectric.buildActivity(ComponentActivity::class.java).setup()
|
||||
val activity = activityController.get()
|
||||
val displays = activity.getSystemService(DisplayManager::class.java)
|
||||
val displayId = ShadowDisplayManager.addDisplay("w640dp-h480dp-land")
|
||||
val display = displays.getDisplay(displayId)
|
||||
shadowOf(display).setFlags(Display.FLAG_PRESENTATION)
|
||||
var dialogComposed = false
|
||||
val presentation = Presentation(activity, display)
|
||||
val view = ComposeView(presentation.context).apply {
|
||||
setViewTreeLifecycleOwner(activity)
|
||||
setViewTreeViewModelStoreOwner(activity)
|
||||
setViewTreeSavedStateRegistryOwner(activity)
|
||||
setViewTreeOnBackPressedDispatcherOwner(activity)
|
||||
setContent {
|
||||
CompositionLocalProvider(LocalPresentationWindow provides true) {
|
||||
DroidDeckTheme {
|
||||
GlideDialog(onDismissRequest = {}) {
|
||||
SideEffect { dialogComposed = true }
|
||||
Text("Panel")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
presentation.setContentView(view)
|
||||
try {
|
||||
presentation.show()
|
||||
shadowOf(Looper.getMainLooper()).idle()
|
||||
assertTrue(dialogComposed)
|
||||
compose.onNodeWithText("Panel").assertIsDisplayed()
|
||||
.assert(hasAnyAncestor(isPopup()))
|
||||
compose.onNode(isDialog()).assertDoesNotExist()
|
||||
} finally {
|
||||
presentation.dismiss()
|
||||
activityController.pause().stop().destroy()
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun dialogOutsidePresentationKeepsThePlatformDialog() {
|
||||
compose.setContent { GlideDialog(onDismissRequest = {}) { Text("Panel") } }
|
||||
compose.onNodeWithText("Panel").assertIsDisplayed()
|
||||
.assert(hasAnyAncestor(isDialog()))
|
||||
compose.onNode(isPopup()).assertDoesNotExist()
|
||||
}
|
||||
|
||||
@Test fun alertOutsidePresentationKeepsThePlatformDialog() {
|
||||
compose.setContent {
|
||||
GlideAlertDialog(
|
||||
onDismissRequest = {},
|
||||
confirmButton = { TextButton(onClick = {}) { Text("Confirm") } },
|
||||
title = { Text("Title") },
|
||||
text = { Text("Body") },
|
||||
)
|
||||
}
|
||||
compose.onNodeWithText("Title").assertIsDisplayed()
|
||||
.assert(hasAnyAncestor(isDialog()))
|
||||
compose.onNodeWithText("Body").assertIsDisplayed()
|
||||
compose.onNode(isPopup()).assertDoesNotExist()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user