-
Hi. I need to have a container that should be focusable only when it has any of its scrollbars Here is my attempt: class CanFocusOnlyWhenScrollbars(Widget, can_focus=False):
"""A Widget that can be focused only when any of it's scrollbars is active."""
@on(Resize)
def __enable_focus_only_when_scrollbar_is_active(self) -> None:
self.can_focus = any(self.scrollbars_enabled) but it doesn't work in all cases because the Any better ideas? I guess it would be much simpler to create if there were more Messages being posted (e.g. Maybe there should be different modes? Like |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
from textual import on
from textual.app import App, ComposeResult
from textual.containers import VerticalScroll
from textual.widgets import Button, Label
class MaybeFocus(VerticalScroll, can_focus=False):
def watch_show_vertical_scrollbar(self) -> None:
self.can_focus = self.show_horizontal_scrollbar or self.show_vertical_scrollbar
def watch_show_horizontal_scrollbar(self) -> None:
self.can_focus = self.show_horizontal_scrollbar or self.show_vertical_scrollbar
class FocusWhenScrollApp(App[None]):
CSS = """
Label {
height: 5;
background: red;
width: 1fr;
}
"""
def compose(self) -> ComposeResult:
yield Button("MORE!")
yield MaybeFocus()
@on(Button.Pressed)
def more(self) -> None:
self.query_one(MaybeFocus).mount(Label("Stuff"))
if __name__ == "__main__":
FocusWhenScrollApp().run() |
Beta Was this translation helpful? Give feedback.
Widget.show_horizontal_scrollbar
andWidget.show_vertical_scrollbar
are reactives that can be watched. I'd probably do something with that: