From 56a6d2ac6fbe21ff56cce9e50963bbbf9e2f59bc Mon Sep 17 00:00:00 2001 From: 1fadi <1fadi@proton.me> Date: Mon, 17 Apr 2023 17:13:32 +0200 Subject: [PATCH 01/16] created a nice battery --- ibattery.py | 237 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 237 insertions(+) create mode 100644 ibattery.py diff --git a/ibattery.py b/ibattery.py new file mode 100644 index 0000000..e70fa3c --- /dev/null +++ b/ibattery.py @@ -0,0 +1,237 @@ +from libqtile.widget import base +from libqtile.log_utils import logger +from libqtile.utils import send_notification +from libqtile import bar + +import math +import cairocffi as cairo +import psutil + + +class Battery(base._Widget): + """A widget to display a nice battery. + + requirements: psutil + optional: dbus-next (used to send notification). + """ + + orientations = base.ORIENTATION_HORIZONTAL + defaults = [ + ( + "padding", + 4, + "padding on either side of of the widget." + ), + ( + "foreground", + "ffffff", + "Battery color in normal mode." + ), + ( + "charging_fg", + "02a724", + "foreground color when battery is charging." + ), + ( + "update_interval", + 30, + "time to wait until the widgets refreshes." + ), + ( + "low_foreground", + "ff0000", + "change color when battery is low." + ), + ( + "warn_below", + 10, + "battery level to indicate battery is low." + ), + ( + "notify", + False, + "send a notification when battery is low." + ), + ( + "notification_timeout", + 10, + "time in seconds to display notification." + ), + ] + + def __init__(self, **config): + self.widget_width = 36 + base._Widget.__init__(self, bar.CALCULATED, **config) + self.add_defaults(Battery.defaults) + + self.length = self.padding * 2 + self.widget_width + self.HEIGHT = 16 # widgets height + self.BAR_WIDTH = 30 # battery bar + + self._has_notified = False + self.timeout = int(self.notification_timeout * 1000) + + self._foreground = self.foreground + + def _notify(self, percent): + if not self._has_notified: + send_notification( + "Warning", + f"Battery at {percent}%\ncharge me baby!", + urgent=True, + timeout=self.timeout + ) + self._has_notified = True + else: + self._has_notified = False + + def update(self): + percent, charging = self.get_bat() + if self.notify and percent < self.warn_below: + self._notify(percent) + self.configure_(percent, charging) + self.draw_battery(percent, charging) + + def configure_(self, percent, plugged): + if plugged: + self.foreground = self.charging_fg + elif percent <= self.warn_below: + self.foreground = self.low_foreground + else: + self.foreground = self._foreground + + def calculate_length(self): + if self.bar.horizontal: + return (self.padding * 2) + self.widget_width + else: + return 0 + + def draw(self): + percent, charging = self.get_bat() + self.configure_(percent, charging) + self.draw_battery(percent, charging) + + def draw_battery(self, percent, charging): + self.drawer.clear(self.background or self.bar.background) + if self.bar.horizontal: + PERCENT = self.BAR_WIDTH / 100 * percent + y_margin = (self.bar.height - self.HEIGHT) / 2 + + self.drawer.set_source_rgb("8c8c8c") + self._fill_body( + 1, + y_margin, + width=self.BAR_WIDTH, + height=self.HEIGHT, + linewidth=1 + ) + self.drawer.set_source_rgb(self.foreground) + self._border( + 1, + y_margin, + width=self.BAR_WIDTH, + height=self.HEIGHT, + linewidth=2.6 + ) + if percent <= self.warn_below: + self.drawer.set_source_rgb(self.low_foreground) + else: + self.drawer.set_source_rgb(self.foreground if charging else "ff8c1a") + self._fill_body( + 2, + y_margin, + width=PERCENT, + height=self.HEIGHT, + linewidth=1 + ) + self.drawer.set_source_rgb("000000") + self._border( + 1, + y_margin, + width=self.BAR_WIDTH, + height=self.HEIGHT, + linewidth=0.6 + ) + self.drawer.set_source_rgb(self.foreground) + self._fill_body( + self.BAR_WIDTH - 2, + y_margin + 1, + width=8.3, + height=self.HEIGHT - 2, + linewidth=5 + ) + self.drawer.ctx.select_font_face( + "sans", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD + ) + self.drawer.ctx.set_font_size(12) + self.drawer.ctx.move_to(5, self.HEIGHT) + self.drawer.set_source_rgb("ffffff") + self.drawer.ctx.show_text(str(percent)) + + self.drawer.draw( + offsetx=self.offset + self.padding, + offsety=self.offsety, + width=self.length + ) + + def get_bat(self): + battery = psutil.sensors_battery() + try: + plugged = battery.power_plugged + except AttributeError: + return "No battery found." + percent = int(battery.percent) + return (percent, plugged) + + def _rounded_body(self, x, y, width, height, linewidth): + aspect = 0.8 + corner_radius = height / 5.0 + radius = corner_radius / aspect + degrees = math.pi / 180.0 + + self.drawer.ctx.new_sub_path() + + delta = radius + linewidth / 2 + self.drawer.ctx.arc( + x + width - delta, + y + delta, + radius, + -90 * degrees, + 0 * degrees + ) + self.drawer.ctx.arc( + x + width - delta, + y + height - delta, + radius, + 0 * degrees, + 90 * degrees + ) + self.drawer.ctx.arc( + x + delta, + y + height - delta, + radius, + 90 * degrees, + 180 * degrees + ) + self.drawer.ctx.arc( + x + delta, + y + delta, + radius, + 180 * degrees, + 270 * degrees + ) + self.drawer.ctx.close_path() + + def _border(self, x, y, width, height, linewidth): + self._rounded_body(x, y, width, height, linewidth) + self.drawer.ctx.set_line_width(linewidth) + self.drawer.ctx.stroke() + + def _fill_body(self, x, y, width, height, linewidth): + self._rounded_body(x, y, width, height, linewidth) + self.drawer.ctx.fill() + + def timer_setup(self): + self.update() + if self.update_interval is not None: + self.timeout_add(self.update_interval, self.timer_setup) From 8f321829700eab4d3b80502abaa1037bc93955df Mon Sep 17 00:00:00 2001 From: 1fadi <1fadi@proton.me> Date: Mon, 17 Apr 2023 17:17:02 +0200 Subject: [PATCH 02/16] added ibattery to plugins. --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 125b211..72f4e75 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ floating_window_snapping| Floating window borders snap to other window/screen bo [qtile-plasma](https://github.com/numirias/qtile-plasma) | An tree-based layout, very similar to i3 toggle_debug | A command to toggle debug logging with notification tidygroups | Function for moving windows to the leftmost groups (like dwm's `reorganizetags`) +ibattery | A nice battery widget similar to the one in ios 16 ## Other links From 64d23a9d045a916ba4ab76ce004a6cdb62b6b8ae Mon Sep 17 00:00:00 2001 From: 1fadi <1fadi@proton.me> Date: Mon, 17 Apr 2023 21:45:00 +0200 Subject: [PATCH 03/16] Fix: get_bat() raises an exception when no battery found. --- ibattery.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ibattery.py b/ibattery.py index e70fa3c..35ea63d 100644 --- a/ibattery.py +++ b/ibattery.py @@ -179,7 +179,7 @@ def get_bat(self): try: plugged = battery.power_plugged except AttributeError: - return "No battery found." + logger.exception("No Battery was found.") percent = int(battery.percent) return (percent, plugged) From f127205f1faafc7c5a3b65ee7bc7511ed2fdda77 Mon Sep 17 00:00:00 2001 From: 1fadi <1fadi@proton.me> Date: Mon, 17 Apr 2023 21:49:50 +0200 Subject: [PATCH 04/16] Fix: padding is now correctly set. --- ibattery.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ibattery.py b/ibattery.py index 35ea63d..400d91d 100644 --- a/ibattery.py +++ b/ibattery.py @@ -119,7 +119,7 @@ def draw_battery(self, percent, charging): self.drawer.set_source_rgb("8c8c8c") self._fill_body( - 1, + 1 + self.padding, y_margin, width=self.BAR_WIDTH, height=self.HEIGHT, @@ -127,7 +127,7 @@ def draw_battery(self, percent, charging): ) self.drawer.set_source_rgb(self.foreground) self._border( - 1, + 1 + self.padding, y_margin, width=self.BAR_WIDTH, height=self.HEIGHT, @@ -138,7 +138,7 @@ def draw_battery(self, percent, charging): else: self.drawer.set_source_rgb(self.foreground if charging else "ff8c1a") self._fill_body( - 2, + 2 + self.padding, y_margin, width=PERCENT, height=self.HEIGHT, @@ -146,7 +146,7 @@ def draw_battery(self, percent, charging): ) self.drawer.set_source_rgb("000000") self._border( - 1, + 1 + self.padding, y_margin, width=self.BAR_WIDTH, height=self.HEIGHT, @@ -154,7 +154,7 @@ def draw_battery(self, percent, charging): ) self.drawer.set_source_rgb(self.foreground) self._fill_body( - self.BAR_WIDTH - 2, + self.BAR_WIDTH - 2 + self.padding, y_margin + 1, width=8.3, height=self.HEIGHT - 2, @@ -164,12 +164,12 @@ def draw_battery(self, percent, charging): "sans", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD ) self.drawer.ctx.set_font_size(12) - self.drawer.ctx.move_to(5, self.HEIGHT) + self.drawer.ctx.move_to(5 + self.padding, self.HEIGHT) self.drawer.set_source_rgb("ffffff") self.drawer.ctx.show_text(str(percent)) self.drawer.draw( - offsetx=self.offset + self.padding, + offsetx=self.offset, offsety=self.offsety, width=self.length ) From 7c4f94fd097f5685bd2670ca3d73202bc131bfac Mon Sep 17 00:00:00 2001 From: 1fadi <1fadi@proton.me> Date: Mon, 17 Apr 2023 22:13:14 +0200 Subject: [PATCH 05/16] added a configurable option to resize the battery icon. --- ibattery.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/ibattery.py b/ibattery.py index 400d91d..64af833 100644 --- a/ibattery.py +++ b/ibattery.py @@ -57,16 +57,20 @@ class Battery(base._Widget): 10, "time in seconds to display notification." ), + ( + "size", + (16, 30), + "Size of the widget. takes a tuple: (height, width). " + ), ] def __init__(self, **config): - self.widget_width = 36 base._Widget.__init__(self, bar.CALCULATED, **config) self.add_defaults(Battery.defaults) + self.HEIGHT, self.BAR_WIDTH = self.size # battery bar + self.widget_width = self.BAR_WIDTH + 6 self.length = self.padding * 2 + self.widget_width - self.HEIGHT = 16 # widgets height - self.BAR_WIDTH = 30 # battery bar self._has_notified = False self.timeout = int(self.notification_timeout * 1000) From 7b3ff1ba34458924dcca58a2f0a806a2c6948e97 Mon Sep 17 00:00:00 2001 From: 1fadi <1fadi@proton.me> Date: Mon, 17 Apr 2023 23:48:18 +0200 Subject: [PATCH 06/16] fontsize is now changeable. --- ibattery.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/ibattery.py b/ibattery.py index 64af833..e7100ac 100644 --- a/ibattery.py +++ b/ibattery.py @@ -62,6 +62,11 @@ class Battery(base._Widget): (16, 30), "Size of the widget. takes a tuple: (height, width). " ), + ( + "font_size", + 12, + "font size of the numbers inside the battery." + ), ] def __init__(self, **config): @@ -144,7 +149,7 @@ def draw_battery(self, percent, charging): self._fill_body( 2 + self.padding, y_margin, - width=PERCENT, + width=max(PERCENT, self.BAR_WIDTH / 100 * 10), height=self.HEIGHT, linewidth=1 ) @@ -167,10 +172,16 @@ def draw_battery(self, percent, charging): self.drawer.ctx.select_font_face( "sans", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD ) - self.drawer.ctx.set_font_size(12) - self.drawer.ctx.move_to(5 + self.padding, self.HEIGHT) + bar_center = self.BAR_WIDTH / 2 + text = str(percent) + self.drawer.ctx.set_font_size(self.font_size) + (x, y, width, height, dx, dy) = self.drawer.ctx.text_extents(text) + self.drawer.ctx.move_to( + bar_center - 10 + self.padding, + (self.bar.height + height) / 2 + ) self.drawer.set_source_rgb("ffffff") - self.drawer.ctx.show_text(str(percent)) + self.drawer.ctx.show_text(text) self.drawer.draw( offsetx=self.offset, From ecd05f1315f03244fdb656c8654ce804eb7f9365 Mon Sep 17 00:00:00 2001 From: 1fadi <1fadi@proton.me> Date: Tue, 18 Apr 2023 00:02:28 +0200 Subject: [PATCH 07/16] added docstring to the module. --- ibattery.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/ibattery.py b/ibattery.py index e7100ac..f8853f4 100644 --- a/ibattery.py +++ b/ibattery.py @@ -1,3 +1,20 @@ +""" +a widget that displays a nice battery icon using cairo +it is similiar to the one in ios 16. + +Use: + 1. import it to your qtile config.py as follows: + from ibattery import Battery as MyBattery + + 2. add it to your widget list in your bar: + MyBattery(), + +requirements: psutil (used to gather info about the battery.) +optional: dbus-next (used to send notification.) +""" + + + from libqtile.widget import base from libqtile.log_utils import logger from libqtile.utils import send_notification From 4f2f6532296e3e6cc13d56f34ec4b5a268cab055 Mon Sep 17 00:00:00 2001 From: 1fadi <1fadi@proton.me> Date: Tue, 18 Apr 2023 00:05:19 +0200 Subject: [PATCH 08/16] deleted empty lines. --- ibattery.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/ibattery.py b/ibattery.py index f8853f4..9f437f1 100644 --- a/ibattery.py +++ b/ibattery.py @@ -13,8 +13,6 @@ optional: dbus-next (used to send notification.) """ - - from libqtile.widget import base from libqtile.log_utils import logger from libqtile.utils import send_notification From 39b417eedb87a22e9418004777593a1fee9c0c62 Mon Sep 17 00:00:00 2001 From: 1fadi <1fadi@proton.me> Date: Wed, 19 Apr 2023 11:54:53 +0200 Subject: [PATCH 09/16] improve: the battery's positive term is now sharper. --- ibattery.py | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/ibattery.py b/ibattery.py index 9f437f1..5e53308 100644 --- a/ibattery.py +++ b/ibattery.py @@ -147,7 +147,8 @@ def draw_battery(self, percent, charging): y_margin, width=self.BAR_WIDTH, height=self.HEIGHT, - linewidth=1 + linewidth=1, + aspect=0.8 ) self.drawer.set_source_rgb(self.foreground) self._border( @@ -155,7 +156,8 @@ def draw_battery(self, percent, charging): y_margin, width=self.BAR_WIDTH, height=self.HEIGHT, - linewidth=2.6 + linewidth=2.6, + aspect=0.8 ) if percent <= self.warn_below: self.drawer.set_source_rgb(self.low_foreground) @@ -166,7 +168,8 @@ def draw_battery(self, percent, charging): y_margin, width=max(PERCENT, self.BAR_WIDTH / 100 * 10), height=self.HEIGHT, - linewidth=1 + linewidth=1, + aspect=0.8 ) self.drawer.set_source_rgb("000000") self._border( @@ -174,15 +177,17 @@ def draw_battery(self, percent, charging): y_margin, width=self.BAR_WIDTH, height=self.HEIGHT, - linewidth=0.6 + linewidth=0.6, + aspect=0.8 ) self.drawer.set_source_rgb(self.foreground) self._fill_body( self.BAR_WIDTH - 2 + self.padding, - y_margin + 1, - width=8.3, - height=self.HEIGHT - 2, - linewidth=5 + y_margin + 1.5, + width=7.5, + height=self.HEIGHT - 3, + linewidth=5, + aspect=5.0 ) self.drawer.ctx.select_font_face( "sans", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD @@ -213,8 +218,8 @@ def get_bat(self): percent = int(battery.percent) return (percent, plugged) - def _rounded_body(self, x, y, width, height, linewidth): - aspect = 0.8 + def _rounded_body(self, x, y, width, height, linewidth, aspect): + aspect = aspect corner_radius = height / 5.0 radius = corner_radius / aspect degrees = math.pi / 180.0 @@ -252,13 +257,13 @@ def _rounded_body(self, x, y, width, height, linewidth): ) self.drawer.ctx.close_path() - def _border(self, x, y, width, height, linewidth): - self._rounded_body(x, y, width, height, linewidth) + def _border(self, x, y, width, height, linewidth, aspect): + self._rounded_body(x, y, width, height, linewidth, aspect) self.drawer.ctx.set_line_width(linewidth) self.drawer.ctx.stroke() - def _fill_body(self, x, y, width, height, linewidth): - self._rounded_body(x, y, width, height, linewidth) + def _fill_body(self, x, y, width, height, linewidth, aspect): + self._rounded_body(x, y, width, height, linewidth, aspect) self.drawer.ctx.fill() def timer_setup(self): From 8f1af0526d0664e72c27824665a9b59ff449cf34 Mon Sep 17 00:00:00 2001 From: 1fadi <1fadi@proton.me> Date: Wed, 19 Apr 2023 20:05:53 +0200 Subject: [PATCH 10/16] Fix: battery bar changes color if charging when low. --- ibattery.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ibattery.py b/ibattery.py index 5e53308..0accd14 100644 --- a/ibattery.py +++ b/ibattery.py @@ -159,10 +159,10 @@ def draw_battery(self, percent, charging): linewidth=2.6, aspect=0.8 ) - if percent <= self.warn_below: - self.drawer.set_source_rgb(self.low_foreground) + if charging or percent <= self.warn_below: + self.drawer.set_source_rgb(self.foreground) else: - self.drawer.set_source_rgb(self.foreground if charging else "ff8c1a") + self.drawer.set_source_rgb("ff8c1a") self._fill_body( 2 + self.padding, y_margin, From f0eeb57dc4eb4033564e1394e42d9c1bd5490965 Mon Sep 17 00:00:00 2001 From: 1fadi <1fadi@proton.me> Date: Wed, 19 Apr 2023 21:40:12 +0200 Subject: [PATCH 11/16] Fix: icon's x-axis and text center. --- ibattery.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ibattery.py b/ibattery.py index 0accd14..dcfc0da 100644 --- a/ibattery.py +++ b/ibattery.py @@ -143,7 +143,7 @@ def draw_battery(self, percent, charging): self.drawer.set_source_rgb("8c8c8c") self._fill_body( - 1 + self.padding, + self.padding, y_margin, width=self.BAR_WIDTH, height=self.HEIGHT, @@ -152,7 +152,7 @@ def draw_battery(self, percent, charging): ) self.drawer.set_source_rgb(self.foreground) self._border( - 1 + self.padding, + self.padding, y_margin, width=self.BAR_WIDTH, height=self.HEIGHT, @@ -164,7 +164,7 @@ def draw_battery(self, percent, charging): else: self.drawer.set_source_rgb("ff8c1a") self._fill_body( - 2 + self.padding, + self.padding, y_margin, width=max(PERCENT, self.BAR_WIDTH / 100 * 10), height=self.HEIGHT, @@ -173,7 +173,7 @@ def draw_battery(self, percent, charging): ) self.drawer.set_source_rgb("000000") self._border( - 1 + self.padding, + self.padding, y_margin, width=self.BAR_WIDTH, height=self.HEIGHT, @@ -182,7 +182,7 @@ def draw_battery(self, percent, charging): ) self.drawer.set_source_rgb(self.foreground) self._fill_body( - self.BAR_WIDTH - 2 + self.padding, + self.BAR_WIDTH - 3 + self.padding, y_margin + 1.5, width=7.5, height=self.HEIGHT - 3, @@ -192,12 +192,12 @@ def draw_battery(self, percent, charging): self.drawer.ctx.select_font_face( "sans", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD ) - bar_center = self.BAR_WIDTH / 2 + text_center = (self.BAR_WIDTH / 2) - (self.font_size / 2) - 4 text = str(percent) self.drawer.ctx.set_font_size(self.font_size) (x, y, width, height, dx, dy) = self.drawer.ctx.text_extents(text) self.drawer.ctx.move_to( - bar_center - 10 + self.padding, + text_center + self.padding, (self.bar.height + height) / 2 ) self.drawer.set_source_rgb("ffffff") From fc386e524f3385baee5d72e688935a10155d4aab Mon Sep 17 00:00:00 2001 From: 1fadi <1fadi@proton.me> Date: Thu, 20 Apr 2023 19:39:25 +0200 Subject: [PATCH 12/16] added some final confiuration options. --- ibattery.py | 96 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 58 insertions(+), 38 deletions(-) diff --git a/ibattery.py b/ibattery.py index dcfc0da..c31902d 100644 --- a/ibattery.py +++ b/ibattery.py @@ -35,52 +35,67 @@ class Battery(base._Widget): ( "padding", 4, - "padding on either side of of the widget." + "int. padding on either side of of the widget." ), ( "foreground", - "ffffff", - "Battery color in normal mode." + "d5d5d5", + "string. Battery color in normal mode." ), ( "charging_fg", "02a724", - "foreground color when battery is charging." + "string. foreground color when battery is charging." ), ( "update_interval", - 30, - "time to wait until the widgets refreshes." + 20, + "int. time to wait until the widgets refreshes." ), ( "low_foreground", "ff0000", - "change color when battery is low." + "string. change color when battery is low." ), ( "warn_below", 10, - "battery level to indicate battery is low." + "int. battery level to indicate battery is low." ), ( "notify", False, - "send a notification when battery is low." + "bool. send a notification when battery is low." ), ( "notification_timeout", 10, - "time in seconds to display notification." + "int. time in seconds to display notification." ), ( "size", - (16, 30), - "Size of the widget. takes a tuple: (height, width). " + (18, 35), + "Size of the widget. takes a tuple: (height:int, width:int). " + ), + ( + "font_family", + "sans", + "string. font family for the numbers inside the battery icon." ), ( "font_size", - 12, - "font size of the numbers inside the battery." + 15, + "int. font size of the numbers inside the battery." + ), + ( + "font_color", + None, + "string. font color" + ), + ( + "battery_border", + False, + "bool. add a border to the battery icon." ), ] @@ -95,13 +110,13 @@ def __init__(self, **config): self._has_notified = False self.timeout = int(self.notification_timeout * 1000) - self._foreground = self.foreground + self._foreground = self.foreground if self.foreground else "d5d5d5" def _notify(self, percent): if not self._has_notified: send_notification( - "Warning", - f"Battery at {percent}%\ncharge me baby!", + "LOW BATTERY", + f"Battery at {percent}%", urgent=True, timeout=self.timeout ) @@ -111,15 +126,15 @@ def _notify(self, percent): def update(self): percent, charging = self.get_bat() - if self.notify and percent < self.warn_below: + if self.notify and percent < self.warn_below and not charging: self._notify(percent) self.configure_(percent, charging) self.draw_battery(percent, charging) def configure_(self, percent, plugged): if plugged: - self.foreground = self.charging_fg - elif percent <= self.warn_below: + self.foreground = self.charging_fg or self._foreground + elif percent <= self.warn_below and self.low_foreground: self.foreground = self.low_foreground else: self.foreground = self._foreground @@ -138,10 +153,11 @@ def draw(self): def draw_battery(self, percent, charging): self.drawer.clear(self.background or self.bar.background) if self.bar.horizontal: + percent = 30 PERCENT = self.BAR_WIDTH / 100 * percent y_margin = (self.bar.height - self.HEIGHT) / 2 - self.drawer.set_source_rgb("8c8c8c") + self.rgb("808080") self._fill_body( self.padding, y_margin, @@ -150,19 +166,17 @@ def draw_battery(self, percent, charging): linewidth=1, aspect=0.8 ) - self.drawer.set_source_rgb(self.foreground) - self._border( - self.padding, - y_margin, - width=self.BAR_WIDTH, - height=self.HEIGHT, - linewidth=2.6, - aspect=0.8 - ) - if charging or percent <= self.warn_below: - self.drawer.set_source_rgb(self.foreground) - else: - self.drawer.set_source_rgb("ff8c1a") + if self.battery_border: + self.rgb(self.foreground) + self._border( + self.padding, + y_margin, + width=self.BAR_WIDTH, + height=self.HEIGHT, + linewidth=2.6, + aspect=0.8 + ) + self.rgb(self.foreground) self._fill_body( self.padding, y_margin, @@ -171,7 +185,7 @@ def draw_battery(self, percent, charging): linewidth=1, aspect=0.8 ) - self.drawer.set_source_rgb("000000") + self.rgb("000000") self._border( self.padding, y_margin, @@ -180,7 +194,10 @@ def draw_battery(self, percent, charging): linewidth=0.6, aspect=0.8 ) - self.drawer.set_source_rgb(self.foreground) + if self.battery_border: + self.rgb(self.foreground) + else: + self.rgb("808080") self._fill_body( self.BAR_WIDTH - 3 + self.padding, y_margin + 1.5, @@ -190,7 +207,7 @@ def draw_battery(self, percent, charging): aspect=5.0 ) self.drawer.ctx.select_font_face( - "sans", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD + self.font_family, cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD ) text_center = (self.BAR_WIDTH / 2) - (self.font_size / 2) - 4 text = str(percent) @@ -200,7 +217,7 @@ def draw_battery(self, percent, charging): text_center + self.padding, (self.bar.height + height) / 2 ) - self.drawer.set_source_rgb("ffffff") + self.rgb(self.font_color or self.bar.background) self.drawer.ctx.show_text(text) self.drawer.draw( @@ -209,6 +226,9 @@ def draw_battery(self, percent, charging): width=self.length ) + def rgb(self, hex): + self.drawer.set_source_rgb(hex) + def get_bat(self): battery = psutil.sensors_battery() try: From 5c588b3590e50665e6c5354b39b6eb2cf4b9868d Mon Sep 17 00:00:00 2001 From: 1fadi <1fadi@proton.me> Date: Thu, 20 Apr 2023 19:42:12 +0200 Subject: [PATCH 13/16] this line was for debugging. --- ibattery.py | 1 - 1 file changed, 1 deletion(-) diff --git a/ibattery.py b/ibattery.py index c31902d..2b559dc 100644 --- a/ibattery.py +++ b/ibattery.py @@ -153,7 +153,6 @@ def draw(self): def draw_battery(self, percent, charging): self.drawer.clear(self.background or self.bar.background) if self.bar.horizontal: - percent = 30 PERCENT = self.BAR_WIDTH / 100 * percent y_margin = (self.bar.height - self.HEIGHT) / 2 From d88b00f2118edc6b001aa5288f7052073935fff4 Mon Sep 17 00:00:00 2001 From: 1fadi <1fadi@proton.me> Date: Sat, 29 Apr 2023 23:08:30 +0200 Subject: [PATCH 14/16] Fix text centering and margin --- ibattery.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/ibattery.py b/ibattery.py index 2b559dc..5065b0f 100644 --- a/ibattery.py +++ b/ibattery.py @@ -34,7 +34,7 @@ class Battery(base._Widget): defaults = [ ( "padding", - 4, + 2, "int. padding on either side of of the widget." ), ( @@ -104,8 +104,8 @@ def __init__(self, **config): self.add_defaults(Battery.defaults) self.HEIGHT, self.BAR_WIDTH = self.size # battery bar - self.widget_width = self.BAR_WIDTH + 6 - self.length = self.padding * 2 + self.widget_width + self.margin = 2 + self.length = self.padding * 2 + self.BAR_WIDTH + 7.5 + self.margin * 2 self._has_notified = False self.timeout = int(self.notification_timeout * 1000) @@ -141,7 +141,7 @@ def configure_(self, percent, plugged): def calculate_length(self): if self.bar.horizontal: - return (self.padding * 2) + self.widget_width + return self.padding * 2 + self.BAR_WIDTH + 7.5 + self.margin * 2 else: return 0 @@ -155,10 +155,11 @@ def draw_battery(self, percent, charging): if self.bar.horizontal: PERCENT = self.BAR_WIDTH / 100 * percent y_margin = (self.bar.height - self.HEIGHT) / 2 + mp = self.padding + self.margin self.rgb("808080") self._fill_body( - self.padding, + mp, y_margin, width=self.BAR_WIDTH, height=self.HEIGHT, @@ -168,7 +169,7 @@ def draw_battery(self, percent, charging): if self.battery_border: self.rgb(self.foreground) self._border( - self.padding, + mp, y_margin, width=self.BAR_WIDTH, height=self.HEIGHT, @@ -177,7 +178,7 @@ def draw_battery(self, percent, charging): ) self.rgb(self.foreground) self._fill_body( - self.padding, + mp, y_margin, width=max(PERCENT, self.BAR_WIDTH / 100 * 10), height=self.HEIGHT, @@ -186,7 +187,7 @@ def draw_battery(self, percent, charging): ) self.rgb("000000") self._border( - self.padding, + mp, y_margin, width=self.BAR_WIDTH, height=self.HEIGHT, @@ -198,7 +199,7 @@ def draw_battery(self, percent, charging): else: self.rgb("808080") self._fill_body( - self.BAR_WIDTH - 3 + self.padding, + self.BAR_WIDTH - 3 + mp, y_margin + 1.5, width=7.5, height=self.HEIGHT - 3, @@ -208,12 +209,12 @@ def draw_battery(self, percent, charging): self.drawer.ctx.select_font_face( self.font_family, cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD ) - text_center = (self.BAR_WIDTH / 2) - (self.font_size / 2) - 4 text = str(percent) self.drawer.ctx.set_font_size(self.font_size) (x, y, width, height, dx, dy) = self.drawer.ctx.text_extents(text) + text_x = (self.length - 7.5 - width) / 2 - x self.drawer.ctx.move_to( - text_center + self.padding, + text_x, (self.bar.height + height) / 2 ) self.rgb(self.font_color or self.bar.background) From a4966e39d1120dff027daf580f5976cf0208ae69 Mon Sep 17 00:00:00 2001 From: 1fadi <1fadi@proton.me> Date: Sun, 7 May 2023 21:45:13 +0200 Subject: [PATCH 15/16] Fix padding while in initialization process --- ibattery.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ibattery.py b/ibattery.py index 5065b0f..8d13f3e 100644 --- a/ibattery.py +++ b/ibattery.py @@ -99,11 +99,12 @@ class Battery(base._Widget): ), ] - def __init__(self, **config): + def __init__(self, padding=None, **config): base._Widget.__init__(self, bar.CALCULATED, **config) self.add_defaults(Battery.defaults) self.HEIGHT, self.BAR_WIDTH = self.size # battery bar + self.padding = padding if padding else 2 self.margin = 2 self.length = self.padding * 2 + self.BAR_WIDTH + 7.5 + self.margin * 2 From 1c7d32614578bc5ca997af5dcaad3e7df841ad33 Mon Sep 17 00:00:00 2001 From: 1fadi <1fadi@proton.me> Date: Sun, 21 May 2023 21:51:32 +0200 Subject: [PATCH 16/16] Fix an issue when the default padding from widgets_defaults is passed --- ibattery.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/ibattery.py b/ibattery.py index 8d13f3e..8b3c6b8 100644 --- a/ibattery.py +++ b/ibattery.py @@ -99,18 +99,13 @@ class Battery(base._Widget): ), ] - def __init__(self, padding=None, **config): + def __init__(self, **config): base._Widget.__init__(self, bar.CALCULATED, **config) self.add_defaults(Battery.defaults) - self.HEIGHT, self.BAR_WIDTH = self.size # battery bar - self.padding = padding if padding else 2 self.margin = 2 - self.length = self.padding * 2 + self.BAR_WIDTH + 7.5 + self.margin * 2 - self._has_notified = False self.timeout = int(self.notification_timeout * 1000) - self._foreground = self.foreground if self.foreground else "d5d5d5" def _notify(self, percent):