cushy icon indicating copy to clipboard operation
cushy copied to clipboard

Support multiple default widgets

Open ecton opened this issue 1 year ago • 2 comments

From a Discord discussion, @danbulant was asking about how to handle directing the enter key to submitting a form more easily than overriding on_key on everything. I pointed to Default widgets but the limitation is that they are one-per-window.

This spawned a thought for me: what if we can support multiple default widgets. Perhaps each time focus is shifted, an "active default" widget is chosen by determining which default widget available has the shortest "tree distance" between itself and the currently focused widget. This widget can then show the default styling, and as the keyboard focus shifts around the window the default widget will shift based on the tree distance.

What is the tree distance? I think it is the sum of the distance between each widget and the closest common ancestor. This should mean that form widgets that are contained within the same layout container will be preferred since the distance between the button and each widget and the layout container will be smaller than the distance to any other widget outside of that layout container.

ecton avatar Nov 24 '24 18:11 ecton

Another idea would be to have a Form widget which would explicitly wrap the elements for submitting data, and have default button/widget send that event up (though that would require documentation that a form widget must exist as a parent somewhere - maybe window could panic if it received this event? Or window could accept those events and process them too?)

danbulant avatar Nov 24 '24 18:11 danbulant

A 'form' widget seems like a reasonable idea to me for non-modal use-cases. Any 'form' widget should probably also have a Validations too.

Currently in PR #238 we have the following modal use-cases:

  1. a model with a form, pressing enter should activate the 'default' button.
  2. nested modals, pressing ok in the modal form presents the user with an 'are you sure' prompt, pressing enter here should activate the prompt's default button too.
  3. a UI with two independent modals, each has a 'default' button, pressing enter here should activate the default button of the modal with the focus.

hydra avatar Jan 13 '25 10:01 hydra