@@ -10,15 +10,21 @@ use egor_app::{
1010} ;
1111use egor_render:: Renderer ;
1212
13- #[ cfg( not( feature = "ui" ) ) ]
14- type UpdateFn = dyn FnMut ( & mut Graphics , & Input , & FrameTimer ) ;
15- #[ cfg( feature = "ui" ) ]
16- type UpdateFn = dyn FnMut ( & mut Graphics , & Input , & FrameTimer , & egui:: Context ) ;
13+ type UpdateFn = dyn FnMut ( & mut FrameContext ) ;
14+
15+ pub struct FrameContext < ' a > {
16+ pub events : Vec < WindowEvent > ,
17+ pub gfx : Graphics < ' a > ,
18+ pub input : & ' a Input ,
19+ pub timer : & ' a FrameTimer ,
20+ #[ cfg( feature = "ui" ) ]
21+ pub egui_ctx : & ' a egui:: Context ,
22+ }
1723
1824pub struct App {
25+ events : Vec < WindowEvent > ,
1926 update : Option < Box < UpdateFn > > ,
2027 config : Option < AppConfig > ,
21- on_quit : Option < Box < dyn FnMut ( ) > > ,
2228 vsync : bool ,
2329 text_renderer : Option < TextRenderer > ,
2430 #[ cfg( feature = "ui" ) ]
@@ -29,9 +35,9 @@ impl App {
2935 /// Create a new [`App`]
3036 pub fn new ( ) -> Self {
3137 Self {
38+ events : Vec :: new ( ) ,
3239 update : None ,
3340 config : Some ( AppConfig :: default ( ) ) ,
34- on_quit : None ,
3541 vsync : true ,
3642 text_renderer : None ,
3743 #[ cfg( feature = "ui" ) ]
@@ -48,7 +54,7 @@ impl App {
4854 }
4955
5056 /// Set window size (width, height in pixels)
51- pub fn screen_size ( mut self , width : u32 , height : u32 ) -> Self {
57+ pub fn window_size ( mut self , width : u32 , height : u32 ) -> Self {
5258 if let Some ( c) = self . config . as_mut ( ) {
5359 c. width = width;
5460 c. height = height;
@@ -71,57 +77,30 @@ impl App {
7177 }
7278
7379 /// Run the app with a per-frame update closure
74- #[ cfg( not( feature = "ui" ) ) ]
75- pub fn run (
76- mut self ,
77- #[ allow( unused_mut) ] mut update : impl FnMut ( & mut Graphics , & Input , & FrameTimer ) + ' static ,
78- ) {
79- #[ cfg( feature = "hot_reload" ) ]
80- let update = {
81- dioxus_devtools:: connect_subsecond ( ) ;
82-
83- move |g : & mut Graphics , i : & Input , t : & FrameTimer | {
84- dioxus_devtools:: subsecond:: call ( || update ( g, i, t) )
85- }
86- } ;
87- self . update = Some ( Box :: new ( update) ) ;
88-
89- let config = self . config . take ( ) . unwrap ( ) ;
90- AppRunner :: new ( self , config) . run ( ) ;
91- }
92- #[ cfg( feature = "ui" ) ]
93- pub fn run (
94- mut self ,
95- #[ allow( unused_mut) ] mut update : impl FnMut ( & mut Graphics , & Input , & FrameTimer , & egui:: Context )
96- + ' static ,
97- ) {
80+ pub fn run ( mut self , #[ allow( unused_mut) ] mut update : impl FnMut ( & mut FrameContext ) + ' static ) {
9881 #[ cfg( feature = "hot_reload" ) ]
9982 let update = {
10083 dioxus_devtools:: connect_subsecond ( ) ;
10184
102- move |g : & mut Graphics , i : & Input , t : & FrameTimer , ui : & egui :: Context | {
103- dioxus_devtools:: subsecond:: call ( || update ( g , i , t , ui ) )
85+ move |ctx : & mut FrameContext | {
86+ dioxus_devtools:: subsecond:: call ( || update ( ctx ) ) ;
10487 }
10588 } ;
10689 self . update = Some ( Box :: new ( update) ) ;
10790
10891 let config = self . config . take ( ) . unwrap ( ) ;
10992 AppRunner :: new ( self , config) . run ( ) ;
11093 }
111-
112- /// Sets a closure to call when the app is quitting
113- pub fn on_quit ( mut self , f : impl FnMut ( ) + ' static ) -> Self {
114- self . on_quit = Some ( Box :: new ( f) ) ;
115- self
116- }
11794}
11895
11996impl AppHandler < Renderer > for App {
120- fn on_window_event ( & mut self , _window : & Window , _event : & WindowEvent ) {
97+ fn on_window_event ( & mut self , _window : & Window , event : & WindowEvent ) {
12198 #[ cfg( feature = "ui" ) ]
12299 if let Some ( egui) = self . egui . as_mut ( ) {
123- egui. handle_event ( _window, _event ) ;
100+ egui. handle_event ( _window, event ) ;
124101 }
102+
103+ self . events . push ( event. clone ( ) ) ;
125104 }
126105
127106 async fn with_resource ( & mut self , window : Arc < Window > ) -> Renderer {
@@ -167,17 +146,21 @@ impl AppHandler<Renderer> for App {
167146 let ( device, queue) = ( renderer. device ( ) . clone ( ) , renderer. queue ( ) . clone ( ) ) ;
168147
169148 let text_renderer = self . text_renderer . as_mut ( ) . unwrap ( ) ;
170- let mut graphics = Graphics :: new ( renderer, text_renderer) ;
171149
172- #[ cfg( not( feature = "ui" ) ) ]
173- update ( & mut graphics, input, timer) ;
174150 #[ cfg( feature = "ui" ) ]
175- {
176- let egui_ctx = self . egui . as_mut ( ) . unwrap ( ) . begin_frame ( _window) ;
177- update ( & mut graphics, input, timer, egui_ctx) ;
178- }
151+ let egui_ctx = self . egui . as_mut ( ) . unwrap ( ) . begin_frame ( _window) ;
152+ let mut ctx = FrameContext {
153+ gfx : Graphics :: new ( renderer, text_renderer) ,
154+ input,
155+ timer,
156+ #[ cfg( feature = "ui" ) ]
157+ egui_ctx,
158+ events : std:: mem:: take ( & mut self . events ) ,
159+ } ;
160+ update ( & mut ctx) ;
161+
162+ let geometry = ctx. gfx . flush ( ) ;
179163
180- let geometry = graphics. flush ( ) ;
181164 text_renderer. prepare ( & device, & queue, width, height) ;
182165
183166 {
@@ -210,10 +193,4 @@ impl AppHandler<Renderer> for App {
210193 fn resize ( & mut self , width : u32 , height : u32 , renderer : & mut Renderer ) {
211194 renderer. resize ( width, height)
212195 }
213-
214- fn on_quit ( & mut self ) {
215- if let Some ( f) = & mut self . on_quit {
216- f ( ) ;
217- }
218- }
219196}
0 commit comments