Skip to content

Commit 74ceab9

Browse files
committed
Only update layers when selection is valid
1 parent 9a1147c commit 74ceab9

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

docs/changelog.rst

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ napari-matplotlib now adheres to `SPEC 0 <https://scientific-python.org/specs/sp
99
- Added support for Python 3.12
1010
- Added a minimum required numpy verison of 1.23
1111

12+
Bug fixes
13+
~~~~~~~~~
14+
- Only trigger layer update code paths if the layer selection is valid for the current
15+
widget. This prevents errors when e.g., a labels layer is selected when a widget
16+
that does not support a labels layer is open.
17+
1218
2.0.1
1319
-----
1420
Bug fixes

src/napari_matplotlib/base.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,23 @@ def _setup_callbacks(self) -> None:
224224
self._update_layers
225225
)
226226

227+
@property
228+
def _valid_layer_selection(self) -> bool:
229+
"""
230+
Return `True` if layer selection is valid.
231+
"""
232+
return self.n_selected_layers in self.n_layers_input and all(
233+
isinstance(layer, self.input_layer_types) for layer in self.layers
234+
)
235+
227236
def _update_layers(self, event: napari.utils.events.Event) -> None:
228237
"""
229238
Update the ``layers`` attribute with currently selected layers and re-draw.
230239
"""
231240
self.layers = list(self.viewer.layers.selection)
232241
self.layers = sorted(self.layers, key=lambda layer: layer.name)
233-
self.on_update_layers()
242+
if self._valid_layer_selection:
243+
self.on_update_layers()
234244
self._draw()
235245

236246
def _draw(self) -> None:
@@ -243,10 +253,7 @@ def _draw(self) -> None:
243253
with mplstyle.context(self.napari_theme_style_sheet):
244254
# everything should be done in the style context
245255
self.clear()
246-
if self.n_selected_layers in self.n_layers_input and all(
247-
isinstance(layer, self.input_layer_types)
248-
for layer in self.layers
249-
):
256+
if self._valid_layer_selection:
250257
self.draw()
251258
self.canvas.draw() # type: ignore[no-untyped-call]
252259

@@ -269,6 +276,7 @@ def on_update_layers(self) -> None:
269276
Called when the selected layers are updated.
270277
271278
This is a no-op, and is intended for derived classes to override.
279+
It is only called if a selected layer is one of the input layer types.
272280
"""
273281

274282

0 commit comments

Comments
 (0)