From 4cd6444bbf486299712c8e7b4b0d271475e64be9 Mon Sep 17 00:00:00 2001 From: tobi-wan-kenobi Date: Sun, 12 Apr 2020 14:01:51 +0200 Subject: [PATCH] [core/theme] Improve items that are lists Until now, using a list as prefix/suffix didn't work as expected, because the corresponding method for retrieving the value was called multiple times, and each time, the next icon would be retrieved. Changed the logic inside the theme to only update the indices every time that an actual update was happening. --- core/theme.py | 10 +++++++--- tests/core/test_theme.py | 4 +++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/core/theme.py b/core/theme.py index cfaa142..1911742 100644 --- a/core/theme.py +++ b/core/theme.py @@ -34,6 +34,7 @@ class Theme(object): self.__previous = {} self.__current = {} self.__keywords = {} + self.__value_idx = {} self.__data = raw_data if raw_data else self.load(name) for icons in self.__data.get('icons', []): util.algorithm.merge(self.__data, self.load(icons, 'icons')) @@ -81,6 +82,9 @@ class Theme(object): self.__current.clear() self.__previous.clear() + for key, value in self.__value_idx.items(): + self.__value_idx[key] = value + 1 + def __next_widget(self): self.__widget_count = self.__widget_count + 1 self.__previous = dict(self.__current) @@ -119,9 +123,9 @@ class Theme(object): value = self.__keywords.get(value, value) if isinstance(value, list): - key = '__{}-idx__'.format(key) - idx = widget.get(key, 0) - widget.set(key, (idx + 1) % len(value)) + idx = self.__value_idx.get('{}::{}'.format(widget.id, key), 0) % len(value) + self.__value_idx['{}::{}'.format(widget.id, key)] = idx + widget.set(key, idx) value = value[idx] self.__current[key] = value return value diff --git a/tests/core/test_theme.py b/tests/core/test_theme.py index 324d892..41e57d8 100644 --- a/tests/core/test_theme.py +++ b/tests/core/test_theme.py @@ -63,7 +63,7 @@ class theme(unittest.TestCase): core.event.trigger('next-widget') self.assertEqual(self.cycleTheme['cycle'][2]['fg'], theme.get('fg')) self.assertEqual(self.cycleTheme['cycle'][2]['bg'], theme.get('bg')) - + with unittest.mock.patch('core.output.sys.stdout'): core.event.trigger('update') self.assertEqual(self.cycleTheme['cycle'][0]['fg'], theme.get('fg')) @@ -108,6 +108,8 @@ class theme(unittest.TestCase): for i in range(0, len(expected)*3): self.assertEqual(expected[i%len(expected)], theme.get('fg', widget)) + self.assertEqual(expected[i%len(expected)], theme.get('fg', widget)) # ensure multiple invocations are OK + core.event.trigger('update') def test_state(self): widget = core.widget.Widget()