From edef35cc9822ebff5d9cd0de65c7f659208afaaa Mon Sep 17 00:00:00 2001
From: David Stansby <dstansby@gmail.com>
Date: Fri, 19 May 2023 11:12:59 +0100
Subject: [PATCH] Clean up layer selection API

---
 docs/changelog.rst                 |  3 +++
 src/napari_matplotlib/base.py      | 19 +++++++++++++------
 src/napari_matplotlib/histogram.py |  2 +-
 src/napari_matplotlib/scatter.py   |  4 ++--
 src/napari_matplotlib/slice.py     |  2 +-
 5 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/docs/changelog.rst b/docs/changelog.rst
index be0ea082..6a5afa08 100644
--- a/docs/changelog.rst
+++ b/docs/changelog.rst
@@ -23,6 +23,9 @@ Changes
   you would be interested in please open an issue at https://github.com/matplotlib/napari-matplotlib.
 - Labels plotting with the features scatter widget no longer have underscores
   replaced with spaces.
+- ``NapariMPLWidget.update_layers()`` has been removed as it is intended to be
+  private API. Use `NapariMPLWidget.on_update_layers` instead to implement
+  funcitonality when layer selection is changed.
 
 Bug fixes
 ~~~~~~~~~
diff --git a/src/napari_matplotlib/base.py b/src/napari_matplotlib/base.py
index d6a39f4c..3cdcc69c 100644
--- a/src/napari_matplotlib/base.py
+++ b/src/napari_matplotlib/base.py
@@ -108,6 +108,12 @@ class NapariMPLWidget(MPLWidget):
     is changed in the napari viewer. To take advantage of this sub-classes
     should implement the ``clear()`` and ``draw()`` methods.
 
+        When both the z-step and layer selection is changed, ``clear()`` is called
+    and if the number a type of selected layers are valid for the widget
+    ``draw()`` is then called. When layer selection is changed ``on_update_layers()``
+    is also called, which can be useful e.g. for updating a layer list in a
+    selection widget.
+
     Attributes
     ----------
     viewer : `napari.Viewer`
@@ -157,14 +163,16 @@ def _setup_callbacks(self) -> None:
         # z-step changed in viewer
         self.viewer.dims.events.current_step.connect(self._draw)
         # Layer selection changed in viewer
-        self.viewer.layers.selection.events.changed.connect(self.update_layers)
+        self.viewer.layers.selection.events.changed.connect(
+            self._update_layers
+        )
 
-    def update_layers(self, event: napari.utils.events.Event) -> None:
+    def _update_layers(self, event: napari.utils.events.Event) -> None:
         """
         Update the ``layers`` attribute with currently selected layers and re-draw.
         """
         self.layers = list(self.viewer.layers.selection)
-        self._on_update_layers()
+        self.on_update_layers()
         self._draw()
 
     def _draw(self) -> None:
@@ -193,10 +201,9 @@ def draw(self) -> None:
         This is a no-op, and is intended for derived classes to override.
         """
 
-    def _on_update_layers(self) -> None:
+    def on_update_layers(self) -> None:
         """
-        Function is called when self.layers is updated via
-        ``self.update_layers()``.
+        Called when the selected layers are updated.
 
         This is a no-op, and is intended for derived classes to override.
         """
diff --git a/src/napari_matplotlib/histogram.py b/src/napari_matplotlib/histogram.py
index 04d91ed1..1e273e70 100644
--- a/src/napari_matplotlib/histogram.py
+++ b/src/napari_matplotlib/histogram.py
@@ -27,7 +27,7 @@ def __init__(
     ):
         super().__init__(napari_viewer, parent=parent)
         self.add_single_axes()
-        self.update_layers(None)
+        self._update_layers(None)
 
     def clear(self) -> None:
         """
diff --git a/src/napari_matplotlib/scatter.py b/src/napari_matplotlib/scatter.py
index 3c180a90..c439677b 100644
--- a/src/napari_matplotlib/scatter.py
+++ b/src/napari_matplotlib/scatter.py
@@ -132,7 +132,7 @@ def __init__(
             self.layout().addWidget(QLabel(f"{dim}-axis:"))
             self.layout().addWidget(self._selectors[dim])
 
-        self.update_layers(None)
+        self._update_layers(None)
 
     @property
     def x_axis_key(self) -> Union[str, None]:
@@ -230,7 +230,7 @@ def _get_data(self) -> Tuple[npt.NDArray[Any], npt.NDArray[Any], str, str]:
 
         return x, y, x_axis_name, y_axis_name
 
-    def _on_update_layers(self) -> None:
+    def on_update_layers(self) -> None:
         """
         Called when the layer selection changes by ``self.update_layers()``.
         """
diff --git a/src/napari_matplotlib/slice.py b/src/napari_matplotlib/slice.py
index ec44842e..54a13ea2 100644
--- a/src/napari_matplotlib/slice.py
+++ b/src/napari_matplotlib/slice.py
@@ -51,7 +51,7 @@ def __init__(
         for d in _dims_sel:
             self.slice_selectors[d].textChanged.connect(self._draw)
 
-        self.update_layers(None)
+        self._update_layers(None)
 
     @property
     def _layer(self) -> napari.layers.Layer: