Skip to content

Commit

Permalink
Add option to enable spinners and fix review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
goanpeca committed Aug 30, 2020
1 parent 841a76b commit 289fc42
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 20 deletions.
12 changes: 4 additions & 8 deletions spyder/api/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,10 +817,10 @@ def __init__(self, parent, configuration=None):
# Widget setup
# ----------------------------------------------------------------
try:
_setup = getattr(container, "_setup", None)
if _setup:
_setup(options=options)
container._setup(options=options)
except AttributeError as error:
pass
except Exception as error:
logger.debug(
"Running `_setup` on {0}: {1}".format(self, error))

Expand Down Expand Up @@ -1607,14 +1607,10 @@ def __init__(self, parent, configuration):
widget.set_icon(self.get_icon())
widget.set_name(self.NAME)

# TODO: Streamline this by moving to postvisible setup
# Render all toolbars as a final separate step on the main window
# in case some plugins want to extend a toolbar. Since the rendering
# can only be done once!
widget._main_toolbar._render()
widget._corner_toolbar._render()
for __, toolbar in widget._auxiliary_toolbars.items():
toolbar._render()
widget.render_toolbars()

# Default Signals
# --------------------------------------------------------------------
Expand Down
78 changes: 71 additions & 7 deletions spyder/api/widgets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,27 @@ class PluginMainWidget(QWidget, SpyderWidgetMixin, SpyderToolBarMixin):
"""
DEFAULT_OPTIONS = {}

# --- Attributes
# ------------------------------------------------------------------------
ENABLE_SPINNER = False
"""
This attribute enables/disables showing a spinner on the top right to the
left of the corner menu widget (Hamburguer menu).
Plugins that provide actions that take time should make this `True` and
use accoringly with the `start_spinner`/`stop_spinner` methods.
The Find in files plugin is an example of a core plugin that uses it.
Parameters
----------
ENABLE_SPINNER: bool
If `True` an extra space will be added to the toolbar (even if the
spinner is not moving) to avoid items jumping to the left/right when
the spinner appears. If `False` no extra space will be added. Default
is False.
"""

# --- Signals
# ------------------------------------------------------------------------
sig_free_memory_requested = Signal()
Expand Down Expand Up @@ -318,6 +339,7 @@ def __init__(self, name, plugin, parent=None, options=DEFAULT_OPTIONS):
self.dock_action = None
self.undock_action = None
self.close_action = None
self._toolbars_already_rendered = False

# We create our toggle action instead of using the one that comes with
# dockwidget because it was not possible to raise and focus the plugin
Expand All @@ -330,7 +352,11 @@ def __init__(self, name, plugin, parent=None, options=DEFAULT_OPTIONS):
self.windowwidget = None
self.dockwidget = None
self._icon = QIcon()
self._spinner = create_waitspinner(size=16, parent=self)
self._spinner = None

if self.ENABLE_SPINNER:
self._spinner = create_waitspinner(size=16, parent=self)

self._corner_widget = MainCornerWidget(
parent=self,
name=PluginMainWidgetWidgets.CornerWidget,
Expand Down Expand Up @@ -405,6 +431,13 @@ def _setup(self, options=DEFAULT_OPTIONS):
# For widgets that use tabs, we add the corner widget using
# the setCornerWidget method.
child.setCornerWidget(self._corner_widget)

# This is needed to ensure the corner ToolButton (hamburguer
# menu) is aligned with plugins that use ToolBars vs
# CornerWidgets
# See: spyder-ide/spyder#13600
# left, top, right, bottom
self._corner_widget.setContentsMargins(0, 2, 2, 0)
break

self._options_button = self.create_toolbutton(
Expand All @@ -417,10 +450,11 @@ def _setup(self, options=DEFAULT_OPTIONS):
PluginMainWidgetWidgets.OptionsToolButton,
self._options_button,
)
self.add_corner_widget(
PluginMainWidgetWidgets.Spinner,
self._spinner,
)
if self.ENABLE_SPINNER:
self.add_corner_widget(
PluginMainWidgetWidgets.Spinner,
self._spinner,
)

# Widget setup
# --------------------------------------------------------------------
Expand Down Expand Up @@ -639,13 +673,15 @@ def start_spinner(self):
"""
Start default status spinner.
"""
self._spinner.start()
if self.ENABLE_SPINNER:
self._spinner.start()

def stop_spinner(self):
"""
Stop default status spinner.
"""
self._spinner.stop()
if self.ENABLE_SPINNER:
self._spinner.stop()

def create_toolbar(self, toolbar_id):
"""
Expand Down Expand Up @@ -740,6 +776,18 @@ def get_main_toolbar(self):
"""
return self._main_toolbar

def get_auxiliary_toolbars(self):
"""
Return the auxiliary toolbars of the plugin.
Returns
-------
OrderedDict
A dictionary of wirh toolbar IDs as keys and auxiliary toolbars as
values.
"""
return self._auxiliary_toolbars

def set_icon_size(self, icon_size):
"""
Set the icon size of the plugin's toolbars.
Expand Down Expand Up @@ -826,6 +874,22 @@ def get_icon(self):
"""
return self._icon

def render_toolbars(self):
"""
Render all the toolbars of the widget.
Notes
-----
This action can only be performed once.
"""
# if not self._toolbars_already_rendered:
self._main_toolbar._render()
self._corner_toolbar._render()
for __, toolbar in self._auxiliary_toolbars.items():
toolbar._render()

# self._toolbars_already_rendered = True

# --- SpyderDockwidget handling ------------------------------------------
# ------------------------------------------------------------------------
@Slot()
Expand Down
6 changes: 4 additions & 2 deletions spyder/plugins/findinfiles/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,7 @@ class FindInFilesWidget(PluginMainWidget):
'supported_encodings': ("utf-8", "iso-8859-1", "cp1252"),
'text_color': MAIN_TEXT_COLOR,
}
ENABLE_SPINNER = True
REGEX_INVALID = "background-color:rgb(255, 80, 80);"
REGEX_ERROR = _("Regular expression error")

Expand Down Expand Up @@ -1046,8 +1047,9 @@ def update_actions(self):
w1 = widget.fontMetrics().width(stop_text)
w2 = widget.fontMetrics().width(search_text)
# Ensure the search/stop button has the same size independent on
# the length of the words.
width = self.get_options_menu_button().width() + max([w1, w2]) + 2
# the length of the words. The 10 additional pixels account for
# operating system spacing differences
width = self.get_options_menu_button().width() + max([w1, w2]) + 10
widget.setMinimumWidth(width)

if self.extras_toolbar and self.more_options_action:
Expand Down
17 changes: 14 additions & 3 deletions spyder/widgets/tabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,16 +256,27 @@ def __init__(self, parent, actions=None, menu=None,
self.corner_widgets = {}
self.menu_use_tooltips = menu_use_tooltips

self.setStyleSheet("QTabWidget::tab-bar {"
"alignment: left;}")

if menu is None:
self.menu = QMenu(self)
if actions:
add_actions(self.menu, actions)
else:
self.menu = menu

# QTabBar forces the corner widgets to be smaller than they should on
# some plugins, like History. The top margin added allows the
# toolbuttons to expand to their normal size.
# See: spyder-ide/spyder#13600
self.setStyleSheet("""
QTabBar::tab {
margin-top: 5px;
}
QTabWidget::tab-bar {
alignment: left;
}
"""
)

# Corner widgets
if corner_widgets is None:
corner_widgets = {}
Expand Down

0 comments on commit 289fc42

Please sign in to comment.