Skip to content

Commit

Permalink
Renames event handlers on RuntimeWindow (#1194)
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon authored Dec 21, 2024
1 parent 3e1c1c9 commit 66b6dea
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 31 deletions.
31 changes: 17 additions & 14 deletions gui/src/rt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ struct Runtime<T> {

impl<T> Runtime<T> {
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 => {
Expand Down Expand Up @@ -197,7 +197,7 @@ impl<T> ApplicationHandler<Event> for Runtime<T> {

// 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,
},
Expand All @@ -211,32 +211,35 @@ impl<T> ApplicationHandler<Event> for Runtime<T> {
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();
Expand Down
19 changes: 14 additions & 5 deletions gui/src/rt/window.rs
Original file line number Diff line number Diff line change
@@ -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<u32>) -> Result<(), Box<dyn Error + Send + Sync>>;
fn set_active(&self, v: bool) -> Result<(), Box<dyn Error + Send + Sync>>;
fn update_cursor(&self, v: PhysicalPosition<f64>) -> Result<(), Box<dyn Error + Send + Sync>>;
fn update_scale_factor(&self, v: f64) -> Result<(), Box<dyn Error + Send + Sync>>;
fn redraw(&self) -> Result<(), Box<dyn Error + Send + Sync>>;
fn on_resized(&self, new: PhysicalSize<u32>) -> Result<(), Box<dyn Error + Send + Sync>>;
fn on_focused(&self, gained: bool) -> Result<(), Box<dyn Error + Send + Sync>>;
fn on_cursor_moved(
&self,
dev: DeviceId,
pos: PhysicalPosition<f64>,
) -> Result<(), Box<dyn Error + Send + Sync>>;
fn on_scale_factor_changed(
&self,
new: f64,
sw: InnerSizeWriter,
) -> Result<(), Box<dyn Error + Send + Sync>>;
fn on_redraw_requested(&self) -> Result<(), Box<dyn Error + Send + Sync>>;
}
30 changes: 18 additions & 12 deletions gui/src/ui/backend/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
Expand Down Expand Up @@ -38,48 +39,53 @@ impl Window {
}

impl RuntimeWindow for Window {
fn update_size(
fn on_resized(
&self,
v: winit::dpi::PhysicalSize<u32>,
new: winit::dpi::PhysicalSize<u32>,
) -> Result<(), Box<dyn Error + Send + Sync>> {
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 });

Ok(())
}

fn set_active(&self, v: bool) -> Result<(), Box<dyn Error + Send + Sync>> {
fn on_focused(&self, gained: bool) -> Result<(), Box<dyn Error + Send + Sync>> {
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<f64>,
_: DeviceId,
pos: winit::dpi::PhysicalPosition<f64>,
) -> Result<(), Box<dyn Error + Send + Sync>> {
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 });

Ok(())
}

fn update_scale_factor(&self, v: f64) -> Result<(), Box<dyn Error + Send + Sync>> {
let scale_factor = v as f32;
fn on_scale_factor_changed(
&self,
new: f64,
_: InnerSizeWriter,
) -> Result<(), Box<dyn Error + Send + Sync>> {
let scale_factor = new as f32;

self.slint
.dispatch_event(WindowEvent::ScaleFactorChanged { scale_factor });

Ok(())
}

fn redraw(&self) -> Result<(), Box<dyn Error + Send + Sync>> {
fn on_redraw_requested(&self) -> Result<(), Box<dyn Error + Send + Sync>> {
// 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) {
Expand Down

0 comments on commit 66b6dea

Please sign in to comment.