From 58891739e291a4241dd590d9896644d657a2811c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Muller?= Date: Wed, 13 Dec 2023 10:36:56 +0100 Subject: [PATCH] Improve back press on search field --- .../pillarbox/demo/tv/ui/TVDemoTopBar.kt | 6 +++--- .../demo/tv/ui/integrationLayer/SearchView.kt | 20 ++++++++++++++++++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/pillarbox-demo-tv/src/main/java/ch/srgssr/pillarbox/demo/tv/ui/TVDemoTopBar.kt b/pillarbox-demo-tv/src/main/java/ch/srgssr/pillarbox/demo/tv/ui/TVDemoTopBar.kt index 75115cfb2..28368965f 100644 --- a/pillarbox-demo-tv/src/main/java/ch/srgssr/pillarbox/demo/tv/ui/TVDemoTopBar.kt +++ b/pillarbox-demo-tv/src/main/java/ch/srgssr/pillarbox/demo/tv/ui/TVDemoTopBar.kt @@ -51,11 +51,11 @@ fun TVDemoTopBar( ) { val focusManager = LocalFocusManager.current val focusedTabIndex by rememberSaveable(currentNavDestination) { + val destinationHierarchy = currentNavDestination?.hierarchy.orEmpty() + mutableIntStateOf( destinations.indexOfFirst { dest -> - currentNavDestination?.hierarchy?.any { - it.route == dest.route - } == true + destinationHierarchy.any { it.route == dest.route } } ) } diff --git a/pillarbox-demo-tv/src/main/java/ch/srgssr/pillarbox/demo/tv/ui/integrationLayer/SearchView.kt b/pillarbox-demo-tv/src/main/java/ch/srgssr/pillarbox/demo/tv/ui/integrationLayer/SearchView.kt index e989cc86b..867125645 100644 --- a/pillarbox-demo-tv/src/main/java/ch/srgssr/pillarbox/demo/tv/ui/integrationLayer/SearchView.kt +++ b/pillarbox-demo-tv/src/main/java/ch/srgssr/pillarbox/demo/tv/ui/integrationLayer/SearchView.kt @@ -23,10 +23,17 @@ import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.focus.FocusDirection import androidx.compose.ui.graphics.Brush import androidx.compose.ui.graphics.graphicsLayer import androidx.compose.ui.graphics.vector.ImageVector +import androidx.compose.ui.input.key.Key +import androidx.compose.ui.input.key.KeyEventType +import androidx.compose.ui.input.key.key +import androidx.compose.ui.input.key.onPreviewKeyEvent +import androidx.compose.ui.input.key.type import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.platform.LocalFocusManager import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.input.ImeAction import androidx.compose.ui.tooling.preview.Preview @@ -115,13 +122,24 @@ private fun SearchRow( onQueryChange: (query: String) -> Unit, onBuChange: (bu: Bu) -> Unit ) { + val focusManager = LocalFocusManager.current + Column( modifier = modifier, verticalArrangement = Arrangement.spacedBy(MaterialTheme.paddings.baseline) ) { SearchInput( query = query, - modifier = Modifier.fillMaxWidth(), + modifier = Modifier + .fillMaxWidth() + .onPreviewKeyEvent { + if (it.key == Key.Back && it.type == KeyEventType.KeyDown) { + focusManager.moveFocus(FocusDirection.Up) + true + } else { + false + } + }, onQueryChange = onQueryChange )