|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING, TypeVar |
| 4 | + |
| 5 | +from ..colour import Colour |
| 6 | +from ..components import Container as ContainerComponent |
| 7 | +from ..components import _component_factory, ActionRow |
| 8 | +from ..enums import ComponentType, SeparatorSpacingSize |
| 9 | +from .item import Item |
| 10 | +from .text_display import TextDisplay |
| 11 | +from .section import Section |
| 12 | +from .media_gallery import MediaGallery |
| 13 | +from .separator import Separator |
| 14 | +from .file import File |
| 15 | + |
| 16 | +__all__ = ("Container",) |
| 17 | + |
| 18 | +if TYPE_CHECKING: |
| 19 | + from ..types.components import ContainerComponent as ContainerComponentPayload |
| 20 | + from .view import View |
| 21 | + |
| 22 | + |
| 23 | +C = TypeVar("C", bound="Container") |
| 24 | +V = TypeVar("V", bound="View", covariant=True) |
| 25 | + |
| 26 | + |
| 27 | +class Container(Item[V]): |
| 28 | + """Represents a UI Container. Containers may contain up to 10 items. |
| 29 | +
|
| 30 | + The current items supported are: |
| 31 | +
|
| 32 | + - :class:`discord.ui.Button` |
| 33 | + - :class:`discord.ui.Select` |
| 34 | + - :class:`discord.ui.Section` |
| 35 | + - :class:`discord.ui.TextDisplay` |
| 36 | + - :class:`discord.ui.MediaGallery` |
| 37 | + - :class:`discord.ui.File` |
| 38 | + - :class:`discord.ui.Separator` |
| 39 | +
|
| 40 | +
|
| 41 | + .. versionadded:: 2.7 |
| 42 | +
|
| 43 | + Parameters |
| 44 | + ---------- |
| 45 | + *items: :class:`Item` |
| 46 | + The initial items in this container, up to 10. |
| 47 | + colour: Union[:class:`Colour`, :class:`int`] |
| 48 | + The accent colour of the container. Aliased to ``color`` as well. |
| 49 | + """ |
| 50 | + |
| 51 | + def __init__( |
| 52 | + self, |
| 53 | + *items: Item, |
| 54 | + colour: int | Colour | None = None, |
| 55 | + color: int | Colour | None = None, |
| 56 | + spoiler: bool = False |
| 57 | + ): |
| 58 | + super().__init__() |
| 59 | + |
| 60 | + self.items = [i for i in items] |
| 61 | + components = [i._underlying for i in items] |
| 62 | + self._color = colour |
| 63 | + |
| 64 | + self._underlying = ContainerComponent._raw_construct( |
| 65 | + type=ComponentType.section, |
| 66 | + id=None, |
| 67 | + components=components, |
| 68 | + accent_color=colour, |
| 69 | + spoiler=spoiler |
| 70 | + ) |
| 71 | + |
| 72 | + def add_item(self, item: Item) -> None: |
| 73 | + """Adds an item to the container. |
| 74 | +
|
| 75 | + Parameters |
| 76 | + ---------- |
| 77 | + item: :class:`Item` |
| 78 | + The item to add to the container. |
| 79 | +
|
| 80 | + Raises |
| 81 | + ------ |
| 82 | + TypeError |
| 83 | + An :class:`Item` was not passed. |
| 84 | + ValueError |
| 85 | + Maximum number of items has been exceeded (10). |
| 86 | + """ |
| 87 | + |
| 88 | + if len(self.items) >= 10: |
| 89 | + raise ValueError("maximum number of children exceeded") |
| 90 | + |
| 91 | + if not isinstance(item, Item): |
| 92 | + raise TypeError(f"expected Item not {item.__class__!r}") |
| 93 | + |
| 94 | + self.items.append(item) |
| 95 | + |
| 96 | + # reuse weight system? |
| 97 | + |
| 98 | + if item._underlying.is_v2(): |
| 99 | + self._underlying.components.append(item._underlying) |
| 100 | + else: |
| 101 | + found = False |
| 102 | + for i in range(len(self._underlying.components) - 1, 0, -1): |
| 103 | + row = self._underlying.components[i] |
| 104 | + if isinstance(row, ActionRow) and row.width + item.width <= 5: # If an actionRow exists |
| 105 | + row.children.append(item._underlying) |
| 106 | + found = True |
| 107 | + if not found: |
| 108 | + row = ActionRow.with_components(item._underlying) |
| 109 | + self._underlying.components.append(row) |
| 110 | + |
| 111 | + def add_section( |
| 112 | + self, |
| 113 | + *items: Item, accessory: Item = None |
| 114 | + ): |
| 115 | + """Adds a :class:`Section` to the container. |
| 116 | +
|
| 117 | + To append a pre-existing :class:`Section` use the |
| 118 | + :meth:`add_item` method instead. |
| 119 | +
|
| 120 | + Parameters |
| 121 | + ---------- |
| 122 | + *items: :class:`Item` |
| 123 | + The items contained in this section, up to 3. |
| 124 | + Currently only supports :class:`~discord.ui.TextDisplay`. |
| 125 | + accessory: Optional[:class:`Item`] |
| 126 | + The section's accessory. This is displayed in the top right of the section. |
| 127 | + Currently only supports :class:`~discord.ui.Button` and :class:`~discord.ui.Thumbnail`. |
| 128 | + """ |
| 129 | + # accept raw strings? |
| 130 | + |
| 131 | + section = Section( |
| 132 | + *items, accessory=accessory |
| 133 | + ) |
| 134 | + |
| 135 | + self.add_item(section) |
| 136 | + |
| 137 | + def add_text(self, content: str) -> None: |
| 138 | + """Adds a :class:`TextDisplay` to the container. |
| 139 | +
|
| 140 | + Parameters |
| 141 | + ---------- |
| 142 | + content: :class:`str` |
| 143 | + The content of the TextDisplay |
| 144 | + """ |
| 145 | + |
| 146 | + text = TextDisplay(content) |
| 147 | + |
| 148 | + self.add_item(text) |
| 149 | + |
| 150 | + def add_gallery( |
| 151 | + self, |
| 152 | + *items: Item, |
| 153 | + ): |
| 154 | + """Adds a :class:`MediaGallery` to the container. |
| 155 | +
|
| 156 | + To append a pre-existing :class:`MediaGallery` use the |
| 157 | + :meth:`add_item` method instead. |
| 158 | +
|
| 159 | + Parameters |
| 160 | + ---------- |
| 161 | + *items: List[:class:`MediaGalleryItem`] |
| 162 | + The media this gallery contains. |
| 163 | + """ |
| 164 | + # accept raw urls? |
| 165 | + |
| 166 | + g = MediaGallery( |
| 167 | + *items |
| 168 | + ) |
| 169 | + |
| 170 | + self.add_item(g) |
| 171 | + |
| 172 | + def add_file(self, url: str, spoiler: bool = False) -> None: |
| 173 | + """Adds a :class:`TextDisplay` to the container. |
| 174 | +
|
| 175 | + Parameters |
| 176 | + ---------- |
| 177 | + url: :class:`str` |
| 178 | + The URL of this file's media. This must be an ``attachment://`` URL that references a :class:`~discord.File`. |
| 179 | + spoiler: Optional[:class:`bool`] |
| 180 | + Whether the file is a spoiler. Defaults to ``False``. |
| 181 | + """ |
| 182 | + |
| 183 | + f = File(url, spoiler=spoiler) |
| 184 | + |
| 185 | + self.add_item(f) |
| 186 | + |
| 187 | + def add_separator(self, *, divider: bool = True, spacing: SeparatorSpacingSize = SeparatorSpacingSize.small) -> None: |
| 188 | + """Adds a :class:`Separator` to the container. |
| 189 | +
|
| 190 | + Parameters |
| 191 | + ---------- |
| 192 | + divider: :class:`bool` |
| 193 | + Whether the separator is a divider. Defaults to ``True``. |
| 194 | + spacing: :class:`~discord.SeparatorSpacingSize` |
| 195 | + The spacing size of the separator. Defaults to :attr:`~discord.SeparatorSpacingSize.small`. |
| 196 | + """ |
| 197 | + |
| 198 | + s = Separator(divider=divider, spacing=spacing) |
| 199 | + |
| 200 | + self.add_item(s) |
| 201 | + |
| 202 | + @property |
| 203 | + def spoiler(self) -> bool: |
| 204 | + """Whether the container is a spoiler. Defaults to ``False``.""" |
| 205 | + return self._spoiler |
| 206 | + |
| 207 | + @spoiler.setter |
| 208 | + def spoiler(self, spoiler: bool) -> None: |
| 209 | + self._spoiler = spoiler |
| 210 | + self._underlying.spoiler = spoiler |
| 211 | + |
| 212 | + @property |
| 213 | + def colour(self) -> Colour | None: |
| 214 | + return getattr(self, "_colour", None) |
| 215 | + |
| 216 | + @colour.setter |
| 217 | + def colour(self, value: int | Colour | None): # type: ignore |
| 218 | + if value is None or isinstance(value, Colour): |
| 219 | + self._colour = value |
| 220 | + elif isinstance(value, int): |
| 221 | + self._colour = Colour(value=value) |
| 222 | + else: |
| 223 | + raise TypeError( |
| 224 | + "Expected discord.Colour, int, or None but received" |
| 225 | + f" {value.__class__.__name__} instead." |
| 226 | + ) |
| 227 | + self._underlying.accent_color = self.colour |
| 228 | + |
| 229 | + color = colour |
| 230 | + |
| 231 | + @property |
| 232 | + def type(self) -> ComponentType: |
| 233 | + return self._underlying.type |
| 234 | + |
| 235 | + @property |
| 236 | + def width(self) -> int: |
| 237 | + return 5 |
| 238 | + |
| 239 | + def to_component_dict(self) -> ContainerComponentPayload: |
| 240 | + return self._underlying.to_dict() |
| 241 | + |
| 242 | + @classmethod |
| 243 | + def from_component(cls: type[C], component: ContainerComponent) -> C: |
| 244 | + from .view import _component_to_item |
| 245 | + |
| 246 | + items = [_component_to_item(c) for c in component.components] |
| 247 | + return cls(*items, colour=component.accent_color, spoiler=component.spoiler) |
| 248 | + |
| 249 | + callback = None |
0 commit comments