Browse Source

fix: LayoutView.__total_children being incorrectly set when adding/removing items from an item

pull/10166/head
DA-344 3 weeks ago
parent
commit
7012cec96a
  1. 10
      discord/ui/action_row.py
  2. 17
      discord/ui/container.py
  3. 11
      discord/ui/section.py
  4. 2
      discord/ui/view.py

10
discord/ui/action_row.py

@ -268,6 +268,10 @@ class ActionRow(Item[V]):
item._view = self._view item._view = self._view
item._parent = self item._parent = self
self._children.append(item) self._children.append(item)
if self._view and getattr(self._view, '__discord_ui_layout_view__', False):
self._view.__total_children += 1
return self return self
def remove_item(self, item: Item[Any]) -> Self: def remove_item(self, item: Item[Any]) -> Self:
@ -286,6 +290,10 @@ class ActionRow(Item[V]):
self._children.remove(item) self._children.remove(item)
except ValueError: except ValueError:
pass pass
else:
if self._view and getattr(self._view, '__discord_ui_layout_view__', False):
self._view.__total_children -= 1
return self return self
def get_item_by_id(self, id: int, /) -> Optional[Item[V]]: def get_item_by_id(self, id: int, /) -> Optional[Item[V]]:
@ -314,6 +322,8 @@ class ActionRow(Item[V]):
This function returns the class instance to allow for fluent-style This function returns the class instance to allow for fluent-style
chaining. chaining.
""" """
if self._view and getattr(self._view, '__discord_ui_layout_view__', False):
self._view.__total_children -= len(self._children)
self._children.clear() self._children.clear()
return self return self

17
discord/ui/container.py

@ -360,9 +360,17 @@ class Container(Item[V]):
else: else:
self.__dispatchable.append(item) self.__dispatchable.append(item)
is_layout_view = self._view and getattr(self._view, '__discord_ui_layout_view__', False)
if getattr(item, '__discord_ui_update_view__', False): if getattr(item, '__discord_ui_update_view__', False):
item._update_children_view(self.view) # type: ignore item._update_children_view(self.view) # type: ignore
if is_layout_view:
self._view.__total_children += len(tuple(item.walk_children())) # type: ignore
else:
if is_layout_view:
self._view.__total_children += 1 # type: ignore
item._view = self.view item._view = self.view
item._parent = self item._parent = self
return self return self
@ -383,6 +391,12 @@ class Container(Item[V]):
self._children.remove(item) self._children.remove(item)
except ValueError: except ValueError:
pass pass
else:
if self._view and getattr(self._view, '__discord_ui_layout_view__', False):
if getattr(item, '__discord_ui_update_view__', False):
self._view.__total_children -= len(tuple(item.walk_children())) # type: ignore
else:
self._view.__total_children -= 1
return self return self
def get_item_by_id(self, id: int, /) -> Optional[Item[V]]: def get_item_by_id(self, id: int, /) -> Optional[Item[V]]:
@ -411,5 +425,8 @@ class Container(Item[V]):
This function returns the class instance to allow for fluent-style This function returns the class instance to allow for fluent-style
chaining. chaining.
""" """
if self._view and getattr(self._view, '__discord_ui_layout_view__', False):
self._view.__total_children -= len(tuple(self.walk_children()))
self._children.clear() self._children.clear()
return self return self

11
discord/ui/section.py

@ -162,6 +162,10 @@ class Section(Item[V]):
item._view = self.view item._view = self.view
item._parent = self item._parent = self
self._children.append(item) self._children.append(item)
if self._view and getattr(self._view, '__discord_ui_layout_view__', False):
self._view.__total_children += 1
return self return self
def remove_item(self, item: Item[Any]) -> Self: def remove_item(self, item: Item[Any]) -> Self:
@ -180,6 +184,10 @@ class Section(Item[V]):
self._children.remove(item) self._children.remove(item)
except ValueError: except ValueError:
pass pass
else:
if self._view and getattr(self._view, '__discord_ui_layout_view__', False):
self._view.__total_children -= 1
return self return self
def get_item_by_id(self, id: int, /) -> Optional[Item[V]]: def get_item_by_id(self, id: int, /) -> Optional[Item[V]]:
@ -208,6 +216,9 @@ class Section(Item[V]):
This function returns the class instance to allow for fluent-style This function returns the class instance to allow for fluent-style
chaining. chaining.
""" """
if self._view and getattr(self._view, '__discord_ui_layout_view__', False):
self._view.__total_children -= len(self._children) + 1 # the + 1 is the accessory
self._children.clear() self._children.clear()
return self return self

2
discord/ui/view.py

@ -756,6 +756,8 @@ class LayoutView(BaseView):
If ``None`` then there is no timeout. If ``None`` then there is no timeout.
""" """
__discord_ui_layout_view__: ClassVar[bool] = True
def __init__(self, *, timeout: Optional[float] = 180.0) -> None: def __init__(self, *, timeout: Optional[float] = 180.0) -> None:
super().__init__(timeout=timeout) super().__init__(timeout=timeout)
self.__total_children: int = len(list(self.walk_children())) self.__total_children: int = len(list(self.walk_children()))

Loading…
Cancel
Save