@@ -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,62 @@ 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
+ #[ 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
+ }
102
128
}
103
129
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 >
106
132
where
107
133
Init : FnMut ( & ActiveEventLoop ) -> T ,
108
134
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 ) ,
110
137
{
111
138
fn resumed ( & mut self , el : & ActiveEventLoop ) {
112
139
debug_assert ! ( self . state. is_none( ) ) ;
@@ -129,22 +156,13 @@ where
129
156
) {
130
157
let state = self . state . as_mut ( ) . unwrap ( ) ;
131
158
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) ;
138
160
}
139
161
140
162
fn about_to_wait ( & mut self , event_loop : & ActiveEventLoop ) {
141
163
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) ;
148
166
}
149
167
}
150
168
}
0 commit comments