[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.
This commit is contained in:
tobi-wan-kenobi 2020-04-12 14:01:51 +02:00
parent 4b402438cc
commit 4cd6444bbf
2 changed files with 10 additions and 4 deletions

View file

@ -34,6 +34,7 @@ class Theme(object):
self.__previous = {} self.__previous = {}
self.__current = {} self.__current = {}
self.__keywords = {} self.__keywords = {}
self.__value_idx = {}
self.__data = raw_data if raw_data else self.load(name) self.__data = raw_data if raw_data else self.load(name)
for icons in self.__data.get('icons', []): for icons in self.__data.get('icons', []):
util.algorithm.merge(self.__data, self.load(icons, 'icons')) util.algorithm.merge(self.__data, self.load(icons, 'icons'))
@ -81,6 +82,9 @@ class Theme(object):
self.__current.clear() self.__current.clear()
self.__previous.clear() self.__previous.clear()
for key, value in self.__value_idx.items():
self.__value_idx[key] = value + 1
def __next_widget(self): def __next_widget(self):
self.__widget_count = self.__widget_count + 1 self.__widget_count = self.__widget_count + 1
self.__previous = dict(self.__current) self.__previous = dict(self.__current)
@ -119,9 +123,9 @@ class Theme(object):
value = self.__keywords.get(value, value) value = self.__keywords.get(value, value)
if isinstance(value, list): if isinstance(value, list):
key = '__{}-idx__'.format(key) idx = self.__value_idx.get('{}::{}'.format(widget.id, key), 0) % len(value)
idx = widget.get(key, 0) self.__value_idx['{}::{}'.format(widget.id, key)] = idx
widget.set(key, (idx + 1) % len(value)) widget.set(key, idx)
value = value[idx] value = value[idx]
self.__current[key] = value self.__current[key] = value
return value return value

View file

@ -63,7 +63,7 @@ class theme(unittest.TestCase):
core.event.trigger('next-widget') core.event.trigger('next-widget')
self.assertEqual(self.cycleTheme['cycle'][2]['fg'], theme.get('fg')) self.assertEqual(self.cycleTheme['cycle'][2]['fg'], theme.get('fg'))
self.assertEqual(self.cycleTheme['cycle'][2]['bg'], theme.get('bg')) self.assertEqual(self.cycleTheme['cycle'][2]['bg'], theme.get('bg'))
with unittest.mock.patch('core.output.sys.stdout'): with unittest.mock.patch('core.output.sys.stdout'):
core.event.trigger('update') core.event.trigger('update')
self.assertEqual(self.cycleTheme['cycle'][0]['fg'], theme.get('fg')) 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): 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))
self.assertEqual(expected[i%len(expected)], theme.get('fg', widget)) # ensure multiple invocations are OK
core.event.trigger('update')
def test_state(self): def test_state(self):
widget = core.widget.Widget() widget = core.widget.Widget()