Skip to content

Commit

Permalink
Include a flag in Focus events to indicate whether they're due to the…
Browse files Browse the repository at this point in the history
… App regaining focus or whether they're a standard focus.
  • Loading branch information
darrenburns committed Dec 12, 2024
1 parent d1464b5 commit b2d9dc3
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/textual/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,12 +869,20 @@ def _update_focus_styles(
[widget for widget in widgets if widget._has_focus_within], animate=True
)

def set_focus(self, widget: Widget | None, scroll_visible: bool = True) -> None:
def set_focus(
self,
widget: Widget | None,
scroll_visible: bool = True,
from_app_focus: bool = False,
) -> None:
"""Focus (or un-focus) a widget. A focused widget will receive key events first.
Args:
widget: Widget to focus, or None to un-focus.
scroll_visible: Scroll widget in to view.
from_app_focus: True if this focus is due to the app itself having regained
focus. False if the focus is being set because a widget within the app
regained focus.
"""
if widget is self.focused:
# Widget is already focused
Expand All @@ -899,7 +907,7 @@ def set_focus(self, widget: Widget | None, scroll_visible: bool = True) -> None:
# Change focus
self.focused = widget
# Send focus event
widget.post_message(events.Focus())
widget.post_message(events.Focus(from_app_focus=from_app_focus))
focused = widget

if scroll_visible:
Expand Down

0 comments on commit b2d9dc3

Please sign in to comment.