From 5ae4e475b6b64884a3bea6b898e07276a90bb507 Mon Sep 17 00:00:00 2001 From: Alfredo Monclus Date: Wed, 9 Oct 2024 17:34:33 -0300 Subject: [PATCH] feat: battery status (#1457) * Initial commit, added psutil for getting tablet/laptop battery percentage, initial code for getting the values and displaying the icon * add battery svgs for light theme * added light styled battery svgs for dark themes * update icon based on battery percentage and if device is charging * indentation fix, and forgot to add battery plugged checks for 33% and 66% battery levels * move battery polling to its own timer now that i understand how to add an additional timer, checks every 30 seconds. check for if show battery is on or off * add toggle to config for settings * show battery callback as screen.restart_ks instead of just reloading panels * move self.battery_percentage() to end of reload_icons this ensures that when a theme is changed the icon is updated instead of waiting for the timeout to update the icon again * remove left over commented out code * only call self.battery_percentage when config is true * adjust tooltip message * split creatation of elements from updating elements * change print to logging.debug, handle rouding for percentage in the f-string * remove code that was moved to `attery_init()`, only destroy icon element when updating icons/percentage text, remove redundant calls to `.pack_start` (just call it once after setting the icon). * only create battery_update timer if config setting is true * new material icons remove old icons * `self.battery_init() in `__init__` def * fix: lint * refactors * style: update icons to follow themes * refactor: remove config, make the show/hide automatic * style: bigger battery icons for z-bolt and colorized --------- Co-authored-by: MrTylerjet --- panels/base_panel.py | 63 +++++++++++++- scripts/KlipperScreen-requirements.txt | 1 + styles/colorized/images/battery-0.svg | 68 +++++++++++++++ styles/colorized/images/battery-100.svg | 75 ++++++++++++++++ styles/colorized/images/battery-25.svg | 86 +++++++++++++++++++ styles/colorized/images/battery-50.svg | 81 +++++++++++++++++ styles/colorized/images/battery-75.svg | 86 +++++++++++++++++++ styles/colorized/images/battery-charging.svg | 83 ++++++++++++++++++ styles/colorized/images/battery-unknown.svg | 84 ++++++++++++++++++ styles/material-dark/images/battery-0.svg | 1 + styles/material-dark/images/battery-100.svg | 1 + styles/material-dark/images/battery-25.svg | 1 + styles/material-dark/images/battery-50.svg | 1 + styles/material-dark/images/battery-75.svg | 1 + .../material-dark/images/battery-charging.svg | 1 + .../material-dark/images/battery-unknown.svg | 1 + styles/material-darker/images/battery-0.svg | 1 + styles/material-darker/images/battery-100.svg | 1 + styles/material-darker/images/battery-25.svg | 1 + styles/material-darker/images/battery-50.svg | 1 + styles/material-darker/images/battery-75.svg | 1 + .../images/battery-charging.svg | 1 + .../images/battery-unknown.svg | 1 + styles/material-light/images/battery-0.svg | 40 +++++++++ styles/material-light/images/battery-100.svg | 40 +++++++++ styles/material-light/images/battery-25.svg | 40 +++++++++ styles/material-light/images/battery-50.svg | 40 +++++++++ styles/material-light/images/battery-75.svg | 40 +++++++++ .../images/battery-charging.svg | 40 +++++++++ .../material-light/images/battery-unknown.svg | 40 +++++++++ styles/z-bolt/images/battery-0.svg | 68 +++++++++++++++ styles/z-bolt/images/battery-100.svg | 68 +++++++++++++++ styles/z-bolt/images/battery-25.svg | 68 +++++++++++++++ styles/z-bolt/images/battery-50.svg | 68 +++++++++++++++ styles/z-bolt/images/battery-75.svg | 68 +++++++++++++++ styles/z-bolt/images/battery-charging.svg | 72 ++++++++++++++++ styles/z-bolt/images/battery-unknown.svg | 73 ++++++++++++++++ 37 files changed, 1405 insertions(+), 1 deletion(-) create mode 100644 styles/colorized/images/battery-0.svg create mode 100644 styles/colorized/images/battery-100.svg create mode 100644 styles/colorized/images/battery-25.svg create mode 100644 styles/colorized/images/battery-50.svg create mode 100644 styles/colorized/images/battery-75.svg create mode 100644 styles/colorized/images/battery-charging.svg create mode 100644 styles/colorized/images/battery-unknown.svg create mode 100644 styles/material-dark/images/battery-0.svg create mode 100644 styles/material-dark/images/battery-100.svg create mode 100644 styles/material-dark/images/battery-25.svg create mode 100644 styles/material-dark/images/battery-50.svg create mode 100644 styles/material-dark/images/battery-75.svg create mode 100644 styles/material-dark/images/battery-charging.svg create mode 100644 styles/material-dark/images/battery-unknown.svg create mode 100644 styles/material-darker/images/battery-0.svg create mode 100644 styles/material-darker/images/battery-100.svg create mode 100644 styles/material-darker/images/battery-25.svg create mode 100644 styles/material-darker/images/battery-50.svg create mode 100644 styles/material-darker/images/battery-75.svg create mode 100644 styles/material-darker/images/battery-charging.svg create mode 100644 styles/material-darker/images/battery-unknown.svg create mode 100644 styles/material-light/images/battery-0.svg create mode 100644 styles/material-light/images/battery-100.svg create mode 100644 styles/material-light/images/battery-25.svg create mode 100644 styles/material-light/images/battery-50.svg create mode 100644 styles/material-light/images/battery-75.svg create mode 100644 styles/material-light/images/battery-charging.svg create mode 100644 styles/material-light/images/battery-unknown.svg create mode 100644 styles/z-bolt/images/battery-0.svg create mode 100644 styles/z-bolt/images/battery-100.svg create mode 100644 styles/z-bolt/images/battery-25.svg create mode 100644 styles/z-bolt/images/battery-50.svg create mode 100644 styles/z-bolt/images/battery-75.svg create mode 100644 styles/z-bolt/images/battery-charging.svg create mode 100644 styles/z-bolt/images/battery-unknown.svg diff --git a/panels/base_panel.py b/panels/base_panel.py index 1e8c17d1..8ca0b0cb 100644 --- a/panels/base_panel.py +++ b/panels/base_panel.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- import logging - +import psutil import gi gi.require_version("Gtk", "3.0") @@ -18,6 +18,7 @@ class BasePanel(ScreenPanel): self.time_min = -1 self.time_format = self._config.get_main_config().getboolean("24htime", True) self.time_update = None + self.battery_update = None self.titlebar_items = [] self.titlebar_name_type = None self.current_extruder = None @@ -85,11 +86,23 @@ class BasePanel(ScreenPanel): self.control['time_box'] = Gtk.Box(halign=Gtk.Align.END) self.control['time_box'].pack_end(self.control['time'], True, True, 10) + self.battery_icons = self.load_battery_icons() + self.labels['battery'] = Gtk.Label() + self.labels['battery_icon'] = self._gtk.Image() + self.labels['battery_icon'].set_from_pixbuf(self.battery_icons['unknown']) + self.control['battery_box'] = Gtk.Box(halign=Gtk.Align.END) + self.control['battery_box'].set_no_show_all(True) + self.control['battery_box'].add(self.labels['battery']) + self.control['battery_box'].add(self.labels['battery_icon']) + for widget in self.control['battery_box']: + widget.show() + self.titlebar = Gtk.Box(spacing=5, valign=Gtk.Align.CENTER) self.titlebar.get_style_context().add_class("title_bar") self.titlebar.add(self.control['temp_box']) self.titlebar.add(self.titlelbl) self.titlebar.add(self.control['time_box']) + self.titlebar.add(self.control['battery_box']) self.set_title(title) # Main layout @@ -108,6 +121,18 @@ class BasePanel(ScreenPanel): self.update_time() + def load_battery_icons(self): + img_size = self._gtk.img_scale * self.bts + return { + 'charging': self._gtk.PixbufFromIcon('battery-charging', img_size, img_size), + '100': self._gtk.PixbufFromIcon('battery-100', img_size, img_size), + '75': self._gtk.PixbufFromIcon('battery-75', img_size, img_size), + '50': self._gtk.PixbufFromIcon('battery-50', img_size, img_size), + '25': self._gtk.PixbufFromIcon('battery-25', img_size, img_size), + '0': self._gtk.PixbufFromIcon('battery-0', img_size, img_size), + 'unknown': self._gtk.PixbufFromIcon('battery-unknown', img_size, img_size), + } + def reload_icons(self): button: Gtk.Button for button in self.action_bar.get_children(): @@ -122,6 +147,9 @@ class BasePanel(ScreenPanel): size = self._gtk.img_scale * self.abscale * 1.4 button.set_image(self._gtk.Image(name, size, size)) + self.battery_icons = self.load_battery_icons() + self.battery_percentage() + def show_heaters(self, show=True): try: for child in self.control['temp_box'].get_children(): @@ -195,6 +223,8 @@ class BasePanel(ScreenPanel): def activate(self): if self.time_update is None: self.time_update = GLib.timeout_add_seconds(1, self.update_time) + if self.battery_update is None: + self.battery_update = GLib.timeout_add_seconds(60, self.battery_percentage) def add_content(self, panel): printing = self._printer and self._printer.state in {"printing", "paused"} @@ -342,6 +372,37 @@ class BasePanel(ScreenPanel): self.time_format = confopt return True + def get_battery_icon(self, charge: float, plugged: bool): + if plugged: + return self.battery_icons['charging'] + elif charge > 75: + return self.battery_icons['100'] + elif charge > 50: + return self.battery_icons['75'] + elif charge > 25: + return self.battery_icons['50'] + elif charge > 10: + return self.battery_icons['25'] + elif charge >= 0: + return self.battery_icons['0'] + else: + return self.battery_icons['unknown'] + + def battery_percentage(self): + battery = psutil.sensors_battery() + if battery and battery.percent: + self.labels['battery_icon'].set_from_pixbuf( + self.get_battery_icon(battery.percent, battery.power_plugged) + ) + self.labels['battery'].set_text(f'{battery.percent:.0f}%') + logging.debug(f"Battery: {battery.percent}% Power plugged in: {'Yes' if battery.power_plugged else 'No'}") + self.control['battery_box'].show() + return True + else: + logging.debug("Battery information not available.") + self.control['battery_box'].hide() + return False + def set_ks_printer_cfg(self, printer): ScreenPanel.ks_printer_cfg = self._config.get_printer_config(printer) if self.ks_printer_cfg is not None: diff --git a/scripts/KlipperScreen-requirements.txt b/scripts/KlipperScreen-requirements.txt index 4246c50d..bb441cd9 100644 --- a/scripts/KlipperScreen-requirements.txt +++ b/scripts/KlipperScreen-requirements.txt @@ -12,3 +12,4 @@ backports.zoneinfo;python_version<"3.9" PyGObject==3.48.1;python_version>="3.8" pycairo==1.26.1;python_version>="3.8" websocket-client==1.8.0;python_version>="3.8" +psutil==6.0.0;python_version>="3.8" diff --git a/styles/colorized/images/battery-0.svg b/styles/colorized/images/battery-0.svg new file mode 100644 index 00000000..67cdd630 --- /dev/null +++ b/styles/colorized/images/battery-0.svg @@ -0,0 +1,68 @@ + + + + + + image/svg+xml + + folder + + + + + + + folder + Created with Sketch. + + diff --git a/styles/colorized/images/battery-100.svg b/styles/colorized/images/battery-100.svg new file mode 100644 index 00000000..82183d9d --- /dev/null +++ b/styles/colorized/images/battery-100.svg @@ -0,0 +1,75 @@ + + + + + + image/svg+xml + + folder + + + + + + + folder + + Created with Sketch. + + diff --git a/styles/colorized/images/battery-25.svg b/styles/colorized/images/battery-25.svg new file mode 100644 index 00000000..50e58dc6 --- /dev/null +++ b/styles/colorized/images/battery-25.svg @@ -0,0 +1,86 @@ + + + + + + image/svg+xml + + folder + + + + + + + + + + folder + + Created with Sketch. + + diff --git a/styles/colorized/images/battery-50.svg b/styles/colorized/images/battery-50.svg new file mode 100644 index 00000000..c04f4452 --- /dev/null +++ b/styles/colorized/images/battery-50.svg @@ -0,0 +1,81 @@ + + + + + + image/svg+xml + + folder + + + + + + + + + folder + + Created with Sketch. + + diff --git a/styles/colorized/images/battery-75.svg b/styles/colorized/images/battery-75.svg new file mode 100644 index 00000000..6eb40659 --- /dev/null +++ b/styles/colorized/images/battery-75.svg @@ -0,0 +1,86 @@ + + + + + + image/svg+xml + + folder + + + + + + + + + + folder + + Created with Sketch. + + diff --git a/styles/colorized/images/battery-charging.svg b/styles/colorized/images/battery-charging.svg new file mode 100644 index 00000000..96013579 --- /dev/null +++ b/styles/colorized/images/battery-charging.svg @@ -0,0 +1,83 @@ + + + + + + image/svg+xml + + folder + + + + + + + + + + folder + Created with Sketch. + + + diff --git a/styles/colorized/images/battery-unknown.svg b/styles/colorized/images/battery-unknown.svg new file mode 100644 index 00000000..b9f22242 --- /dev/null +++ b/styles/colorized/images/battery-unknown.svg @@ -0,0 +1,84 @@ + + + + + + image/svg+xml + + folder + + + + + + + + + + folder + Created with Sketch. + + + diff --git a/styles/material-dark/images/battery-0.svg b/styles/material-dark/images/battery-0.svg new file mode 100644 index 00000000..a6f93c5f --- /dev/null +++ b/styles/material-dark/images/battery-0.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/styles/material-dark/images/battery-100.svg b/styles/material-dark/images/battery-100.svg new file mode 100644 index 00000000..e7adbc02 --- /dev/null +++ b/styles/material-dark/images/battery-100.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/styles/material-dark/images/battery-25.svg b/styles/material-dark/images/battery-25.svg new file mode 100644 index 00000000..f1d3ebbf --- /dev/null +++ b/styles/material-dark/images/battery-25.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/styles/material-dark/images/battery-50.svg b/styles/material-dark/images/battery-50.svg new file mode 100644 index 00000000..51c18769 --- /dev/null +++ b/styles/material-dark/images/battery-50.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/styles/material-dark/images/battery-75.svg b/styles/material-dark/images/battery-75.svg new file mode 100644 index 00000000..3caa0989 --- /dev/null +++ b/styles/material-dark/images/battery-75.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/styles/material-dark/images/battery-charging.svg b/styles/material-dark/images/battery-charging.svg new file mode 100644 index 00000000..4115f8aa --- /dev/null +++ b/styles/material-dark/images/battery-charging.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/styles/material-dark/images/battery-unknown.svg b/styles/material-dark/images/battery-unknown.svg new file mode 100644 index 00000000..1c517bad --- /dev/null +++ b/styles/material-dark/images/battery-unknown.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/styles/material-darker/images/battery-0.svg b/styles/material-darker/images/battery-0.svg new file mode 100644 index 00000000..a6f93c5f --- /dev/null +++ b/styles/material-darker/images/battery-0.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/styles/material-darker/images/battery-100.svg b/styles/material-darker/images/battery-100.svg new file mode 100644 index 00000000..e7adbc02 --- /dev/null +++ b/styles/material-darker/images/battery-100.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/styles/material-darker/images/battery-25.svg b/styles/material-darker/images/battery-25.svg new file mode 100644 index 00000000..f1d3ebbf --- /dev/null +++ b/styles/material-darker/images/battery-25.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/styles/material-darker/images/battery-50.svg b/styles/material-darker/images/battery-50.svg new file mode 100644 index 00000000..51c18769 --- /dev/null +++ b/styles/material-darker/images/battery-50.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/styles/material-darker/images/battery-75.svg b/styles/material-darker/images/battery-75.svg new file mode 100644 index 00000000..3caa0989 --- /dev/null +++ b/styles/material-darker/images/battery-75.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/styles/material-darker/images/battery-charging.svg b/styles/material-darker/images/battery-charging.svg new file mode 100644 index 00000000..4115f8aa --- /dev/null +++ b/styles/material-darker/images/battery-charging.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/styles/material-darker/images/battery-unknown.svg b/styles/material-darker/images/battery-unknown.svg new file mode 100644 index 00000000..1c517bad --- /dev/null +++ b/styles/material-darker/images/battery-unknown.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/styles/material-light/images/battery-0.svg b/styles/material-light/images/battery-0.svg new file mode 100644 index 00000000..a1083d9a --- /dev/null +++ b/styles/material-light/images/battery-0.svg @@ -0,0 +1,40 @@ + + + + + + diff --git a/styles/material-light/images/battery-100.svg b/styles/material-light/images/battery-100.svg new file mode 100644 index 00000000..e95b5409 --- /dev/null +++ b/styles/material-light/images/battery-100.svg @@ -0,0 +1,40 @@ + + + + + + diff --git a/styles/material-light/images/battery-25.svg b/styles/material-light/images/battery-25.svg new file mode 100644 index 00000000..95d029f7 --- /dev/null +++ b/styles/material-light/images/battery-25.svg @@ -0,0 +1,40 @@ + + + + + + diff --git a/styles/material-light/images/battery-50.svg b/styles/material-light/images/battery-50.svg new file mode 100644 index 00000000..2bf25cbb --- /dev/null +++ b/styles/material-light/images/battery-50.svg @@ -0,0 +1,40 @@ + + + + + + diff --git a/styles/material-light/images/battery-75.svg b/styles/material-light/images/battery-75.svg new file mode 100644 index 00000000..88c8aa52 --- /dev/null +++ b/styles/material-light/images/battery-75.svg @@ -0,0 +1,40 @@ + + + + + + diff --git a/styles/material-light/images/battery-charging.svg b/styles/material-light/images/battery-charging.svg new file mode 100644 index 00000000..d296bd13 --- /dev/null +++ b/styles/material-light/images/battery-charging.svg @@ -0,0 +1,40 @@ + + + + + + diff --git a/styles/material-light/images/battery-unknown.svg b/styles/material-light/images/battery-unknown.svg new file mode 100644 index 00000000..47f97129 --- /dev/null +++ b/styles/material-light/images/battery-unknown.svg @@ -0,0 +1,40 @@ + + + + + + diff --git a/styles/z-bolt/images/battery-0.svg b/styles/z-bolt/images/battery-0.svg new file mode 100644 index 00000000..3d584919 --- /dev/null +++ b/styles/z-bolt/images/battery-0.svg @@ -0,0 +1,68 @@ + + + + + + image/svg+xml + + folder + + + + + + + folder + Created with Sketch. + + diff --git a/styles/z-bolt/images/battery-100.svg b/styles/z-bolt/images/battery-100.svg new file mode 100644 index 00000000..0bb21e09 --- /dev/null +++ b/styles/z-bolt/images/battery-100.svg @@ -0,0 +1,68 @@ + + + + + + image/svg+xml + + folder + + + + + + + folder + Created with Sketch. + + diff --git a/styles/z-bolt/images/battery-25.svg b/styles/z-bolt/images/battery-25.svg new file mode 100644 index 00000000..aa8a9721 --- /dev/null +++ b/styles/z-bolt/images/battery-25.svg @@ -0,0 +1,68 @@ + + + + + + image/svg+xml + + folder + + + + + + + folder + Created with Sketch. + + diff --git a/styles/z-bolt/images/battery-50.svg b/styles/z-bolt/images/battery-50.svg new file mode 100644 index 00000000..ce2e1cfa --- /dev/null +++ b/styles/z-bolt/images/battery-50.svg @@ -0,0 +1,68 @@ + + + + + + image/svg+xml + + folder + + + + + + + folder + Created with Sketch. + + diff --git a/styles/z-bolt/images/battery-75.svg b/styles/z-bolt/images/battery-75.svg new file mode 100644 index 00000000..0941d713 --- /dev/null +++ b/styles/z-bolt/images/battery-75.svg @@ -0,0 +1,68 @@ + + + + + + image/svg+xml + + folder + + + + + + + folder + Created with Sketch. + + diff --git a/styles/z-bolt/images/battery-charging.svg b/styles/z-bolt/images/battery-charging.svg new file mode 100644 index 00000000..518e89b0 --- /dev/null +++ b/styles/z-bolt/images/battery-charging.svg @@ -0,0 +1,72 @@ + + + + + + image/svg+xml + + folder + + + + + + + folder + Created with Sketch. + + + diff --git a/styles/z-bolt/images/battery-unknown.svg b/styles/z-bolt/images/battery-unknown.svg new file mode 100644 index 00000000..0c618086 --- /dev/null +++ b/styles/z-bolt/images/battery-unknown.svg @@ -0,0 +1,73 @@ + + + + + + image/svg+xml + + folder + + + + + + + folder + Created with Sketch. + + +