[core/theme] Add true cycling support

Using the freshly introduced eventing system, enable cycling of widget
attributes.
This commit is contained in:
Tobias Witek 2020-02-16 14:39:10 +01:00
parent a4904d998f
commit 6e5e297d93
3 changed files with 26 additions and 6 deletions

View file

@ -49,6 +49,7 @@ class i3(object):
'background': self._theme.bg(widget),
'separator': self._theme.default_separators(),
})
core.event.trigger('next-widget')
return widgets
def update(self, affected_modules=None):

View file

@ -2,6 +2,8 @@ import os
import io
import json
import core.event
THEME_BASE_DIR=os.path.dirname(os.path.realpath(__file__))
PATHS=[
'.',
@ -12,10 +14,13 @@ PATHS=[
class Theme(object):
def __init__(self, name='default', iconset=None, raw_data=None):
self.name = name
self.__widget_count = 0
if raw_data:
self._data = raw_data
self.__data = raw_data
else:
self._data = self.load(name)
self.__data = self.load(name)
core.event.register('start', self.__start)
core.event.register('next-widget', self.__next_widget)
def load(self, name):
for path in PATHS:
@ -34,14 +39,20 @@ class Theme(object):
def default_separators(self, widget=None):
return self.__get(widget, 'default-separators', True)
def __start(self):
self.__widget_count = 0
def __next_widget(self):
self.__widget_count = self.__widget_count + 1
def __get(self, widget, key, default=None):
value = default
for option in ['defaults', 'cycle']:
if option in self._data:
tmp = self._data[option]
if option in self.__data:
tmp = self.__data[option]
if isinstance(tmp, list):
tmp = tmp[0]
tmp = tmp[self.__widget_count % len(tmp)]
value = tmp.get(key, value)
return value

View file

@ -14,7 +14,8 @@ class theme(unittest.TestCase):
self.cycleTheme = {
'cycle': [
{ 'fg': 'red', 'bg': 'black' },
{ 'fg': 'black', 'bg': 'red' }
{ 'fg': 'black', 'bg': 'red' },
{ 'fg': 'white', 'bg': 'blue' }
]
}
@ -35,5 +36,12 @@ class theme(unittest.TestCase):
theme = core.theme.Theme(raw_data=self.cycleTheme)
self.assertEqual(self.cycleTheme['cycle'][0]['fg'], theme.fg())
self.assertEqual(self.cycleTheme['cycle'][0]['bg'], theme.bg())
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]['bg'], theme.bg())
core.event.trigger('start')
self.assertEqual(self.cycleTheme['cycle'][0]['fg'], theme.fg())
self.assertEqual(self.cycleTheme['cycle'][0]['bg'], theme.bg())
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4