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

Cannot return focus to ListView from Select #5416

Closed
ndjhartman opened this issue Dec 20, 2024 · 4 comments · Fixed by #5420
Closed

Cannot return focus to ListView from Select #5416

ndjhartman opened this issue Dec 20, 2024 · 4 comments · Fixed by #5420

Comments

@ndjhartman
Copy link

ndjhartman commented Dec 20, 2024

Have you checked closed issues? https://github.com/Textualize/textual/issues?q=is%3Aissue+is%3Aclosed
Yes.

Have you checked against the most recent version of Textual? https://pypi.org/search/?q=textual
Yes.

Issue

Please give a brief but clear explanation of the issue. If you can, include a complete working example that demonstrates the bug. Check it can run without modifications.

After focusing on a Select, I cannot programmatically return focus to a ListView and/or ListItem.

from textual.app import App, ComposeResult
from textual.containers import HorizontalGroup, Container
from textual.widgets import ListView, ListItem, Label, Select

opts = [ "foo", "bar", "zoo" ]

class MyListItem(ListItem):

	def __init__(self, opts: list[str]) -> None:
		self.opts = opts
		self.lab = Label("Hello!")
		self.sel = Select(options=[ (opt, opt) for opt in self.opts ])
		super().__init__()

	def compose(self):
		with HorizontalGroup():
			yield self.lab
			yield self.sel

	def on_select_changed(self, event: Select.Changed):
	        self.app.query_one(MyListView).focus()
                # self.focus()
	
class MyListView(ListView):

	def compose(self):
		yield MyListItem(opts)
		yield MyListItem(opts)
		yield MyListItem(opts)

	def on_list_view_selected(self, event: ListView.Selected):
		event.item.sel.focus()
		event.item.sel.expanded = True

class TUI(App):

	def compose(self):
		with Container():
			yield MyListView()

TUI().run()

What happens:

  1. Hit enter
  2. Focus transfers to Select
  3. Update the Item
  4. Focus is not returned to the ListView or ListItem
  5. Hitting up/down opens the Select rather than navigating the ListView

Expected:

  1. Focus is returned to the ListView or ListItem.
  2. Hitting up/down iterates over the ListItems
Copy link

Thank you for your issue. Give us a little time to review it.

PS. You might want to check the FAQ if you haven't done so already.

This is an automated reply, generated by FAQtory

@ndjhartman ndjhartman changed the title Cannot return focus to ListItem / ListView Cannot return focus to ListView Dec 20, 2024
@ndjhartman ndjhartman changed the title Cannot return focus to ListView Cannot return focus to ListView from Select Dec 20, 2024
@willmcgugan
Copy link
Collaborator

The Select will switch focus from the drop down back to the current value display, after the Change message is sent. So it undoes your change of focus.

You could work around this, by focusing with the following:

self.call_after_refresh(self.app.query_one(MyListView).focus)

Copy link

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

@ndjhartman
Copy link
Author

Thanks!

For anyone else encountering this, I had worked around this by setting

self.sel.can_focus = False

Setting this makes the Select work as intended.

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 a pull request may close this issue.

2 participants