@@ -31,76 +31,95 @@ pub(crate) fn make_window(
31
31
}
32
32
33
33
/// Easily constructable winit application.
34
- pub ( crate ) struct WinitApp < T , Init , Handler > {
35
- /// Closure to initialize state.
34
+ pub ( crate ) struct WinitApp < T , S , Init , InitSurface , Handler > {
35
+ /// Closure to initialize ` state` .
36
36
init : Init ,
37
37
38
+ /// Closure to initialize `surface_state`.
39
+ init_surface : InitSurface ,
40
+
38
41
/// Closure to run on window events.
39
42
event : Handler ,
40
43
41
44
/// Contained state.
42
45
state : Option < T > ,
46
+
47
+ ///
48
+ surface_state : Option < S > ,
43
49
}
44
50
45
51
/// Builder that makes it so we don't have to name `T`.
46
- pub ( crate ) struct WinitAppBuilder < T , Init > {
47
- /// Closure to initialize state.
52
+ pub ( crate ) struct WinitAppBuilder < T , S , Init , InitSurface > {
53
+ /// Closure to initialize ` state` .
48
54
init : Init ,
49
55
56
+ /// Closure to initialize `surface_state`.
57
+ init_surface : InitSurface ,
58
+
50
59
/// Eat the type parameter.
51
- _marker : PhantomData < Option < T > > ,
60
+ _marker : PhantomData < ( Option < T > , Option < S > ) > ,
52
61
}
53
62
54
- impl < T , Init > WinitAppBuilder < T , Init >
63
+ impl < T , S , Init , InitSurface > WinitAppBuilder < T , S , Init , InitSurface >
55
64
where
56
65
Init : FnMut ( & ActiveEventLoop ) -> T ,
66
+ InitSurface : FnMut ( & ActiveEventLoop , & mut T ) -> S ,
57
67
{
58
68
/// Create with an "init" closure.
59
- pub ( crate ) fn with_init ( init : Init ) -> Self {
69
+ pub ( crate ) fn with_init ( init : Init , init_surface : InitSurface ) -> Self {
60
70
Self {
61
71
init,
72
+ init_surface,
62
73
_marker : PhantomData ,
63
74
}
64
75
}
65
76
66
77
/// Build a new application.
67
- pub ( crate ) fn with_event_handler < F > ( self , handler : F ) -> WinitApp < T , Init , F >
78
+ pub ( crate ) fn with_event_handler < F > ( self , handler : F ) -> WinitApp < T , S , Init , InitSurface , F >
68
79
where
69
- F : FnMut ( & mut T , Event < ( ) > , & ActiveEventLoop ) ,
80
+ F : FnMut ( & mut T , Option < & mut S > , Event < ( ) > , & ActiveEventLoop ) ,
70
81
{
71
- WinitApp :: new ( self . init , handler)
82
+ WinitApp :: new ( self . init , self . init_surface , handler)
72
83
}
73
84
}
74
85
75
- impl < T , Init , Handler > WinitApp < T , Init , Handler >
86
+ impl < T , S , Init , InitSurface , Handler > WinitApp < T , S , Init , InitSurface , Handler >
76
87
where
77
88
Init : FnMut ( & ActiveEventLoop ) -> T ,
78
- Handler : FnMut ( & mut T , Event < ( ) > , & ActiveEventLoop ) ,
89
+ InitSurface : FnMut ( & ActiveEventLoop , & mut T ) -> S ,
90
+ Handler : FnMut ( & mut T , Option < & mut S > , Event < ( ) > , & ActiveEventLoop ) ,
79
91
{
80
92
/// Create a new application.
81
- pub ( crate ) fn new ( init : Init , event : Handler ) -> Self {
93
+ pub ( crate ) fn new ( init : Init , init_surface : InitSurface , event : Handler ) -> Self {
82
94
Self {
83
95
init,
96
+ init_surface,
84
97
event,
85
98
state : None ,
99
+ surface_state : None ,
86
100
}
87
101
}
88
102
}
89
103
90
- impl < T , Init , Handler > ApplicationHandler for WinitApp < T , Init , Handler >
104
+ impl < T , S , Init , InitSurface , Handler > ApplicationHandler
105
+ for WinitApp < T , S , Init , InitSurface , Handler >
91
106
where
92
107
Init : FnMut ( & ActiveEventLoop ) -> T ,
93
- Handler : FnMut ( & mut T , Event < ( ) > , & ActiveEventLoop ) ,
108
+ InitSurface : FnMut ( & ActiveEventLoop , & mut T ) -> S ,
109
+ Handler : FnMut ( & mut T , Option < & mut S > , Event < ( ) > , & ActiveEventLoop ) ,
94
110
{
95
111
fn resumed ( & mut self , el : & ActiveEventLoop ) {
96
112
debug_assert ! ( self . state. is_none( ) ) ;
97
- self . state = Some ( ( self . init ) ( el) ) ;
113
+ let mut state = ( self . init ) ( el) ;
114
+ self . surface_state = Some ( ( self . init_surface ) ( el, & mut state) ) ;
115
+ self . state = Some ( state) ;
98
116
}
99
117
100
118
fn suspended ( & mut self , _event_loop : & ActiveEventLoop ) {
101
- let state = self . state . take ( ) ;
102
- debug_assert ! ( state. is_some( ) ) ;
103
- drop ( state) ;
119
+ // TODO: Should we run a destruction function for the user?
120
+ let surface_state = self . surface_state . take ( ) ;
121
+ debug_assert ! ( surface_state. is_some( ) ) ;
122
+ drop ( surface_state) ;
104
123
}
105
124
106
125
fn window_event (
@@ -110,12 +129,23 @@ where
110
129
event : WindowEvent ,
111
130
) {
112
131
let state = self . state . as_mut ( ) . unwrap ( ) ;
113
- ( self . event ) ( state, Event :: WindowEvent { window_id, event } , event_loop) ;
132
+ let surface_state = self . surface_state . as_mut ( ) ;
133
+ ( self . event ) (
134
+ state,
135
+ surface_state,
136
+ Event :: WindowEvent { window_id, event } ,
137
+ event_loop,
138
+ ) ;
114
139
}
115
140
116
141
fn about_to_wait ( & mut self , event_loop : & ActiveEventLoop ) {
117
142
if let Some ( state) = self . state . as_mut ( ) {
118
- ( self . event ) ( state, Event :: AboutToWait , event_loop) ;
143
+ ( self . event ) (
144
+ state,
145
+ self . surface_state . as_mut ( ) ,
146
+ Event :: AboutToWait ,
147
+ event_loop,
148
+ ) ;
119
149
}
120
150
}
121
151
}
0 commit comments