@@ -2342,11 +2342,11 @@ def key_press_handler(event, canvas, toolbar=None):
23422342 # pan mnemonic (default key 'p')
23432343 elif event .key in pan_keys :
23442344 toolbar .pan ()
2345- toolbar ._set_cursor (event )
2345+ toolbar ._update_cursor (event )
23462346 # zoom mnemonic (default key 'o')
23472347 elif event .key in zoom_keys :
23482348 toolbar .zoom ()
2349- toolbar ._set_cursor (event )
2349+ toolbar ._update_cursor (event )
23502350 # saving current figure (default key 's')
23512351 elif event .key in save_keys :
23522352 toolbar .save_figure ()
@@ -2700,7 +2700,10 @@ class implementation.
27002700 """
27012701 raise NotImplementedError
27022702
2703- def _set_cursor (self , event ):
2703+ def _update_cursor (self , event ):
2704+ """
2705+ Update the cursor after a mouse move event or a tool (de)activation.
2706+ """
27042707 if not event .inaxes or not self ._active :
27052708 if self ._lastCursor != cursors .POINTER :
27062709 self .set_cursor (cursors .POINTER )
@@ -2715,8 +2718,30 @@ def _set_cursor(self, event):
27152718 self .set_cursor (cursors .MOVE )
27162719 self ._lastCursor = cursors .MOVE
27172720
2721+ @contextmanager
2722+ def _wait_cursor_for_draw_cm (self ):
2723+ """
2724+ Set the cursor to a wait cursor when drawing the canvas.
2725+
2726+ In order to avoid constantly changing the cursor when the canvas
2727+ changes frequently, do nothing if this context was triggered during the
2728+ last second. (Optimally we'd prefer only setting the wait cursor if
2729+ the *current* draw takes too long, but the current draw blocks the GUI
2730+ thread).
2731+ """
2732+ self ._draw_time , last_draw_time = (
2733+ time .time (), getattr (self , "_draw_time" , - np .inf ))
2734+ if self ._draw_time - last_draw_time > 1 :
2735+ try :
2736+ self .set_cursor (cursors .WAIT )
2737+ yield
2738+ finally :
2739+ self .set_cursor (self ._lastCursor )
2740+ else :
2741+ yield
2742+
27182743 def mouse_move (self , event ):
2719- self ._set_cursor (event )
2744+ self ._update_cursor (event )
27202745
27212746 if event .inaxes and event .inaxes .get_navigate ():
27222747
0 commit comments