[modules/title] Update to latest API

This commit is contained in:
tobi-wan-kenobi 2020-04-25 10:40:11 +02:00
parent efbd9588f7
commit 806d08ad4c
2 changed files with 35 additions and 44 deletions

View file

@ -64,7 +64,6 @@ def main():
core.event.trigger('stop') core.event.trigger('stop')
if __name__ == "__main__": if __name__ == "__main__":
main()
try: try:
main() main()
except Exception as e: except Exception as e:

View file

@ -18,68 +18,60 @@ try:
except ImportError: except ImportError:
pass pass
import bumblebee.util no_title = 'n/a'
import bumblebee.input
import bumblebee.output
import bumblebee.engine
from bumblebee.output import scrollable import core.module
import core.widget
import core.decorators
_no_title = 'n/a' import util.format
class Module(bumblebee.engine.Module): class Module(core.module.Module):
"""Window title module.""" def __init__(self, config):
super().__init__(config, [])
def __init__(self, engine, config):
super(Module, self).__init__(
engine,
config
)
# parsing of parameters # parsing of parameters
self._scroll = bumblebee.util.asbool(self.parameter('scroll', False)) self.__scroll = util.format.asbool(self.parameter('scroll', False))
self._max = int(self.parameter('max', 64)) self.__max = int(self.parameter('max', 64))
self._placeholder = self.parameter('placeholder', '...') self.__placeholder = self.parameter('placeholder', '...')
self.__title = ''
# set output of the module # set output of the module
self.widgets(bumblebee.output.Widget(full_text= self.widgets([core.widget.Widget(full_text=
self._scrolling_focused_title if self._scroll else self._focused_title)) self.__scrolling_focused_title if self.__scroll else self.__focused_title)])
# create a connection with i3ipc # create a connection with i3ipc
try: self.__i3 = i3ipc.Connection()
self._i3 = i3ipc.Connection() # event is called both on focus change and title change
# event is called both on focus change and title change self.__i3.on('window', lambda __p_i3, __p_e: self.__pollTitle())
self._i3.on('window', lambda _p_i3, _p_e: self._pollTitle()) # begin listening for events
# begin listening for events threading.Thread(target=self.__i3.main).start()
threading.Thread(target=self._i3.main).start()
except:
pass
# initialize the first title # initialize the first title
self._pollTitle() self.__pollTitle()
def _focused_title(self, widget): def __focused_title(self, widget):
return self._title return self.__title
@scrollable @core.decorators.scrollable
def _scrolling_focused_title(self, widget): def __scrolling_focused_title(self, widget):
return self._full_title return self.__full_title
def _pollTitle(self): def __pollTitle(self):
"""Updating current title.""" """Updating current title."""
try: try:
self._full_title = self._i3.get_tree().find_focused().name self.__full_title = self.__i3.get_tree().find_focused().name
except: except:
self._full_title = _no_title self.__full_title = no_title
if self._full_title is None: if self.__full_title is None:
self._full_title = _no_title self.__full_title = no_title
if not self._scroll: if not self.__scroll:
# cut the text if it is too long # cut the text if it is too long
if len(self._full_title) > self._max: if len(self.__full_title) > self.__max:
self._title = self._full_title[0:self._max - len(self._placeholder)] self.__title = self.__full_title[0:self.__max - len(self.__placeholder)]
self._title = '{}{}'.format(self._title, self._placeholder) self.__title = '{}{}'.format(self.__title, self.__placeholder)
else: else:
self._title = self._full_title self.__title = self.__full_title
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4