Skip to content

Commit

Permalink
Merge pull request napari#86 from goanpeca/fix/counter-on-refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
goanpeca authored Jul 31, 2024
2 parents e6187fc + a0fda43 commit d5bba24
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions napari_plugin_manager/qt_plugin_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,14 @@ def __init__(self, parent=None, prefix=None) -> None:
self._plugin_data_map = {}
self._add_items_timer = QTimer(self)

# Timer to avoid race conditions and incorrect count of plugins when
# refreshing multiple times in a row. After click we disable the
# `Refresh` button and re-enable it after 3 seconds.
self._refresh_timer = QTimer(self)
self._refresh_timer.setInterval(3000) # ms
self._refresh_timer.setSingleShot(True)
self._refresh_timer.timeout.connect(self._enable_refresh_button)

# Add items in batches with a pause to avoid blocking the UI
self._add_items_timer.setInterval(61) # ms
self._add_items_timer.timeout.connect(self._add_items)
Expand All @@ -897,6 +905,9 @@ def __init__(self, parent=None, prefix=None) -> None:

# region - Private methods
# ------------------------------------------------------------------------
def _enable_refresh_button(self):
self.refresh_button.setEnabled(True)

def _quit(self):
self.close()
with contextlib.suppress(AttributeError):
Expand Down Expand Up @@ -1269,10 +1280,12 @@ def _install_packages(
def _tag_outdated_plugins(self):
"""Tag installed plugins that might be outdated."""
for pkg_name in self.installed_list.packages():
metadata, is_available_in_conda, _ = self._plugin_data_map.get(
pkg_name
)
self.installed_list.tag_outdated(metadata, is_available_in_conda)
_data = self._plugin_data_map.get(pkg_name)
if _data is not None:
metadata, is_available_in_conda, _ = _data
self.installed_list.tag_outdated(
metadata, is_available_in_conda
)

def _add_items(self):
"""
Expand Down Expand Up @@ -1427,6 +1440,11 @@ def filter(self, text: Optional[str] = None, skip=False) -> None:
self._update_plugin_count()

def refresh(self, clear_cache: bool = False):
self.refresh_button.setDisabled(True)

if self.worker is not None:
self.worker.quit()

if self._add_items_timer.isActive():
self._add_items_timer.stop()

Expand All @@ -1443,6 +1461,8 @@ def refresh(self, clear_cache: bool = False):
self._add_installed()
self._fetch_available_plugins(clear_cache=clear_cache)

self._refresh_timer.start()

def toggle_status(self, show=None):
show = not self.stdout_text.isVisible() if show is None else show
if show:
Expand Down

0 comments on commit d5bba24

Please sign in to comment.