Skip to content

Commit

Permalink
Make GroupBox2 compatible with vertical bars
Browse files Browse the repository at this point in the history
  • Loading branch information
elParaguayo committed Nov 12, 2024
1 parent 91af5eb commit 99f2ce9
Showing 1 changed file with 40 additions and 13 deletions.
53 changes: 40 additions & 13 deletions qtile_extras/widget/groupbox2.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ class Box:
fontshadow: bool | None
markup: bool
padding_x: int
paddint_y: int
padding_y: int
rules: list[GroupBoxRule]
margin_x: int
margin_y: int
Expand Down Expand Up @@ -402,6 +402,10 @@ def get_image(self, filename: str, scale=True) -> Img | None:
def rule_attrs(self) -> list[str]:
return GroupBoxRule.attrs

@property
def horizontal(self) -> bool:
return self.bar.horizontal

def _reset_format(self) -> None:
"""Clears all formatting for the box."""
# Use ``SENTINEL`` instance as default value so we can allow a value
Expand All @@ -424,16 +428,33 @@ def size(self) -> int:
del self.layout.width
self.layout.text = self.text

if not self.horizontal:
self.layout.width = self.bar.width

if self.visible is False:
return 0

if self.box_size:
return self.box_size

elif self.image and self.image in IMAGE_CACHE:
return IMAGE_CACHE[self.image].width + 2 * self.margin_x
if self.horizontal:
return IMAGE_CACHE[self.image].width + 2 * self.margin_x
else:
return IMAGE_CACHE[self.image].height + 2 * self.margin_y

if self.horizontal:
return self.layout.width + 2 * self.padding_x
else:
return self.layout.height + 2 * self.padding_y

return self.layout.width + 2 * self.padding_x
@property
def bottom(self):
return self.bar.height if self.horizontal else self.size

@property
def right(self):
return self.size if self.horizontal else self.bar.width

@property
def has_block(self) -> bool:
Expand Down Expand Up @@ -499,10 +520,10 @@ def _draw_line(self, offset, vertical=False) -> None:
"""
if vertical:
start = (offset, 0)
end = (0, self.bar.height)
end = (0, self.bottom)
else:
start = (0, offset)
end = (self.size, 0)
end = (self.right, 0)

ctx = self.drawer.ctx
ctx.save()
Expand All @@ -523,13 +544,13 @@ def draw_line(self, line_width: int) -> None:
self._draw_line(line_width // 2)

if self.line_position & LinePosition.BOTTOM:
self._draw_line(self.bar.height - line_width // 2)
self._draw_line(self.bottom - line_width // 2)

if self.line_position & LinePosition.LEFT:
self._draw_line(line_width // 2, vertical=True)

if self.line_position & LinePosition.RIGHT:
self._draw_line(self.size - line_width // 2, vertical=True)
self._draw_line(self.right - line_width // 2, vertical=True)

def draw_image(self) -> None:
"""Draws the image, offset by margin_x and margin_y."""
Expand All @@ -550,13 +571,16 @@ def draw_image(self) -> None:
def draw_text(self) -> None:
"""Draws text, centered vertically."""
self.layout.colour = self.text_colour
self.layout.draw(self.padding_x, (self.bar.height - self.layout.height) // 2)
if self.horizontal:
self.layout.draw(self.padding_x, (self.bar.height - self.layout.height) // 2)
else:
self.layout.draw(0, self.padding_y)

def draw(self, offset) -> None:
"""Main method to draw all formatting."""
self.drawer.ctx.save()

self.drawer.ctx.translate(offset, 0)
self.drawer.ctx.translate(*offset)

if self.has_block:
self.draw_block()
Expand Down Expand Up @@ -828,7 +852,7 @@ def set_label(rule, box):

_experimental = True

orientations = base.ORIENTATION_HORIZONTAL
orientations = base.ORIENTATION_BOTH

defaults: list[tuple[str, Any, str]] = [
(
Expand Down Expand Up @@ -932,6 +956,7 @@ def box_config(self):
"fontshadow",
"markup",
"padding_x",
"padding_y",
"rules",
"margin_x",
"margin_y",
Expand All @@ -946,23 +971,25 @@ def draw(self):
for box in self.boxes:
if box.visible is False:
continue
box.draw(offset)
box.draw((offset, 0) if self.bar.horizontal else (0, offset))
offset += box.size

self.drawer.draw(
offsetx=self.offsetx, offsety=self.offsety, height=self.height, width=self.width
)

def button_press(self, x, y, button):
self.click_pos = x
self.click_pos = x, y
base._Widget.button_press(self, x, y, button)

def get_clicked_group(self):
group = None
offset = 0
for box in self.boxes:
offset += box.size
if self.click_pos <= offset:
if (self.bar.horizontal and self.click_pos[0] <= offset) or (
not self.bar.horizontal and self.click_pos[1] <= offset
):
group = box.group
break
return group
Expand Down

0 comments on commit 99f2ce9

Please sign in to comment.