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

allow_focus doesn't work as expected #5609

Open
TomJGooding opened this issue Mar 4, 2025 · 3 comments
Open

allow_focus doesn't work as expected #5609

TomJGooding opened this issue Mar 4, 2025 · 3 comments

Comments

@TomJGooding
Copy link
Contributor

Initially I wasn't sure if I'd just misunderstood the allow_focus method, but following on from #5605 it sounds like this might not be working as expected.

I've created this as a separate issue as its somewhat tangential. but feel free to close this as a duplicate if you'd prefer to just re-open the original issue.

Here's another minimal example:

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


class Focusable(Widget, can_focus=False):
    def allow_focus(self) -> bool:
        return True

    def compose(self) -> ComposeResult:
        for _ in range(100):
            yield Static("Focusable")


class NonFocusable(VerticalScroll, can_focus=True):
    def allow_focus(self) -> bool:
        return False

    def compose(self) -> ComposeResult:
        for _ in range(100):
            yield Static("NonFocusable")


class ExampleApp(App):
    CSS = """
    Focusable, NonFocusable {
        height: 1fr;
        border: tall red;
        &:focus {
            border: tall green;
        }
    }
    """

    def compose(self) -> ComposeResult:
        yield Focusable()
        yield NonFocusable()


if __name__ == "__main__":
    app = ExampleApp()
    app.run()
Copy link

github-actions bot commented Mar 4, 2025

We found the following entries in the FAQ which you may find helpful:

Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

This is an automated reply, generated by FAQtory

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

mzebrak commented Mar 7, 2025

It looks like in this MRE, the can_focus takes precedence over allow_focus which may be the intended behavior, but also may not be, IDK.

But there is also another problem with allow_focus as you mentioned? In the MRE you mentioned in the #5605 (comment) it doesn't look like it shows the same thing; there is no can_focus. Or am I missing something? This one is more about "allow_focus is not called when widget is mounted for the first time"


Edit

I think I get the point - you also wanted to show that allow_focus is not called when the widget is mounted. But actually it is interesting which one can_focus or allow_focus should take precedence in that example.

@TomJGooding
Copy link
Contributor Author

Right, this is just another example. Remember in the original example that VerticalScroll will have inherited can_focus=True.

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

2 participants