[core/theme] Add true cycling support
Using the freshly introduced eventing system, enable cycling of widget attributes.
This commit is contained in:
parent
a4904d998f
commit
6e5e297d93
3 changed files with 26 additions and 6 deletions
|
@ -49,6 +49,7 @@ class i3(object):
|
||||||
'background': self._theme.bg(widget),
|
'background': self._theme.bg(widget),
|
||||||
'separator': self._theme.default_separators(),
|
'separator': self._theme.default_separators(),
|
||||||
})
|
})
|
||||||
|
core.event.trigger('next-widget')
|
||||||
return widgets
|
return widgets
|
||||||
|
|
||||||
def update(self, affected_modules=None):
|
def update(self, affected_modules=None):
|
||||||
|
|
|
@ -2,6 +2,8 @@ import os
|
||||||
import io
|
import io
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
import core.event
|
||||||
|
|
||||||
THEME_BASE_DIR=os.path.dirname(os.path.realpath(__file__))
|
THEME_BASE_DIR=os.path.dirname(os.path.realpath(__file__))
|
||||||
PATHS=[
|
PATHS=[
|
||||||
'.',
|
'.',
|
||||||
|
@ -12,10 +14,13 @@ PATHS=[
|
||||||
class Theme(object):
|
class Theme(object):
|
||||||
def __init__(self, name='default', iconset=None, raw_data=None):
|
def __init__(self, name='default', iconset=None, raw_data=None):
|
||||||
self.name = name
|
self.name = name
|
||||||
|
self.__widget_count = 0
|
||||||
if raw_data:
|
if raw_data:
|
||||||
self._data = raw_data
|
self.__data = raw_data
|
||||||
else:
|
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):
|
def load(self, name):
|
||||||
for path in PATHS:
|
for path in PATHS:
|
||||||
|
@ -34,14 +39,20 @@ class Theme(object):
|
||||||
def default_separators(self, widget=None):
|
def default_separators(self, widget=None):
|
||||||
return self.__get(widget, 'default-separators', True)
|
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):
|
def __get(self, widget, key, default=None):
|
||||||
value = default
|
value = default
|
||||||
|
|
||||||
for option in ['defaults', 'cycle']:
|
for option in ['defaults', 'cycle']:
|
||||||
if option in self._data:
|
if option in self.__data:
|
||||||
tmp = self._data[option]
|
tmp = self.__data[option]
|
||||||
if isinstance(tmp, list):
|
if isinstance(tmp, list):
|
||||||
tmp = tmp[0]
|
tmp = tmp[self.__widget_count % len(tmp)]
|
||||||
value = tmp.get(key, value)
|
value = tmp.get(key, value)
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,8 @@ class theme(unittest.TestCase):
|
||||||
self.cycleTheme = {
|
self.cycleTheme = {
|
||||||
'cycle': [
|
'cycle': [
|
||||||
{ 'fg': 'red', 'bg': 'black' },
|
{ '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)
|
theme = core.theme.Theme(raw_data=self.cycleTheme)
|
||||||
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'][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
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||||
|
|
Loading…
Reference in a new issue