pytermgui.widgets.collapsible

The Collapsible widget type.

  1"""The Collapsible widget type."""
  2
  3from __future__ import annotations
  4
  5from typing import Any
  6
  7from ..input import keys
  8from ..enums import Overflow
  9from .interactive import Toggle
 10from .containers import Container
 11
 12
 13__all__ = ["Collapsible"]
 14
 15
 16class Collapsible(Container):
 17    """A collapsible section of UI."""
 18
 19    is_bindable = True
 20
 21    def __init__(
 22        self, label: str, *items: Any, keyboard: bool = False, **attrs: Any
 23    ) -> None:
 24        """Initializes the widget.
 25
 26        Args:
 27            label: The label for the trigger toggle.
 28            *items: The items that will be hidden when the object is collapsed.
 29            keyboard: If set, the first character of the label will be used as
 30                a `CTRL_` binding to toggle the object.
 31        """
 32
 33        if keyboard:
 34            bind = label[0]
 35            self.trigger = Toggle(
 36                (f"▶ ({bind}){label[1:]}", f"▼ ({bind}){label[1:]}"),
 37                lambda *_: self.toggle(),
 38            )
 39        else:
 40            self.trigger = Toggle(
 41                (f"▶ {label}", f"▼ {label}"), lambda *_: self.toggle()
 42            )
 43
 44        super().__init__(self.trigger, *items, box="EMPTY", **attrs)
 45
 46        if keyboard:
 47            self.bind(
 48                getattr(keys, f"CTRL_{bind}"),
 49                lambda *_: self.trigger.toggle(),
 50                "Open dropdown",
 51            )
 52
 53        self.collapsed_height = 1
 54        self.overflow = Overflow.HIDE
 55        self.height = self.collapsed_height
 56
 57        self._is_expanded = False
 58
 59    def toggle(self) -> Collapsible:
 60        """Toggles expanded state.
 61
 62        Returns:
 63            This object.
 64        """
 65
 66        if self.trigger.checked != self._is_expanded:
 67            self.trigger.toggle(run_callback=False)
 68
 69        self._is_expanded = not self._is_expanded
 70
 71        if self._is_expanded:
 72            self.overflow = Overflow.RESIZE
 73        else:
 74            self.overflow = Overflow.HIDE
 75            self.height = self.collapsed_height
 76
 77        return self
 78
 79    def collapse(self) -> Collapsible:
 80        """Collapses the dropdown.
 81
 82        Does nothing if already collapsed.
 83
 84        Returns:
 85            This object.
 86        """
 87
 88        if self._is_expanded:
 89            self.toggle()
 90
 91        return self
 92
 93    def expand(self) -> Collapsible:
 94        """Expands the dropdown.
 95
 96        Does nothing if already expanded.
 97
 98        Returns:
 99            This object.
100        """
101
102        if not self._is_expanded:
103            self.toggle()
104
105        return self
class Collapsible(pytermgui.widgets.containers.Container):
 17class Collapsible(Container):
 18    """A collapsible section of UI."""
 19
 20    is_bindable = True
 21
 22    def __init__(
 23        self, label: str, *items: Any, keyboard: bool = False, **attrs: Any
 24    ) -> None:
 25        """Initializes the widget.
 26
 27        Args:
 28            label: The label for the trigger toggle.
 29            *items: The items that will be hidden when the object is collapsed.
 30            keyboard: If set, the first character of the label will be used as
 31                a `CTRL_` binding to toggle the object.
 32        """
 33
 34        if keyboard:
 35            bind = label[0]
 36            self.trigger = Toggle(
 37                (f"▶ ({bind}){label[1:]}", f"▼ ({bind}){label[1:]}"),
 38                lambda *_: self.toggle(),
 39            )
 40        else:
 41            self.trigger = Toggle(
 42                (f"▶ {label}", f"▼ {label}"), lambda *_: self.toggle()
 43            )
 44
 45        super().__init__(self.trigger, *items, box="EMPTY", **attrs)
 46
 47        if keyboard:
 48            self.bind(
 49                getattr(keys, f"CTRL_{bind}"),
 50                lambda *_: self.trigger.toggle(),
 51                "Open dropdown",
 52            )
 53
 54        self.collapsed_height = 1
 55        self.overflow = Overflow.HIDE
 56        self.height = self.collapsed_height
 57
 58        self._is_expanded = False
 59
 60    def toggle(self) -> Collapsible:
 61        """Toggles expanded state.
 62
 63        Returns:
 64            This object.
 65        """
 66
 67        if self.trigger.checked != self._is_expanded:
 68            self.trigger.toggle(run_callback=False)
 69
 70        self._is_expanded = not self._is_expanded
 71
 72        if self._is_expanded:
 73            self.overflow = Overflow.RESIZE
 74        else:
 75            self.overflow = Overflow.HIDE
 76            self.height = self.collapsed_height
 77
 78        return self
 79
 80    def collapse(self) -> Collapsible:
 81        """Collapses the dropdown.
 82
 83        Does nothing if already collapsed.
 84
 85        Returns:
 86            This object.
 87        """
 88
 89        if self._is_expanded:
 90            self.toggle()
 91
 92        return self
 93
 94    def expand(self) -> Collapsible:
 95        """Expands the dropdown.
 96
 97        Does nothing if already expanded.
 98
 99        Returns:
100            This object.
101        """
102
103        if not self._is_expanded:
104            self.toggle()
105
106        return self

A collapsible section of UI.

Collapsible(label: str, *items: Any, keyboard: bool = False, **attrs: Any)
22    def __init__(
23        self, label: str, *items: Any, keyboard: bool = False, **attrs: Any
24    ) -> None:
25        """Initializes the widget.
26
27        Args:
28            label: The label for the trigger toggle.
29            *items: The items that will be hidden when the object is collapsed.
30            keyboard: If set, the first character of the label will be used as
31                a `CTRL_` binding to toggle the object.
32        """
33
34        if keyboard:
35            bind = label[0]
36            self.trigger = Toggle(
37                (f"▶ ({bind}){label[1:]}", f"▼ ({bind}){label[1:]}"),
38                lambda *_: self.toggle(),
39            )
40        else:
41            self.trigger = Toggle(
42                (f"▶ {label}", f"▼ {label}"), lambda *_: self.toggle()
43            )
44
45        super().__init__(self.trigger, *items, box="EMPTY", **attrs)
46
47        if keyboard:
48            self.bind(
49                getattr(keys, f"CTRL_{bind}"),
50                lambda *_: self.trigger.toggle(),
51                "Open dropdown",
52            )
53
54        self.collapsed_height = 1
55        self.overflow = Overflow.HIDE
56        self.height = self.collapsed_height
57
58        self._is_expanded = False

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.
is_bindable = True

Allow binding support

def toggle(self) -> pytermgui.widgets.collapsible.Collapsible:
60    def toggle(self) -> Collapsible:
61        """Toggles expanded state.
62
63        Returns:
64            This object.
65        """
66
67        if self.trigger.checked != self._is_expanded:
68            self.trigger.toggle(run_callback=False)
69
70        self._is_expanded = not self._is_expanded
71
72        if self._is_expanded:
73            self.overflow = Overflow.RESIZE
74        else:
75            self.overflow = Overflow.HIDE
76            self.height = self.collapsed_height
77
78        return self

Toggles expanded state.

Returns

This object.

def collapse(self) -> pytermgui.widgets.collapsible.Collapsible:
80    def collapse(self) -> Collapsible:
81        """Collapses the dropdown.
82
83        Does nothing if already collapsed.
84
85        Returns:
86            This object.
87        """
88
89        if self._is_expanded:
90            self.toggle()
91
92        return self

Collapses the dropdown.

Does nothing if already collapsed.

Returns

This object.

def expand(self) -> pytermgui.widgets.collapsible.Collapsible:
 94    def expand(self) -> Collapsible:
 95        """Expands the dropdown.
 96
 97        Does nothing if already expanded.
 98
 99        Returns:
100            This object.
101        """
102
103        if not self._is_expanded:
104            self.toggle()
105
106        return self

Expands the dropdown.

Does nothing if already expanded.

Returns

This object.