6
6
import time
7
7
from functools import partial
8
8
from itertools import groupby
9
- from typing import TYPE_CHECKING , Any , Callable , TypeVar
9
+ from typing import TYPE_CHECKING , Any , Callable , TypeVar , Union
10
10
11
11
from ..enums import ComponentType
12
12
from ..utils import find
29
29
30
30
M = TypeVar ("M" , bound = "Modal" , covariant = True )
31
31
32
+ ModalItem = Union [InputText , Item [M ]]
32
33
33
34
class Modal :
34
35
"""Represents a UI Modal dialog.
@@ -64,7 +65,7 @@ class Modal:
64
65
65
66
def __init__ (
66
67
self ,
67
- * children : InputText | Item [ M ] ,
68
+ * children : ModalItem ,
68
69
title : str ,
69
70
custom_id : str | None = None ,
70
71
timeout : float | None = None ,
@@ -78,7 +79,7 @@ def __init__(
78
79
if len (title ) > 45 :
79
80
raise ValueError ("title must be 45 characters or fewer" )
80
81
self ._title = title
81
- self ._children : list [InputText | Item [ M ] ] = list (children )
82
+ self ._children : list [ModalItem ] = list (children )
82
83
self ._weights = _ModalWeights (self ._children )
83
84
loop = asyncio .get_running_loop ()
84
85
self ._stopped : asyncio .Future [bool ] = loop .create_future ()
@@ -149,12 +150,12 @@ def title(self, value: str):
149
150
self ._title = value
150
151
151
152
@property
152
- def children (self ) -> list [InputText | Item [ M ] ]:
153
+ def children (self ) -> list [ModalItem ]:
153
154
"""The child components associated with the modal dialog."""
154
155
return self ._children
155
156
156
157
@children .setter
157
- def children (self , value : list [InputText | Item [ M ] ]):
158
+ def children (self , value : list [ModalItem ]):
158
159
for item in value :
159
160
if not isinstance (item , (InputText , Item )):
160
161
raise TypeError (
@@ -193,7 +194,7 @@ async def callback(self, interaction: Interaction):
193
194
self .stop ()
194
195
195
196
def to_components (self ) -> list [dict [str , Any ]]:
196
- def key (item : InputText | Item [ M ] ) -> int :
197
+ def key (item : ModalItem ) -> int :
197
198
return item ._rendered_row or 0
198
199
199
200
children = sorted (self ._children , key = key )
@@ -235,7 +236,7 @@ def key(item: InputText | Item[M]) -> int:
235
236
236
237
return components
237
238
238
- def add_item (self , item : InputText | Item [ M ] ) -> Self :
239
+ def add_item (self , item : ModalItem ) -> Self :
239
240
"""Adds a component to the modal dialog.
240
241
241
242
Parameters
@@ -256,7 +257,7 @@ def add_item(self, item: InputText | Item[M]) -> Self:
256
257
self ._children .append (item )
257
258
return self
258
259
259
- def remove_item (self , item : InputText | Item [ M ] ) -> Self :
260
+ def remove_item (self , item : ModalItem ) -> Self :
260
261
"""Removes a component from the modal dialog.
261
262
262
263
Parameters
@@ -270,7 +271,7 @@ def remove_item(self, item: InputText | Item[M]) -> Self:
270
271
pass
271
272
return self
272
273
273
- def get_item (self , id : str | int ) -> InputText | Item [ M ] | None :
274
+ def get_item (self , id : str | int ) -> ModalItem | None :
274
275
"""Gets an item from the modal. Roughly equal to `utils.get(modal.children, ...)`.
275
276
If an :class:`int` is provided, the item will be retrieved by ``id``, otherwise by ``custom_id``.
276
277
@@ -335,7 +336,7 @@ async def on_timeout(self) -> None:
335
336
class _ModalWeights :
336
337
__slots__ = ("weights" ,)
337
338
338
- def __init__ (self , children : list [InputText | Item [ M ] ]):
339
+ def __init__ (self , children : list [ModalItem ]):
339
340
self .weights : list [int ] = [0 , 0 , 0 , 0 , 0 ]
340
341
341
342
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]]):
344
345
for item in group :
345
346
self .add_item (item )
346
347
347
- def find_open_space (self , item : InputText | Item [ M ] ) -> int :
348
+ def find_open_space (self , item : ModalItem ) -> int :
348
349
for index , weight in enumerate (self .weights ):
349
350
if weight + item .width <= 5 :
350
351
return index
351
352
352
353
raise ValueError ("could not find open space for item" )
353
354
354
- def add_item (self , item : InputText | Item [ M ] ) -> None :
355
+ def add_item (self , item : ModalItem ) -> None :
355
356
if item .row is not None :
356
357
total = self .weights [item .row ] + item .width
357
358
if total > 5 :
@@ -365,7 +366,7 @@ def add_item(self, item: InputText | Item[M]) -> None:
365
366
self .weights [index ] += item .width
366
367
item ._rendered_row = index
367
368
368
- def remove_item (self , item : InputText | Item [ M ] ) -> None :
369
+ def remove_item (self , item : ModalItem ) -> None :
369
370
if item ._rendered_row is not None :
370
371
self .weights [item ._rendered_row ] -= item .width
371
372
item ._rendered_row = None
0 commit comments