Skip to content

Commit 2a7a075

Browse files
authored
ModalItem
1 parent 54ad575 commit 2a7a075

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

discord/ui/modal.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import time
77
from functools import partial
88
from itertools import groupby
9-
from typing import TYPE_CHECKING, Any, Callable, TypeVar
9+
from typing import TYPE_CHECKING, Any, Callable, TypeVar, Union
1010

1111
from ..enums import ComponentType
1212
from ..utils import find
@@ -29,6 +29,7 @@
2929

3030
M = TypeVar("M", bound="Modal", covariant=True)
3131

32+
ModalItem = Union[InputText, Item[M]]
3233

3334
class Modal:
3435
"""Represents a UI Modal dialog.
@@ -64,7 +65,7 @@ class Modal:
6465

6566
def __init__(
6667
self,
67-
*children: InputText | Item[M],
68+
*children: ModalItem,
6869
title: str,
6970
custom_id: str | None = None,
7071
timeout: float | None = None,
@@ -78,7 +79,7 @@ def __init__(
7879
if len(title) > 45:
7980
raise ValueError("title must be 45 characters or fewer")
8081
self._title = title
81-
self._children: list[InputText | Item[M]] = list(children)
82+
self._children: list[ModalItem] = list(children)
8283
self._weights = _ModalWeights(self._children)
8384
loop = asyncio.get_running_loop()
8485
self._stopped: asyncio.Future[bool] = loop.create_future()
@@ -149,12 +150,12 @@ def title(self, value: str):
149150
self._title = value
150151

151152
@property
152-
def children(self) -> list[InputText | Item[M]]:
153+
def children(self) -> list[ModalItem]:
153154
"""The child components associated with the modal dialog."""
154155
return self._children
155156

156157
@children.setter
157-
def children(self, value: list[InputText | Item[M]]):
158+
def children(self, value: list[ModalItem]):
158159
for item in value:
159160
if not isinstance(item, (InputText, Item)):
160161
raise TypeError(
@@ -193,7 +194,7 @@ async def callback(self, interaction: Interaction):
193194
self.stop()
194195

195196
def to_components(self) -> list[dict[str, Any]]:
196-
def key(item: InputText | Item[M]) -> int:
197+
def key(item: ModalItem) -> int:
197198
return item._rendered_row or 0
198199

199200
children = sorted(self._children, key=key)
@@ -235,7 +236,7 @@ def key(item: InputText | Item[M]) -> int:
235236

236237
return components
237238

238-
def add_item(self, item: InputText | Item[M]) -> Self:
239+
def add_item(self, item: ModalItem) -> Self:
239240
"""Adds a component to the modal dialog.
240241
241242
Parameters
@@ -256,7 +257,7 @@ def add_item(self, item: InputText | Item[M]) -> Self:
256257
self._children.append(item)
257258
return self
258259

259-
def remove_item(self, item: InputText | Item[M]) -> Self:
260+
def remove_item(self, item: ModalItem) -> Self:
260261
"""Removes a component from the modal dialog.
261262
262263
Parameters
@@ -270,7 +271,7 @@ def remove_item(self, item: InputText | Item[M]) -> Self:
270271
pass
271272
return self
272273

273-
def get_item(self, id: str | int) -> InputText | Item[M] | None:
274+
def get_item(self, id: str | int) -> ModalItem | None:
274275
"""Gets an item from the modal. Roughly equal to `utils.get(modal.children, ...)`.
275276
If an :class:`int` is provided, the item will be retrieved by ``id``, otherwise by ``custom_id``.
276277
@@ -335,7 +336,7 @@ async def on_timeout(self) -> None:
335336
class _ModalWeights:
336337
__slots__ = ("weights",)
337338

338-
def __init__(self, children: list[InputText | Item[M]]):
339+
def __init__(self, children: list[ModalItem]):
339340
self.weights: list[int] = [0, 0, 0, 0, 0]
340341

341342
key = lambda i: sys.maxsize if i.row is None else i.row
@@ -344,14 +345,14 @@ def __init__(self, children: list[InputText | Item[M]]):
344345
for item in group:
345346
self.add_item(item)
346347

347-
def find_open_space(self, item: InputText | Item[M]) -> int:
348+
def find_open_space(self, item: ModalItem) -> int:
348349
for index, weight in enumerate(self.weights):
349350
if weight + item.width <= 5:
350351
return index
351352

352353
raise ValueError("could not find open space for item")
353354

354-
def add_item(self, item: InputText | Item[M]) -> None:
355+
def add_item(self, item: ModalItem) -> None:
355356
if item.row is not None:
356357
total = self.weights[item.row] + item.width
357358
if total > 5:
@@ -365,7 +366,7 @@ def add_item(self, item: InputText | Item[M]) -> None:
365366
self.weights[index] += item.width
366367
item._rendered_row = index
367368

368-
def remove_item(self, item: InputText | Item[M]) -> None:
369+
def remove_item(self, item: ModalItem) -> None:
369370
if item._rendered_row is not None:
370371
self.weights[item._rendered_row] -= item.width
371372
item._rendered_row = None

0 commit comments

Comments
 (0)