Skip to content

Commit 70dc256

Browse files
committed
Only update layers when selection is valid
1 parent a70cd37 commit 70dc256

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

docs/changelog.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ napari-matplotlib now adheres to `SPEC 0 <https://scientific-python.org/specs/sp
1313
- Pinned the maximum napari version to ``< 0.5``.
1414
Version 3.0 of ``napari-matplotlib`` will introduce support for ``napari`` version 0.5.
1515

16+
Bug fixes
17+
~~~~~~~~~
18+
- Only trigger layer update code paths if the layer selection is valid for the current
19+
widget. This prevents errors when e.g., a labels layer is selected when a widget
20+
that does not support a labels layer is open.
21+
1622
2.0.1
1723
-----
1824
Bug fixes

src/napari_matplotlib/base.py

Lines changed: 13 additions & 5 deletions
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)