Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Focus "Home" tab by default #378

Merged
merged 3 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
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
Expand All @@ -23,14 +23,15 @@ 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 contentFocusRequester = remember { FocusRequester() }
LaunchedEffect(contentFocusRequester) {
contentFocusRequester.requestFocus()
}

Toolbar(
modifier = Modifier
.padding(
Expand All @@ -42,7 +43,6 @@ fun LauncherScreen() {
AnimatedContent(
targetState = tabIndex,
modifier = Modifier
.focusGroup()
.focusRequester(contentFocusRequester),
label = "content"
) { tabIndex ->
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/kotlin/nl/ndat/tvlauncher/ui/tab/apps/AppsTab.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import androidx.compose.foundation.lazy.grid.items
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.focusRestorer
import androidx.compose.ui.unit.dp
import nl.ndat.tvlauncher.ui.component.card.AppCard
import org.koin.androidx.compose.koinViewModel

@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun AppsTab(
modifier: Modifier = Modifier
Expand All @@ -31,6 +34,7 @@ fun AppsTab(
horizontalArrangement = Arrangement.spacedBy(14.dp),
columns = GridCells.Adaptive(90.dp * (16f / 9f)),
modifier = modifier
.focusRestorer()
.fillMaxSize()
) {
items(
Expand Down
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