Merge pull request #747 from Textualize/focus-scroll-fix

Scroll issue
This commit is contained in:
Will McGugan
2022-09-08 14:37:53 +01:00
committed by GitHub
3 changed files with 14 additions and 6 deletions

View File

@@ -899,7 +899,7 @@ class App(Generic[ReturnType], DOMNode):
self.log.system(f"{self.screen} is active")
return previous_screen
def set_focus(self, widget: Widget | None, scroll_visible: bool = False) -> None:
def set_focus(self, widget: Widget | None, scroll_visible: bool = True) -> None:
"""Focus (or unfocus) a widget. A focused widget will receive key events first.
Args:

View File

@@ -300,8 +300,11 @@ class Screen(Widget):
except errors.NoWidget:
self.app.set_focus(None)
else:
if isinstance(event, events.MouseDown) and widget.can_focus:
self.app.set_focus(widget)
if isinstance(event, events.MouseUp) and widget.can_focus:
if self.app.focused is not widget:
self.app.set_focus(widget)
event.stop()
return
event.style = self.get_style_at(event.screen_x, event.screen_y)
if widget is self:
event.set_forwarded()

View File

@@ -1435,9 +1435,14 @@ class Widget(DOMNode):
self._layout_required = False
screen.post_message_no_wait(messages.Layout(self))
def focus(self) -> None:
"""Give input focus to this widget."""
self.app.set_focus(self)
def focus(self, scroll_visible: bool = True) -> None:
"""Give focus to this widget.
Args:
scroll_visible (bool, optional): Scroll parent to make this widget
visible. Defaults to True.
"""
self.app.set_focus(self, scroll_visible=scroll_visible)
def capture_mouse(self, capture: bool = True) -> None:
"""Capture (or release) the mouse.