add stack event handling

This commit is contained in:
Jakob Ketterl 2020-03-24 00:08:48 +01:00
parent c83d8580ba
commit d5c2f8414e
2 changed files with 38 additions and 4 deletions

View File

@ -144,12 +144,22 @@ class PropertyStack(PropertyManager):
"""
self.layers.append({"priority": priority, "props": pm})
def eventClosure(name, value):
self.receiveEvent(pm, name, value)
pm.wire(eventClosure)
def receiveEvent(self, layer, name, value):
if layer != self._getTopLayer(name):
return
self._fireCallbacks(name, value)
def removeLayer(self, pm: PropertyManager):
for layer in self.layers:
if layer["props"] == pm:
self.layers.remove(layer)
def _getLayer(self, item):
def _getTopLayer(self, item):
layers = [la["props"] for la in sorted(self.layers, key=lambda l: l["priority"])]
for m in layers:
if item in m:
@ -158,15 +168,15 @@ class PropertyStack(PropertyManager):
return layers[0]
def __getitem__(self, item):
layer = self._getLayer(item)
layer = self._getTopLayer(item)
return layer.__getitem__(item)
def __setitem__(self, key, value):
layer = self._getLayer(key)
layer = self._getTopLayer(key)
return layer.__setitem__(key, value)
def __contains__(self, item):
layer = self._getLayer(item)
layer = self._getTopLayer(item)
return layer.__contains__(item)
def __dict__(self):

View File

@ -41,3 +41,27 @@ class PropertyStackTest(TestCase):
self.assertEqual(om["testkey"], "high value")
om.removeLayer(high_pm)
self.assertEqual(om["testkey"], "low value")
def testPropertyChange(self):
layer = PropertyLayer()
stack = PropertyStack()
stack.addLayer(0, layer)
mock = Mock()
stack.wire(mock.method)
layer["testkey"] = "testvalue"
mock.method.assert_called_once_with("testkey", "testvalue")
def testPropertyChangeEventPriority(self):
low_layer = PropertyLayer()
high_layer = PropertyLayer()
low_layer["testkey"] = "initial low value"
high_layer["testkey"] = "initial high value"
stack = PropertyStack()
stack.addLayer(1, low_layer)
stack.addLayer(0, high_layer)
mock = Mock()
stack.wire(mock.method)
low_layer["testkey"] = "modified low value"
mock.method.assert_not_called()
high_layer["testkey"] = "modified high value"
mock.method.assert_called_once_with("testkey", "modified high value")