[App] Remove the focus timer: we now focus from a widget to the next/prev one instantly

This commit is contained in:
Olivier Philippon
2022-05-12 16:42:42 +01:00
parent 96ce4202a5
commit cdad0ff020
2 changed files with 1 additions and 37 deletions

View File

@@ -192,7 +192,6 @@ class App(Generic[ReturnType], DOMNode):
self.registry: set[MessagePump] = set()
self.devtools = DevtoolsClient()
self._return_value: ReturnType | None = None
self._focus_timer: Timer | None = None
self.css_monitor = (
FileMonitor(css_path, self._on_css_change)
@@ -263,10 +262,6 @@ class App(Generic[ReturnType], DOMNode):
Returns:
Widget | None: Newly focused widget, or None for no focus.
"""
if self._focus_timer:
# Cancel the timer that clears the show focus class
# We will be creating a new timer to extend the time until the focus is hidden
self._focus_timer.stop_no_wait()
focusable_widgets = self.focus_chain
if not focusable_widgets:
@@ -284,12 +279,10 @@ class App(Generic[ReturnType], DOMNode):
self.set_focus(focusable_widgets[0])
else:
# Only move the focus if we are currently showing the focus
if direction and self.has_class("-show-focus"):
if direction:
current_index = (current_index + direction) % len(focusable_widgets)
self.set_focus(focusable_widgets[current_index])
self._focus_timer = self.set_timer(2, self.hide_focus)
self.add_class("-show-focus")
return self.focused
def show_focus(self) -> Widget | None:
@@ -316,15 +309,6 @@ class App(Generic[ReturnType], DOMNode):
"""
return self._move_focus(-1)
def hide_focus(self) -> None:
"""Hide the focus.
Returns:
Widget | None: Newly focused widget, or None for no focus.
"""
self.remove_class("-show-focus")
def compose(self) -> ComposeResult:
"""Yield child widgets for a container."""
return

View File

@@ -31,26 +31,6 @@ async def test_focus_chain():
assert focused == ["foo", "Paul", "baz"]
async def test_show_focus():
app = App()
app.push_screen(Screen())
app.screen.add_children(
Focusable(id="foo"),
NonFocusable(id="bar"),
Focusable(Focusable(id="Paul"), id="container1"),
NonFocusable(Focusable(id="Jessica"), id="container2"),
Focusable(id="baz"),
)
focused = [widget.id for widget in app.focus_chain]
assert focused == ["foo", "Paul", "baz"]
assert app.focused is None
assert not app.has_class("-show-focus")
app.show_focus()
assert app.has_class("-show-focus")
async def test_focus_next_and_previous():
app = App()