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

Issue with focusRestorer and handleDPadKeyEvents #194

Open
GeorgiBurgazliev opened this issue Nov 7, 2024 · 0 comments
Open

Issue with focusRestorer and handleDPadKeyEvents #194

GeorgiBurgazliev opened this issue Nov 7, 2024 · 0 comments

Comments

@GeorgiBurgazliev
Copy link

GeorgiBurgazliev commented Nov 7, 2024

When you override onKeyPresser with "handleDPadKeyEvents" and you have "focusRestoter" pressing dPag up (triggering on up) results in focusing the first item. If you remove the focus restorer this issue is not reproduced.

Example code:

@Composable
fun Screen() {
 
    val focusManager = LocalFocusManager.current
 
    LazyColumn {
        item {
            repeat(10){
                MyLazyRow(focusManager)
            }
        }
    }
}
 
@Composable
fun MyLazyRow(focusManager: FocusManager) {
 
    val (lazyRow, firstItem) = remember { FocusRequester.createRefs() }
 
    LazyRow(
        horizontalArrangement = Arrangement.spacedBy(20.dp),
        modifier = Modifier
            .focusRequester(lazyRow)
            .focusRestorer {
                firstItem
            },
    ) {
        repeat(10) { index ->
            item {
                val itemModifier = if (index == 0) {
                    Modifier.focusRequester(firstItem)
                } else {
                    Modifier
                }
 
                OutlinedButton(
                    onClick = {
                        focusRequester.saveFocusedChild()
                        println("Button clicked")
                    },
                    content = {
                        Text("My Button $index")
 
                    },
                    modifier = itemModifier
                        .handleDPadKeyEvents(onUp = {
                            focusManager.moveFocus(FocusDirection.Up)
                        }),
                    border = ButtonDefaults.border(
                        border = Border(
                            border = BorderStroke(
                                width = 1.dp,
                                color = Color.Red
                            ),
                            shape = RoundedCornerShape(6.dp)
                        ),
                        focusedBorder = Border(
                            border = BorderStroke(width = 10.dp, color = Color.Green),
                            shape = RoundedCornerShape(6.dp)
                        ),
                        disabledBorder = Border(
                            BorderStroke(
                                width = 1.dp,
                                color = colors.onBackground.copy(0.4f)
                            )
                        ),
                        focusedDisabledBorder = Border(
                            BorderStroke(
                                width = 1.dp,
                                color = colors.onBackground.copy(0.4f)
                            )
                        )
                    ),
                )
            }
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant