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. + + +