From aff2fa73bcf954a3a2df3aefe4264655cbf39462 Mon Sep 17 00:00:00 2001 From: Putta Khunchalee Date: Sat, 21 Dec 2024 16:24:49 +0700 Subject: [PATCH] Renames event handlers on RuntimeWindow --- gui/src/rt/mod.rs | 31 +++++++++++++++++-------------- gui/src/rt/window.rs | 19 ++++++++++++++----- gui/src/ui/backend/window.rs | 30 ++++++++++++++++++------------ 3 files changed, 49 insertions(+), 31 deletions(-) diff --git a/gui/src/rt/mod.rs b/gui/src/rt/mod.rs index 0526f7d68..003848b3c 100644 --- a/gui/src/rt/mod.rs +++ b/gui/src/rt/mod.rs @@ -113,7 +113,7 @@ struct Runtime { impl Runtime { fn dispatch_task(&mut self, el: &ActiveEventLoop, id: u64) -> bool { - // Take target task so can mutable borrow the task list for context. + // Take target task so can mutable borrow the task list for the context. let mut task = match self.tasks.remove(id) { Some(v) => v, None => { @@ -197,7 +197,7 @@ impl ApplicationHandler for Runtime { // Process the event. let e = match event { - WindowEvent::Resized(v) => match self.dispatch_window(el, id, |w| w.update_size(v)) { + WindowEvent::Resized(v) => match self.dispatch_window(el, id, |w| w.on_resized(v)) { Some(Err(e)) => RuntimeError::UpdateWindowSize(e), _ => return, }, @@ -211,32 +211,35 @@ impl ApplicationHandler for Runtime { self.windows.remove(&id); return; } - WindowEvent::Focused(v) => match self.dispatch_window(el, id, |w| w.set_active(v)) { + WindowEvent::Focused(v) => match self.dispatch_window(el, id, |w| w.on_focused(v)) { Some(Err(e)) => RuntimeError::ChangeActiveWindow(e), _ => return, }, WindowEvent::CursorMoved { - device_id: _, - position, - } => match self.dispatch_window(el, id, move |w| w.update_cursor(position)) { + device_id: dev, + position: pos, + } => match self.dispatch_window(el, id, move |w| w.on_cursor_moved(dev, pos)) { Some(Err(e)) => RuntimeError::UpdateWindowCursor(e), _ => return, }, + WindowEvent::CursorLeft { device_id } => todo!(), WindowEvent::ScaleFactorChanged { - scale_factor, - inner_size_writer: _, - } => match self.dispatch_window(el, id, move |w| w.update_scale_factor(scale_factor)) { + scale_factor: new, + inner_size_writer: sw, + } => match self.dispatch_window(el, id, move |w| w.on_scale_factor_changed(new, sw)) { Some(Err(e)) => RuntimeError::UpdateWindowScaleFactor(e), _ => return, }, - WindowEvent::RedrawRequested => match self.dispatch_window(el, id, |w| w.redraw()) { - Some(Err(e)) => RuntimeError::RedrawWindow(e), - _ => return, - }, + WindowEvent::RedrawRequested => { + match self.dispatch_window(el, id, |w| w.on_redraw_requested()) { + Some(Err(e)) => RuntimeError::RedrawWindow(e), + _ => return, + } + } _ => return, }; - // Store error then exit. + // Store the error then exit. self.exit.set(Some(Err(e))); el.exit(); diff --git a/gui/src/rt/window.rs b/gui/src/rt/window.rs index e88614b98..c7ba0cc01 100644 --- a/gui/src/rt/window.rs +++ b/gui/src/rt/window.rs @@ -1,13 +1,22 @@ use std::error::Error; use winit::dpi::{PhysicalPosition, PhysicalSize}; +use winit::event::{DeviceId, InnerSizeWriter}; /// Encapsulates winit window with window-specific logic. /// /// The event loop will exit immediately if any method return an error. pub trait RuntimeWindow { - fn update_size(&self, v: PhysicalSize) -> Result<(), Box>; - fn set_active(&self, v: bool) -> Result<(), Box>; - fn update_cursor(&self, v: PhysicalPosition) -> Result<(), Box>; - fn update_scale_factor(&self, v: f64) -> Result<(), Box>; - fn redraw(&self) -> Result<(), Box>; + fn on_resized(&self, new: PhysicalSize) -> Result<(), Box>; + fn on_focused(&self, gained: bool) -> Result<(), Box>; + fn on_cursor_moved( + &self, + dev: DeviceId, + pos: PhysicalPosition, + ) -> Result<(), Box>; + fn on_scale_factor_changed( + &self, + new: f64, + sw: InnerSizeWriter, + ) -> Result<(), Box>; + fn on_redraw_requested(&self) -> Result<(), Box>; } diff --git a/gui/src/ui/backend/window.rs b/gui/src/ui/backend/window.rs index 6961a6684..d5deec6ce 100644 --- a/gui/src/ui/backend/window.rs +++ b/gui/src/ui/backend/window.rs @@ -8,6 +8,7 @@ use std::any::Any; use std::cell::Cell; use std::error::Error; use std::rc::Rc; +use winit::event::{DeviceId, InnerSizeWriter}; use winit::window::WindowId; /// Implementation of [`WindowAdapter`]. @@ -38,11 +39,11 @@ impl Window { } impl RuntimeWindow for Window { - fn update_size( + fn on_resized( &self, - v: winit::dpi::PhysicalSize, + new: winit::dpi::PhysicalSize, ) -> Result<(), Box> { - let size = PhysicalSize::new(v.width, v.height); + let size = PhysicalSize::new(new.width, new.height); let size = LogicalSize::from_physical(size, self.winit.scale_factor() as f32); self.slint.dispatch_event(WindowEvent::Resized { size }); @@ -50,19 +51,20 @@ impl RuntimeWindow for Window { Ok(()) } - fn set_active(&self, v: bool) -> Result<(), Box> { + fn on_focused(&self, gained: bool) -> Result<(), Box> { self.slint - .dispatch_event(WindowEvent::WindowActiveChanged(v)); + .dispatch_event(WindowEvent::WindowActiveChanged(gained)); Ok(()) } - fn update_cursor( + fn on_cursor_moved( &self, - v: winit::dpi::PhysicalPosition, + _: DeviceId, + pos: winit::dpi::PhysicalPosition, ) -> Result<(), Box> { - let v = v.to_logical(self.winit.scale_factor()); - let position = LogicalPosition::new(v.x, v.y); + let pos = pos.to_logical(self.winit.scale_factor()); + let position = LogicalPosition::new(pos.x, pos.y); self.slint .dispatch_event(WindowEvent::PointerMoved { position }); @@ -70,8 +72,12 @@ impl RuntimeWindow for Window { Ok(()) } - fn update_scale_factor(&self, v: f64) -> Result<(), Box> { - let scale_factor = v as f32; + fn on_scale_factor_changed( + &self, + new: f64, + _: InnerSizeWriter, + ) -> Result<(), Box> { + let scale_factor = new as f32; self.slint .dispatch_event(WindowEvent::ScaleFactorChanged { scale_factor }); @@ -79,7 +85,7 @@ impl RuntimeWindow for Window { Ok(()) } - fn redraw(&self) -> Result<(), Box> { + fn on_redraw_requested(&self) -> Result<(), Box> { // Wayland will show the window on the first render so we need to check visibility flag // here. if self.visible.get().is_some_and(|v| v) {