Skip to content

Commit

Permalink
Make stretch optional for TaskList
Browse files Browse the repository at this point in the history
Adds ability to disable strech behaviour for the TaskList widget. This
is helpful for users who wish to give the widget a background colour but
don't want the widget to fill all available space.

Closes #3854
  • Loading branch information
elParaguayo authored and tych0 committed Feb 9, 2024
1 parent e11aec2 commit b7e647c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
30 changes: 27 additions & 3 deletions libqtile/widget/tasklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
except ImportError:
has_xdg = False

from libqtile import bar, hook, pangocffi
import libqtile.bar
from libqtile import hook, pangocffi
from libqtile.images import Img
from libqtile.log_utils import logger
from libqtile.widget import base
Expand Down Expand Up @@ -178,10 +179,15 @@ class TaskList(base._Widget, base.PaddingMixin, base.MarginMixin):
0,
"The offset given to the window location",
),
(
"stretch",
True,
"Widget fills available space in bar. Set to `False` to limit widget width to size of its contents.",
),
]

def __init__(self, **config):
base._Widget.__init__(self, bar.STRETCH, **config)
base._Widget.__init__(self, libqtile.bar.STRETCH, **config)
self.add_defaults(TaskList.defaults)
self.add_defaults(base.PaddingMixin.defaults)
self.add_defaults(base.MarginMixin.defaults)
Expand Down Expand Up @@ -280,6 +286,12 @@ def windows(self):
]
return self.bar.screen.group.windows

@property
def max_width(self):
width = self.bar.width
width -= sum(w.width for w in self.bar.widgets if w is not self)
return width

def calc_box_widths(self):
"""
Calculate box width for each window in current group.
Expand All @@ -294,7 +306,7 @@ def calc_box_widths(self):
return []

# Determine available and max average width for task name boxes.
width_total = self.width - 2 * self.margin_x - (window_count - 1) * self.spacing
width_total = self.max_width - 2 * self.margin_x - (window_count - 1) * self.spacing
width_avg = width_total / window_count

names = [self.get_taskname(w) for w in windows]
Expand Down Expand Up @@ -338,9 +350,21 @@ def calc_box_widths(self):

return zip(windows, icons, names, width_boxes)

def calculate_length(self):
width = 10
box_widths = [box[3] for box in self.calc_box_widths()]
if box_widths:
width += self.spacing * len(box_widths) - 1
width += sum(w for w in box_widths)

return width

def _configure(self, qtile, bar):
base._Widget._configure(self, qtile, bar)

if not self.stretch:
self.length_type = libqtile.bar.CALCULATED

if not has_xdg and self.theme_mode is not None:
logger.warning("You must install pyxdg to use theme icons.")
self.theme_mode = None
Expand Down
12 changes: 12 additions & 0 deletions test/widgets/test_tasklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,15 @@ def test_tasklist_bad_theme_mode(tasklist_manager, logger):
def test_tasklist_no_xdg(tasklist_manager, logger):
msgs = [rec.msg for rec in logger.get_records("setup")]
assert "You must install pyxdg to use theme icons." in msgs


@configure_tasklist(stretch=False)
def test_tasklist_no_stretch(tasklist_manager):
widget = tasklist_manager.c.widget["tasklist"]
tasklist_manager.test_window("One")
width_one = widget.info()["width"]

tasklist_manager.test_window("Two")
width_two = widget.info()["width"]

assert width_one != width_two

0 comments on commit b7e647c

Please sign in to comment.