@@ -3,7 +3,7 @@ use std::marker::PhantomData;
3
3
use std:: rc:: Rc ;
4
4
5
5
use winit:: application:: ApplicationHandler ;
6
- use winit:: event:: { Event , WindowEvent } ;
6
+ use winit:: event:: WindowEvent ;
7
7
use winit:: event_loop:: { ActiveEventLoop , EventLoop } ;
8
8
use winit:: window:: { Window , WindowAttributes , WindowId } ;
9
9
@@ -31,7 +31,7 @@ pub(crate) fn make_window(
31
31
}
32
32
33
33
/// 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 > {
35
35
/// Closure to initialize `state`.
36
36
init : Init ,
37
37
@@ -41,6 +41,9 @@ pub(crate) struct WinitApp<T, S, Init, InitSurface, Handler> {
41
41
/// Closure to run on window events.
42
42
event : Handler ,
43
43
44
+ /// Closure to run on about_to_wait events.
45
+ about_to_wait : AboutToWaitHandler ,
46
+
44
47
/// Contained state.
45
48
state : Option < T > ,
46
49
@@ -75,38 +78,61 @@ where
75
78
}
76
79
77
80
/// 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 ) >
79
85
where
80
- F : FnMut ( & mut T , Option < & mut S > , Event < ( ) > , & ActiveEventLoop ) ,
86
+ F : FnMut ( & mut T , Option < & mut S > , WindowId , WindowEvent , & ActiveEventLoop ) ,
81
87
{
82
- WinitApp :: new ( self . init , self . init_surface , handler)
88
+ WinitApp :: new ( self . init , self . init_surface , handler, |_ , _ , _| { } )
83
89
}
84
90
}
85
91
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 >
87
94
where
88
95
Init : FnMut ( & ActiveEventLoop ) -> T ,
89
96
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 ) ,
91
99
{
92
100
/// 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 {
94
107
Self {
95
108
init,
96
109
init_surface,
97
110
event,
111
+ about_to_wait,
98
112
state : None ,
99
113
surface_state : None ,
100
114
}
101
115
}
116
+
117
+ /// Build a new application.
118
+ pub ( crate ) fn with_about_to_wait_handler < F > (
119
+ self ,
120
+ about_to_wait : F ,
121
+ ) -> WinitApp < T , S , Init , InitSurface , Handler , F >
122
+ where
123
+ F : FnMut ( & mut T , Option < & mut S > , & ActiveEventLoop ) ,
124
+ {
125
+ WinitApp :: new ( self . init , self . init_surface , self . event , about_to_wait)
126
+ }
102
127
}
103
128
104
- impl < T , S , Init , InitSurface , Handler > ApplicationHandler
105
- for WinitApp < T , S , Init , InitSurface , Handler >
129
+ impl < T , S , Init , InitSurface , Handler , AboutToWaitHandler > ApplicationHandler
130
+ for WinitApp < T , S , Init , InitSurface , Handler , AboutToWaitHandler >
106
131
where
107
132
Init : FnMut ( & ActiveEventLoop ) -> T ,
108
133
InitSurface : FnMut ( & ActiveEventLoop , & mut T ) -> S ,
109
- Handler : FnMut ( & mut T , Option < & mut S > , Event < ( ) > , & ActiveEventLoop ) ,
134
+ Handler : FnMut ( & mut T , Option < & mut S > , WindowId , WindowEvent , & ActiveEventLoop ) ,
135
+ AboutToWaitHandler : FnMut ( & mut T , Option < & mut S > , & ActiveEventLoop ) ,
110
136
{
111
137
fn resumed ( & mut self , el : & ActiveEventLoop ) {
112
138
debug_assert ! ( self . state. is_none( ) ) ;
@@ -129,22 +155,13 @@ where
129
155
) {
130
156
let state = self . state . as_mut ( ) . unwrap ( ) ;
131
157
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
- ) ;
158
+ ( self . event ) ( state, surface_state, window_id, event, event_loop) ;
138
159
}
139
160
140
161
fn about_to_wait ( & mut self , event_loop : & ActiveEventLoop ) {
141
162
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
- ) ;
163
+ let surface_state = self . surface_state . as_mut ( ) ;
164
+ ( self . about_to_wait ) ( state, surface_state, event_loop) ;
148
165
}
149
166
}
150
167
}
0 commit comments