diff --git a/app/src/main/kotlin/nl/ndat/tvlauncher/ui/screen/launcher/LauncherScreen.kt b/app/src/main/kotlin/nl/ndat/tvlauncher/ui/screen/launcher/LauncherScreen.kt index 08e85df..05d48e7 100644 --- a/app/src/main/kotlin/nl/ndat/tvlauncher/ui/screen/launcher/LauncherScreen.kt +++ b/app/src/main/kotlin/nl/ndat/tvlauncher/ui/screen/launcher/LauncherScreen.kt @@ -1,53 +1,61 @@ package nl.ndat.tvlauncher.ui.screen.launcher import androidx.compose.animation.AnimatedContent -import androidx.compose.foundation.focusGroup import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.remember +import androidx.compose.ui.ExperimentalComposeUiApi import androidx.compose.ui.Modifier import androidx.compose.ui.focus.FocusRequester +import androidx.compose.ui.focus.focusProperties import androidx.compose.ui.focus.focusRequester +import androidx.compose.ui.focus.focusRestorer import androidx.compose.ui.unit.dp import nl.ndat.tvlauncher.ui.tab.apps.AppsTab import nl.ndat.tvlauncher.ui.tab.home.HomeTab import nl.ndat.tvlauncher.ui.toolbar.Toolbar import org.koin.androidx.compose.koinViewModel +@OptIn(ExperimentalComposeUiApi::class) @Composable fun LauncherScreen() { val viewModel = koinViewModel() val tabIndex by viewModel.tabIndex.collectAsState() - val contentFocusRequester = remember { FocusRequester() } - Column( modifier = Modifier .fillMaxSize() - // TODO This focus restorer somehow disallows focus going to the toolbar - // .focusRestorer { contentFocusRequester } ) { + val (first, second) = remember { FocusRequester.createRefs() } + + LaunchedEffect(Unit) { + first.requestFocus() + } + Toolbar( modifier = Modifier .padding( vertical = 27.dp, horizontal = 48.dp, ) + .focusRequester(second) + .focusProperties { next = first } ) AnimatedContent( targetState = tabIndex, modifier = Modifier - .focusGroup() - .focusRequester(contentFocusRequester), + .focusRequester(first) + .focusProperties { next = second }, label = "content" ) { tabIndex -> - if (tabIndex == 0) HomeTab() - else if (tabIndex == 1) AppsTab() + if (tabIndex == 0) HomeTab(modifier = Modifier.focusRestorer()) + else if (tabIndex == 1) AppsTab(modifier = Modifier.focusRestorer()) } } } diff --git a/app/src/main/kotlin/nl/ndat/tvlauncher/ui/toolbar/Toolbar.kt b/app/src/main/kotlin/nl/ndat/tvlauncher/ui/toolbar/Toolbar.kt index 3b9fb06..abe87f9 100644 --- a/app/src/main/kotlin/nl/ndat/tvlauncher/ui/toolbar/Toolbar.kt +++ b/app/src/main/kotlin/nl/ndat/tvlauncher/ui/toolbar/Toolbar.kt @@ -1,14 +1,16 @@ package nl.ndat.tvlauncher.ui.toolbar -import androidx.compose.foundation.focusGroup import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.ExperimentalComposeUiApi import androidx.compose.ui.Modifier +import androidx.compose.ui.focus.FocusRequester +import androidx.compose.ui.focus.focusRequester import androidx.compose.ui.focus.focusRestorer import androidx.compose.ui.unit.dp @@ -17,16 +19,19 @@ import androidx.compose.ui.unit.dp fun Toolbar( modifier: Modifier = Modifier, ) { + val focusRequester = remember { FocusRequester() } Row( modifier = Modifier .fillMaxWidth() - .focusGroup() - .focusRestorer() + .focusRestorer { focusRequester } .then(modifier), horizontalArrangement = Arrangement.spacedBy(10.dp), verticalAlignment = Alignment.CenterVertically, ) { - ToolbarTabs() + ToolbarTabs( + modifier = Modifier + .focusRequester(focusRequester) + ) Spacer(modifier = Modifier.weight(1f)) ToolbarInputsButton() ToolbarSettingsButton() diff --git a/app/src/main/kotlin/nl/ndat/tvlauncher/ui/toolbar/ToolbarTabs.kt b/app/src/main/kotlin/nl/ndat/tvlauncher/ui/toolbar/ToolbarTabs.kt index b931af0..6ecf979 100644 --- a/app/src/main/kotlin/nl/ndat/tvlauncher/ui/toolbar/ToolbarTabs.kt +++ b/app/src/main/kotlin/nl/ndat/tvlauncher/ui/toolbar/ToolbarTabs.kt @@ -19,7 +19,7 @@ import org.koin.androidx.compose.koinViewModel @OptIn(ExperimentalComposeUiApi::class) @Composable -fun ToolbarTabs() { +fun ToolbarTabs(modifier: Modifier) { val viewModel = koinViewModel() val selectedTabIndex by viewModel.tabIndex.collectAsState() @@ -30,8 +30,7 @@ fun ToolbarTabs() { TabRow( selectedTabIndex = selectedTabIndex, - modifier = Modifier - .focusRestorer(), + modifier = modifier.focusRestorer(), ) { tabs.forEachIndexed { tabIndex, name -> key(tabIndex) {