Merge pull request #429 from adario7/master

performance improvements on module 'title'
This commit is contained in:
tobi-wan-kenobi 2019-08-24 19:15:02 +02:00 committed by GitHub
commit 9b7155b459
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 28 deletions

View file

@ -63,7 +63,7 @@ class Module(object):
def widgets(self, widgets=None): def widgets(self, widgets=None):
"""Return the widgets to draw for this module""" """Return the widgets to draw for this module"""
if widgets: if widgets:
self._widgets = widgets self._widgets = widgets if isinstance(widgets, list) else [widgets]
return self._widgets return self._widgets
def hidden(self): def hidden(self):

View file

@ -11,6 +11,8 @@ Parameters:
* title.scroll : Boolean flag for scrolling title. Defaults to False * title.scroll : Boolean flag for scrolling title. Defaults to False
""" """
import threading
try: try:
import i3ipc import i3ipc
except ImportError: except ImportError:
@ -23,7 +25,7 @@ import bumblebee.engine
from bumblebee.output import scrollable from bumblebee.output import scrollable
no_title = "n/a" _no_title = "n/a"
class Module(bumblebee.engine.Module): class Module(bumblebee.engine.Module):
"""Window title module.""" """Window title module."""
@ -31,43 +33,53 @@ class Module(bumblebee.engine.Module):
def __init__(self, engine, config): def __init__(self, engine, config):
super(Module, self).__init__( super(Module, self).__init__(
engine, engine,
config, config
bumblebee.output.Widget(full_text=self.get_title)
) )
# parsing of parameters
self._scroll = bumblebee.util.asbool(self.parameter("scroll", False))
self._max = int(self.parameter("max", 64))
self._placeholder = self.parameter("placeholder", "...")
# set output of the module
self.widgets(bumblebee.output.Widget(full_text=
self._scrolling_focused_title if self._scroll else self._focused_title))
# create a connection with i3ipc
try: try:
self._i3 = i3ipc.Connection() self._i3 = i3ipc.Connection()
self._full_title = self._i3.get_tree().find_focused().name # event is called both on focus change and title change
except Exception: self._i3.on("window", lambda _p_i3, _p_e: self._pollTitle())
self._full_title = no_title # begin listening for events
threading.Thread(target=self._i3.main).start()
except:
pass
def get_title(self, widget): # initialize the first title
if bumblebee.util.asbool(self.parameter("scroll", False)): self._pollTitle()
return self.scrolling_focused_title(widget)
else:
return self.focused_title(widget)
def focused_title(self, widget): def _focused_title(self, widget):
max = int(self.parameter("max", 64)) return self._title
title = self._full_title[0:max]
placeholder = self.parameter("placeholder", "...")
if title != self._full_title:
title = self._full_title[0:max - len(placeholder)]
title = "{}{}".format(title, placeholder)
return title
@scrollable @scrollable
def scrolling_focused_title(self, widget): def _scrolling_focused_title(self, widget):
return self._full_title return self._full_title
def update(self, widgets): def _pollTitle(self):
"""Update 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 Exception: 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:
# cut the text if it is too long
if len(self._full_title) > self._max:
self._title = self._full_title[0:self._max - len(self._placeholder)]
self._title = "{}{}".format(self._title, self._placeholder)
else:
self._title = self._full_title
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4