66import napari
77import numpy as np
88from funtracks .data_model import NodeType , Tracks
9+ from napari .layers .points ._points_mouse_bindings import select
910from napari .utils .notifications import show_info
1011from psygnal import Signal
1112
2526 from motile_tracker .data_views .views_coordinator .tracks_viewer import TracksViewer
2627
2728
29+ def custom_select (layer : napari .layers .Points , event : Event ):
30+ """Block the current_size signal when selecting points to avoid changing the point
31+ size by accident."""
32+
33+ with layer .events .current_size .blocker ():
34+ yield from select (layer , event )
35+
36+
2837class TrackPoints (napari .layers .Points ):
2938 """Extended points layer that holds the track information and emits and
3039 responds to dynamics visualization signals
3140 """
3241
42+ # overwrite the select function to block the current_size event signal
43+ _drag_modes = napari .layers .Points ._drag_modes .copy ()
44+ _drag_modes [napari .layers .Points ._modeclass .SELECT ] = custom_select
3345 data_updated = Signal ()
3446
3547 @property
@@ -89,7 +101,7 @@ def click(layer, event):
89101 # listen to updates of the data
90102 self .events .data .connect (self ._update_data )
91103
92- # connect to changing the point size in the UI
104+ # connect to changing the point size in the UI (see note)
93105 self .events .current_size .connect (
94106 lambda : self .set_point_size (size = self .current_size )
95107 )
@@ -98,6 +110,13 @@ def click(layer, event):
98110 # to update the nodes in self.tracks_viewer.selected_nodes
99111 self .selected_data .events .items_changed .connect (self ._update_selection )
100112
113+ def add (self , coords : list [float ]):
114+ """Block the current_size event before calling the 'add' function to avoid calling
115+ set_point_size (triggered by the current_size event) with a new point size."""
116+
117+ with self .events .current_size .blocker ():
118+ super ().add (coords )
119+
101120 def process_click (self , event : Event , point_index : int | None ):
102121 """Select the clicked point(s)"""
103122
@@ -109,7 +128,12 @@ def process_click(self, event: Event, point_index: int | None):
109128 self .tracks_viewer .selected_nodes .add (node_id , append )
110129
111130 def set_point_size (self , size : int ) -> None :
112- """Sets a new default point size"""
131+ """Sets a new default point size.
132+ NOTE: This function call is triggered by the current_size event, which is emitted
133+ when the user moves the 'point size' slider in the layer controls. However, this
134+ event is also emitted in the 'add' and 'select' functions, so we have to block the
135+ signals there to avoid increasing the point size by accident, since new or
136+ selected points are displayed at a 30% bigger size."""
113137
114138 self .default_size = size
115139 self ._refresh ()
@@ -160,7 +184,7 @@ def _create_node_attrs(self, new_point: np.array) -> tuple[np.array, dict]:
160184 }
161185 return attributes
162186
163- def _update_data (self , event ):
187+ def _update_data (self , event : Event ):
164188 """Calls the tracks controller with to update the data in the Tracks object and
165189 dispatch the update
166190 """
0 commit comments