@@ -3,7 +3,7 @@ use std::marker::PhantomData;
33use std:: rc:: Rc ;
44
55use winit:: application:: ApplicationHandler ;
6- use winit:: event:: { Event , WindowEvent } ;
6+ use winit:: event:: WindowEvent ;
77use winit:: event_loop:: { ActiveEventLoop , EventLoop } ;
88use winit:: window:: { Window , WindowAttributes , WindowId } ;
99
@@ -31,7 +31,7 @@ pub(crate) fn make_window(
3131}
3232
3333/// Easily constructable winit application.
34- pub ( crate ) struct WinitApp < T , S , Init , InitSurface , Handler > {
34+ pub ( crate ) struct WinitApp < T , S , Init , InitSurface , Handler , AboutToWaitHandler > {
3535 /// Closure to initialize `state`.
3636 init : Init ,
3737
@@ -41,6 +41,9 @@ pub(crate) struct WinitApp<T, S, Init, InitSurface, Handler> {
4141 /// Closure to run on window events.
4242 event : Handler ,
4343
44+ /// Closure to run on about_to_wait events.
45+ about_to_wait : AboutToWaitHandler ,
46+
4447 /// Contained state.
4548 state : Option < T > ,
4649
@@ -75,38 +78,63 @@ where
7578 }
7679
7780 /// Build a new application.
78- pub ( crate ) fn with_event_handler < F > ( self , handler : F ) -> WinitApp < T , S , Init , InitSurface , F >
81+ #[ allow( clippy:: type_complexity) ]
82+ pub ( crate ) fn with_event_handler < F > (
83+ self ,
84+ handler : F ,
85+ ) -> WinitApp < T , S , Init , InitSurface , F , impl FnMut ( & mut T , Option < & mut S > , & ActiveEventLoop ) >
7986 where
80- F : FnMut ( & mut T , Option < & mut S > , Event < ( ) > , & ActiveEventLoop ) ,
87+ F : FnMut ( & mut T , Option < & mut S > , WindowId , WindowEvent , & ActiveEventLoop ) ,
8188 {
82- WinitApp :: new ( self . init , self . init_surface , handler)
89+ WinitApp :: new ( self . init , self . init_surface , handler, |_ , _ , _| { } )
8390 }
8491}
8592
86- impl < T , S , Init , InitSurface , Handler > WinitApp < T , S , Init , InitSurface , Handler >
93+ impl < T , S , Init , InitSurface , Handler , AboutToWaitHandler >
94+ WinitApp < T , S , Init , InitSurface , Handler , AboutToWaitHandler >
8795where
8896 Init : FnMut ( & ActiveEventLoop ) -> T ,
8997 InitSurface : FnMut ( & ActiveEventLoop , & mut T ) -> S ,
90- Handler : FnMut ( & mut T , Option < & mut S > , Event < ( ) > , & ActiveEventLoop ) ,
98+ Handler : FnMut ( & mut T , Option < & mut S > , WindowId , WindowEvent , & ActiveEventLoop ) ,
99+ AboutToWaitHandler : FnMut ( & mut T , Option < & mut S > , & ActiveEventLoop ) ,
91100{
92101 /// Create a new application.
93- pub ( crate ) fn new ( init : Init , init_surface : InitSurface , event : Handler ) -> Self {
102+ pub ( crate ) fn new (
103+ init : Init ,
104+ init_surface : InitSurface ,
105+ event : Handler ,
106+ about_to_wait : AboutToWaitHandler ,
107+ ) -> Self {
94108 Self {
95109 init,
96110 init_surface,
97111 event,
112+ about_to_wait,
98113 state : None ,
99114 surface_state : None ,
100115 }
101116 }
117+
118+ /// Build a new application.
119+ #[ allow( dead_code) ]
120+ pub ( crate ) fn with_about_to_wait_handler < F > (
121+ self ,
122+ about_to_wait : F ,
123+ ) -> WinitApp < T , S , Init , InitSurface , Handler , F >
124+ where
125+ F : FnMut ( & mut T , Option < & mut S > , & ActiveEventLoop ) ,
126+ {
127+ WinitApp :: new ( self . init , self . init_surface , self . event , about_to_wait)
128+ }
102129}
103130
104- impl < T , S , Init , InitSurface , Handler > ApplicationHandler
105- for WinitApp < T , S , Init , InitSurface , Handler >
131+ impl < T , S , Init , InitSurface , Handler , AboutToWaitHandler > ApplicationHandler
132+ for WinitApp < T , S , Init , InitSurface , Handler , AboutToWaitHandler >
106133where
107134 Init : FnMut ( & ActiveEventLoop ) -> T ,
108135 InitSurface : FnMut ( & ActiveEventLoop , & mut T ) -> S ,
109- Handler : FnMut ( & mut T , Option < & mut S > , Event < ( ) > , & ActiveEventLoop ) ,
136+ Handler : FnMut ( & mut T , Option < & mut S > , WindowId , WindowEvent , & ActiveEventLoop ) ,
137+ AboutToWaitHandler : FnMut ( & mut T , Option < & mut S > , & ActiveEventLoop ) ,
110138{
111139 fn resumed ( & mut self , el : & ActiveEventLoop ) {
112140 debug_assert ! ( self . state. is_none( ) ) ;
@@ -129,22 +157,13 @@ where
129157 ) {
130158 let state = self . state . as_mut ( ) . unwrap ( ) ;
131159 let surface_state = self . surface_state . as_mut ( ) ;
132- ( self . event ) (
133- state,
134- surface_state,
135- Event :: WindowEvent { window_id, event } ,
136- event_loop,
137- ) ;
160+ ( self . event ) ( state, surface_state, window_id, event, event_loop) ;
138161 }
139162
140163 fn about_to_wait ( & mut self , event_loop : & ActiveEventLoop ) {
141164 if let Some ( state) = self . state . as_mut ( ) {
142- ( self . event ) (
143- state,
144- self . surface_state . as_mut ( ) ,
145- Event :: AboutToWait ,
146- event_loop,
147- ) ;
165+ let surface_state = self . surface_state . as_mut ( ) ;
166+ ( self . about_to_wait ) ( state, surface_state, event_loop) ;
148167 }
149168 }
150169}
0 commit comments