[core] Widget creation/update overhaul

Until now, widgets were re-created during each iteration. For multiple,
reasons, using static widget objects is much easier, so instead of
creating new widgets continuously, modules now create the widgets during
instantiation and get the list of widgets passed as parameter whenever
an update occurs. During the update, they can still manipulate the
widget list by removing and adding elements as needed.

Advantages:
* Less memory fragmentation (fewer (de)allocations)
* Easier event management (widgets now have static IDs)
* Easier module code (widget contents can simply be the result of a
  callback)

see #23
This commit is contained in:
Tobi-wan Kenobi 2016-12-08 08:44:54 +01:00
parent 60ae92c8e3
commit f645203579
9 changed files with 65 additions and 17 deletions

View file

@ -12,8 +12,14 @@ class Module(object):
(e.g. CPU utilization, disk usage, etc.) derive from
this base class.
"""
def __init__(self, engine):
pass
def __init__(self, engine, widgets):
self._widgets = []
if widgets:
self._widgets = widgets if isinstance(widgets, list) else [widgets]
def widgets(self):
"""Return the widgets to draw for this module"""
return self._widgets
class Engine(object):
"""Engine for driving the application
@ -53,11 +59,9 @@ class Engine(object):
"""Start the event loop"""
self._output.start()
while self.running():
widgets = []
for module in self._modules:
module_widgets = module.widgets()
widgets += module_widgets if isinstance(module_widgets, list) else [module_widgets]
self._output.draw(widgets=widgets, engine=self)
module.update(module.widgets())
self._output.draw(widgets=module.widgets(), engine=self)
self._output.flush()
if self.running():
time.sleep(1)