From 68c402590217c98dfed1f86bf0e92c0814c7c252 Mon Sep 17 00:00:00 2001 From: alfrix Date: Fri, 8 Mar 2024 21:23:42 -0300 Subject: [PATCH] create the shutdown panel, add it to the action bar, remove items from system panel shutdown maintain estop during printing add option to shudown both host and printer close #1207 simplify host and printer dialog if ks is local, do both --- ks_includes/screen_panel.py | 1 + panels/base_panel.py | 24 +++++++++---- panels/shutdown.py | 70 +++++++++++++++++++++++++++++++++++++ panels/system.py | 58 ++++++++---------------------- 4 files changed, 103 insertions(+), 50 deletions(-) create mode 100644 panels/shutdown.py diff --git a/ks_includes/screen_panel.py b/ks_includes/screen_panel.py index aea6655b..96f0f64b 100644 --- a/ks_includes/screen_panel.py +++ b/ks_includes/screen_panel.py @@ -43,6 +43,7 @@ class ScreenPanel: "printer.emergency_stop") else: self._screen._ws.klippy.emergency_stop() + self._screen._ws.klippy.emergency_stop() def get_file_image(self, filename, width=None, height=None, small=False): if not self._files.has_thumbnail(filename): diff --git a/panels/base_panel.py b/panels/base_panel.py index f0dea68c..55c9ec5e 100644 --- a/panels/base_panel.py +++ b/panels/base_panel.py @@ -27,10 +27,19 @@ class BasePanel(ScreenPanel): self.control['back'].connect("clicked", self.back) self.control['home'] = self._gtk.Button('main', scale=abscale) self.control['home'].connect("clicked", self._screen._menu_go_back, True) - self.control['estop'] = self._gtk.Button('emergency', scale=abscale) - self.control['estop'].connect("clicked", self.emergency_stop) for control in self.control: self.set_control_sensitive(False, control) + self.control['estop'] = self._gtk.Button('emergency', scale=abscale) + self.control['estop'].connect("clicked", self.emergency_stop) + self.control['estop'].set_no_show_all(True) + self.shutdown = { + "name": None, + "panel": "shutdown", + "icon": "shutdown", + } + self.control['shutdown'] = self._gtk.Button('shutdown', scale=abscale) + self.control['shutdown'].connect("clicked", self.menu_item_clicked, self.shutdown) + self.control['shutdown'].set_no_show_all(True) self.control['printer_select'] = self._gtk.Button('shuffle', scale=abscale) self.control['printer_select'].connect("clicked", self._screen.show_printer_select) self.control['printer_select'].set_no_show_all(True) @@ -63,6 +72,7 @@ class BasePanel(ScreenPanel): self.action_bar.add(self.control['printer_select']) self.action_bar.add(self.control['shortcut']) self.action_bar.add(self.control['estop']) + self.action_bar.add(self.control['shutdown']) self.show_printer_select(len(self._config.get_printers()) > 1) # Titlebar @@ -174,10 +184,12 @@ class BasePanel(ScreenPanel): self.time_update = GLib.timeout_add_seconds(1, self.update_time) def add_content(self, panel): - show = self._printer is not None and self._printer.state not in ('disconnected', 'startup', 'shutdown', 'error') - self.show_shortcut(show) - self.show_heaters(show) - self.set_control_sensitive(show, control='estop') + printing = self._printer and self._printer.state in {"printing", "paused"} + connected = self._printer and self._printer.state not in {'disconnected', 'startup', 'shutdown', 'error'} + self.control['estop'].set_visible(printing) + self.control['shutdown'].set_visible(not printing) + self.show_shortcut(connected) + self.show_heaters(connected) for control in ('back', 'home'): self.set_control_sensitive(len(self._screen._cur_panels) > 1, control=control) self.current_panel = panel diff --git a/panels/shutdown.py b/panels/shutdown.py new file mode 100644 index 00000000..524c6bda --- /dev/null +++ b/panels/shutdown.py @@ -0,0 +1,70 @@ +import logging +import os +import gi + +gi.require_version("Gtk", "3.0") +from gi.repository import Gtk +from ks_includes.screen_panel import ScreenPanel + + +class Panel(ScreenPanel): + def __init__(self, screen, title): + super().__init__(screen, title) + + estop = self._gtk.Button("emergency", _("Emergency Stop"), "color2") + estop.connect("clicked", self.emergency_stop) + + poweroff = self._gtk.Button("shutdown", _("Shutdown"), "color1") + poweroff.connect("clicked", self.reboot_poweroff, "shutdown") + + restart = self._gtk.Button("refresh", _("Restart"), "color3") + restart.connect("clicked", self.reboot_poweroff, "reboot") + + self.main = Gtk.Grid(row_homogeneous=True, column_homogeneous=True) + if self._printer and self._printer.state not in {'disconnected', 'startup', 'shutdown', 'error'}: + self.main.attach(estop, 0, 0, 2, 1) + self.main.attach(poweroff, 0, 1, 1, 1) + self.main.attach(restart, 1, 1, 1, 1) + self.content.add(self.main) + + def reboot_poweroff(self, widget, method): + label = Gtk.Label(wrap=True, hexpand=True, vexpand=True) + if method == "reboot": + label.set_label(_("Are you sure you wish to reboot the system?")) + title = _("Restart") + else: + label.set_label(_("Are you sure you wish to shutdown the system?")) + title = _("Shutdown") + buttons = [] + logging.info(self._screen.apiclient.endpoint) + local = {"127.0.0.1", "localhost"} + if any(endpoint in self._screen.apiclient.endpoint for endpoint in local): + buttons.append({"name": _("Accept"), "response": Gtk.ResponseType.ACCEPT, "style": 'dialog-primary'}) + else: + buttons.extend([ + {"name": _("Host"), "response": Gtk.ResponseType.OK, "style": 'dialog-info'}, + {"name": _("Printer"), "response": Gtk.ResponseType.APPLY, "style": 'dialog-warning'}, + {"name": _("Both"), "response": Gtk.ResponseType.ACCEPT, "style": 'dialog-primary'}, + ]) + buttons.append({"name": _("Cancel"), "response": Gtk.ResponseType.CANCEL, "style": 'dialog-error'}) + self._gtk.Dialog(title, buttons, label, self.reboot_poweroff_confirm, method) + + def reboot_poweroff_confirm(self, dialog, response_id, method): + self._gtk.remove_dialog(dialog) + if response_id == Gtk.ResponseType.ACCEPT: + if method == "reboot": + self._screen._ws.send_method("machine.reboot") + os.system("systemctl reboot -i") + else: + self._screen._ws.send_method("machine.shutdown") + os.system("systemctl poweroff -i") + elif response_id == Gtk.ResponseType.OK: + if method == "reboot": + os.system("systemctl reboot -i") + else: + os.system("systemctl poweroff -i") + elif response_id == Gtk.ResponseType.APPLY: + if method == "reboot": + self._screen._ws.send_method("machine.reboot") + else: + self._screen._ws.send_method("machine.shutdown") diff --git a/panels/system.py b/panels/system.py index 4e9fb415..c2ddf638 100644 --- a/panels/system.py +++ b/panels/system.py @@ -13,19 +13,16 @@ class Panel(ScreenPanel): self.update_status = None self.system_info = self._screen.apiclient.send_request("machine/system_info") - self.update_all = self._gtk.Button('arrow-up', _('Full Update'), 'color1') + self.update_all = self._gtk.Button('arrow-up', _('Full Update'), 'color1', self.bts, Gtk.PositionType.LEFT, 1) self.update_all.connect("clicked", self.show_update_info, "full") self.update_all.set_vexpand(False) - self.refresh = self._gtk.Button('arrow-down', _('Refresh'), 'color2') + self.refresh = self._gtk.Button('arrow-down', _('Refresh'), 'color3', self.bts, Gtk.PositionType.LEFT, 1) self.refresh.connect("clicked", self.refresh_updates) self.refresh.set_vexpand(False) - reboot = self._gtk.Button('refresh', _('Restart'), 'color3') - reboot.connect("clicked", self.reboot_poweroff, "reboot") - reboot.set_vexpand(False) - shutdown = self._gtk.Button('shutdown', _('Shutdown'), 'color4') - shutdown.connect("clicked", self.reboot_poweroff, "poweroff") - shutdown.set_vexpand(False) + sbox = Gtk.Box(vexpand=False) + sbox.pack_start(self.update_all, True, True, 0) + sbox.pack_start(self.refresh, True, True, 0) self.update_msg = Gtk.Label(label=_("Checking for updates, please wait..."), vexpand=True) @@ -33,13 +30,11 @@ class Panel(ScreenPanel): self.scroll.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC) self.scroll.add(self.update_msg) - self.main_grid = Gtk.Grid(column_homogeneous=True) - self.main_grid.attach(self.scroll, 0, 0, 4, 2) - self.main_grid.attach(self.update_all, 0, 2, 1, 1) - self.main_grid.attach(self.refresh, 1, 2, 1, 1) - self.main_grid.attach(reboot, 2, 2, 1, 1) - self.main_grid.attach(shutdown, 3, 2, 1, 1) - self.content.add(self.main_grid) + self.main_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, vexpand=True) + self.main_box.pack_start(sbox, False, False, 0) + self.main_box.pack_start(self.scroll, True, True, 0) + + self.content.add(self.main_box) def activate(self): self._screen._ws.send_method('machine.update.status', callback=self.get_updates) @@ -56,7 +51,10 @@ class Panel(ScreenPanel): try: if prog in self.system_info['result']['system_info']['available_services']: - self.labels[f"{prog}_restart"] = self._gtk.Button("refresh", scale=.7) + self.labels[f"{prog}_restart"] = self._gtk.Button("refresh", _("Restart"), + "color2", + position=Gtk.PositionType.LEFT, + scale=self.bts) self.labels[f"{prog}_restart"].connect("clicked", self.restart, prog) infogrid.attach(self.labels[f"{prog}_restart"], 0, i, 1, 1) except Exception as e: @@ -277,31 +275,3 @@ class Panel(ScreenPanel): self.labels[f"{p}_status"].set_label(_("Update")) self.labels[f"{p}_status"].get_style_context().add_class('update') self.labels[f"{p}_status"].set_sensitive(True) - - def reboot_poweroff(self, widget, method): - label = Gtk.Label(wrap=True, hexpand=True, vexpand=True) - if method == "reboot": - label.set_label(_("Are you sure you wish to reboot the system?")) - title = _("Restart") - else: - label.set_label(_("Are you sure you wish to shutdown the system?")) - title = _("Shutdown") - buttons = [ - {"name": _("Host"), "response": Gtk.ResponseType.OK, "style": 'dialog-info'}, - {"name": _("Printer"), "response": Gtk.ResponseType.APPLY, "style": 'dialog-warning'}, - {"name": _("Cancel"), "response": Gtk.ResponseType.CANCEL, "style": 'dialog-error'} - ] - self._gtk.Dialog(title, buttons, label, self.reboot_poweroff_confirm, method) - - def reboot_poweroff_confirm(self, dialog, response_id, method): - self._gtk.remove_dialog(dialog) - if response_id == Gtk.ResponseType.OK: - if method == "reboot": - os.system("systemctl reboot -i") - else: - os.system("systemctl poweroff -i") - elif response_id == Gtk.ResponseType.APPLY: - if method == "reboot": - self._screen._ws.send_method("machine.reboot") - else: - self._screen._ws.send_method("machine.shutdown")