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

Snippets for focus management #409

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions compose/snippets/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
android:exported="false"/>

<activity android:name="com.example.compose.snippets.touchinput.gestures.NestedScrollInterop$MainActivity" />
<activity android:name=".touchinput.keyboardinput.KeyboardShortcutsHelperRequestActivity" />
<activity android:name=".touchinput.keyboardinput.ShortcutCustomizableKeyboardShortcutsHelperActivity" />


<!-- [START android_compose_glance_declare] -->
<receiver android:name=".glance.MyReceiver"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import com.example.compose.snippets.landing.LandingScreen
import com.example.compose.snippets.layouts.PagerExamples
import com.example.compose.snippets.navigation.Destination
import com.example.compose.snippets.navigation.TopComponentsDestination
import com.example.compose.snippets.touchinput.focus.FocusExample
import com.example.compose.snippets.ui.theme.SnippetsTheme

class SnippetsActivity : ComponentActivity() {
Expand Down Expand Up @@ -92,6 +93,7 @@ class SnippetsActivity : ComponentActivity() {
Destination.ShapesExamples -> ApplyPolygonAsClipImage()
Destination.SharedElementExamples -> PlaceholderSizeAnimated_Demo()
Destination.PagerExamples -> PagerExamples()
Destination.FocusExample -> FocusExample()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ enum class Destination(val route: String, val title: String) {
ScreenshotExample("screenshotExample", "Screenshot Examples"),
ShapesExamples("shapesExamples", "Shapes Examples"),
SharedElementExamples("sharedElement", "Shared elements"),
PagerExamples("pagerExamples", "Pager examples")
PagerExamples("pagerExamples", "Pager examples"),
FocusExample("focusExample", "Keyboard Focus"),
}

// Enum class for compose components navigation screen.
Expand All @@ -49,5 +50,5 @@ enum class TopComponentsDestination(val route: String, val title: String) {
MenusExample("menusExamples", "Menus"),
TooltipExamples("tooltipExamples", "Tooltips"),
NavigationDrawerExamples("navigationDrawerExamples", "Navigation drawer"),
SegmentedButtonExamples("segmentedButtonExamples", "Segmented button")
SegmentedButtonExamples("segmentedButtonExamples", "Segmented button"),
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.compose.snippets.touchinput.focus

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.ListItem
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
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.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController

enum class FocusExample(
val route: String,
val title: String
) {
Home("home", "Home"),
FocusTraversalOrder("focusTraversal", "Focus Traversal Order"),
InitialFocus("initialFocus", "Initial Focus"),
InitialFocusWithScrollableContainer(
"initialFocusWithScrollableContainer",
"Initial Focus with Scrollable Container"
),
InitialFocusEnablingContentReload(
"initialFocusEnablingContentReload",
"Initial Focus Enabling Content Reload"
),
FocusRestoration("focusRestoration", "Focus Restoration"),
FocusInListDetailLayout("focusInListDetailLayout", "Focus In List-Detail Layout"),
}

@Composable
fun FocusExample(
navController: NavHostController = rememberNavController()
) {
NavHost(navController, startDestination = FocusExample.Home.route) {
composable(FocusExample.Home.route) {
val entries = remember {
listOf(
FocusExample.FocusTraversalOrder,
FocusExample.InitialFocus,
FocusExample.InitialFocusWithScrollableContainer,
FocusExample.InitialFocusEnablingContentReload,
FocusExample.FocusRestoration,
FocusExample.FocusInListDetailLayout,
)
}
FocusExampleScreen(entries) {
navController.navigate(it.route)
}
}
composable(FocusExample.FocusTraversalOrder.route) {
FocusTraversalScreen(modifier = Modifier.padding(16.dp))
}
composable(FocusExample.InitialFocus.route) {
InitialFocusScreen()
}
composable(FocusExample.InitialFocusWithScrollableContainer.route) {
InitialFocusWithScrollableContainerScreen()
}
composable(FocusExample.InitialFocusEnablingContentReload.route) {
InitialFocusWithContentReloadScreen()
}
composable(FocusExample.FocusRestoration.route) {
FocusRestorationScreen()
}
composable(FocusExample.FocusInListDetailLayout.route) {
FocusRestorationInListDetailScreen()
}
}
}

@OptIn(ExperimentalComposeUiApi::class)
@Composable
private fun FocusExampleScreen(
examples: List<FocusExample>,
navigateToDetails: (FocusExample) -> Unit,
) {
val focusRequester = remember { FocusRequester() }

Box(contentAlignment = Alignment.TopCenter) {
ExampleList(
examples = examples,
showDetails = {
// Save the focused child before navigating to the detail pane
focusRequester.saveFocusedChild()
navigateToDetails(it)
},
modifier = Modifier
.widthIn(max = 600.dp)
.focusRequester(focusRequester)
)
}

LaunchedEffect(Unit) {
focusRequester.requestFocus()
}
}

@OptIn(ExperimentalComposeUiApi::class)
@Composable
private fun ExampleList(
examples: List<FocusExample>,
showDetails: (FocusExample) -> Unit = {},
modifier: Modifier = Modifier,

) {
val context = LocalContext.current
// [START android_compose_touchinput_focus_restoration]
LazyColumn(
modifier = modifier.focusRestorer()
) {
items(examples) {
ListItem(
headlineContent = { Text(it.title) },
modifier = Modifier.clickable {
showDetails(it)
}
)
}
}
// [END android_compose_touchinput_focus_restoration]
}
Loading