@@ -40,9 +40,7 @@ def __init__(self, napari_viewer: Viewer):
4040 self .autocontrast = QCheckBox ("Auto-contrast on new images" )
4141 self .autocontrast .setChecked (True )
4242
43- self .btn_start = QPushButton ("Start" )
44- self .btn_stop = QPushButton ("Stop" )
45- self .btn_stop .setEnabled (False )
43+ self .btn_run = QPushButton ("Start" )
4644 self .btn_copy = QPushButton ("Copy Endpoint" )
4745
4846 top = QVBoxLayout (self )
@@ -55,16 +53,22 @@ def __init__(self, napari_viewer: Viewer):
5553 top .addWidget (self .autocontrast )
5654 top .addWidget (self .status_label )
5755 row2 = QHBoxLayout ()
58- row2 .addWidget (self .btn_start )
59- row2 .addWidget (self .btn_stop )
56+ row2 .addWidget (self .btn_run )
6057 top .addLayout (row2 )
6158
62- self .btn_start .clicked .connect (self ._on_start )
63- self .btn_stop .clicked .connect (self ._on_stop )
59+ self .btn_run .clicked .connect (self ._on_toggle_clicked )
6460 self .btn_copy .clicked .connect (self ._copy_endpoint )
6561 self .public_access .stateChanged .connect (self ._on_public_toggled )
6662
63+ def _on_toggle_clicked (self ):
64+ if self ._is_running ():
65+ self ._on_stop ()
66+ else :
67+ self ._on_start ()
68+
6769 def _on_start (self ):
70+ if self ._is_running ():
71+ return
6872 endpoint = self .endpoint_edit .text ().strip ()
6973 bind_endpoint = self ._resolve_endpoint_for_worker (endpoint )
7074 self ._thread = QThread ()
@@ -76,8 +80,8 @@ def _on_start(self):
7680 self ._worker .status .connect (self .status_label .setText )
7781 self ._worker .error .connect (self ._on_error )
7882
79- self .btn_start . setEnabled ( False )
80- self .btn_stop .setEnabled (True )
83+ self .btn_run . setText ( "Stop" )
84+ self .btn_run .setEnabled (True )
8185 self ._thread .start ()
8286
8387 def _on_stop (self ):
@@ -86,8 +90,10 @@ def _on_stop(self):
8690 if self ._thread is not None :
8791 self ._thread .quit ()
8892 self ._thread .wait ()
89- self .btn_start .setEnabled (True )
90- self .btn_stop .setEnabled (False )
93+ self ._thread = None
94+ self ._worker = None
95+ self .btn_run .setText ("Start" )
96+ self .btn_run .setEnabled (True )
9197
9298 def _on_public_toggled (self , checked : int ):
9399 is_public = bool (checked )
@@ -96,6 +102,8 @@ def _on_public_toggled(self, checked: int):
96102 if current == self ._last_auto_endpoint :
97103 self .endpoint_edit .setText (suggested )
98104 self ._last_auto_endpoint = suggested
105+ if self ._is_running ():
106+ self ._restart_listener ()
99107
100108 def _copy_endpoint (self ):
101109 endpoint = self .endpoint_edit .text ().strip ()
@@ -112,6 +120,13 @@ def _resolve_endpoint_for_worker(self, endpoint: str) -> str:
112120 self ._last_auto_endpoint = fallback
113121 return bind_endpoint_for_public (fallback )
114122
123+ def _is_running (self ) -> bool :
124+ return self ._thread is not None and self ._thread .isRunning ()
125+
126+ def _restart_listener (self ):
127+ self ._on_stop ()
128+ self ._on_start ()
129+
115130 def _on_received (self , arr : np .ndarray , meta : dict ):
116131 name = meta .get ("name" , "array" )
117132 is_labels = bool (meta .get ("is_labels" , False ))
0 commit comments