From 6e7920c119824650006e7357ca2f4ff95d413e13 Mon Sep 17 00:00:00 2001 From: wakonig_k Date: Tue, 29 Apr 2025 09:43:19 +0200 Subject: [PATCH] fix(launcher): hide launcher when launcher is closed even though it is not the last widget --- bec_widgets/applications/launch_window.py | 43 +++++++++++++++++++++++ bec_widgets/cli/server.py | 27 -------------- 2 files changed, 43 insertions(+), 27 deletions(-) diff --git a/bec_widgets/applications/launch_window.py b/bec_widgets/applications/launch_window.py index 28f688bc..d3597ac7 100644 --- a/bec_widgets/applications/launch_window.py +++ b/bec_widgets/applications/launch_window.py @@ -204,6 +204,10 @@ class LaunchWindow(BECMainWindow): list(self.available_auto_updates.keys()) + ["Default"] ) + self.register = RPCRegister() + self.register.callbacks.append(self._turn_off_the_lights) + self.register.broadcast() + def launch( self, launch_script: str, @@ -377,6 +381,45 @@ class LaunchWindow(BECMainWindow): super().showEvent(event) self.setFixedSize(self.size()) + def _launcher_is_last_widget(self, connections: dict) -> bool: + """ + Check if the launcher is the last widget in the application. + """ + + remaining_connections = [ + connection for connection in connections.values() if connection.parent_id != self.gui_id + ] + return len(remaining_connections) <= 1 + + def _turn_off_the_lights(self, connections: dict): + """ + If there is only one connection remaining, it is the launcher, so we show it. + Once the launcher is closed as the last window, we quit the application. + """ + if self._launcher_is_last_widget(connections): + self.show() + self.activateWindow() + self.raise_() + if self.app: + self.app.setQuitOnLastWindowClosed(True) # type: ignore + return + + self.hide() + if self.app: + self.app.setQuitOnLastWindowClosed(False) # type: ignore + + def closeEvent(self, event): + """ + Close the launcher window. + """ + connections = self.register.list_all_connections() + if self._launcher_is_last_widget(connections): + event.accept() + return + + event.ignore() + self.hide() + if __name__ == "__main__": import sys diff --git a/bec_widgets/cli/server.py b/bec_widgets/cli/server.py index d3510f31..14e8d39f 100644 --- a/bec_widgets/cli/server.py +++ b/bec_widgets/cli/server.py @@ -83,29 +83,6 @@ class GUIServer: service_config = ServiceConfig() return service_config - def _turn_off_the_lights(self, connections: dict): - """ - If there is only one connection remaining, it is the launcher, so we show it. - Once the launcher is closed as the last window, we quit the application. - """ - self.launcher_window = cast(LaunchWindow, self.launcher_window) - - remaining_connections = [ - connection - for connection in connections.values() - if connection.parent_id != self.launcher_window.gui_id - ] - if len(remaining_connections) <= 1: - self.launcher_window.show() - self.launcher_window.activateWindow() - self.launcher_window.raise_() - if self.app: - self.app.setQuitOnLastWindowClosed(True) - else: - self.launcher_window.hide() - if self.app: - self.app.setQuitOnLastWindowClosed(False) - def _run(self): """ Run the GUI server. @@ -125,10 +102,6 @@ class GUIServer: self.app.aboutToQuit.connect(self.shutdown) self.app.setQuitOnLastWindowClosed(False) - register = RPCRegister() - register.callbacks.append(self._turn_off_the_lights) - register.broadcast() - if self.gui_class: # If the server is started with a specific gui class, we launch it. # This will automatically hide the launcher.