Refactor total children count to an overrideable method

This commit is contained in:
Rapptz
2025-08-18 04:49:20 -04:00
parent 5887ce1430
commit 9ef5b054e5
4 changed files with 41 additions and 35 deletions

View File

@ -319,6 +319,9 @@ class BaseView:
self.__timeout = value
def _add_count(self, value: int) -> None:
self._total_children = max(0, self._total_children + value)
@property
def children(self) -> List[Item[Self]]:
"""List[:class:`Item`]: The list of children attached to this view."""
@ -419,10 +422,7 @@ class BaseView:
if item._has_children():
added += len(tuple(item.walk_children())) # type: ignore
if self._is_layout() and self._total_children + added > 40:
raise ValueError('maximum number of children exceeded')
self._total_children += added
self._add_count(added)
self._children.append(item)
return self
@ -446,11 +446,7 @@ class BaseView:
removed = 1
if item._has_children():
removed += len(tuple(item.walk_children())) # type: ignore
if self._total_children - removed < 0:
self._total_children = 0
else:
self._total_children -= removed
self._add_count(-removed)
return self
@ -822,6 +818,12 @@ class LayoutView(BaseView):
def _is_layout(self) -> bool:
return True
def _add_count(self, value: int) -> None:
if self._total_children + value > 40:
raise ValueError('maximum number of children exceeded (40)')
self._total_children = max(0, self._total_children + value)
def to_components(self):
components: List[Dict[str, Any]] = []
for i in self._children: