Skip to content

Commit

Permalink
Implements RuntimeWindow::update_cursor (#1191)
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon authored Dec 18, 2024
1 parent d81f3dd commit 99ed881
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
10 changes: 10 additions & 0 deletions gui/src/rt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,13 @@ impl<T> ApplicationHandler<Event> for Runtime<T> {
self.windows.remove(&id);
return;
}
WindowEvent::CursorMoved {
device_id: _,
position,
} => match self.dispatch_window(el, id, move |w| w.update_cursor(position)) {
Some(Err(e)) => RuntimeError::UpdateWindowCursor(e),
_ => return,
},
WindowEvent::ScaleFactorChanged {
scale_factor,
inner_size_writer: _,
Expand Down Expand Up @@ -249,6 +256,9 @@ pub enum RuntimeError {
#[error("couldn't update window size")]
UpdateWindowSize(#[source] Box<dyn Error + Send + Sync>),

#[error("couldn't update window cursor")]
UpdateWindowCursor(#[source] Box<dyn Error + Send + Sync>),

#[error("couldn't update window scale factor")]
UpdateWindowScaleFactor(#[source] Box<dyn Error + Send + Sync>),

Expand Down
3 changes: 2 additions & 1 deletion gui/src/rt/window.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::error::Error;
use winit::dpi::PhysicalSize;
use winit::dpi::{PhysicalPosition, PhysicalSize};

/// 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 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>>;
}
19 changes: 18 additions & 1 deletion gui/src/ui/backend/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use i_slint_core::window::WindowAdapterInternal;
use i_slint_core::InternalToken;
use i_slint_renderer_skia::SkiaRenderer;
use slint::platform::{Renderer, WindowAdapter, WindowEvent, WindowProperties};
use slint::{LogicalSize, PhysicalSize, PlatformError, WindowSize};
use slint::{LogicalPosition, LogicalSize, PhysicalSize, PlatformError, WindowSize};
use std::any::Any;
use std::cell::Cell;
use std::error::Error;
Expand Down Expand Up @@ -50,6 +50,19 @@ impl RuntimeWindow for Window {
Ok(())
}

fn update_cursor(
&self,
v: 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);

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;

Expand Down Expand Up @@ -101,6 +114,10 @@ impl WindowAdapter for Window {
PhysicalSize::new(s.width, s.height)
}

fn request_redraw(&self) {
self.winit.request_redraw();
}

fn renderer(&self) -> &dyn Renderer {
&self.renderer
}
Expand Down

0 comments on commit 99ed881

Please sign in to comment.