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

@ -296,18 +296,20 @@ class Container(Item[V]):
------
TypeError
An :class:`Item` was not passed.
ValueError
Maximum number of children has been exceeded (40) for the entire view.
"""
if not isinstance(item, Item):
raise TypeError(f'expected Item not {item.__class__.__name__}')
if item._has_children() and self._view:
self._view._add_count(len(tuple(item.walk_children()))) # type: ignore
elif self._view:
self._view._add_count(1)
self._children.append(item)
item._update_view(self.view)
item._parent = self
if item._has_children() and self._view:
self._view._total_children += len(tuple(item.walk_children())) # type: ignore
elif self._view:
self._view._total_children += 1
return self
def remove_item(self, item: Item[Any]) -> Self:
@ -327,11 +329,11 @@ class Container(Item[V]):
except ValueError:
pass
else:
if self._view and self._view._is_layout():
if self._view:
if item._has_children():
self._view._total_children -= len(tuple(item.walk_children())) # type: ignore
self._view._add_count(-len(tuple(item.walk_children()))) # type: ignore
else:
self._view._total_children -= 1
self._view._add_count(-1)
return self
def find_item(self, id: int, /) -> Optional[Item[V]]:
@ -361,7 +363,7 @@ class Container(Item[V]):
chaining.
"""
if self._view and self._view._is_layout():
self._view._total_children -= len(tuple(self.walk_children()))
if self._view:
self._view._add_count(-len(tuple(self.walk_children())))
self._children.clear()
return self