Skip to content

Commit 2f1bdea

Browse files
committed
optional actions
1 parent c20bf66 commit 2f1bdea

File tree

3 files changed

+50
-40
lines changed

3 files changed

+50
-40
lines changed

src/textual/demo.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Sidebar {
2929
}
3030

3131
Sidebar:focus-within {
32-
offset: 0 0 !important;
32+
offset: 0 0 !important;
3333
}
3434

3535
Sidebar.-hidden {

src/textual/screen.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,22 +295,19 @@ def set_focus(self, widget: Widget | None, scroll_visible: bool = True) -> None:
295295
# No focus, so blur currently focused widget if it exists
296296
if self.focused is not None:
297297
self.focused.post_message_no_wait(events.Blur(self))
298-
self.focused.emit_no_wait(events.DescendantBlur(self))
299298
self.focused = None
300299
self.log.debug("focus was removed")
301300
elif widget.can_focus:
302301
if self.focused != widget:
303302
if self.focused is not None:
304303
# Blur currently focused widget
305304
self.focused.post_message_no_wait(events.Blur(self))
306-
self.focused.emit_no_wait(events.DescendantBlur(self))
307305
# Change focus
308306
self.focused = widget
309307
# Send focus event
310308
if scroll_visible:
311309
self.screen.scroll_to_widget(widget)
312310
widget.post_message_no_wait(events.Focus(self))
313-
widget.emit_no_wait(events.DescendantFocus(self))
314311
self.log.debug(widget, "was focused")
315312

316313
async def _on_idle(self, event: events.Idle) -> None:

src/textual/widget.py

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2344,17 +2344,22 @@ def _on_enter(self, event: events.Enter) -> None:
23442344
self.mouse_over = True
23452345

23462346
def _on_focus(self, event: events.Focus) -> None:
2347-
for node in self.ancestors_with_self:
2348-
if node._has_focus_within:
2349-
self.app.update_styles(node)
23502347
self.has_focus = True
23512348
self.refresh()
2349+
self.emit_no_wait(events.DescendantFocus(self))
23522350

23532351
def _on_blur(self, event: events.Blur) -> None:
2354-
if any(node._has_focus_within for node in self.ancestors_with_self):
2355-
self.app.update_styles(self)
23562352
self.has_focus = False
23572353
self.refresh()
2354+
self.emit_no_wait(events.DescendantBlur(self))
2355+
2356+
def _on_descendant_blur(self, event: events.DescendantBlur) -> None:
2357+
if self._has_focus_within:
2358+
self.app.update_styles(self)
2359+
2360+
def _on_descendant_focus(self, event: events.DescendantBlur) -> None:
2361+
if self._has_focus_within:
2362+
self.app.update_styles(self)
23582363

23592364
def _on_mouse_scroll_down(self, event) -> None:
23602365
if self.allow_vertical_scroll:
@@ -2398,34 +2403,42 @@ def _on_hide(self, event: events.Hide) -> None:
23982403
def _on_scroll_to_region(self, message: messages.ScrollToRegion) -> None:
23992404
self.scroll_to_region(message.region, animate=True)
24002405

2401-
def action_scroll_home(self) -> None:
2402-
if self._allow_scroll:
2403-
self.scroll_home()
2404-
2405-
def action_scroll_end(self) -> None:
2406-
if self._allow_scroll:
2407-
self.scroll_end()
2408-
2409-
def action_scroll_left(self) -> None:
2410-
if self.allow_horizontal_scroll:
2411-
self.scroll_left()
2412-
2413-
def action_scroll_right(self) -> None:
2414-
if self.allow_horizontal_scroll:
2415-
self.scroll_right()
2416-
2417-
def action_scroll_up(self) -> None:
2418-
if self.allow_vertical_scroll:
2419-
self.scroll_up()
2420-
2421-
def action_scroll_down(self) -> None:
2422-
if self.allow_vertical_scroll:
2423-
self.scroll_down()
2424-
2425-
def action_page_down(self) -> None:
2426-
if self.allow_vertical_scroll:
2427-
self.scroll_page_down()
2428-
2429-
def action_page_up(self) -> None:
2430-
if self.allow_vertical_scroll:
2431-
self.scroll_page_up()
2406+
def action_scroll_home(self) -> bool | None:
2407+
if not self._allow_scroll:
2408+
return False
2409+
self.scroll_home()
2410+
2411+
def action_scroll_end(self) -> bool | None:
2412+
if not self._allow_scroll:
2413+
return False
2414+
self.scroll_end()
2415+
2416+
def action_scroll_left(self) -> bool | None:
2417+
if not self.allow_horizontal_scroll:
2418+
return False
2419+
self.scroll_left()
2420+
2421+
def action_scroll_right(self) -> bool | None:
2422+
if not self.allow_horizontal_scroll:
2423+
return False
2424+
self.scroll_right()
2425+
2426+
def action_scroll_up(self) -> bool | None:
2427+
if not self.allow_vertical_scroll:
2428+
return False
2429+
self.scroll_up()
2430+
2431+
def action_scroll_down(self) -> bool | None:
2432+
if not self.allow_vertical_scroll:
2433+
return False
2434+
self.scroll_down()
2435+
2436+
def action_page_down(self) -> bool | None:
2437+
if not self.allow_vertical_scroll:
2438+
return False
2439+
self.scroll_page_down()
2440+
2441+
def action_page_up(self) -> bool | None:
2442+
if not self.allow_vertical_scroll:
2443+
return False
2444+
self.scroll_page_up()

0 commit comments

Comments
 (0)