11from __future__ import annotations
22
3+ from typing import TYPE_CHECKING , Any
4+
35import moderngl_window as mglw
46from moderngl_window .context .pyglet .window import Window as PygletWindow
57from moderngl_window .timers .clock import Timer
6- from screeninfo import get_monitors
8+ from screeninfo import Monitor , get_monitors
79
810from .. import __version__ , config
911
12+ if TYPE_CHECKING :
13+ from .opengl_renderer import OpenGLRenderer
14+
1015__all__ = ["Window" ]
1116
1217
@@ -17,15 +22,19 @@ class Window(PygletWindow):
1722 vsync = True
1823 cursor = True
1924
20- def __init__ (self , renderer , size = config .window_size , ** kwargs ):
25+ def __init__ (
26+ self ,
27+ renderer : OpenGLRenderer ,
28+ window_size : str = config .window_size ,
29+ ** kwargs : Any ,
30+ ) -> None :
2131 monitors = get_monitors ()
2232 mon_index = config .window_monitor
2333 monitor = monitors [min (mon_index , len (monitors ) - 1 )]
2434
25- if size == "default" :
35+ if window_size == "default" :
2636 # make window_width half the width of the monitor
2737 # but make it full screen if --fullscreen
28-
2938 window_width = monitor .width
3039 if not config .fullscreen :
3140 window_width //= 2
@@ -35,8 +44,13 @@ def __init__(self, renderer, size=config.window_size, **kwargs):
3544 window_width * config .frame_height // config .frame_width ,
3645 )
3746 size = (window_width , window_height )
47+ elif len (window_size .split ("," )) == 2 :
48+ (window_width , window_height ) = tuple (map (int , window_size .split ("," )))
49+ size = (window_width , window_height )
3850 else :
39- size = tuple (size )
51+ raise ValueError (
52+ "Window_size must be specified as 'width,height' or 'default'." ,
53+ )
4054
4155 super ().__init__ (size = size )
4256
@@ -55,13 +69,13 @@ def __init__(self, renderer, size=config.window_size, **kwargs):
5569 self .position = initial_position
5670
5771 # Delegate event handling to scene.
58- def on_mouse_motion (self , x , y , dx , dy ) :
72+ def on_mouse_motion (self , x : int , y : int , dx : int , dy : int ) -> None :
5973 super ().on_mouse_motion (x , y , dx , dy )
6074 point = self .renderer .pixel_coords_to_space_coords (x , y )
6175 d_point = self .renderer .pixel_coords_to_space_coords (dx , dy , relative = True )
6276 self .renderer .scene .on_mouse_motion (point , d_point )
6377
64- def on_mouse_scroll (self , x , y , x_offset : float , y_offset : float ):
78+ def on_mouse_scroll (self , x : int , y : int , x_offset : float , y_offset : float ) -> None :
6579 super ().on_mouse_scroll (x , y , x_offset , y_offset )
6680 point = self .renderer .pixel_coords_to_space_coords (x , y )
6781 offset = self .renderer .pixel_coords_to_space_coords (
@@ -71,28 +85,32 @@ def on_mouse_scroll(self, x, y, x_offset: float, y_offset: float):
7185 )
7286 self .renderer .scene .on_mouse_scroll (point , offset )
7387
74- def on_key_press (self , symbol , modifiers ) :
88+ def on_key_press (self , symbol : int , modifiers : int ) -> bool :
7589 self .renderer .pressed_keys .add (symbol )
76- super ().on_key_press (symbol , modifiers )
90+ event_handled : bool = super ().on_key_press (symbol , modifiers )
7791 self .renderer .scene .on_key_press (symbol , modifiers )
92+ return event_handled
7893
79- def on_key_release (self , symbol , modifiers ) :
94+ def on_key_release (self , symbol : int , modifiers : int ) -> None :
8095 if symbol in self .renderer .pressed_keys :
8196 self .renderer .pressed_keys .remove (symbol )
8297 super ().on_key_release (symbol , modifiers )
8398 self .renderer .scene .on_key_release (symbol , modifiers )
8499
85- def on_mouse_drag (self , x , y , dx , dy , buttons , modifiers ):
100+ def on_mouse_drag (
101+ self , x : int , y : int , dx : int , dy : int , buttons : int , modifiers : int
102+ ) -> None :
86103 super ().on_mouse_drag (x , y , dx , dy , buttons , modifiers )
87104 point = self .renderer .pixel_coords_to_space_coords (x , y )
88105 d_point = self .renderer .pixel_coords_to_space_coords (dx , dy , relative = True )
89106 self .renderer .scene .on_mouse_drag (point , d_point , buttons , modifiers )
90107
91- def find_initial_position (self , size , monitor ):
108+ def find_initial_position (
109+ self , size : tuple [int , int ], monitor : Monitor
110+ ) -> tuple [int , int ]:
92111 custom_position = config .window_position
93112 window_width , window_height = size
94- # Position might be specified with a string of the form
95- # x,y for integers x and y
113+ # Position might be specified with a string of the form x,y for integers x and y
96114 if len (custom_position ) == 1 :
97115 raise ValueError (
98116 "window_position must specify both Y and X positions (Y/X -> UR). Also accepts LEFT/RIGHT/ORIGIN/UP/DOWN." ,
@@ -105,20 +123,21 @@ def find_initial_position(self, size, monitor):
105123 elif custom_position == "ORIGIN" :
106124 custom_position = "O" * 2
107125 elif "," in custom_position :
108- return tuple (map (int , custom_position .split ("," )))
126+ pos_y , pos_x = tuple (map (int , custom_position .split ("," )))
127+ return (pos_x , pos_y )
109128
110129 # Alternatively, it might be specified with a string like
111130 # UR, OO, DL, etc. specifying what corner it should go to
112131 char_to_n = {"L" : 0 , "U" : 0 , "O" : 1 , "R" : 2 , "D" : 2 }
113- width_diff = monitor .width - window_width
114- height_diff = monitor .height - window_height
132+ width_diff : int = monitor .width - window_width
133+ height_diff : int = monitor .height - window_height
115134
116135 return (
117136 monitor .x + char_to_n [custom_position [1 ]] * width_diff // 2 ,
118137 - monitor .y + char_to_n [custom_position [0 ]] * height_diff // 2 ,
119138 )
120139
121- def on_mouse_press (self , x , y , button , modifiers ) :
140+ def on_mouse_press (self , x : int , y : int , button : int , modifiers : int ) -> None :
122141 super ().on_mouse_press (x , y , button , modifiers )
123142 point = self .renderer .pixel_coords_to_space_coords (x , y )
124143 mouse_button_map = {
0 commit comments