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

Scrollbar getting focus when not visible #3909

Closed
chrisstjohn opened this issue Dec 20, 2023 · 4 comments
Closed

Scrollbar getting focus when not visible #3909

chrisstjohn opened this issue Dec 20, 2023 · 4 comments

Comments

@chrisstjohn
Copy link

I have a simple app with an input box, vertical scroll containing markdown, and a button to exit the application.

When the markdown content isn't large enough to show the vertical scroll it still gets focus on TAB from input to button which is poor UX since you can't tell when it has focus because the vertical scroll is not visible. My app is based on the dictionary.py example which I think suffers from the same problem.

I'd like to see an update to the dictionary.py example that demonstrates selective enabling the ability to focus the vertical scroll.

@Textualize Textualize deleted a comment from github-actions bot Dec 21, 2023
@rodrigogiraoserrao
Copy link
Contributor

Hey @chrisstjohn, thanks for your issue.

When I run dictionary.py and I TAB around, I can focus the vertical scroll and I get a blue border showing me where the focus is.
Don't you get a similar visual indication in your app?
If you don't, you can use TCSS and the :focus pseudoclass to add a visual indication when your focus is in another widget.

To me, it makes sense that the VerticalScroll in dictionary.py is focusable even when there's nothing to scroll.
I feel it's similar to how you're able to focus a button with TAB, regardless of whether clicking that button does something or not.

Now, what I'm focusing is a VerticalScroll and not a Markdown widget, so maybe there's some confusion in your case and focus is actually going somewhere else (you also can't see?).
Maybe share an MRE of your app/context.

@mzebrak
Copy link

mzebrak commented Jan 3, 2024

@chrisstjohn IDK if this will help you in any way, but I'll leave a link to #3717

@willmcgugan
Copy link
Collaborator

The dictionary example does now show focus. If you want to implement a widget that is only focusable when it can be scrolled, you can use this pattern:

from textual import on
from textual.app import App, ComposeResult
from textual.containers import VerticalScroll
from textual.widgets import Button, Label

class MaybeFocus(VerticalScroll, can_focus=False):

    def watch_show_vertical_scrollbar(self) -> None:
        self.can_focus = self.show_horizontal_scrollbar or self.show_vertical_scrollbar

    def watch_show_horizontal_scrollbar(self) -> None:
        self.can_focus = self.show_horizontal_scrollbar or self.show_vertical_scrollbar

class FocusWhenScrollApp(App[None]):

    CSS = """
    Label {
        height: 5;
        background: red;
        width: 1fr;
    }
    """

    def compose(self) -> ComposeResult:
        yield Button("MORE!")
        yield MaybeFocus()

    @on(Button.Pressed)
    def more(self) -> None:
        self.query_one(MaybeFocus).mount(Label("Stuff"))

if __name__ == "__main__":
    FocusWhenScrollApp().run()

Copy link

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

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

No branches or pull requests

4 participants