@@ -12,6 +12,7 @@ use winit::event::StartCause;
1212use winit:: event:: WindowEvent ;
1313use winit:: event_loop:: ActiveEventLoop ;
1414use winit:: event_loop:: ControlFlow ;
15+ use winit:: event_loop:: EventLoopProxy ;
1516use winit:: window:: Window ;
1617use winit:: window:: WindowId ;
1718
@@ -21,35 +22,24 @@ pub(crate) struct WinitApp {
2122 pub ( crate ) cef_context : cef:: Context < cef:: Initialized > ,
2223 pub ( crate ) window : Option < Arc < Window > > ,
2324 cef_schedule : Option < Instant > ,
24- ui_dirty : bool ,
2525 ui_frame_buffer : Option < FrameBuffer > ,
2626 window_size_sender : Sender < WindowSize > ,
2727 _viewport_frame_buffer : Option < FrameBuffer > ,
2828 graphics_state : Option < GraphicsState > ,
29+ event_loop_proxy : EventLoopProxy < CustomEvent > ,
2930}
3031
3132impl WinitApp {
32- pub ( crate ) fn new ( cef_context : cef:: Context < cef:: Initialized > , window_size_sender : Sender < WindowSize > ) -> Self {
33+ pub ( crate ) fn new ( cef_context : cef:: Context < cef:: Initialized > , window_size_sender : Sender < WindowSize > , event_loop_proxy : EventLoopProxy < CustomEvent > ) -> Self {
3334 Self {
3435 cef_context,
3536 window : None ,
3637 cef_schedule : Some ( Instant :: now ( ) ) ,
3738 _viewport_frame_buffer : None ,
3839 ui_frame_buffer : None ,
39- ui_dirty : false ,
4040 graphics_state : None ,
4141 window_size_sender,
42- }
43- }
44- fn run_cef ( & mut self ) {
45- if self . ui_frame_buffer . is_none ( ) {
46- self . cef_context . work ( ) ;
47- }
48- if let Some ( schedule) = self . cef_schedule
49- && schedule < Instant :: now ( )
50- {
51- self . cef_schedule = None ;
52- self . cef_context . work ( ) ;
42+ event_loop_proxy,
5343 }
5444 }
5545}
@@ -62,7 +52,15 @@ impl ApplicationHandler<CustomEvent> for WinitApp {
6252 }
6353
6454 fn new_events ( & mut self , _event_loop : & ActiveEventLoop , _cause : StartCause ) {
65- self . run_cef ( ) ;
55+ if self . ui_frame_buffer . is_none ( ) {
56+ self . cef_context . work ( ) ;
57+ }
58+ if let Some ( schedule) = self . cef_schedule
59+ && schedule < Instant :: now ( )
60+ {
61+ self . cef_schedule = None ;
62+ self . cef_context . work ( ) ;
63+ }
6664 }
6765
6866 fn resumed ( & mut self , event_loop : & ActiveEventLoop ) {
@@ -84,26 +82,30 @@ impl ApplicationHandler<CustomEvent> for WinitApp {
8482 }
8583
8684 fn user_event ( & mut self , _: & ActiveEventLoop , event : CustomEvent ) {
87- self . run_cef ( ) ;
8885 match event {
8986 CustomEvent :: UiUpdate ( frame_buffer) => {
9087 if let Some ( graphics_state) = self . graphics_state . as_mut ( ) {
9188 graphics_state. update_texture ( & frame_buffer) ;
92- self . ui_dirty = true ;
9389 }
9490 self . ui_frame_buffer = Some ( frame_buffer) ;
9591 if let Some ( window) = & self . window {
9692 window. request_redraw ( ) ;
9793 }
9894 }
9995 CustomEvent :: ScheduleBrowserWork ( instant) => {
96+ if let Some ( graphics_state) = self . graphics_state . as_mut ( )
97+ && let Some ( frame_buffer) = & self . ui_frame_buffer
98+ && graphics_state. ui_texture_outdated ( frame_buffer)
99+ {
100+ self . cef_context . work ( ) ;
101+ let _ = self . event_loop_proxy . send_event ( CustomEvent :: ScheduleBrowserWork ( Instant :: now ( ) + Duration :: from_millis ( 1 ) ) ) ;
102+ }
100103 self . cef_schedule = Some ( instant) ;
101104 }
102105 }
103106 }
104107
105108 fn window_event ( & mut self , event_loop : & ActiveEventLoop , _window_id : WindowId , event : WindowEvent ) {
106- self . run_cef ( ) ;
107109 let Some ( event) = self . cef_context . handle_window_event ( event) else { return } ;
108110
109111 match event {
@@ -113,25 +115,25 @@ impl ApplicationHandler<CustomEvent> for WinitApp {
113115 }
114116 WindowEvent :: Resized ( PhysicalSize { width, height } ) => {
115117 let _ = self . window_size_sender . send ( WindowSize :: new ( width as usize , height as usize ) ) ;
118+ if let Some ( ref mut graphics_state) = self . graphics_state {
119+ graphics_state. resize ( width, height) ;
120+ }
116121 self . cef_context . notify_of_resize ( ) ;
117122 }
118123
119124 WindowEvent :: RedrawRequested => {
120125 let Some ( ref mut graphics_state) = self . graphics_state else { return } ;
121126 // Only rerender once we have a new ui texture to display
122- if self . ui_dirty {
123- self . ui_dirty = false ;
124127
125- match graphics_state. render ( ) {
126- Ok ( _) => { }
127- Err ( wgpu:: SurfaceError :: Lost ) => {
128- tracing:: warn!( "lost surface" ) ;
129- }
130- Err ( wgpu:: SurfaceError :: OutOfMemory ) => {
131- event_loop. exit ( ) ;
132- }
133- Err ( e) => tracing:: error!( "{:?}" , e) ,
128+ match graphics_state. render ( ) {
129+ Ok ( _) => { }
130+ Err ( wgpu:: SurfaceError :: Lost ) => {
131+ tracing:: warn!( "lost surface" ) ;
132+ }
133+ Err ( wgpu:: SurfaceError :: OutOfMemory ) => {
134+ event_loop. exit ( ) ;
134135 }
136+ Err ( e) => tracing:: error!( "{:?}" , e) ,
135137 }
136138 }
137139 _ => { }
0 commit comments