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

Widget focus keybindings #546

Merged
merged 2 commits into from
May 27, 2022
Merged

Widget focus keybindings #546

merged 2 commits into from
May 27, 2022

Conversation

darrenburns
Copy link
Member

@darrenburns darrenburns commented May 27, 2022

You can create a keybinding to immediately focus a widget based on the widget ID. For example:

    def on_load(self) -> None:
        self.bind("1", "focus('widget1')")

This will focus the widget with the ID widget1 when you press 1 on your keyboard.


Here's an example video showing how we can use the keyboard to quickly shift our focus across multiple widgets:

keybind-focus.mov

This demo is available in sandbox/focus_keybindings.py.


This PR also fixes an issue with descendant_focus, although the approach is quite inefficient.

I would like to write an integration test for this, but it depends on some of the in-flux stuff inside #543, so I will wait til that is merged before doing so.

Closes #504

@darrenburns darrenburns changed the title Support binding widget focus to keypress Widget focus keybindings May 27, 2022
sandbox/focus_keybindings.py Show resolved Hide resolved
src/textual/app.py Outdated Show resolved Hide resolved
src/textual/widget.py Show resolved Hide resolved
@darrenburns darrenburns marked this pull request as ready for review May 27, 2022 14:48
Copy link
Collaborator

@willmcgugan willmcgugan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I'll leave that suggested change up to you. Merge when you're happy.

src/textual/app.py Outdated Show resolved Hide resolved
src/textual/widget.py Show resolved Hide resolved
Co-authored-by: Olivier Philippon <[email protected]>
@darrenburns darrenburns merged commit eb3b21f into css May 27, 2022
@darrenburns darrenburns deleted the focus-bindings branch May 27, 2022 15:32
willmcgugan pushed a commit that referenced this pull request May 29, 2022
* Support binding widget focus to keypress, fix focus-within

* Update src/textual/app.py

Co-authored-by: Olivier Philippon <[email protected]>

Co-authored-by: Olivier Philippon <[email protected]>
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

Successfully merging this pull request may close these issues.

Add built-in action handler for switching focus (e.g. self.bind("f1", "focus(widget_id)"))
3 participants