@@ -31,76 +31,94 @@ pub(crate) fn make_window(
3131}
3232
3333/// 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` .
3636 init : Init ,
3737
38+ /// Closure to initialize `surface_state`.
39+ init_surface : InitSurface ,
40+
3841 /// Closure to run on window events.
3942 event : Handler ,
4043
4144 /// Contained state.
4245 state : Option < T > ,
46+
47+ /// Contained surface state.
48+ surface_state : Option < S > ,
4349}
4450
4551/// 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` .
4854 init : Init ,
4955
56+ /// Closure to initialize `surface_state`.
57+ init_surface : InitSurface ,
58+
5059 /// Eat the type parameter.
51- _marker : PhantomData < Option < T > > ,
60+ _marker : PhantomData < ( Option < T > , Option < S > ) > ,
5261}
5362
54- impl < T , Init > WinitAppBuilder < T , Init >
63+ impl < T , S , Init , InitSurface > WinitAppBuilder < T , S , Init , InitSurface >
5564where
5665 Init : FnMut ( & ActiveEventLoop ) -> T ,
66+ InitSurface : FnMut ( & ActiveEventLoop , & mut T ) -> S ,
5767{
5868 /// 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 {
6070 Self {
6171 init,
72+ init_surface,
6273 _marker : PhantomData ,
6374 }
6475 }
6576
6677 /// 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 >
6879 where
69- F : FnMut ( & mut T , Event < ( ) > , & ActiveEventLoop ) ,
80+ F : FnMut ( & mut T , Option < & mut S > , Event < ( ) > , & ActiveEventLoop ) ,
7081 {
71- WinitApp :: new ( self . init , handler)
82+ WinitApp :: new ( self . init , self . init_surface , handler)
7283 }
7384}
7485
75- impl < T , Init , Handler > WinitApp < T , Init , Handler >
86+ impl < T , S , Init , InitSurface , Handler > WinitApp < T , S , Init , InitSurface , Handler >
7687where
7788 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 ) ,
7991{
8092 /// 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 {
8294 Self {
8395 init,
96+ init_surface,
8497 event,
8598 state : None ,
99+ surface_state : None ,
86100 }
87101 }
88102}
89103
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 >
91106where
92107 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 ) ,
94110{
95111 fn resumed ( & mut self , el : & ActiveEventLoop ) {
96112 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) ;
98116 }
99117
100118 fn suspended ( & mut self , _event_loop : & ActiveEventLoop ) {
101- let state = self . state . take ( ) ;
102- debug_assert ! ( state . is_some( ) ) ;
103- drop ( state ) ;
119+ let surface_state = self . surface_state . take ( ) ;
120+ debug_assert ! ( surface_state . is_some( ) ) ;
121+ drop ( surface_state ) ;
104122 }
105123
106124 fn window_event (
@@ -110,12 +128,23 @@ where
110128 event : WindowEvent ,
111129 ) {
112130 let state = self . state . as_mut ( ) . unwrap ( ) ;
113- ( self . event ) ( state, Event :: WindowEvent { window_id, event } , event_loop) ;
131+ 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+ ) ;
114138 }
115139
116140 fn about_to_wait ( & mut self , event_loop : & ActiveEventLoop ) {
117141 if let Some ( state) = self . state . as_mut ( ) {
118- ( self . event ) ( state, Event :: AboutToWait , event_loop) ;
142+ ( self . event ) (
143+ state,
144+ self . surface_state . as_mut ( ) ,
145+ Event :: AboutToWait ,
146+ event_loop,
147+ ) ;
119148 }
120149 }
121150}
0 commit comments