pytermgui.widgets.collapsible

The Collapsible widget type.

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

A collapsible section of UI.

Collapsible(label: str, *items: Any, keyboard: bool = False, **attrs: Any)
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

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:
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

Toggles expanded state.

Returns

This object.

def collapse(self) -> pytermgui.widgets.collapsible.Collapsible:
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

Collapses the dropdown.

Does nothing if already collapsed.

Returns

This object.

def expand(self) -> pytermgui.widgets.collapsible.Collapsible:
 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

Expands the dropdown.

Does nothing if already expanded.

Returns

This object.