[core/theme] Better separator handling

Add a generic method to the theme to get the "previous" value of an
attribute.
This commit is contained in:
Tobias Witek 2020-02-23 14:31:30 +01:00
parent 66bdfacf6f
commit a2446a36af
3 changed files with 16 additions and 11 deletions

View file

@ -44,7 +44,7 @@ class i3(object):
return [{ return [{
'full_text': self._theme.separator(), 'full_text': self._theme.separator(),
'color': self._theme.bg(widget), 'color': self._theme.bg(widget),
'background': self._theme.prev_bg(widget), 'background': self._theme.bg('previous'),
'separator': False, 'separator': False,
'separator_block_width': self._theme.separator_block_width(), 'separator_block_width': self._theme.separator_block_width(),
'border_top': self._theme.border_top(), 'border_top': self._theme.border_top(),

View file

@ -16,6 +16,8 @@ class Theme(object):
def __init__(self, name='default', iconset='auto', raw_data=None): def __init__(self, name='default', iconset='auto', raw_data=None):
self.name = name self.name = name
self.__widget_count = 0 self.__widget_count = 0
self.__previous = {}
self.__current = {}
if raw_data: if raw_data:
self.__data = raw_data self.__data = raw_data
else: else:
@ -50,19 +52,20 @@ class Theme(object):
def __start(self): def __start(self):
self.__widget_count = 0 self.__widget_count = 0
self.__current.clear()
def prev_bg(self, widget): self.__previous.clear()
if self.__widget_count == 0:
return None
self.__widget_count = self.__widget_count - 1
value = self.bg(widget)
self.__widget_count = self.__widget_count + 1
return value
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.__current.clear()
def __get(self, widget, key, default=None): def __get(self, widget, key, default=None):
if widget and isinstance(widget, str):
# special handling
if widget == 'previous':
return self.__previous.get(key, None)
value = default value = default
for option in ['defaults', 'cycle']: for option in ['defaults', 'cycle']:
@ -71,6 +74,8 @@ class Theme(object):
if isinstance(tmp, list): if isinstance(tmp, list):
tmp = tmp[self.__widget_count % len(tmp)] tmp = tmp[self.__widget_count % len(tmp)]
value = tmp.get(key, value) value = tmp.get(key, value)
self.__current[key] = value
return value return value
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -36,11 +36,11 @@ class theme(unittest.TestCase):
def test_cycle(self): def test_cycle(self):
theme = core.theme.Theme(raw_data=self.cycleTheme) theme = core.theme.Theme(raw_data=self.cycleTheme)
self.assertEqual(None, theme.prev_bg(None)) self.assertEqual(None, theme.bg('previous'))
self.assertEqual(self.cycleTheme['cycle'][0]['fg'], theme.fg()) self.assertEqual(self.cycleTheme['cycle'][0]['fg'], theme.fg())
self.assertEqual(self.cycleTheme['cycle'][0]['bg'], theme.bg()) self.assertEqual(self.cycleTheme['cycle'][0]['bg'], theme.bg())
core.event.trigger('next-widget') core.event.trigger('next-widget')
self.assertEqual(self.cycleTheme['cycle'][0]['bg'], theme.prev_bg(None)) self.assertEqual(self.cycleTheme['cycle'][0]['bg'], theme.bg('previous'))
core.event.trigger('next-widget') core.event.trigger('next-widget')
self.assertEqual(self.cycleTheme['cycle'][2]['fg'], theme.fg()) self.assertEqual(self.cycleTheme['cycle'][2]['fg'], theme.fg())
self.assertEqual(self.cycleTheme['cycle'][2]['bg'], theme.bg()) self.assertEqual(self.cycleTheme['cycle'][2]['bg'], theme.bg())