Focus default control within window after opening

This commit is contained in:
Isaiah Odhner 2023-04-20 12:12:36 -04:00
parent ec4d044f8a
commit 8a51bd325c

View File

@ -52,6 +52,7 @@ class Window(Container):
self.content = Container(classes="window_content")
# must be after title_bar is defined
self.title = title
self.can_focus = True
def on_mount(self) -> None:
"""Called when the widget is mounted."""
@ -61,6 +62,15 @@ class Window(Container):
# (I peaked into mouse over handling and it calls update_styles.)
# This can still briefly show the incorrect layout, since it relies on a timer.
self.set_timer(0.01, lambda: self.app.update_styles(self))
# Set focus. (In the future, some windows will not want default focus...)
self.set_timer(0.01, lambda: self.focus())
def on_focus(self, event: events.Focus) -> None:
"""Called when the window is focused."""
# TODO: focus last focused widget if re-focusing
controls = self.content.query(".submit, Input, Button")
if controls:
controls[0].focus()
# def compose(self) -> ComposeResult:
# """Add our widgets."""
@ -157,6 +167,9 @@ class DialogWindow(Window):
if event.button in self.content.query("Button").nodes:
self.handle_button(event.button)
# def on_input_submitted(self, event: Input.Submitted) -> None:
# """Called when a the enter key is pressed in an Input."""
class CharacterSelectorDialog(DialogWindow):
"""A dialog window that lets the user select a character."""