2020-02-15 14:05:27 +01:00
|
|
|
import os
|
|
|
|
import io
|
|
|
|
import json
|
|
|
|
|
2020-02-16 14:39:10 +01:00
|
|
|
import core.event
|
2020-02-22 14:07:24 +01:00
|
|
|
import util.algorithm
|
2020-02-16 14:39:10 +01:00
|
|
|
|
2020-02-15 14:05:27 +01:00
|
|
|
THEME_BASE_DIR=os.path.dirname(os.path.realpath(__file__))
|
|
|
|
PATHS=[
|
|
|
|
'.',
|
|
|
|
os.path.join(THEME_BASE_DIR, '../themes'),
|
|
|
|
os.path.expanduser('~/.config/bumblebee-status/themes'),
|
|
|
|
]
|
2020-02-09 13:46:56 +01:00
|
|
|
|
|
|
|
class Theme(object):
|
2020-02-22 14:11:10 +01:00
|
|
|
def __init__(self, name='default', iconset='auto', raw_data=None):
|
2020-02-15 14:05:27 +01:00
|
|
|
self.name = name
|
2020-02-16 14:39:10 +01:00
|
|
|
self.__widget_count = 0
|
2020-02-15 14:05:27 +01:00
|
|
|
if raw_data:
|
2020-02-16 14:39:10 +01:00
|
|
|
self.__data = raw_data
|
2020-02-15 14:05:27 +01:00
|
|
|
else:
|
2020-02-16 14:39:10 +01:00
|
|
|
self.__data = self.load(name)
|
2020-02-22 14:07:24 +01:00
|
|
|
for icons in self.__data['icons']:
|
|
|
|
util.algorithm.merge(self.__data, self.load(icons, 'icons'))
|
2020-02-22 14:11:10 +01:00
|
|
|
if iconset != 'auto':
|
2020-02-22 14:08:28 +01:00
|
|
|
util.algorithm.merge(self.__data, self.load(iconset, 'icons'))
|
2020-02-22 14:07:24 +01:00
|
|
|
|
2020-02-22 13:42:44 +01:00
|
|
|
core.event.register('update', self.__start)
|
2020-02-16 14:39:10 +01:00
|
|
|
core.event.register('next-widget', self.__next_widget)
|
2020-02-15 14:05:27 +01:00
|
|
|
|
2020-02-16 21:15:14 +01:00
|
|
|
for attr, default in [
|
|
|
|
('fg', None), ('bg', None),
|
|
|
|
('default-separators', True),
|
|
|
|
('separator-block-width', 0),
|
2020-02-22 13:42:44 +01:00
|
|
|
('separator', None),
|
|
|
|
('border-top', 0),
|
|
|
|
('border-bottom', 0),
|
|
|
|
('border-left', 0),
|
|
|
|
('border-right', 0),
|
2020-02-16 21:15:14 +01:00
|
|
|
]:
|
|
|
|
setattr(self, attr.replace('-', '_'), lambda widget=None, default=default, attr=attr: self.__get(widget, attr, default))
|
|
|
|
|
2020-02-22 14:07:24 +01:00
|
|
|
def load(self, name, subdir=''):
|
2020-02-15 14:05:27 +01:00
|
|
|
for path in PATHS:
|
2020-02-22 14:07:24 +01:00
|
|
|
theme_file = os.path.join(path, subdir, '{}.json'.format(name))
|
2020-02-15 14:05:27 +01:00
|
|
|
if os.path.isfile(theme_file):
|
|
|
|
with io.open(theme_file, encoding='utf-8') as data:
|
|
|
|
return json.load(data)
|
|
|
|
raise RuntimeError('unable to find theme {}'.format(name))
|
|
|
|
|
2020-02-16 14:39:10 +01:00
|
|
|
def __start(self):
|
|
|
|
self.__widget_count = 0
|
|
|
|
|
2020-02-22 13:42:44 +01:00
|
|
|
def prev_bg(self, widget):
|
|
|
|
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
|
|
|
|
|
2020-02-16 14:39:10 +01:00
|
|
|
def __next_widget(self):
|
|
|
|
self.__widget_count = self.__widget_count + 1
|
|
|
|
|
2020-02-15 14:05:27 +01:00
|
|
|
def __get(self, widget, key, default=None):
|
|
|
|
value = default
|
|
|
|
|
2020-02-16 14:02:21 +01:00
|
|
|
for option in ['defaults', 'cycle']:
|
2020-02-16 14:39:10 +01:00
|
|
|
if option in self.__data:
|
|
|
|
tmp = self.__data[option]
|
2020-02-16 14:02:21 +01:00
|
|
|
if isinstance(tmp, list):
|
2020-02-16 14:39:10 +01:00
|
|
|
tmp = tmp[self.__widget_count % len(tmp)]
|
2020-02-16 14:02:21 +01:00
|
|
|
value = tmp.get(key, value)
|
2020-02-15 14:05:27 +01:00
|
|
|
return value
|
2020-02-09 13:46:56 +01:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|