bastd.ui.radiogroup

UI functionality for creating radio groups of buttons.

 1# Released under the MIT License. See LICENSE for details.
 2#
 3"""UI functionality for creating radio groups of buttons."""
 4
 5from __future__ import annotations
 6
 7from typing import TYPE_CHECKING
 8
 9import ba
10
11if TYPE_CHECKING:
12    from typing import Any, Callable, Sequence
13
14
15def make_radio_group(
16    check_boxes: Sequence[ba.Widget],
17    value_names: Sequence[str],
18    value: str,
19    value_change_call: Callable[[str], Any],
20) -> None:
21    """Link the provided check_boxes together into a radio group."""
22
23    def _radio_press(
24        check_string: str, other_check_boxes: list[ba.Widget], val: int
25    ) -> None:
26        if val == 1:
27            value_change_call(check_string)
28            for cbx in other_check_boxes:
29                ba.checkboxwidget(edit=cbx, value=False)
30
31    for i, check_box in enumerate(check_boxes):
32        ba.checkboxwidget(
33            edit=check_box,
34            value=(value == value_names[i]),
35            is_radio_button=True,
36            on_value_change_call=ba.Call(
37                _radio_press,
38                value_names[i],
39                [c for c in check_boxes if c != check_box],
40            ),
41        )
def make_radio_group( check_boxes: Sequence[_ba.Widget], value_names: Sequence[str], value: str, value_change_call: Callable[[str], Any]) -> None:
16def make_radio_group(
17    check_boxes: Sequence[ba.Widget],
18    value_names: Sequence[str],
19    value: str,
20    value_change_call: Callable[[str], Any],
21) -> None:
22    """Link the provided check_boxes together into a radio group."""
23
24    def _radio_press(
25        check_string: str, other_check_boxes: list[ba.Widget], val: int
26    ) -> None:
27        if val == 1:
28            value_change_call(check_string)
29            for cbx in other_check_boxes:
30                ba.checkboxwidget(edit=cbx, value=False)
31
32    for i, check_box in enumerate(check_boxes):
33        ba.checkboxwidget(
34            edit=check_box,
35            value=(value == value_names[i]),
36            is_radio_button=True,
37            on_value_change_call=ba.Call(
38                _radio_press,
39                value_names[i],
40                [c for c in check_boxes if c != check_box],
41            ),
42        )

Link the provided check_boxes together into a radio group.