mkpaz / atlantafx

Modern JavaFX CSS theme collection with additional controls.
https://mkpaz.github.io/atlantafx
MIT License
791 stars 64 forks source link

ModalPane allows focus to escape under it #63

Open iajn opened 1 year ago

iajn commented 1 year ago

Noticed while playing around in the sampler app, when a ModalPane is visible its possible to shift the focus using tab somewhere under it and then activate buttons using spacebar. I'd imagine focus should shift only inside the dialog. Not sure if this can be limited with some existing property?

In the sampler app, its possible to lock the page UI by opening the persistent ModalPane, shifting the focus to some other button and opening a different dialog that doesn't have a close button. Update: noticed that reopening the page is still possible and it fixes the issue, so I guess its not entirely locked.

mkpaz commented 1 year ago

Yes, the focus should be cleared somehow before moving the front layer to the back. The only issue is that JavaFX does not provide an API for this.

mkpaz commented 3 weeks ago

I dug a little deeper into this issue and don't see anything that can be done without platform support. When one press TAB, the control that has the focus (e.g. TextField) consumes the KeyEvent so that it doesn't bubble up to its parent (the ModalPane) and calls a TraverseEngine. The algorithm passes the focus to the nearest element in the scene graph, based on distance. Like almost everything in JavaFX, it's a private API, so no tweaks allowed.

Workaround: on the show event set focusTraversable to false for every node in the current scene except the ModalPane and its children. Set it back to true on hide.

It's quite messy because we don't know the depth of the scene graph beforehand, so I wouldn't implement this in the control itself.