[core/theme|output] Add separators

Add a way for themes to specify custom separators. Doing that, make
nicer interfaces for drawing "supplementary" components (separators)
for widgets and generalize the attribute retrieval within the theme.
This commit is contained in:
Tobias Witek 2020-02-22 13:42:44 +01:00
parent d3fc648c08
commit 38410adcb8
6 changed files with 63 additions and 15 deletions

View file

@ -6,6 +6,9 @@ def register(event, callback, *args, **kwargs):
lambda: callback(*args, **kwargs) lambda: callback(*args, **kwargs)
) )
def clear():
callbacks.clear()
def trigger(event): def trigger(event):
for callback in callbacks.get(event, []): for callback in callbacks.get(event, []):
callback() callback()

View file

@ -38,10 +38,23 @@ class i3(object):
def stop(self): def stop(self):
return { 'suffix': '\n]' } return { 'suffix': '\n]' }
def widgets(self, module): def __separator(self, widget):
widgets = [] if not self._theme.separator():
for widget in module.widgets(): return []
widgets.append({ return [{
'full_text': self._theme.separator(),
'color': self._theme.bg(widget),
'background': self._theme.prev_bg(widget),
'separator': False,
'separator_block_width': self._theme.separator_block_width(),
'border_top': self._theme.border_top(),
'border_left': self._theme.border_left(),
'border_right': self._theme.border_right(),
'border_bottom': self._theme.border_bottom(),
}]
def __main(self, module, widget):
return [{
'full_text': widget.full_text(), 'full_text': widget.full_text(),
'instance': widget.id(), 'instance': widget.id(),
'name': module.id(), 'name': module.id(),
@ -49,7 +62,17 @@ class i3(object):
'background': self._theme.bg(widget), 'background': self._theme.bg(widget),
'separator': self._theme.default_separators(), 'separator': self._theme.default_separators(),
'separator_block_width': self._theme.separator_block_width(), 'separator_block_width': self._theme.separator_block_width(),
}) 'border_top': self._theme.border_top(),
'border_left': self._theme.border_left(),
'border_right': self._theme.border_right(),
'border_bottom': self._theme.border_bottom(),
}]
def widgets(self, module):
widgets = []
for widget in module.widgets():
widgets += self.__separator(widget)
widgets += self.__main(module, widget)
core.event.trigger('next-widget') core.event.trigger('next-widget')
return widgets return widgets
@ -61,6 +84,7 @@ class i3(object):
def statusline(self): def statusline(self):
widgets = [] widgets = []
for module in self._modules: for module in self._modules:
if module in self._status:
widgets += self._status[module] widgets += self._status[module]
return { return {
'data': widgets, 'data': widgets,

View file

@ -19,14 +19,18 @@ class Theme(object):
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('update', self.__start)
core.event.register('next-widget', self.__next_widget) core.event.register('next-widget', self.__next_widget)
for attr, default in [ for attr, default in [
('fg', None), ('bg', None), ('fg', None), ('bg', None),
('default-separators', True), ('default-separators', True),
('separator-block-width', 0), ('separator-block-width', 0),
('separator', None) ('separator', None),
('border-top', 0),
('border-bottom', 0),
('border-left', 0),
('border-right', 0),
]: ]:
setattr(self, attr.replace('-', '_'), lambda widget=None, default=default, attr=attr: self.__get(widget, attr, default)) setattr(self, attr.replace('-', '_'), lambda widget=None, default=default, attr=attr: self.__get(widget, attr, default))
@ -41,6 +45,14 @@ class Theme(object):
def __start(self): def __start(self):
self.__widget_count = 0 self.__widget_count = 0
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
def __next_widget(self): def __next_widget(self):
self.__widget_count = self.__widget_count + 1 self.__widget_count = self.__widget_count + 1

View file

@ -19,6 +19,7 @@
- hide modules if not in warning/error state (-a) - hide modules if not in warning/error state (-a)
- WAL support - WAL support
- tkinter / popups - tkinter / popups
- scrolling decorator (incl. minwidth, alignment)
## Improvements ## Improvements
- pango output (improve - maybe autodetect? see #531) - pango output (improve - maybe autodetect? see #531)

View file

@ -15,6 +15,8 @@ class i3(unittest.TestCase):
self.someModule = TestModule(widgets=[widget, widget, widget]) self.someModule = TestModule(widgets=[widget, widget, widget])
def test_start(self): def test_start(self):
core.event.clear()
all_data = self.i3.start() all_data = self.i3.start()
data = all_data['data'] data = all_data['data']
self.assertEqual(1, data['version'], 'i3bar protocol version 1 expected') self.assertEqual(1, data['version'], 'i3bar protocol version 1 expected')

View file

@ -1,9 +1,11 @@
import unittest import unittest
import core.theme import core.theme
import core.event
class theme(unittest.TestCase): class theme(unittest.TestCase):
def setUp(self): def setUp(self):
core.event.clear()
self.invalidThemeName = 'this-theme-does-not-exist' self.invalidThemeName = 'this-theme-does-not-exist'
self.validThemeName = 'default' self.validThemeName = 'default'
self.defaultsTheme = { self.defaultsTheme = {
@ -34,13 +36,17 @@ 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(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))
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())
core.event.trigger('start')
with unittest.mock.patch('core.output.sys.stdout'):
core.event.trigger('update')
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())