Skip to content

Commit

Permalink
Add padding to BatteryIcon
Browse files Browse the repository at this point in the history
The existing padding was broken on `BatteryIcon`. It was 0 when the
images were loaded/resized and is then set (to a fixed value)
afterwards. However, this value was never used when caluclating the
widget's width or the icon's position.

This PR replaces the old method with a new, user-defined `padding`
parameter.
  • Loading branch information
elParaguayo authored and tych0 committed Mar 15, 2024
1 parent c4d3c6e commit d605e88
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions libqtile/widget/battery.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ class BatteryIcon(base._Widget):
("update_interval", 60, "Seconds between status updates"),
("theme_path", default_icon_path(), "Path of the icons"),
("scale", 1, "Scale factor relative to the bar height. " "Defaults to 1"),
("padding", 0, "Additional padding either side of the icon"),
]

icon_names = (
Expand Down Expand Up @@ -626,8 +627,6 @@ def __init__(self, **config) -> None:
self.add_defaults(self.defaults)
self.scale: float = 1.0 / self.scale

self.length_type = bar.STATIC
self.length = 0
self.image_padding = 0
self.images: dict[str, Img] = {}
self.current_icon = "battery-missing"
Expand All @@ -649,20 +648,23 @@ def timer_setup(self) -> None:

def _configure(self, qtile, bar) -> None:
base._Widget._configure(self, qtile, bar)
self.image_padding = 0
self.setup_images()
self.image_padding = (self.bar.height - self.bar.height / 5) / 2

def setup_images(self) -> None:
d_imgs = images.Loader(self.theme_path)(*self.icon_names)

new_height = self.bar.height * self.scale - self.image_padding
new_height = self.bar.height * self.scale
for key, img in d_imgs.items():
img.resize(height=new_height)
if img.width > self.length:
self.length = int(img.width + self.image_padding * 2)
self.images[key] = img

def calculate_length(self):
if not self.images:
return 0

icon = self.images[self.current_icon]
return icon.width + 2 * self.padding

def update(self) -> None:
status = self._battery.update_status()
icon = self._get_icon_key(status)
Expand All @@ -674,7 +676,7 @@ def draw(self) -> None:
self.drawer.clear(self.background or self.bar.background)
image = self.images[self.current_icon]
self.drawer.ctx.save()
self.drawer.ctx.translate(0, (self.bar.height - image.height) // 2)
self.drawer.ctx.translate(self.padding, (self.bar.height - image.height) // 2)
self.drawer.ctx.set_source(image.pattern)
self.drawer.ctx.paint()
self.drawer.ctx.restore()
Expand Down

0 comments on commit d605e88

Please sign in to comment.