Skip to content

Commit

Permalink
Add visible option to GroupBox2
Browse files Browse the repository at this point in the history
Rules now allow ability to hide boxes from the widget.
  • Loading branch information
elParaguayo committed Jul 22, 2024
1 parent 774f8d1 commit b068bd3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 13 additions & 1 deletion qtile_extras/widget/groupbox2.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class GroupBoxRule:
"custom_draw",
"text",
"box_size",
"visible",
]

def __init__(
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
27 changes: 27 additions & 0 deletions test/widget/test_groupbox2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit b068bd3

Please sign in to comment.