11use crate :: CustomEvent ;
2- use crate :: FrameBuffer ;
32use crate :: WindowSize ;
43use crate :: render:: GraphicsState ;
4+ use crate :: render:: WgpuContext ;
55use std:: sync:: Arc ;
66use std:: sync:: mpsc:: Sender ;
77use std:: time:: Duration ;
@@ -12,7 +12,6 @@ use winit::event::StartCause;
1212use winit:: event:: WindowEvent ;
1313use winit:: event_loop:: ActiveEventLoop ;
1414use winit:: event_loop:: ControlFlow ;
15- use winit:: event_loop:: EventLoopProxy ;
1615use winit:: window:: Window ;
1716use winit:: window:: WindowId ;
1817
@@ -22,33 +21,34 @@ pub(crate) struct WinitApp {
2221 pub ( crate ) cef_context : cef:: Context < cef:: Initialized > ,
2322 pub ( crate ) window : Option < Arc < Window > > ,
2423 cef_schedule : Option < Instant > ,
25- ui_frame_buffer : Option < FrameBuffer > ,
24+ _ui_frame_buffer : Option < wgpu :: Texture > ,
2625 window_size_sender : Sender < WindowSize > ,
27- _viewport_frame_buffer : Option < FrameBuffer > ,
26+ _viewport_frame_buffer : Option < wgpu :: Texture > ,
2827 graphics_state : Option < GraphicsState > ,
29- event_loop_proxy : EventLoopProxy < CustomEvent > ,
28+ wgpu_context : WgpuContext ,
3029}
3130
3231impl WinitApp {
33- pub ( crate ) fn new ( cef_context : cef:: Context < cef:: Initialized > , window_size_sender : Sender < WindowSize > , event_loop_proxy : EventLoopProxy < CustomEvent > ) -> Self {
32+ pub ( crate ) fn new ( cef_context : cef:: Context < cef:: Initialized > , window_size_sender : Sender < WindowSize > , wgpu_context : WgpuContext ) -> Self {
3433 Self {
3534 cef_context,
3635 window : None ,
3736 cef_schedule : Some ( Instant :: now ( ) ) ,
3837 _viewport_frame_buffer : None ,
39- ui_frame_buffer : None ,
38+ _ui_frame_buffer : None ,
4039 graphics_state : None ,
4140 window_size_sender,
42- event_loop_proxy ,
41+ wgpu_context ,
4342 }
4443 }
4544}
4645
4746impl ApplicationHandler < CustomEvent > for WinitApp {
4847 fn about_to_wait ( & mut self , event_loop : & ActiveEventLoop ) {
4948 // Set a timeout in case we miss any cef schedule requests
50- let timeout = Instant :: now ( ) + Duration :: from_millis ( 100 ) ;
49+ let timeout = Instant :: now ( ) + Duration :: from_millis ( 10 ) ;
5150 let wait_until = timeout. min ( self . cef_schedule . unwrap_or ( timeout) ) ;
51+ self . cef_context . work ( ) ;
5252 event_loop. set_control_flow ( ControlFlow :: WaitUntil ( wait_until) ) ;
5353 }
5454
@@ -71,7 +71,7 @@ impl ApplicationHandler<CustomEvent> for WinitApp {
7171 )
7272 . unwrap ( ) ,
7373 ) ;
74- let graphics_state = futures :: executor :: block_on ( GraphicsState :: new ( window. clone ( ) ) ) ;
74+ let graphics_state = GraphicsState :: new ( window. clone ( ) , self . wgpu_context . clone ( ) ) ;
7575
7676 self . window = Some ( window) ;
7777 self . graphics_state = Some ( graphics_state) ;
@@ -81,24 +81,21 @@ impl ApplicationHandler<CustomEvent> for WinitApp {
8181
8282 fn user_event ( & mut self , _: & ActiveEventLoop , event : CustomEvent ) {
8383 match event {
84- CustomEvent :: UiUpdate ( frame_buffer ) => {
84+ CustomEvent :: UiUpdate ( texture ) => {
8585 if let Some ( graphics_state) = self . graphics_state . as_mut ( ) {
86- graphics_state. update_texture ( & frame_buffer) ;
86+ graphics_state. bind_texture ( & texture) ;
87+ graphics_state. resize ( texture. width ( ) , texture. height ( ) ) ;
8788 }
88- self . ui_frame_buffer = Some ( frame_buffer) ;
8989 if let Some ( window) = & self . window {
9090 window. request_redraw ( ) ;
9191 }
9292 }
9393 CustomEvent :: ScheduleBrowserWork ( instant) => {
94- if let Some ( graphics_state) = self . graphics_state . as_mut ( )
95- && let Some ( frame_buffer) = & self . ui_frame_buffer
96- && graphics_state. ui_texture_outdated ( frame_buffer)
97- {
94+ if instant <= Instant :: now ( ) {
9895 self . cef_context . work ( ) ;
99- let _ = self . event_loop_proxy . send_event ( CustomEvent :: ScheduleBrowserWork ( Instant :: now ( ) + Duration :: from_millis ( 1 ) ) ) ;
96+ } else {
97+ self . cef_schedule = Some ( instant) ;
10098 }
101- self . cef_schedule = Some ( instant) ;
10299 }
103100 }
104101 }
@@ -113,9 +110,6 @@ impl ApplicationHandler<CustomEvent> for WinitApp {
113110 }
114111 WindowEvent :: Resized ( PhysicalSize { width, height } ) => {
115112 let _ = self . window_size_sender . send ( WindowSize :: new ( width as usize , height as usize ) ) ;
116- if let Some ( ref mut graphics_state) = self . graphics_state {
117- graphics_state. resize ( width, height) ;
118- }
119113 self . cef_context . notify_of_resize ( ) ;
120114 }
121115
0 commit comments