From 5181ed299f4d80913ac344e6126c42a5750f5467 Mon Sep 17 00:00:00 2001 From: jsbautista Date: Thu, 30 Jan 2025 00:29:18 -0500 Subject: [PATCH] add INTERACT_ON_CLICK and SpyderMenu --- .../plugins/ipythonconsole/widgets/status.py | 38 ++++++++++++++----- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/spyder/plugins/ipythonconsole/widgets/status.py b/spyder/plugins/ipythonconsole/widgets/status.py index 6673c5f4351..6826def4071 100644 --- a/spyder/plugins/ipythonconsole/widgets/status.py +++ b/spyder/plugins/ipythonconsole/widgets/status.py @@ -16,15 +16,17 @@ # Third-party imports from IPython.core import release as ipython_release from qtpy.QtCore import QPoint, Signal -from qtpy.QtWidgets import QMenu +from qtpy.QtGui import QFontMetrics from spyder_kernels.comms.frontendcomm import CommError from spyder_kernels.utils.pythonenv import PythonEnvInfo, PythonEnvType # Local imports from spyder.api.shellconnect.status import ShellConnectStatusBarWidget from spyder.api.translations import _ +from spyder.api.widgets.menus import SpyderMenu from spyder.config.base import running_in_ci from spyder.utils.qthelpers import add_actions, create_action +from spyder.utils.stylesheet import MAC, WIN logger = logging.getLogger(__name__) @@ -179,6 +181,7 @@ class PythonEnvironmentStatus(ShellConnectStatusBarWidget): ID = 'pythonenv_status' CONF_SECTION = 'ipython_console' + INTERACT_ON_CLICK = True sig_interpreter_changed = Signal(str) @@ -190,7 +193,7 @@ class PythonEnvironmentStatus(ShellConnectStatusBarWidget): def __init__(self, parent): self._current_env_info: PythonEnvInfo | None = None super().__init__(parent) - self.menu = QMenu(self) + self.menu = SpyderMenu(self) self.sig_clicked.connect(self.show_menu) # ---- StatusBarWidget API @@ -268,20 +271,35 @@ def on_kernel_start(self, shellwidget): def show_menu(self): """Display a menu when clicking on the widget.""" - menu = self.menu - menu.clear() + self.menu.clear_actions() text = _("Change default environment in Preferences...") - change_action = create_action( - self, + change_action = self.create_action( + "change_environment", text=text, triggered=self.open_interpreter_preferences, + register_action=False, + ) + self.add_item_to_menu(change_action, self.menu) + + x_offset = ( + # Margin of menu items to left and right + 2 * SpyderMenu.HORIZONTAL_MARGIN_FOR_ITEMS + # Padding of menu items to left and right + + 2 * SpyderMenu.HORIZONTAL_PADDING_FOR_ITEMS ) - add_actions(menu, [change_action]) + y_offset = 4 if MAC else (3 if WIN else 2) + + metrics = QFontMetrics(self.font()) rect = self.contentsRect() - os_height = 7 if os.name == 'nt' else 12 pos = self.mapToGlobal( - rect.topLeft() + QPoint(-40, -rect.height() - os_height)) - menu.popup(pos) + rect.topLeft() + + QPoint( + -metrics.width(text) // 2 + x_offset, + -2 * self.parent().height() + y_offset, + ) + ) + + self.menu.popup(pos) def open_interpreter_preferences(self): """Request to open the main interpreter preferences."""