Skip to content

collapsible

The Collapsible widget type.

Collapsible

Bases: Container

A collapsible section of UI.

Source code in pytermgui/widgets/collapsible.py
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
class Collapsible(Container):
    """A collapsible section of UI."""

    def __init__(
        self, label: str, *items: Any, keyboard: bool = False, **attrs: Any
    ) -> None:
        """Initializes the widget.

        Args:
            label: The label for the trigger toggle.
            *items: The items that will be hidden when the object is collapsed.
            keyboard: If set, the first character of the label will be used as
                a `CTRL_` binding to toggle the object.
        """

        if keyboard:
            bind = label[0]
            self.trigger = Toggle(
                (f"▶ ({bind}){label[1:]}", f"▼ ({bind}){label[1:]}"),
                lambda *_: self.toggle(),
            )
        else:
            self.trigger = Toggle(
                (f"▶ {label}", f"▼ {label}"), lambda *_: self.toggle()
            )

        super().__init__(self.trigger, *items, box="EMPTY", **attrs)

        if keyboard:
            self.bind(
                getattr(keys, f"CTRL_{bind}"),
                lambda *_: self.trigger.toggle(),
                "Open dropdown",
            )

        self.collapsed_height = 1
        self.overflow = Overflow.HIDE
        self.height = self.collapsed_height

        self._is_expanded = False

    def toggle(self) -> Collapsible:
        """Toggles expanded state.

        Returns:
            This object.
        """

        if self.trigger.checked != self._is_expanded:
            self.trigger.toggle(run_callback=False)

        self._is_expanded = not self._is_expanded

        if self._is_expanded:
            self.overflow = Overflow.RESIZE
        else:
            self.overflow = Overflow.HIDE
            self.height = self.collapsed_height

        return self

    def collapse(self) -> Collapsible:
        """Collapses the dropdown.

        Does nothing if already collapsed.

        Returns:
            This object.
        """

        if self._is_expanded:
            self.toggle()

        return self

    def expand(self) -> Collapsible:
        """Expands the dropdown.

        Does nothing if already expanded.

        Returns:
            This object.
        """

        if not self._is_expanded:
            self.toggle()

        return self

__init__(label, *items, keyboard=False, **attrs)

Initializes the widget.

Parameters:

Name Type Description Default
label str

The label for the trigger toggle.

required
*items Any

The items that will be hidden when the object is collapsed.

()
keyboard bool

If set, the first character of the label will be used as a CTRL_ binding to toggle the object.

False
Source code in pytermgui/widgets/collapsible.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def __init__(
    self, label: str, *items: Any, keyboard: bool = False, **attrs: Any
) -> None:
    """Initializes the widget.

    Args:
        label: The label for the trigger toggle.
        *items: The items that will be hidden when the object is collapsed.
        keyboard: If set, the first character of the label will be used as
            a `CTRL_` binding to toggle the object.
    """

    if keyboard:
        bind = label[0]
        self.trigger = Toggle(
            (f"▶ ({bind}){label[1:]}", f"▼ ({bind}){label[1:]}"),
            lambda *_: self.toggle(),
        )
    else:
        self.trigger = Toggle(
            (f"▶ {label}", f"▼ {label}"), lambda *_: self.toggle()
        )

    super().__init__(self.trigger, *items, box="EMPTY", **attrs)

    if keyboard:
        self.bind(
            getattr(keys, f"CTRL_{bind}"),
            lambda *_: self.trigger.toggle(),
            "Open dropdown",
        )

    self.collapsed_height = 1
    self.overflow = Overflow.HIDE
    self.height = self.collapsed_height

    self._is_expanded = False

collapse()

Collapses the dropdown.

Does nothing if already collapsed.

Returns:

Type Description
Collapsible

This object.

Source code in pytermgui/widgets/collapsible.py
76
77
78
79
80
81
82
83
84
85
86
87
88
def collapse(self) -> Collapsible:
    """Collapses the dropdown.

    Does nothing if already collapsed.

    Returns:
        This object.
    """

    if self._is_expanded:
        self.toggle()

    return self

expand()

Expands the dropdown.

Does nothing if already expanded.

Returns:

Type Description
Collapsible

This object.

Source code in pytermgui/widgets/collapsible.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
def expand(self) -> Collapsible:
    """Expands the dropdown.

    Does nothing if already expanded.

    Returns:
        This object.
    """

    if not self._is_expanded:
        self.toggle()

    return self

toggle()

Toggles expanded state.

Returns:

Type Description
Collapsible

This object.

Source code in pytermgui/widgets/collapsible.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
def toggle(self) -> Collapsible:
    """Toggles expanded state.

    Returns:
        This object.
    """

    if self.trigger.checked != self._is_expanded:
        self.trigger.toggle(run_callback=False)

    self._is_expanded = not self._is_expanded

    if self._is_expanded:
        self.overflow = Overflow.RESIZE
    else:
        self.overflow = Overflow.HIDE
        self.height = self.collapsed_height

    return self