menu: optimize container populate

- don't populate all items at once
- populate items when container is pushed to stack
- precreate List back item, don't create it during populate.
- don't update items during populate

Signed-off-by: Janar Sööt <janar.soot@gmail.com>
This commit is contained in:
Janar Sööt 2020-08-18 22:04:53 +03:00 committed by KevinOConnor
parent 2a27093f69
commit ccfcc3789b
1 changed files with 8 additions and 13 deletions

View File

@ -328,7 +328,6 @@ class MenuContainer(MenuElement):
item.add_parents(self._parents)
item.add_parents(self)
item.assert_recursive_relation()
item.populate()
if index is None:
self._allitems.append((item, name))
else:
@ -346,7 +345,6 @@ class MenuContainer(MenuElement):
self._populate()
# send populate event
self.send_event('populate', self)
self.update_items()
def update_items(self):
_a = [(item, name) for item, name in self._allitems
@ -553,7 +551,12 @@ class MenuInput(MenuCommand):
class MenuList(MenuContainer):
def __init__(self, manager, config):
super(MenuList, self).__init__(manager, config)
self._show_title = True
# create back item
self._itemBack = self.manager.menuitem_from({
'type': 'command',
'name': '..',
'gcode': '{menu.back()}'
})
def _names_aslist(self):
return self.manager.lookup_children(self.get_ns())
@ -561,15 +564,7 @@ class MenuList(MenuContainer):
def _populate(self):
super(MenuList, self)._populate()
# add back as first item
name = '..'
if self._show_title:
name += ' %s' % str(self._name())
item = self.manager.menuitem_from({
'type': 'command',
'name': self.manager.asliteral(name),
'gcode': '{menu.back()}'
})
self.insert_item(item, 0)
self.insert_item(self._itemBack, 0)
def render_container(self, eventtime):
rows = []
@ -702,7 +697,6 @@ class MenuManager:
self.update_context(eventtime)
if isinstance(self.root, MenuContainer):
self.root.init_selection()
self.root.populate()
self.stack_push(self.root)
self.running = True
return
@ -748,6 +742,7 @@ class MenuManager:
def stack_push(self, container):
if not isinstance(container, MenuContainer):
raise error("Wrong type, expected MenuContainer")
container.populate()
top = self.stack_peek()
if top is not None:
if isinstance(top, MenuList):