Skip to content

Commit

Permalink
Change default focus behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix authored and nielsvanvelzen committed Oct 9, 2024
1 parent 602b3a5 commit 01bdd2f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -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<LauncherScreenViewModel>()
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())
}
}
}
13 changes: 9 additions & 4 deletions app/src/main/kotlin/nl/ndat/tvlauncher/ui/toolbar/Toolbar.kt
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import org.koin.androidx.compose.koinViewModel

@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun ToolbarTabs() {
fun ToolbarTabs(modifier: Modifier) {
val viewModel = koinViewModel<LauncherScreenViewModel>()
val selectedTabIndex by viewModel.tabIndex.collectAsState()

Expand All @@ -30,8 +30,7 @@ fun ToolbarTabs() {

TabRow(
selectedTabIndex = selectedTabIndex,
modifier = Modifier
.focusRestorer(),
modifier = modifier.focusRestorer(),
) {
tabs.forEachIndexed { tabIndex, name ->
key(tabIndex) {
Expand Down

0 comments on commit 01bdd2f

Please sign in to comment.