compose-multiplatform icon indicating copy to clipboard operation
compose-multiplatform copied to clipboard

Focus the first item at the start

Open igordmn opened this issue 1 year ago • 6 comments

When we:

  • start a window, a dialog or popup, focus the first.
  • call focusManager.clearFocus we should focus the first focusable node, otherwise remove the focus completely (the root node focusState = Inactive). In Compose 1.6.10 we always focus the root node on non-Android platforms.

A relevant change in Jetpack Compose was in https://android-review.googlesource.com/c/platform/frameworks/support/+/2813125

igordmn avatar Jun 03 '24 09:06 igordmn

Welcome change, but it is a breaking one...

m-sasha avatar Jun 03 '24 09:06 m-sasha

Welcome change, but it is a breaking one...

This happened in Jetpack Compose 1.7 with TextField as well (checked a screen with one TextField on 1.6 and 1.7)

igordmn avatar Jun 03 '24 09:06 igordmn

After https://github.com/JetBrains/compose-multiplatform-core/pull/1352, it already works for ComposePanel

igordmn avatar Jun 03 '24 12:06 igordmn

Right, but there one could think of it as making ComposePanel behave as a proper Swing widget, where initial selection is the norm.

m-sasha avatar Jun 03 '24 13:06 m-sasha

This happened in Jetpack Compose 1.7 with TextField as well

Seems like it happens only when we use physical keyboard. Probably it wasn't intentional

igordmn avatar Jun 07 '24 08:06 igordmn

+1 on this, just ran into this and was about to report as a bug.

Discovered when onPreviewKeyEvent wasn't initially working within my window.

iamcalledrob avatar Jun 17 '24 10:06 iamcalledrob

I just ran into this again, and it's become somewhat high priority (for me) to find a fix or workaround.

It appears, in my testing, that it's impossible to receive keyboard events reliably in Compose Desktop, because if FocusManager.clearFocus is ever called, no further key events will ever be received by any composable.

I might be missing something, but if true, this is a huge issue. I just want Esc to close my window, but can't implement it reliably if clearFocus is called anywhere in my codebase.

iamcalledrob avatar Jul 04 '24 14:07 iamcalledrob

Your issue is solvable using https://github.com/JetBrains/compose-multiplatform/issues/4764#issue-2280501620 (answered in Slack)

This enhancement won't solve it, as if there is no focusable elements, there will be no focus.

igordmn avatar Jul 04 '24 15:07 igordmn

Agreed.

The challenge with hooking in to this at the Window level is implementing hotkeys adds tight coupling between window code and the screen-level code, which I try and keep separate.

It's also arguably an unexpected behaviour, which means it'll be a source of bugs. I know it was for me.

This is mentioned in the second bullet point in #4764, but just echoing here to give it a +1

iamcalledrob avatar Jul 14 '24 09:07 iamcalledrob

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.

okushnikov avatar Aug 23 '24 13:08 okushnikov