Skip to content

Commit db64309

Browse files
Phil-Duacursoragent
andcommitted
Fix ROI shape-layer desync when switching drawing modes.
When napari emits shape-count drops during tool switches, the canvas could lose previously drawn ROIs even though they remained in the ROI manager list. Add a guarded resync path that repopulates the shapes layer from canonical ROI state, and reassert active-image context before recreating the shapes layer. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 11a0bad commit db64309

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/napari_curvealign/widget.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,10 @@ def _recreate_shapes_layer(self) -> Optional[napari.layers.Shapes]:
825825
self.roi_manager.shapes_layer = None
826826
layer = self.roi_manager.create_shapes_layer()
827827
self._connect_shapes_events(layer)
828+
# Ensure active-image scoping is current before repopulating shapes.
829+
active_shape = self.current_image_shape
830+
if active_shape:
831+
self.roi_manager.set_active_image(self._active_image_label(), active_shape)
828832
self.roi_manager._update_shapes_layer()
829833
return layer
830834

@@ -934,8 +938,20 @@ def on_data_change(event):
934938
finally:
935939
self._syncing_shapes = False
936940
elif count_diff < 0:
937-
# Shape deleted - just update count
941+
# Shape deleted (or layer switched) - keep counts in sync.
938942
layer._curvealign_last_shape_count = current_count
943+
# If the canvas now has fewer shapes than the ROI model for this image,
944+
# restore from canonical ROI state to prevent visual disappearance.
945+
expected_count = len(self.roi_manager.get_rois_for_active_image())
946+
if current_count < expected_count:
947+
try:
948+
self._syncing_shapes = True
949+
self.roi_manager._update_shapes_layer()
950+
layer._curvealign_last_shape_count = len(layer.data)
951+
except Exception as exc:
952+
print(f"Warning: failed to resync ROI shapes after mode switch: {exc}")
953+
finally:
954+
self._syncing_shapes = False
939955

940956
layer.events.data.connect(on_data_change)
941957
layer._curvealign_events_connected = True

0 commit comments

Comments
 (0)