From 41eb4f10e195a9ca427584e440d5fb37264e9ef1 Mon Sep 17 00:00:00 2001 From: Wu Wayne Date: Wed, 6 Nov 2024 22:13:22 +0900 Subject: [PATCH 1/9] Only render on window event --- src/compositor.rs | 7 +++++++ src/main.rs | 29 +++++++++++++++++++++-------- src/verso.rs | 11 +++++++++++ 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src/compositor.rs b/src/compositor.rs index 27689b40..b17f7898 100644 --- a/src/compositor.rs +++ b/src/compositor.rs @@ -2082,6 +2082,13 @@ impl IOCompositor { } if let Some((window, _)) = windows.get(&self.current_window) { + // if self.ready_to_present { + // if let Err(err) = self.rendering_context.present(&window.surface) { + // log::warn!("Failed to present surface: {:?}", err); + // } + // self.ready_to_present = false; + // } + match self.composition_request { CompositionRequest::NoCompositingNecessary => {} CompositionRequest::CompositeNow(_) => { diff --git a/src/main.rs b/src/main.rs index 1bf4c691..f05ee53f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use versoview::config::Config; use versoview::verso::EventLoopProxyMessage; use versoview::{Result, Verso}; use winit::application::ApplicationHandler; +use winit::event::WindowEvent; use winit::event_loop::{self, DeviceEvents}; use winit::event_loop::{EventLoop, EventLoopProxy}; @@ -26,15 +27,23 @@ impl ApplicationHandler for App { event: winit::event::WindowEvent, ) { if let Some(v) = self.verso.as_mut() { - v.handle_winit_window_event(window_id, event); + if let WindowEvent::Resized(_) = event { + v.handle_winit_window_event(window_id, event); + + if let Err(e) = self.proxy.send_event(EventLoopProxyMessage::Wake2) { + log::error!("Failed to send wake message to Verso: {e}"); + } + } else { + v.handle_winit_window_event(window_id, event); + v.handle_servo_messages(_event_loop); + } // XXX: Windows seems to be able to handle servo directly. // We can think about how to handle all platforms the same way in the future. - #[cfg(windows)] - v.handle_servo_messages(_event_loop); - #[cfg(not(windows))] - if let Err(e) = self.proxy.send_event(EventLoopProxyMessage::Wake) { - log::error!("Failed to send wake message to Verso: {e}"); - } + // #[cfg(windows)] + // #[cfg(not(windows))] + // if let Err(e) = self.proxy.send_event(EventLoopProxyMessage::Wake) { + // log::error!("Failed to send wake message to Verso: {e}"); + // } } } @@ -46,11 +55,15 @@ impl ApplicationHandler for App { if let Some(v) = self.verso.as_mut() { match event { EventLoopProxyMessage::Wake => { - v.handle_servo_messages(event_loop); + // v.handle_servo_messages(event_loop); + v.wake_window(); } EventLoopProxyMessage::IpcMessage(message) => { v.handle_incoming_webview_message(message); } + EventLoopProxyMessage::Wake2 => { + v.handle_servo_messages(event_loop); + } } } } diff --git a/src/verso.rs b/src/verso.rs index 64463905..bd96f419 100644 --- a/src/verso.rs +++ b/src/verso.rs @@ -518,6 +518,15 @@ impl Verso { } } + /// Handle message came from Servo. + pub fn wake_window(&mut self) { + if let Some(compositor) = &mut self.compositor { + if let Some(window) = self.windows.get(&compositor.current_window) { + window.0.request_redraw(); + } + } + } + /// Handle message came from webview controller. pub fn handle_incoming_webview_message(&self, message: ControllerMessage) { match message { @@ -561,6 +570,8 @@ impl Verso { pub enum EventLoopProxyMessage { /// Wake Wake, + /// Wake + Wake2, /// Message coming from the webview controller IpcMessage(ControllerMessage), } From 03af89b84c73dc71573a7b6bd2448b6209420830 Mon Sep 17 00:00:00 2001 From: Wu Wayne Date: Thu, 7 Nov 2024 00:53:42 +0900 Subject: [PATCH 2/9] Update check condition --- src/main.rs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index f05ee53f..e5903e8f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,9 +27,8 @@ impl ApplicationHandler for App { event: winit::event::WindowEvent, ) { if let Some(v) = self.verso.as_mut() { - if let WindowEvent::Resized(_) = event { + if let WindowEvent::RedrawRequested = event { v.handle_winit_window_event(window_id, event); - if let Err(e) = self.proxy.send_event(EventLoopProxyMessage::Wake2) { log::error!("Failed to send wake message to Verso: {e}"); } @@ -37,13 +36,6 @@ impl ApplicationHandler for App { v.handle_winit_window_event(window_id, event); v.handle_servo_messages(_event_loop); } - // XXX: Windows seems to be able to handle servo directly. - // We can think about how to handle all platforms the same way in the future. - // #[cfg(windows)] - // #[cfg(not(windows))] - // if let Err(e) = self.proxy.send_event(EventLoopProxyMessage::Wake) { - // log::error!("Failed to send wake message to Verso: {e}"); - // } } } From a5dd51ef043d4f1accd583b89e11897a2be22156 Mon Sep 17 00:00:00 2001 From: Wu Wayne Date: Thu, 7 Nov 2024 10:20:49 +0900 Subject: [PATCH 3/9] Remove redundant variant --- src/main.rs | 13 ++++--------- src/verso.rs | 11 +++++++---- src/window.rs | 10 +++++++++- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index e5903e8f..2b368826 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,13 +28,12 @@ impl ApplicationHandler for App { ) { if let Some(v) = self.verso.as_mut() { if let WindowEvent::RedrawRequested = event { - v.handle_winit_window_event(window_id, event); - if let Err(e) = self.proxy.send_event(EventLoopProxyMessage::Wake2) { - log::error!("Failed to send wake message to Verso: {e}"); + if !v.handle_winit_window_event(window_id, event) { + v.handle_servo_messages(_event_loop); } } else { - v.handle_winit_window_event(window_id, event); v.handle_servo_messages(_event_loop); + v.handle_winit_window_event(window_id, event); } } } @@ -47,15 +46,11 @@ impl ApplicationHandler for App { if let Some(v) = self.verso.as_mut() { match event { EventLoopProxyMessage::Wake => { - // v.handle_servo_messages(event_loop); - v.wake_window(); + v.wake_window(event_loop); } EventLoopProxyMessage::IpcMessage(message) => { v.handle_incoming_webview_message(message); } - EventLoopProxyMessage::Wake2 => { - v.handle_servo_messages(event_loop); - } } } } diff --git a/src/verso.rs b/src/verso.rs index bd96f419..aae392cc 100644 --- a/src/verso.rs +++ b/src/verso.rs @@ -413,7 +413,7 @@ impl Verso { } /// Handle Winit window events - pub fn handle_winit_window_event(&mut self, window_id: WindowId, event: WindowEvent) { + pub fn handle_winit_window_event(&mut self, window_id: WindowId, event: WindowEvent) -> bool { log::trace!("Verso is handling Winit event: {event:?}"); if let Some(compositor) = &mut self.compositor { if let WindowEvent::CloseRequested = event { @@ -423,8 +423,11 @@ impl Verso { window .0 .handle_winit_window_event(&self.constellation_sender, compositor, &event); + return window.0.resizing; } } + + false } /// Handle message came from Servo. @@ -519,10 +522,12 @@ impl Verso { } /// Handle message came from Servo. - pub fn wake_window(&mut self) { + pub fn wake_window(&mut self, evl: &ActiveEventLoop) { if let Some(compositor) = &mut self.compositor { if let Some(window) = self.windows.get(&compositor.current_window) { window.0.request_redraw(); + } else { + self.handle_servo_messages(evl); } } } @@ -570,8 +575,6 @@ impl Verso { pub enum EventLoopProxyMessage { /// Wake Wake, - /// Wake - Wake2, /// Message coming from the webview controller IpcMessage(ControllerMessage), } diff --git a/src/window.rs b/src/window.rs index a6b809b5..b266bfb1 100644 --- a/src/window.rs +++ b/src/window.rs @@ -50,6 +50,7 @@ pub struct Window { mouse_position: Cell>>, /// Modifiers state of the keyboard. modifiers_state: Cell, + pub(crate) resizing: bool, } impl Window { @@ -94,6 +95,7 @@ impl Window { webview: None, mouse_position: Default::default(), modifiers_state: Cell::new(ModifiersState::default()), + resizing: false, }, rendering_context, ) @@ -130,6 +132,7 @@ impl Window { webview: None, mouse_position: Default::default(), modifiers_state: Cell::new(ModifiersState::default()), + resizing: false, }; compositor.swap_current_window(&mut window); window @@ -200,6 +203,7 @@ impl Window { match event { WindowEvent::RedrawRequested => { if compositor.ready_to_present { + self.window.pre_present_notify(); if let Err(err) = compositor.rendering_context.present(&self.surface) { log::warn!("Failed to present surface: {:?}", err); } @@ -212,6 +216,7 @@ impl Window { } } WindowEvent::Resized(size) => { + self.resizing = true; let size = Size2D::new(size.width, size.height); compositor.resize(size.to_i32(), self); } @@ -272,7 +277,10 @@ impl Window { let event: MouseWindowEvent = match state { ElementState::Pressed => MouseWindowEvent::MouseDown(button, position), - ElementState::Released => MouseWindowEvent::MouseUp(button, position), + ElementState::Released => { + self.resizing = false; + MouseWindowEvent::MouseUp(button, position) + } }; compositor.on_mouse_window_event_class(event); From cf647a78960f1833167af47474054f82fa358c0c Mon Sep 17 00:00:00 2001 From: Wu Yuwei Date: Thu, 7 Nov 2024 12:06:54 +0900 Subject: [PATCH 4/9] Try different strategy on different platforms --- src/main.rs | 25 +++++++++++++++++++------ src/verso.rs | 4 +++- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2b368826..bb6d94ef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,14 +27,24 @@ impl ApplicationHandler for App { event: winit::event::WindowEvent, ) { if let Some(v) = self.verso.as_mut() { - if let WindowEvent::RedrawRequested = event { - if !v.handle_winit_window_event(window_id, event) { - v.handle_servo_messages(_event_loop); + // if let WindowEvent::RedrawRequested = event { + let _resizing = v.handle_winit_window_event(window_id, event); + #[cfg(apple)] + if !_resizing { + if let Err(e) = self.proxy.send_event(EventLoopProxyMessage::Wake2) { + log::error!("Failed to send controller message to Verso: {e}"); } - } else { - v.handle_servo_messages(_event_loop); - v.handle_winit_window_event(window_id, event); } + #[cfg(linux)] + if let Err(e) = self.proxy.send_event(EventLoopProxyMessage::Wake2) { + log::error!("Failed to send controller message to Verso: {e}"); + } + #[cfg(windows)] + v.handle_servo_messages(_event_loop); + // } else { + // v.handle_servo_messages(_event_loop); + // v.handle_winit_window_event(window_id, event); + // } } } @@ -51,6 +61,9 @@ impl ApplicationHandler for App { EventLoopProxyMessage::IpcMessage(message) => { v.handle_incoming_webview_message(message); } + EventLoopProxyMessage::Wake2 => { + v.handle_servo_messages(event_loop); + } } } } diff --git a/src/verso.rs b/src/verso.rs index aae392cc..d8fa2ae5 100644 --- a/src/verso.rs +++ b/src/verso.rs @@ -423,7 +423,7 @@ impl Verso { window .0 .handle_winit_window_event(&self.constellation_sender, compositor, &event); - return window.0.resizing; + return window.0.resizing && window.0.window.has_focus(); } } @@ -575,6 +575,8 @@ impl Verso { pub enum EventLoopProxyMessage { /// Wake Wake, + /// Wake + Wake2, /// Message coming from the webview controller IpcMessage(ControllerMessage), } From 3b0039e8e78b85bac4c9e561e1eff11c7a846468 Mon Sep 17 00:00:00 2001 From: Wu Wayne Date: Thu, 7 Nov 2024 12:14:25 +0900 Subject: [PATCH 5/9] Update resizing condition --- src/main.rs | 28 ++++++++++++++-------------- src/verso.rs | 2 +- src/window.rs | 4 +++- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/main.rs b/src/main.rs index bb6d94ef..08977805 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,24 +27,24 @@ impl ApplicationHandler for App { event: winit::event::WindowEvent, ) { if let Some(v) = self.verso.as_mut() { - // if let WindowEvent::RedrawRequested = event { - let _resizing = v.handle_winit_window_event(window_id, event); - #[cfg(apple)] - if !_resizing { + if let WindowEvent::RedrawRequested = event { + let _resizing = v.handle_winit_window_event(window_id, event); + #[cfg(apple)] + if !_resizing { + if let Err(e) = self.proxy.send_event(EventLoopProxyMessage::Wake2) { + log::error!("Failed to send controller message to Verso: {e}"); + } + } + #[cfg(linux)] if let Err(e) = self.proxy.send_event(EventLoopProxyMessage::Wake2) { log::error!("Failed to send controller message to Verso: {e}"); } + #[cfg(windows)] + v.handle_servo_messages(_event_loop); + } else { + v.handle_servo_messages(_event_loop); + v.handle_winit_window_event(window_id, event); } - #[cfg(linux)] - if let Err(e) = self.proxy.send_event(EventLoopProxyMessage::Wake2) { - log::error!("Failed to send controller message to Verso: {e}"); - } - #[cfg(windows)] - v.handle_servo_messages(_event_loop); - // } else { - // v.handle_servo_messages(_event_loop); - // v.handle_winit_window_event(window_id, event); - // } } } diff --git a/src/verso.rs b/src/verso.rs index d8fa2ae5..aeec4b13 100644 --- a/src/verso.rs +++ b/src/verso.rs @@ -423,7 +423,7 @@ impl Verso { window .0 .handle_winit_window_event(&self.constellation_sender, compositor, &event); - return window.0.resizing && window.0.window.has_focus(); + return window.0.resizing; } } diff --git a/src/window.rs b/src/window.rs index b266bfb1..d5bcc7c7 100644 --- a/src/window.rs +++ b/src/window.rs @@ -216,7 +216,9 @@ impl Window { } } WindowEvent::Resized(size) => { - self.resizing = true; + if self.window.has_focus() { + self.resizing = true; + } let size = Size2D::new(size.width, size.height); compositor.resize(size.to_i32(), self); } From 043c983a83839f5c70f72bb8824052fbab7564d1 Mon Sep 17 00:00:00 2001 From: Wu Yuwei Date: Thu, 7 Nov 2024 15:46:27 +0900 Subject: [PATCH 6/9] Update request check --- src/compositor.rs | 10 ++++++++-- src/main.rs | 5 ++++- src/verso.rs | 1 + 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/compositor.rs b/src/compositor.rs index b17f7898..95ee3c17 100644 --- a/src/compositor.rs +++ b/src/compositor.rs @@ -2091,9 +2091,15 @@ impl IOCompositor { match self.composition_request { CompositionRequest::NoCompositingNecessary => {} - CompositionRequest::CompositeNow(_) => { + CompositionRequest::CompositeNow(r) => { self.composite(window); - window.request_redraw(); + if window.resizing { + if r == CompositingReason::Resize { + window.request_redraw(); + } + } else { + window.request_redraw(); + } } } diff --git a/src/main.rs b/src/main.rs index 08977805..51a88128 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,8 +42,11 @@ impl ApplicationHandler for App { #[cfg(windows)] v.handle_servo_messages(_event_loop); } else { - v.handle_servo_messages(_event_loop); v.handle_winit_window_event(window_id, event); + // v.handle_servo_messages(_event_loop); + if let Err(e) = self.proxy.send_event(EventLoopProxyMessage::Wake2) { + log::error!("Failed to send controller message to Verso: {e}"); + } } } } diff --git a/src/verso.rs b/src/verso.rs index aeec4b13..83fd2269 100644 --- a/src/verso.rs +++ b/src/verso.rs @@ -572,6 +572,7 @@ impl Verso { } /// Message send to the event loop +#[derive(Debug)] pub enum EventLoopProxyMessage { /// Wake Wake, From 51733503082462f2fa0e90317ddcf199052c9daa Mon Sep 17 00:00:00 2001 From: Wu Yuwei Date: Thu, 7 Nov 2024 19:01:56 +0900 Subject: [PATCH 7/9] Yet another kind of check --- src/compositor.rs | 15 +++++++-------- src/main.rs | 40 ++++++++++++++++++++-------------------- src/verso.rs | 3 +-- 3 files changed, 28 insertions(+), 30 deletions(-) diff --git a/src/compositor.rs b/src/compositor.rs index 95ee3c17..88ffba1a 100644 --- a/src/compositor.rs +++ b/src/compositor.rs @@ -1915,6 +1915,11 @@ impl IOCompositor { /// Composite to the given target if any, or the current target otherwise. fn composite_specific_target(&mut self, window: &Window) -> Result<(), UnableToComposite> { + // if self.ready_to_present { + // return Err(UnableToComposite::NotReadyToPaintImage( + // NotReadyToPaint::AnimationsActive, + // )); + // } if let Err(err) = self .rendering_context .make_gl_context_current(&window.surface) @@ -2091,15 +2096,9 @@ impl IOCompositor { match self.composition_request { CompositionRequest::NoCompositingNecessary => {} - CompositionRequest::CompositeNow(r) => { + CompositionRequest::CompositeNow(_) => { self.composite(window); - if window.resizing { - if r == CompositingReason::Resize { - window.request_redraw(); - } - } else { - window.request_redraw(); - } + window.request_redraw(); } } diff --git a/src/main.rs b/src/main.rs index 51a88128..40dc15f3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,31 +22,34 @@ impl ApplicationHandler for App { fn window_event( &mut self, - _event_loop: &winit::event_loop::ActiveEventLoop, + event_loop: &winit::event_loop::ActiveEventLoop, window_id: winit::window::WindowId, event: winit::event::WindowEvent, ) { if let Some(v) = self.verso.as_mut() { + #[cfg(linux)] + if let WindowEvent::Resized(_) = event { + v.handle_winit_window_event(window_id, event); + } else { + v.handle_winit_window_event(window_id, event); + v.handle_servo_messages(event_loop); + } + + #[cfg(apple)] if let WindowEvent::RedrawRequested = event { - let _resizing = v.handle_winit_window_event(window_id, event); - #[cfg(apple)] - if !_resizing { - if let Err(e) = self.proxy.send_event(EventLoopProxyMessage::Wake2) { - log::error!("Failed to send controller message to Verso: {e}"); - } - } - #[cfg(linux)] - if let Err(e) = self.proxy.send_event(EventLoopProxyMessage::Wake2) { - log::error!("Failed to send controller message to Verso: {e}"); + let resizing = v.handle_winit_window_event(window_id, event); + if !resizing { + v.handle_servo_messages(event_loop); } - #[cfg(windows)] - v.handle_servo_messages(_event_loop); } else { v.handle_winit_window_event(window_id, event); - // v.handle_servo_messages(_event_loop); - if let Err(e) = self.proxy.send_event(EventLoopProxyMessage::Wake2) { - log::error!("Failed to send controller message to Verso: {e}"); - } + v.handle_servo_messages(event_loop); + } + + #[cfg(windows)] + { + v.handle_winit_window_event(window_id, event); + v.handle_servo_messages(event_loop); } } } @@ -64,9 +67,6 @@ impl ApplicationHandler for App { EventLoopProxyMessage::IpcMessage(message) => { v.handle_incoming_webview_message(message); } - EventLoopProxyMessage::Wake2 => { - v.handle_servo_messages(event_loop); - } } } } diff --git a/src/verso.rs b/src/verso.rs index 83fd2269..67371f1e 100644 --- a/src/verso.rs +++ b/src/verso.rs @@ -525,6 +525,7 @@ impl Verso { pub fn wake_window(&mut self, evl: &ActiveEventLoop) { if let Some(compositor) = &mut self.compositor { if let Some(window) = self.windows.get(&compositor.current_window) { + // evl.set_control_flow(ControlFlow::Poll); window.0.request_redraw(); } else { self.handle_servo_messages(evl); @@ -576,8 +577,6 @@ impl Verso { pub enum EventLoopProxyMessage { /// Wake Wake, - /// Wake - Wake2, /// Message coming from the webview controller IpcMessage(ControllerMessage), } From ce7f8cda2d226b683d59e84117e8e1d73fe59a1b Mon Sep 17 00:00:00 2001 From: Wu Wayne Date: Fri, 8 Nov 2024 11:49:13 +0900 Subject: [PATCH 8/9] Cleanup --- src/compositor.rs | 16 ++-------------- src/main.rs | 2 +- src/verso.rs | 6 +++--- src/window.rs | 1 + 4 files changed, 7 insertions(+), 18 deletions(-) diff --git a/src/compositor.rs b/src/compositor.rs index 88ffba1a..2fc78bba 100644 --- a/src/compositor.rs +++ b/src/compositor.rs @@ -574,8 +574,8 @@ impl IOCompositor { self.pending_paint_metrics.insert(pipeline_id, epoch); } - CompositorMsg::CrossProcess(cross_proces_message) => { - self.handle_cross_process_message(cross_proces_message); + CompositorMsg::CrossProcess(cross_process_message) => { + self.handle_cross_process_message(cross_process_message); } } @@ -1915,11 +1915,6 @@ impl IOCompositor { /// Composite to the given target if any, or the current target otherwise. fn composite_specific_target(&mut self, window: &Window) -> Result<(), UnableToComposite> { - // if self.ready_to_present { - // return Err(UnableToComposite::NotReadyToPaintImage( - // NotReadyToPaint::AnimationsActive, - // )); - // } if let Err(err) = self .rendering_context .make_gl_context_current(&window.surface) @@ -2087,13 +2082,6 @@ impl IOCompositor { } if let Some((window, _)) = windows.get(&self.current_window) { - // if self.ready_to_present { - // if let Err(err) = self.rendering_context.present(&window.surface) { - // log::warn!("Failed to present surface: {:?}", err); - // } - // self.ready_to_present = false; - // } - match self.composition_request { CompositionRequest::NoCompositingNecessary => {} CompositionRequest::CompositeNow(_) => { diff --git a/src/main.rs b/src/main.rs index 40dc15f3..d3d2157c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -62,7 +62,7 @@ impl ApplicationHandler for App { if let Some(v) = self.verso.as_mut() { match event { EventLoopProxyMessage::Wake => { - v.wake_window(event_loop); + v.request_redraw(event_loop); } EventLoopProxyMessage::IpcMessage(message) => { v.handle_incoming_webview_message(message); diff --git a/src/verso.rs b/src/verso.rs index 67371f1e..05447532 100644 --- a/src/verso.rs +++ b/src/verso.rs @@ -126,7 +126,7 @@ impl Verso { // Initialize servo media with dummy backend // This will create a thread to initialize a global static of servo media. - // The thread will be closed once the static is initialzed. + // The thread will be closed once the static is initialized. // TODO: This is used by content process. Spawn it there once if we have multiprocess mode. servo_media::ServoMedia::init::(); @@ -521,8 +521,8 @@ impl Verso { } } - /// Handle message came from Servo. - pub fn wake_window(&mut self, evl: &ActiveEventLoop) { + /// Request Verso to redraw. It will queue a redraw event on current focused window. + pub fn request_redraw(&mut self, evl: &ActiveEventLoop) { if let Some(compositor) = &mut self.compositor { if let Some(window) = self.windows.get(&compositor.current_window) { // evl.set_control_flow(ControlFlow::Poll); diff --git a/src/window.rs b/src/window.rs index d5bcc7c7..cb679c1f 100644 --- a/src/window.rs +++ b/src/window.rs @@ -50,6 +50,7 @@ pub struct Window { mouse_position: Cell>>, /// Modifiers state of the keyboard. modifiers_state: Cell, + /// State to indicate if the window is resizing. pub(crate) resizing: bool, } From 5e0b7c575c6db81d4012a5b610d650ad007202ef Mon Sep 17 00:00:00 2001 From: Wu Wayne Date: Fri, 8 Nov 2024 12:05:34 +0900 Subject: [PATCH 9/9] Add method --- src/main.rs | 26 +------------------------- src/verso.rs | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/src/main.rs b/src/main.rs index d3d2157c..f9ea7e90 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,6 @@ use versoview::config::Config; use versoview::verso::EventLoopProxyMessage; use versoview::{Result, Verso}; use winit::application::ApplicationHandler; -use winit::event::WindowEvent; use winit::event_loop::{self, DeviceEvents}; use winit::event_loop::{EventLoop, EventLoopProxy}; @@ -27,30 +26,7 @@ impl ApplicationHandler for App { event: winit::event::WindowEvent, ) { if let Some(v) = self.verso.as_mut() { - #[cfg(linux)] - if let WindowEvent::Resized(_) = event { - v.handle_winit_window_event(window_id, event); - } else { - v.handle_winit_window_event(window_id, event); - v.handle_servo_messages(event_loop); - } - - #[cfg(apple)] - if let WindowEvent::RedrawRequested = event { - let resizing = v.handle_winit_window_event(window_id, event); - if !resizing { - v.handle_servo_messages(event_loop); - } - } else { - v.handle_winit_window_event(window_id, event); - v.handle_servo_messages(event_loop); - } - - #[cfg(windows)] - { - v.handle_winit_window_event(window_id, event); - v.handle_servo_messages(event_loop); - } + v.handle_window_event(event_loop, window_id, event); } } diff --git a/src/verso.rs b/src/verso.rs index 05447532..7ad5a49c 100644 --- a/src/verso.rs +++ b/src/verso.rs @@ -412,8 +412,42 @@ impl Verso { verso } + /// Handle Winit window events. The strategy to handle event are different between platforms + /// because the order of events might be different. + pub fn handle_window_event( + &mut self, + event_loop: &ActiveEventLoop, + window_id: WindowId, + event: WindowEvent, + ) { + #[cfg(linux)] + if let WindowEvent::Resized(_) = event { + self.handle_winit_window_event(window_id, event); + } else { + self.handle_winit_window_event(window_id, event); + self.handle_servo_messages(event_loop); + } + + #[cfg(apple)] + if let WindowEvent::RedrawRequested = event { + let resizing = self.handle_winit_window_event(window_id, event); + if !resizing { + self.handle_servo_messages(event_loop); + } + } else { + self.handle_winit_window_event(window_id, event); + self.handle_servo_messages(event_loop); + } + + #[cfg(windows)] + { + self.handle_winit_window_event(window_id, event); + self.handle_servo_messages(event_loop); + } + } + /// Handle Winit window events - pub fn handle_winit_window_event(&mut self, window_id: WindowId, event: WindowEvent) -> bool { + fn handle_winit_window_event(&mut self, window_id: WindowId, event: WindowEvent) -> bool { log::trace!("Verso is handling Winit event: {event:?}"); if let Some(compositor) = &mut self.compositor { if let WindowEvent::CloseRequested = event {