Skip to content

Commit 6500808

Browse files
authored
Support focusable containers (#1130)
* Fix containers not being focusable * Fix tests to allow for focusable containers * Add test for non-focusable container with focusable children * Fix a typo in a test
1 parent 4034d95 commit 6500808

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/textual/screen.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,8 @@ def focus_chain(self) -> list[Widget]:
169169
else:
170170
if node.is_container and node.can_focus_children:
171171
push(iter(node.focusable_children))
172-
else:
173-
if node.can_focus:
174-
add_widget(node)
172+
if node.can_focus:
173+
add_widget(node)
175174

176175
return widgets
177176

tests/test_focus.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ class Focusable(Widget, can_focus=True):
1010
class NonFocusable(Widget, can_focus=False, can_focus_children=False):
1111
pass
1212

13+
class ChildrenFocusableOnly(Widget, can_focus=False, can_focus_children=True):
14+
pass
15+
1316

14-
async def test_focus_chain():
17+
def test_focus_chain():
1518
app = App()
1619
app._set_active()
1720
app.push_screen(Screen())
@@ -27,13 +30,14 @@ async def test_focus_chain():
2730
Focusable(Focusable(id="Paul"), id="container1"),
2831
NonFocusable(Focusable(id="Jessica"), id="container2"),
2932
Focusable(id="baz"),
33+
ChildrenFocusableOnly(Focusable(id="child")),
3034
)
3135

32-
focused = [widget.id for widget in screen.focus_chain]
33-
assert focused == ["foo", "Paul", "baz"]
36+
focus_chain = [widget.id for widget in screen.focus_chain]
37+
assert focus_chain == ["foo", "container1", "Paul", "baz", "child"]
3438

3539

36-
async def test_focus_next_and_previous():
40+
def test_focus_next_and_previous():
3741
app = App()
3842
app._set_active()
3943
app.push_screen(Screen())
@@ -46,11 +50,16 @@ async def test_focus_next_and_previous():
4650
Focusable(Focusable(id="Paul"), id="container1"),
4751
NonFocusable(Focusable(id="Jessica"), id="container2"),
4852
Focusable(id="baz"),
53+
ChildrenFocusableOnly(Focusable(id="child")),
4954
)
5055

5156
assert screen.focus_next().id == "foo"
57+
assert screen.focus_next().id == "container1"
5258
assert screen.focus_next().id == "Paul"
5359
assert screen.focus_next().id == "baz"
60+
assert screen.focus_next().id == "child"
5461

62+
assert screen.focus_previous().id == "baz"
5563
assert screen.focus_previous().id == "Paul"
64+
assert screen.focus_previous().id == "container1"
5665
assert screen.focus_previous().id == "foo"

0 commit comments

Comments
 (0)