https://kotlinlang.org logo
#compose
Title
# compose
z

Zoltan Demant

10/25/2023, 10:05 AM
Is there a way to prevent child composables from being focused, through the parent?
My child composable has
Modifier.focusRequester(x)
and
requestFocus()
is called when its rendered (in a
LaunchedEffect
). As expected.. it gets the focus. Now my parent composable overlays the child composable, but the keyboard still shows up. Ideally Id like to have something akin to how I block touch events.
Copy code
private fun Modifier.disableInteractions(): Modifier {
    return pointerInput(Unit) {
        awaitPointerEventScope {
            while (true) {
                awaitPointerEvent(Initial).run {
                    changes.forEach(PointerInputChange::consume)
                }
            }
        }
    }
}
Ive tried overriding
LocalFocusManager
with a noop one, and
LocalSoftwareKeyboardController
with null; but the keyboard still shows up. I suspect Ill need to create a way to block the entire
Modifier.focusRequester(x)
part as well. Is there an easier way to go about it?
Ultimately ended up creating a
val LocalFocusEnabled: CompositionLocal<Boolean>
that I check before requesting focus. Would I have loved a better way? Yes. Does it work? Yes. If anyone knows a better approach, please enlighten me 😃
z

Zach Klippenstein (he/him) [MOD]

10/25/2023, 5:11 PM
I think this should do it:
Copy code
Modifier.focusProperties { canFocus = false }
z

Zoltan Demant

10/25/2023, 5:20 PM
It works for the focus itself but the keyboard still shows up as a result of the disabled focus, maybe bug?
z

Zach Klippenstein (he/him) [MOD]

10/25/2023, 5:22 PM
So you have a text field under this modifier, which doesn’t get focus, but it still shows the keyboard?
BasicTextField2 or the old one?
z

Zoltan Demant

10/25/2023, 5:24 PM
Yes, old one
z

Zach Klippenstein (he/him) [MOD]

10/25/2023, 5:24 PM
Can you file a bug with a minimal reproducer?
If the field is showing the keyboard while not focused, that’s definitely a bug
Oh you know what it might be https://issuetracker.google.com/301477279
But still a text field bug, so please file
z

Zoltan Demant

10/25/2023, 5:26 PM
Will du tomorrow 💪🏽
gratitude thank you 1
It was a bit more complicated to repro than I thought, but here it is 😃 Apparently linked to input fields in lazy layouts, I tested
BasicTextField2
as well - but same exact behavior there too!
gratitude thank you 1
z

Zach Klippenstein (he/him) [MOD]

10/26/2023, 5:04 PM
I suspect this has nothing to do with text but a bug in the focus system, or modifiers, so i’m not surprised
🥲 1