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

Add auto_focus to screens #2527

Merged
merged 4 commits into from
May 15, 2023
Merged

Add auto_focus to screens #2527

merged 4 commits into from
May 15, 2023

Conversation

rodrigogiraoserrao
Copy link
Contributor

This will close #2457.

I've added a docstring but I haven't added documentation elsewhere. Should I?

@rodrigogiraoserrao rodrigogiraoserrao marked this pull request as ready for review May 9, 2023 14:01
@@ -101,6 +101,12 @@ class Screen(Generic[ScreenResultType], Widget):
}
"""

auto_focus: str | None = "*"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's make this an instance var, which should be the default unless there is good rationale for it being a classvar.

Copy link
Contributor Author

@rodrigogiraoserrao rodrigogiraoserrao May 9, 2023

Choose a reason for hiding this comment

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

Mostly because it's a pain to set it as an instance attribute-only for people creating their own screens:

class FormScreen(Screen):
    auto_focus = "Input"

    ...

vs

class FormScreen(Screen):
    def __init__(
        self,
        name: str | None = None,
        id: str | None = None,
        classes: str | None = None,
    ) -> None:
        self.auto_focus = "Input"
        super().__init__(name=name, id=id, classes=classes)

    ...

Copy link
Collaborator

Choose a reason for hiding this comment

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

If we want it to be a classvar, suggest we make it a constant, i.e. AUTO_FOCUS.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Classvar it is.

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.

I'm actually rather surprised this didn't break any unit tests.

@rodrigogiraoserrao rodrigogiraoserrao merged commit 83618db into main May 15, 2023
@rodrigogiraoserrao rodrigogiraoserrao deleted the auto-focus branch May 15, 2023 10:04
davep added a commit to davep/textual that referenced this pull request Jun 1, 2023
See Textualize#2718. The problem is that the work done on Textualize#2527 and related PRs has
changed the starting position of focus, which means that any code example
that has key presses in them that start out by tabbing to a control will be
off by one.
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 option to auto focus the first thing on a screen.
2 participants