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

The moment of setting the focus has changed (omits can_focus set in on_mount) after the 2.0.0 #5605

Closed
mzebrak opened this issue Mar 3, 2025 · 4 comments

Comments

@mzebrak
Copy link

mzebrak commented Mar 3, 2025

This works correctly in 1.0.0 but changed in 2.0.0.

Right after launch, Long widget should have focus (so, e.g. key_down binding should work and scroll). But it is not focused since 2.0.0.

CanFocusOnlyWhenScrollbars originated from: #3717

MRE:

from __future__ import annotations

from textual.app import App, ComposeResult
from textual.containers import VerticalScroll
from textual.widget import Widget
from textual.widgets import Static


class CanFocusWithScrollbarsOnly(Widget):
    """
    A Widget that can be focused only when scrollbar is active.

    Inherit from this class to make a widget focusable only when any scrollbar is active.
    """

    def on_mount(self) -> None:
        self._enable_focus_only_when_scrollbar_is_active()

    def watch_show_vertical_scrollbar(self) -> None:
        self._enable_focus_only_when_scrollbar_is_active()

    def watch_show_horizontal_scrollbar(self) -> None:
        self._enable_focus_only_when_scrollbar_is_active()

    def _enable_focus_only_when_scrollbar_is_active(self) -> None:
        self.can_focus = any(self.scrollbars_enabled)


class Long(VerticalScroll, CanFocusWithScrollbarsOnly):
    """Will be focused right after launching  in 1.0.0 but not in 2.0.0."""

    def compose(self) -> ComposeResult:
        for i in range(50):
            yield Static(f"static no. {i}")


class MyApp(App):
    def compose(self) -> ComposeResult:
        yield Long()


if __name__ == "__main__":
    MyApp().run()
@Textualize Textualize deleted a comment from github-actions bot Mar 3, 2025
@willmcgugan
Copy link
Collaborator

from __future__ import annotations

from textual.app import App, ComposeResult
from textual.containers import VerticalScroll
from textual.widget import Widget
from textual.widgets import Static


class Long(VerticalScroll):
    """Will be focused right after launching  in 1.0.0 but not in 2.0.0."""

    def allow_focus(self) -> bool:
        return any(self.scrollbars_enabled)

    def compose(self) -> ComposeResult:
        for i in range(50):
            yield Static(f"static no. {i}")


class MyApp(App):
    def compose(self) -> ComposeResult:
        yield Long()

Copy link

github-actions bot commented Mar 4, 2025

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

@TomJGooding
Copy link
Contributor

I almost raised an issue about allow_focus earlier, as I don't think it works as expected.

Here's a slightly modified version of your example. When you first start the app, this widget is still focusable despite not having any scrollbars:

from __future__ import annotations

from textual.app import App, ComposeResult
from textual.containers import VerticalScroll
from textual.widget import Widget
from textual.widgets import Button, Static


class Long(VerticalScroll):
    DEFAULT_CSS = """
    Long {
        border: tall red;
        &:focus {
            border: tall green;
        }
    }
    """

    def allow_focus(self) -> bool:
        return any(self.scrollbars_enabled)

    def compose(self) -> ComposeResult:
        for i in range(10):
            yield Static(f"static no. {i}")


class MyApp(App):
    def compose(self) -> ComposeResult:
        yield Long()
        yield Button()


if __name__ == "__main__":
    app = MyApp()
    app.run()

Gandalf-the-Grey pushed a commit to openhive-network/clive that referenced this issue Mar 5, 2025
It stopped working behaving correctly after 2.0.0:
Textualize/textual#5605

Still there is a know issue:
Textualize/textual#5609
Gandalf-the-Grey pushed a commit to openhive-network/clive that referenced this issue Mar 5, 2025
It stopped working behaving correctly after 2.0.0:
Textualize/textual#5605

Still there is a know issue:
Textualize/textual#5609
Gandalf-the-Grey pushed a commit to openhive-network/clive that referenced this issue Mar 6, 2025
It stopped working behaving correctly after 2.0.0:
Textualize/textual#5605

Still there is a know issue:
Textualize/textual#5609
Gandalf-the-Grey pushed a commit to openhive-network/clive that referenced this issue Mar 6, 2025
It stopped working behaving correctly after 2.0.0:
Textualize/textual#5605

Still there is a know issue:
Textualize/textual#5609
Gandalf-the-Grey pushed a commit to openhive-network/clive that referenced this issue Mar 6, 2025
It stopped working behaving correctly after 2.0.0:
Textualize/textual#5605

Still there is a know issue:
Textualize/textual#5609
@mzebrak
Copy link
Author

mzebrak commented Mar 7, 2025

Adding basic logging to the allow_focus method shows that it is not called when the widget is mounted. So initially Widget does have focus even though allow_focus would return False.

from __future__ import annotations

from textual.app import App, ComposeResult
from textual.containers import VerticalScroll
from textual.widget import Widget
from textual.widgets import Button, Static


class Long(VerticalScroll):
    DEFAULT_CSS = """
    Long {
        border: tall red;
        &:focus {
            border: tall green;
        }
    }
    """

    def allow_focus(self) -> bool:
        v = any(self.scrollbars_enabled)
        self.log(f"allow_focus value:: {v}")
        return v

    def compose(self) -> ComposeResult:
        for i in range(10):
            yield Static(f"static no. {i}")


class MyApp(App):
    def compose(self) -> ComposeResult:
        yield Long()
        yield Button()


if __name__ == "__main__":
    app = MyApp()
    app.run()

Gandalf-the-Grey pushed a commit to openhive-network/clive that referenced this issue Mar 7, 2025
It stopped working behaving correctly after 2.0.0:
Textualize/textual#5605

Still there is a know issue:
Textualize/textual#5609
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

3 participants