From 18509c9f377d1172530ebd743a027b691938d917 Mon Sep 17 00:00:00 2001 From: elParaguayo Date: Mon, 22 Jul 2024 21:39:28 +0100 Subject: [PATCH] Add `visible` option to GroupBox2 Rules now allow ability to hide boxes from the widget. --- CHANGELOG | 1 + qtile_extras/widget/groupbox2.py | 14 +++++++++++++- test/widget/test_groupbox2.py | 27 +++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 3a30df6e..ffd33934 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +2024-07-22: [FEATURE] Add ability to toggle visibility of groups in `GroupBox2` widget 2024-07-18: [BUGFIX] Fix volume widgets bug volume display triggered on opening apps using sound device 2024-07-16: [POST RELEASE] v0.27.0.post1 incorporates bugfix below. Still compatible with v0.27.0 2024-07-16: [BUGFIX] Fix bug where border decorations failed on x11 if user did not have wayland libraries installed diff --git a/qtile_extras/widget/groupbox2.py b/qtile_extras/widget/groupbox2.py index 6c57ce6f..e12852cd 100644 --- a/qtile_extras/widget/groupbox2.py +++ b/qtile_extras/widget/groupbox2.py @@ -117,6 +117,7 @@ class GroupBoxRule: "custom_draw", "text", "box_size", + "visible", ] def __init__( @@ -133,6 +134,7 @@ def __init__( custom_draw: Callable[[Box], None] | None | Sentinel = SENTINEL, text: str | Sentinel | None = SENTINEL, box_size: int | Sentinel | None = SENTINEL, + visible: bool | Sentinel | None = SENTINEL, ): self.text_colour = text_colour self.block_colour = block_colour @@ -146,6 +148,7 @@ def __init__( self.custom_draw = custom_draw self.text = text self.box_size = box_size + self.visible = visible self.screen = ScreenRule.UNSET self.focused: bool | None = None self.occupied: bool | None = None @@ -249,6 +252,7 @@ class Box: custom_draw: Callable[[Box], None] | Sentinel | None text: str | Sentinel | None box_size: int | Sentinel | None + visible: bool | Sentinel | None def __init__(self, group, index, bar, qtile, drawer, config): self.group = group @@ -414,6 +418,9 @@ def size(self) -> int: del self.layout.width self.layout.text = self.text + if self.visible is False: + return 0 + if self.box_size: return self.box_size @@ -585,6 +592,7 @@ class GroupBox2(base._Widget, base.MarginMixin, base.PaddingMixin): * custom_draw - a function that draws to the box * text - string representing text to display * box_size - integer to force the size of the individual box + * visible - boolean to set visibility of box. Box will display by default unless a rule sets ``visible=False`` Whether a rule is applied will depend on whether it meets the relevant conditions set for each rule. A rule can set any combination of the following conditions: @@ -919,6 +927,8 @@ def draw(self): self.drawer.clear(self.background or self.bar.background) offset = 0 for box in self.boxes: + if box.visible is False: + continue box.draw(offset) offset += box.size @@ -975,5 +985,7 @@ def finalize(self): def info(self): info = base._Widget.info(self) - info["text"] = "|".join(box.text if box.text else "" for box in self.boxes) + info["text"] = "|".join( + box.text if box.text else "" for box in self.boxes if box.visible is not False + ) return info diff --git a/test/widget/test_groupbox2.py b/test/widget/test_groupbox2.py index cb9f989f..ca7595e0 100644 --- a/test/widget/test_groupbox2.py +++ b/test/widget/test_groupbox2.py @@ -423,3 +423,30 @@ def test_visible_groups(gbmanager): def test_box_size(gbmanager): info = gbmanager.c.widget["groupbox2"].info() assert info["width"] == 250 + + +@pytest.mark.parametrize( + "gbmanager", + [ + { + "rules": [ + GroupBoxRule(visible=True).when(focused=True), + GroupBoxRule(visible=False).when(occupied=False), + ] + } + ], + indirect=True, +) +def test_box_visibility(gbmanager): + def text(): + return gbmanager.c.widget["groupbox2"].info()["text"] + + assert text() == "a|c" + + gbmanager.c.group["b"].toscreen() + assert text() == "b|c" + + gbmanager.c.group["c"].toscreen() + gbmanager.c.window.kill() + gbmanager.c.group["d"].toscreen() + assert text() == "d"