Check that clicking a non-focusable widget will focus any (focusable) ancestors.
()
| 429 | |
| 430 | |
| 431 | async def test_get_focusable_widget_at() -> None: |
| 432 | """Check that clicking a non-focusable widget will focus any (focusable) ancestors.""" |
| 433 | |
| 434 | class FocusApp(App): |
| 435 | AUTO_FOCUS = None |
| 436 | |
| 437 | def compose(self) -> ComposeResult: |
| 438 | with ScrollableContainer(id="focusable"): |
| 439 | with Container(): |
| 440 | yield Label("Foo", id="foo") |
| 441 | yield Label("Bar", id="bar") |
| 442 | yield Label("Egg", id="egg") |
| 443 | |
| 444 | app = FocusApp() |
| 445 | async with app.run_test() as pilot: |
| 446 | # Nothing focused |
| 447 | assert app.screen.focused is None |
| 448 | # Click foo |
| 449 | await pilot.click("#foo") |
| 450 | # Confirm container is focused |
| 451 | assert app.screen.focused is not None |
| 452 | assert app.screen.focused.id == "focusable" |
| 453 | # Reset focus |
| 454 | app.screen.set_focus(None) |
| 455 | assert app.screen.focused is None |
| 456 | # Click bar |
| 457 | await pilot.click("#bar") |
| 458 | # Confirm container is focused |
| 459 | assert app.screen.focused is not None |
| 460 | assert app.screen.focused.id == "focusable" |
| 461 | # Reset focus |
| 462 | app.screen.set_focus(None) |
| 463 | assert app.screen.focused is None |
| 464 | # Click egg (outside of focusable widget) |
| 465 | await pilot.click("#egg") |
| 466 | # Confirm nothing focused |
| 467 | assert app.screen.focused is None |
| 468 | |
| 469 | |
| 470 | async def test_allow_focus_override(): |