Skip to content

Commit

Permalink
add INTERACT_ON_CLICK and SpyderMenu
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbautista committed Jan 30, 2025
1 parent cea2dcf commit 5181ed2
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions spyder/plugins/ipythonconsole/widgets/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -179,6 +181,7 @@ class PythonEnvironmentStatus(ShellConnectStatusBarWidget):

ID = 'pythonenv_status'
CONF_SECTION = 'ipython_console'
INTERACT_ON_CLICK = True

sig_interpreter_changed = Signal(str)

Expand All @@ -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
Expand Down Expand Up @@ -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."""
Expand Down

0 comments on commit 5181ed2

Please sign in to comment.