11
11
12
12
from ._enums import (
13
13
EventTypeEnum ,
14
- UpdateMode ,
15
14
UpdateModeEnum ,
16
15
CursorShape ,
17
16
CursorShapeEnum ,
45
44
class BaseCanvasGroup :
46
45
"""Represents a group of canvas objects from the same class, that share a loop."""
47
46
48
- def __init__ (self , default_loop ):
47
+ def __init__ (self , default_loop : BaseLoop ):
49
48
self ._canvases = weakref .WeakSet ()
50
49
self ._loop = None
51
50
self .select_loop (default_loop )
@@ -71,11 +70,11 @@ def select_loop(self, loop: BaseLoop) -> None:
71
70
self ._loop ._unregister_canvas_group (self )
72
71
self ._loop = loop
73
72
74
- def get_loop (self ) -> BaseLoop :
73
+ def get_loop (self ) -> BaseLoop | None :
75
74
"""Get the currently associated loop (can be None for canvases that don't run a scheduler)."""
76
75
return self ._loop
77
76
78
- def get_canvases (self ) -> List [" BaseRenderCanvas" ]:
77
+ def get_canvases (self ) -> List [BaseRenderCanvas ]:
79
78
"""Get a list of currently active (not-closed) canvases for this group."""
80
79
return [canvas for canvas in self ._canvases if not canvas .get_closed ()]
81
80
@@ -123,7 +122,7 @@ def select_loop(cls, loop: BaseLoop) -> None:
123
122
def __init__ (
124
123
self ,
125
124
* args ,
126
- size : Tuple [int ] = (640 , 480 ),
125
+ size : Tuple [int , int ] = (640 , 480 ),
127
126
title : str = "$backend" ,
128
127
update_mode : UpdateModeEnum = "ondemand" ,
129
128
min_fps : float = 0.0 ,
@@ -190,8 +189,8 @@ def _final_canvas_init(self):
190
189
del self .__kwargs_for_later
191
190
# Apply
192
191
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
195
194
196
195
def __del__ (self ):
197
196
# On delete, we call the custom destroy method.
@@ -202,15 +201,15 @@ def __del__(self):
202
201
# Since this is sometimes used in a multiple inheritance, the
203
202
# superclass may (or may not) have a __del__ method.
204
203
try :
205
- super ().__del__ ()
204
+ super ().__del__ () # type: ignore
206
205
except Exception :
207
206
pass
208
207
209
208
# %% Implement WgpuCanvasInterface
210
209
211
210
_canvas_context = None # set in get_context()
212
211
213
- def get_physical_size (self ) -> Tuple [int ]:
212
+ def get_physical_size (self ) -> Tuple [int , int ]:
214
213
"""Get the physical size of the canvas in integer pixels."""
215
214
return self ._rc_get_physical_size ()
216
215
@@ -368,7 +367,10 @@ def set_update_mode(
368
367
max_fps (float): The maximum fps with update mode 'ondemand' and 'continuous'.
369
368
370
369
"""
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
+ )
372
374
373
375
def request_draw (self , draw_function : Optional [DrawFunction ] = None ) -> None :
374
376
"""Schedule a new draw event.
@@ -463,7 +465,7 @@ def _draw_frame_and_present(self):
463
465
464
466
# %% Primary canvas management methods
465
467
466
- def get_logical_size (self ) -> Tuple [float ]:
468
+ def get_logical_size (self ) -> Tuple [float , float ]:
467
469
"""Get the logical size (width, height) in float pixels.
468
470
469
471
The logical size can be smaller than the physical size, e.g. on HiDPI
@@ -485,12 +487,12 @@ def get_pixel_ratio(self) -> float:
485
487
def close (self ) -> None :
486
488
"""Close the canvas."""
487
489
# 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
489
491
# Clear the canvas context too.
490
492
if hasattr (self ._canvas_context , "_release" ):
491
493
# ContextInterface (and GPUCanvasContext) has _release()
492
494
try :
493
- self ._canvas_context ._release ()
495
+ self ._canvas_context ._release () # type: ignore
494
496
except Exception :
495
497
pass
496
498
self ._canvas_context = None
@@ -541,12 +543,12 @@ def set_cursor(self, cursor: CursorShapeEnum) -> None:
541
543
cursor = "default"
542
544
if not isinstance (cursor , str ):
543
545
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 :
546
548
raise ValueError (
547
549
f"Canvas cursor { cursor !r} not known, must be one of { CursorShape } "
548
550
)
549
- self ._rc_set_cursor (cursor )
551
+ self ._rc_set_cursor (cursor_normed )
550
552
551
553
# %% Methods for the subclass to implement
552
554
@@ -609,19 +611,19 @@ def _rc_present_bitmap(self, *, data, format, **kwargs):
609
611
"""
610
612
raise NotImplementedError ()
611
613
612
- def _rc_get_physical_size (self ):
614
+ def _rc_get_physical_size (self ) -> Tuple [ int , int ] :
613
615
"""Get the physical size (with, height) in integer pixels."""
614
616
raise NotImplementedError ()
615
617
616
- def _rc_get_logical_size (self ):
618
+ def _rc_get_logical_size (self ) -> Tuple [ float , float ] :
617
619
"""Get the logical size (with, height) in float pixels."""
618
620
raise NotImplementedError ()
619
621
620
- def _rc_get_pixel_ratio (self ):
622
+ def _rc_get_pixel_ratio (self ) -> float :
621
623
"""Get ratio between physical and logical size."""
622
624
raise NotImplementedError ()
623
625
624
- def _rc_set_logical_size (self , width , height ):
626
+ def _rc_set_logical_size (self , width : float , height : float ):
625
627
"""Set the logical size. May be ignired when it makes no sense.
626
628
627
629
The default implementation does nothing.
@@ -644,18 +646,18 @@ def _rc_close(self):
644
646
"""
645
647
pass
646
648
647
- def _rc_get_closed (self ):
649
+ def _rc_get_closed (self ) -> bool :
648
650
"""Get whether the canvas is closed."""
649
651
return False
650
652
651
- def _rc_set_title (self , title ):
653
+ def _rc_set_title (self , title : str ):
652
654
"""Set the canvas title. May be ignored when it makes no sense.
653
655
654
656
The default implementation does nothing.
655
657
"""
656
658
pass
657
659
658
- def _rc_set_cursor (self , cursor ):
660
+ def _rc_set_cursor (self , cursor : str ):
659
661
"""Set the cursor shape. May be ignored.
660
662
661
663
The default implementation does nothing.
@@ -672,15 +674,16 @@ class WrapperRenderCanvas(BaseRenderCanvas):
672
674
"""
673
675
674
676
_rc_canvas_group = None # No grouping for these wrappers
677
+ _subwidget : BaseRenderCanvas
675
678
676
679
@classmethod
677
680
def select_loop (cls , loop : BaseLoop ) -> None :
678
681
m = sys .modules [cls .__module__ ]
679
682
return m .RenderWidget .select_loop (loop )
680
683
681
684
def add_event_handler (
682
- self , * args : str | EventHandlerFunction , order : float = 0
683
- ) -> None :
685
+ self , * args : EventTypeEnum | EventHandlerFunction , order : float = 0
686
+ ) -> Callable :
684
687
return self ._subwidget ._events .add_handler (* args , order = order )
685
688
686
689
def remove_event_handler (self , callback : EventHandlerFunction , * types : str ) -> None :
@@ -694,7 +697,7 @@ def get_context(self, context_type: str) -> object:
694
697
695
698
def set_update_mode (
696
699
self ,
697
- update_mode : UpdateMode ,
700
+ update_mode : UpdateModeEnum ,
698
701
* ,
699
702
min_fps : Optional [float ] = None ,
700
703
max_fps : Optional [float ] = None ,
@@ -707,10 +710,10 @@ def request_draw(self, draw_function: Optional[DrawFunction] = None) -> None:
707
710
def force_draw (self ) -> None :
708
711
self ._subwidget .force_draw ()
709
712
710
- def get_physical_size (self ) -> Tuple [int ]:
713
+ def get_physical_size (self ) -> Tuple [int , int ]:
711
714
return self ._subwidget .get_physical_size ()
712
715
713
- def get_logical_size (self ) -> Tuple [float ]:
716
+ def get_logical_size (self ) -> Tuple [float , float ]:
714
717
return self ._subwidget .get_logical_size ()
715
718
716
719
def get_pixel_ratio (self ) -> float :
0 commit comments