@@ -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,62 @@ 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+ pub ( crate ) fn with_event_handler < F > (
82+ self ,
83+ handler : F ,
84+ ) -> WinitApp < T , S , Init , InitSurface , F , impl FnMut ( & mut T , Option < & mut S > , & ActiveEventLoop ) >
7985 where
80- F : FnMut ( & mut T , Option < & mut S > , Event < ( ) > , & ActiveEventLoop ) ,
86+ F : FnMut ( & mut T , Option < & mut S > , WindowId , WindowEvent , & ActiveEventLoop ) ,
8187 {
82- WinitApp :: new ( self . init , self . init_surface , handler)
88+ WinitApp :: new ( self . init , self . init_surface , handler, |_ , _ , _| { } )
8389 }
8490}
8591
86- impl < T , S , Init , InitSurface , Handler > WinitApp < T , S , Init , InitSurface , Handler >
92+ impl < T , S , Init , InitSurface , Handler , AboutToWaitHandler >
93+ WinitApp < T , S , Init , InitSurface , Handler , AboutToWaitHandler >
8794where
8895 Init : FnMut ( & ActiveEventLoop ) -> T ,
8996 InitSurface : FnMut ( & ActiveEventLoop , & mut T ) -> S ,
90- Handler : FnMut ( & mut T , Option < & mut S > , Event < ( ) > , & ActiveEventLoop ) ,
97+ Handler : FnMut ( & mut T , Option < & mut S > , WindowId , WindowEvent , & ActiveEventLoop ) ,
98+ AboutToWaitHandler : FnMut ( & mut T , Option < & mut S > , & ActiveEventLoop ) ,
9199{
92100 /// Create a new application.
93- pub ( crate ) fn new ( init : Init , init_surface : InitSurface , event : Handler ) -> Self {
101+ pub ( crate ) fn new (
102+ init : Init ,
103+ init_surface : InitSurface ,
104+ event : Handler ,
105+ about_to_wait : AboutToWaitHandler ,
106+ ) -> Self {
94107 Self {
95108 init,
96109 init_surface,
97110 event,
111+ about_to_wait,
98112 state : None ,
99113 surface_state : None ,
100114 }
101115 }
116+
117+ /// Build a new application.
118+ #[ allow( dead_code) ]
119+ pub ( crate ) fn with_about_to_wait_handler < F > (
120+ self ,
121+ about_to_wait : F ,
122+ ) -> WinitApp < T , S , Init , InitSurface , Handler , F >
123+ where
124+ F : FnMut ( & mut T , Option < & mut S > , & ActiveEventLoop ) ,
125+ {
126+ WinitApp :: new ( self . init , self . init_surface , self . event , about_to_wait)
127+ }
102128}
103129
104- impl < T , S , Init , InitSurface , Handler > ApplicationHandler
105- for WinitApp < T , S , Init , InitSurface , Handler >
130+ impl < T , S , Init , InitSurface , Handler , AboutToWaitHandler > ApplicationHandler
131+ for WinitApp < T , S , Init , InitSurface , Handler , AboutToWaitHandler >
106132where
107133 Init : FnMut ( & ActiveEventLoop ) -> T ,
108134 InitSurface : FnMut ( & ActiveEventLoop , & mut T ) -> S ,
109- Handler : FnMut ( & mut T , Option < & mut S > , Event < ( ) > , & ActiveEventLoop ) ,
135+ Handler : FnMut ( & mut T , Option < & mut S > , WindowId , WindowEvent , & ActiveEventLoop ) ,
136+ AboutToWaitHandler : FnMut ( & mut T , Option < & mut S > , & ActiveEventLoop ) ,
110137{
111138 fn resumed ( & mut self , el : & ActiveEventLoop ) {
112139 debug_assert ! ( self . state. is_none( ) ) ;
@@ -129,22 +156,13 @@ where
129156 ) {
130157 let state = self . state . as_mut ( ) . unwrap ( ) ;
131158 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- ) ;
159+ ( self . event ) ( state, surface_state, window_id, event, event_loop) ;
138160 }
139161
140162 fn about_to_wait ( & mut self , event_loop : & ActiveEventLoop ) {
141163 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- ) ;
164+ let surface_state = self . surface_state . as_mut ( ) ;
165+ ( self . about_to_wait ) ( state, surface_state, event_loop) ;
148166 }
149167 }
150168}
0 commit comments