9
9
10
10
from textual import _widget_navigation
11
11
from textual .binding import Binding , BindingType
12
- from textual .containers import Container
12
+ from textual .containers import VerticalScroll
13
13
from textual .events import Click , Mount
14
14
from textual .message import Message
15
15
from textual .reactive import var
16
16
from textual .widgets ._radio_button import RadioButton
17
17
18
18
19
- class RadioSet (Container , can_focus = True , can_focus_children = False ):
19
+ class RadioSet (VerticalScroll , can_focus = True , can_focus_children = False ):
20
20
"""Widget for grouping a collection of radio buttons into a set.
21
21
22
22
When a collection of [`RadioButton`][textual.widgets.RadioButton]s are
@@ -33,17 +33,17 @@ class RadioSet(Container, can_focus=True, can_focus_children=False):
33
33
height: auto;
34
34
width: auto;
35
35
36
+ & > RadioButton {
37
+ background: transparent;
38
+ border: none;
39
+ padding: 0 1;
40
+ }
41
+
36
42
& > RadioButton.-selected {
37
43
color: $text;
38
44
background: $highlight-cursor-blurred;
39
45
}
40
46
41
- & > * {
42
- background: transparent;
43
- border: none;
44
- padding: 0;
45
- }
46
-
47
47
& .toggle--button {
48
48
color: $surface;
49
49
background: $foreground 15%;
@@ -84,8 +84,6 @@ class RadioSet(Container, can_focus=True, can_focus_children=False):
84
84
}
85
85
}
86
86
}
87
-
88
-
89
87
"""
90
88
91
89
BINDINGS : ClassVar [list [BindingType ]] = [
@@ -215,6 +213,7 @@ def watch__selected(self) -> None:
215
213
self .query (RadioButton ).remove_class ("-selected" )
216
214
if self ._selected is not None :
217
215
self ._nodes [self ._selected ].add_class ("-selected" )
216
+ self ._scroll_to_selected ()
218
217
219
218
def _on_radio_button_changed (self , event : RadioButton .Changed ) -> None :
220
219
"""Respond to the value of a button in the set being changed.
@@ -303,3 +302,9 @@ def action_toggle_button(self) -> None:
303
302
button = self ._nodes [self ._selected ]
304
303
assert isinstance (button , RadioButton )
305
304
button .toggle ()
305
+
306
+ def _scroll_to_selected (self ) -> None :
307
+ """Ensure that the selected button is in view."""
308
+ if self ._selected is not None :
309
+ button = self ._nodes [self ._selected ]
310
+ self .call_after_refresh (self .scroll_to_widget , button , animate = False )
0 commit comments