diff --git a/core/input.py b/core/input.py new file mode 100644 index 0000000..1b76016 --- /dev/null +++ b/core/input.py @@ -0,0 +1,35 @@ +import uuid + +LEFT_MOUSE = 1 +MIDDLE_MOUSE = 2 +RIGHT_MOUSE = 3 +WHEEL_UP = 4 +WHEEL_DOWN = 5 + +callbacks = {} + +class Object(object): + def __init__(self): + self._id = uuid.uuid4() + + def id(self): + return self._id + +def register(obj, button=None, cmd=None): + callbacks.setdefault(obj.id(), {}).setdefault(button, []).append(cmd) + +def trigger(event): + for field in ['instance', 'name']: + if field in event: + cb = callbacks.get(event[field]) + _invoke(event, cb) + +def _invoke(event, callback): + if not callback: return + if not 'button' in event: return + + for cb in callback.get(event['button']): + if callable(cb): + cb(event) + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/core/module.py b/core/module.py index 86cf320..85a45f4 100644 --- a/core/module.py +++ b/core/module.py @@ -1,6 +1,8 @@ import importlib import logging +import core.input + log = logging.getLogger(__name__) def load(module_name, config=None): @@ -11,8 +13,9 @@ def load(module_name, config=None): return Error(module_name) return getattr(mod, 'Module')(config) -class Module(object): +class Module(core.input.Object): def __init__(self, config=None, widgets=[]): + super().__init__() self._config = config self._widgets = widgets if isinstance(widgets, list) else [ widgets ] self._name = None diff --git a/modules/datetime.py b/modules/datetime.py index 58d586b..744498a 100644 --- a/modules/datetime.py +++ b/modules/datetime.py @@ -13,13 +13,13 @@ import locale import core.module import core.widget +import core.input class Module(core.module.Module): def __init__(self, config): super().__init__(config, core.widget.Widget(self.full_text)) - # TODO: register callback - #engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, - # cmd='calendar') + + core.input.register(self, button=core.input.LEFT_MOUSE, cmd='calendar') self._fmt = self.parameter('format', self.default_format()) l = locale.getdefaultlocale() if not l or l == (None, None): diff --git a/tests/core/test_input.py b/tests/core/test_input.py new file mode 100644 index 0000000..5399e4f --- /dev/null +++ b/tests/core/test_input.py @@ -0,0 +1,37 @@ +import unittest + +import core.input + +class config(unittest.TestCase): + def setUp(self): + self.inputObject = core.input.Object() + self.anotherObject = core.input.Object() + self.someEvent = { 'button': core.input.LEFT_MOUSE, 'instance': self.inputObject.id() } + self.anotherEvent = { 'button': core.input.RIGHT_MOUSE, 'instance': self.inputObject.id() } + self.callback = unittest.mock.MagicMock() + self.callback2 = unittest.mock.MagicMock() + + def test_callable_gets_called(self): + core.input.register(self.inputObject, self.someEvent['button'], self.callback) + core.input.trigger(self.someEvent) + self.callback.assert_called_once_with(self.someEvent) + + def test_different_events(self): + core.input.register(self.inputObject, self.someEvent['button'], self.callback) + core.input.register(self.inputObject, self.anotherEvent['button'], self.callback) + core.input.register(self.anotherObject, self.someEvent['button'], self.callback2) + core.input.register(self.anotherObject, self.anotherEvent['button'], self.callback2) + core.input.trigger(self.someEvent) + core.input.trigger(self.anotherEvent) + self.callback.assert_any_call(self.someEvent) + self.callback.assert_any_call(self.anotherEvent) + self.callback2.assert_not_called() + + def test_multiple_registrations(self): + core.input.register(self.inputObject, self.someEvent['button'], self.callback) + core.input.register(self.inputObject, self.someEvent['button'], self.callback2) + core.input.trigger(self.someEvent) + self.callback.assert_called_once_with(self.someEvent) + self.callback2.assert_called_once_with(self.someEvent) + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4