Skip to content

Commit 1518fd0

Browse files
committed
Only update layers when selection is valid
1 parent dc326e7 commit 1518fd0

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

docs/changelog.rst

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
Changelog
22
=========
3+
4+
2.0.2
5+
-----
6+
Bug fixes
7+
~~~~~~~~~
8+
- Only trigger layer update code paths if the layer selection is valid for the current
9+
widget. This prevents errors when e.g., a labels layer is selected when a widget
10+
that does not support a labels layer is open.
11+
312
2.0.1
413
-----
514
Bug fixes

src/napari_matplotlib/base.py

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

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

237247
def _draw(self) -> None:
@@ -244,10 +254,7 @@ def _draw(self) -> None:
244254
with mplstyle.context(self.napari_theme_style_sheet):
245255
# everything should be done in the style context
246256
self.clear()
247-
if self.n_selected_layers in self.n_layers_input and all(
248-
isinstance(layer, self.input_layer_types)
249-
for layer in self.layers
250-
):
257+
if self._valid_layer_selection:
251258
self.draw()
252259
self.canvas.draw() # type: ignore[no-untyped-call]
253260

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

275283

0 commit comments

Comments
 (0)