diff --git a/bec_widgets/applications/views/developer_view/developer_widget.py b/bec_widgets/applications/views/developer_view/developer_widget.py index ce7030c3..acb56d22 100644 --- a/bec_widgets/applications/views/developer_view/developer_widget.py +++ b/bec_widgets/applications/views/developer_view/developer_widget.py @@ -124,6 +124,7 @@ class DeveloperWidget(DockAreaWidget): # Connect editor signals self.explorer.file_open_requested.connect(self._open_new_file) self.monaco.macro_file_updated.connect(self.explorer.refresh_macro_file) + self.monaco.focused_editor.connect(self._on_focused_editor_changed) self.toolbar.show_bundles(["save", "execution", "settings"]) @@ -336,6 +337,28 @@ class DeveloperWidget(DockAreaWidget): self.on_script_execution_info, MessageEndpoints.script_execution_info(new_script_id) ) + @SafeSlot(CDockWidget) + def _on_focused_editor_changed(self, tab_widget: CDockWidget): + """ + Disable the run / stop buttons if the focused editor is a macro file. + Args: + tab_widget: The currently focused tab widget in the Monaco editor. + """ + if not isinstance(tab_widget, CDockWidget): + return + widget = tab_widget.widget() + if not isinstance(widget, MonacoWidget): + return + file_scope = widget.metadata.get("scope", "") + run_action = self.toolbar.components.get_action("run") + stop_action = self.toolbar.components.get_action("stop") + if "macro" in file_scope: + run_action.action.setEnabled(False) + stop_action.action.setEnabled(False) + else: + run_action.action.setEnabled(True) + stop_action.action.setEnabled(True) + @SafeSlot(dict, dict) def on_script_execution_info(self, content: dict, metadata: dict): """ diff --git a/tests/unit_tests/test_developer_view.py b/tests/unit_tests/test_developer_view.py index 56971d3b..f4b756fc 100644 --- a/tests/unit_tests/test_developer_view.py +++ b/tests/unit_tests/test_developer_view.py @@ -322,6 +322,52 @@ class TestToolbarIntegration: # Check that state changed assert vim_action.action.isChecked() != initial_state + def test_run_stop_buttons_disabled_for_macros(self, developer_view, temp_python_file, qtbot): + """Test that run and stop buttons are disabled when a macro file is focused.""" + # Open a file with macro scope + developer_view._open_new_file(temp_python_file, "macros/local") + + qtbot.waitUntil( + lambda: temp_python_file in developer_view.monaco._get_open_files(), timeout=2000 + ) + + # Get the editor dock for the macro file + dock = developer_view.monaco._get_editor_dock(temp_python_file) + assert dock is not None + + # Simulate focusing on the macro file + developer_view._on_focused_editor_changed(dock) + + # Check that run and stop buttons are disabled + run_action = developer_view.toolbar.components.get_action("run") + stop_action = developer_view.toolbar.components.get_action("stop") + + assert not run_action.action.isEnabled() + assert not stop_action.action.isEnabled() + + def test_run_stop_buttons_enabled_for_scripts(self, developer_view, temp_python_file, qtbot): + """Test that run and stop buttons are enabled when a script file is focused.""" + # Open a file with script scope + developer_view._open_new_file(temp_python_file, "scripts/local") + + qtbot.waitUntil( + lambda: temp_python_file in developer_view.monaco._get_open_files(), timeout=2000 + ) + + # Get the editor dock for the script file + dock = developer_view.monaco._get_editor_dock(temp_python_file) + assert dock is not None + + # Simulate focusing on the script file + developer_view._on_focused_editor_changed(dock) + + # Check that run and stop buttons are enabled + run_action = developer_view.toolbar.components.get_action("run") + stop_action = developer_view.toolbar.components.get_action("stop") + + assert run_action.action.isEnabled() + assert stop_action.action.isEnabled() + class TestErrorHandling: """Test error handling in various scenarios."""