1111
1212from ._enums import (
1313 EventTypeEnum ,
14- UpdateMode ,
1514 UpdateModeEnum ,
1615 CursorShape ,
1716 CursorShapeEnum ,
4544class BaseCanvasGroup :
4645 """Represents a group of canvas objects from the same class, that share a loop."""
4746
48- def __init__ (self , default_loop ):
47+ def __init__ (self , default_loop : BaseLoop ):
4948 self ._canvases = weakref .WeakSet ()
5049 self ._loop = None
5150 self .select_loop (default_loop )
@@ -71,11 +70,11 @@ def select_loop(self, loop: BaseLoop) -> None:
7170 self ._loop ._unregister_canvas_group (self )
7271 self ._loop = loop
7372
74- def get_loop (self ) -> BaseLoop :
73+ def get_loop (self ) -> BaseLoop | None :
7574 """Get the currently associated loop (can be None for canvases that don't run a scheduler)."""
7675 return self ._loop
7776
78- def get_canvases (self ) -> List [" BaseRenderCanvas" ]:
77+ def get_canvases (self ) -> List [BaseRenderCanvas ]:
7978 """Get a list of currently active (not-closed) canvases for this group."""
8079 return [canvas for canvas in self ._canvases if not canvas .get_closed ()]
8180
@@ -123,7 +122,7 @@ def select_loop(cls, loop: BaseLoop) -> None:
123122 def __init__ (
124123 self ,
125124 * args ,
126- size : Tuple [int ] = (640 , 480 ),
125+ size : Tuple [int , int ] = (640 , 480 ),
127126 title : str = "$backend" ,
128127 update_mode : UpdateModeEnum = "ondemand" ,
129128 min_fps : float = 0.0 ,
@@ -190,8 +189,8 @@ def _final_canvas_init(self):
190189 del self .__kwargs_for_later
191190 # Apply
192191 if not isinstance (self , WrapperRenderCanvas ):
193- self .set_logical_size (* kwargs ["size" ])
194- self .set_title (kwargs ["title" ])
192+ self .set_logical_size (* kwargs ["size" ]) # type: ignore
193+ self .set_title (kwargs ["title" ]) # type: ignore
195194
196195 def __del__ (self ):
197196 # On delete, we call the custom destroy method.
@@ -202,15 +201,15 @@ def __del__(self):
202201 # Since this is sometimes used in a multiple inheritance, the
203202 # superclass may (or may not) have a __del__ method.
204203 try :
205- super ().__del__ ()
204+ super ().__del__ () # type: ignore
206205 except Exception :
207206 pass
208207
209208 # %% Implement WgpuCanvasInterface
210209
211210 _canvas_context = None # set in get_context()
212211
213- def get_physical_size (self ) -> Tuple [int ]:
212+ def get_physical_size (self ) -> Tuple [int , int ]:
214213 """Get the physical size of the canvas in integer pixels."""
215214 return self ._rc_get_physical_size ()
216215
@@ -368,7 +367,10 @@ def set_update_mode(
368367 max_fps (float): The maximum fps with update mode 'ondemand' and 'continuous'.
369368
370369 """
371- self .__scheduler .set_update_mode (update_mode , min_fps = min_fps , max_fps = max_fps )
370+ if self .__scheduler is not None :
371+ self .__scheduler .set_update_mode (
372+ update_mode , min_fps = min_fps , max_fps = max_fps
373+ )
372374
373375 def request_draw (self , draw_function : Optional [DrawFunction ] = None ) -> None :
374376 """Schedule a new draw event.
@@ -463,7 +465,7 @@ def _draw_frame_and_present(self):
463465
464466 # %% Primary canvas management methods
465467
466- def get_logical_size (self ) -> Tuple [float ]:
468+ def get_logical_size (self ) -> Tuple [float , float ]:
467469 """Get the logical size (width, height) in float pixels.
468470
469471 The logical size can be smaller than the physical size, e.g. on HiDPI
@@ -485,12 +487,12 @@ def get_pixel_ratio(self) -> float:
485487 def close (self ) -> None :
486488 """Close the canvas."""
487489 # Clear the draw-function, to avoid it holding onto e.g. wgpu objects.
488- self ._draw_frame = None
490+ self ._draw_frame = None # type: ignore
489491 # Clear the canvas context too.
490492 if hasattr (self ._canvas_context , "_release" ):
491493 # ContextInterface (and GPUCanvasContext) has _release()
492494 try :
493- self ._canvas_context ._release ()
495+ self ._canvas_context ._release () # type: ignore
494496 except Exception :
495497 pass
496498 self ._canvas_context = None
@@ -541,12 +543,12 @@ def set_cursor(self, cursor: CursorShapeEnum) -> None:
541543 cursor = "default"
542544 if not isinstance (cursor , str ):
543545 raise TypeError ("Canvas cursor must be str." )
544- cursor = cursor .lower ().replace ("_" , "-" )
545- if cursor not in CursorShape :
546+ cursor_normed = cursor .lower ().replace ("_" , "-" )
547+ if cursor_normed not in CursorShape :
546548 raise ValueError (
547549 f"Canvas cursor { cursor !r} not known, must be one of { CursorShape } "
548550 )
549- self ._rc_set_cursor (cursor )
551+ self ._rc_set_cursor (cursor_normed )
550552
551553 # %% Methods for the subclass to implement
552554
@@ -609,19 +611,19 @@ def _rc_present_bitmap(self, *, data, format, **kwargs):
609611 """
610612 raise NotImplementedError ()
611613
612- def _rc_get_physical_size (self ):
614+ def _rc_get_physical_size (self ) -> Tuple [ int , int ] :
613615 """Get the physical size (with, height) in integer pixels."""
614616 raise NotImplementedError ()
615617
616- def _rc_get_logical_size (self ):
618+ def _rc_get_logical_size (self ) -> Tuple [ float , float ] :
617619 """Get the logical size (with, height) in float pixels."""
618620 raise NotImplementedError ()
619621
620- def _rc_get_pixel_ratio (self ):
622+ def _rc_get_pixel_ratio (self ) -> float :
621623 """Get ratio between physical and logical size."""
622624 raise NotImplementedError ()
623625
624- def _rc_set_logical_size (self , width , height ):
626+ def _rc_set_logical_size (self , width : float , height : float ):
625627 """Set the logical size. May be ignired when it makes no sense.
626628
627629 The default implementation does nothing.
@@ -644,18 +646,18 @@ def _rc_close(self):
644646 """
645647 pass
646648
647- def _rc_get_closed (self ):
649+ def _rc_get_closed (self ) -> bool :
648650 """Get whether the canvas is closed."""
649651 return False
650652
651- def _rc_set_title (self , title ):
653+ def _rc_set_title (self , title : str ):
652654 """Set the canvas title. May be ignored when it makes no sense.
653655
654656 The default implementation does nothing.
655657 """
656658 pass
657659
658- def _rc_set_cursor (self , cursor ):
660+ def _rc_set_cursor (self , cursor : str ):
659661 """Set the cursor shape. May be ignored.
660662
661663 The default implementation does nothing.
@@ -672,15 +674,16 @@ class WrapperRenderCanvas(BaseRenderCanvas):
672674 """
673675
674676 _rc_canvas_group = None # No grouping for these wrappers
677+ _subwidget : BaseRenderCanvas
675678
676679 @classmethod
677680 def select_loop (cls , loop : BaseLoop ) -> None :
678681 m = sys .modules [cls .__module__ ]
679682 return m .RenderWidget .select_loop (loop )
680683
681684 def add_event_handler (
682- self , * args : str | EventHandlerFunction , order : float = 0
683- ) -> None :
685+ self , * args : EventTypeEnum | EventHandlerFunction , order : float = 0
686+ ) -> Callable :
684687 return self ._subwidget ._events .add_handler (* args , order = order )
685688
686689 def remove_event_handler (self , callback : EventHandlerFunction , * types : str ) -> None :
@@ -694,7 +697,7 @@ def get_context(self, context_type: str) -> object:
694697
695698 def set_update_mode (
696699 self ,
697- update_mode : UpdateMode ,
700+ update_mode : UpdateModeEnum ,
698701 * ,
699702 min_fps : Optional [float ] = None ,
700703 max_fps : Optional [float ] = None ,
@@ -707,10 +710,10 @@ def request_draw(self, draw_function: Optional[DrawFunction] = None) -> None:
707710 def force_draw (self ) -> None :
708711 self ._subwidget .force_draw ()
709712
710- def get_physical_size (self ) -> Tuple [int ]:
713+ def get_physical_size (self ) -> Tuple [int , int ]:
711714 return self ._subwidget .get_physical_size ()
712715
713- def get_logical_size (self ) -> Tuple [float ]:
716+ def get_logical_size (self ) -> Tuple [float , float ]:
714717 return self ._subwidget .get_logical_size ()
715718
716719 def get_pixel_ratio (self ) -> float :
0 commit comments