Skip to content

Fix allow_focus method #5737

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

Merged
merged 4 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Fixed footer / key panel not updating when keymaps are applied https://github.com/Textualize/textual/pull/5724
- Fixed alignment not being applied when there are min and max limits on dimensions https://github.com/Textualize/textual/pull/5732
- Fixed issues with OptionList scrollbar not updating https://github.com/Textualize/textual/pull/5736
- Fixed allow_focus method not overriding `can_focus()` https://github.com/Textualize/textual/pull/5737

### Changed

Expand Down
2 changes: 1 addition & 1 deletion examples/theme_sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ def action_widget_search(self) -> None:
widget.__class__.__name__,
(
partial(self.set_focus, widget)
if widget.can_focus
if widget.allow_focus()
else lambda: None
),
f"Focus on {widget.__class__.__name__}",
Expand Down
4 changes: 2 additions & 2 deletions src/textual/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ class Widget(DOMNode):
"hover": lambda widget: widget.mouse_hover,
"focus": lambda widget: widget.has_focus,
"blur": lambda widget: not widget.has_focus,
"can-focus": lambda widget: widget.can_focus,
"can-focus": lambda widget: widget.allow_focus(),
"disabled": lambda widget: widget.is_disabled,
"enabled": lambda widget: not widget.is_disabled,
"dark": lambda widget: widget.app.current_theme.dark,
Expand Down Expand Up @@ -2111,7 +2111,7 @@ def focusable(self) -> bool:
"""Can this widget currently be focused?"""
return (
not self.loading
and self.can_focus
and self.allow_focus()
and self.visible
and not self._self_or_ancestors_disabled
)
Expand Down
152 changes: 152 additions & 0 deletions tests/snapshot_tests/__snapshots__/test_snapshots/test_allow_focus.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions tests/snapshot_tests/test_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -3894,6 +3894,7 @@ def on_mount(self) -> None:

assert snap_compare(ToastApp())


def test_option_list_size_when_options_removed(snap_compare):
"""Regression test for https://github.com/Textualize/textual/issues/5728

Expand Down Expand Up @@ -3935,6 +3936,7 @@ def action_clear_options(self) -> None:

assert snap_compare(OptionListApp(), press=["x"])


def test_alignment_with_auto_and_min_height(snap_compare):
"""Regression test for https://github.com/Textualize/textual/issues/5608
You should see a blue label that is centered both horizontally and vertically
Expand Down Expand Up @@ -3962,3 +3964,41 @@ def compose(self) -> ComposeResult:
yield Label("centered")

assert snap_compare(AlignmentApp())


def test_allow_focus(snap_compare):
"""Regression test for https://github.com/Textualize/textual/issues/5609

You should see two placeholders split vertically.
The top should have the label "FOCUSABLE", and have a heavy red border.
The bottom should have the label "NON FOCUSABLE" and have the default border.
"""

class Focusable(Placeholder, can_focus=False):
"""Override can_focus from False to True"""

def allow_focus(self) -> bool:
return True

class NonFocusable(Placeholder, can_focus=True):
"""Override can_focus from True to False"""

def allow_focus(self) -> bool:
return False

class FocusApp(App):
CSS = """
Placeholder {
height: 1fr;
}
*:can-focus {
border: heavy red;
}

"""

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

assert snap_compare(FocusApp())
43 changes: 43 additions & 0 deletions tests/test_focus.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from textual.containers import Container, ScrollableContainer
from textual.widget import Widget
from textual.widgets import Button, Label
from textual.widgets._placeholder import Placeholder


class Focusable(Widget, can_focus=True):
Expand Down Expand Up @@ -464,3 +465,45 @@ def compose(self) -> ComposeResult:
await pilot.click("#egg")
# Confirm nothing focused
assert app.screen.focused is None


async def test_allow_focus_override():
"""Test that allow_focus() method override can_focus."""

class Focusable(Placeholder, can_focus=False):
"""Override can_focus from False to True"""

def allow_focus(self) -> bool:
return True

class NonFocusable(Placeholder, can_focus=True):
"""Override can_focus from True to False"""

def allow_focus(self) -> bool:
return False

class FocusApp(App):
CSS = """
Placeholder {
height: 1fr;
}
*:can-focus {
border: heavy red;
}

"""

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

app = FocusApp()
async with app.run_test():
# Default should be focused
assert isinstance(app.focused, Focusable)
# Attempt to focus non focusable
app.query(NonFocusable).focus()
# Unable to focus NonFocusable
assert isinstance(app.focused, Focusable)
# Check focus chain
assert app.screen.focus_chain == [app.query_one(Focusable)]
Loading