Skip to content

Commit

Permalink
Implements RuntimeWindow::on_mouse_input (#1196)
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon authored Dec 21, 2024
1 parent 2da3952 commit b12a51a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 26 deletions.
24 changes: 1 addition & 23 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,7 @@
{
"configurations": [
{
"name": "Qt",
"type": "lldb",
"request": "launch",
"args": [
"--kernel",
"${workspaceFolder}/build/obkrnl"
],
"cwd": "${workspaceFolder}",
"windows": {
"program": "${workspaceFolder}/build/gui/Obliteration.exe",
"env": {
"Path": "${env:Path};${env:CMAKE_PREFIX_PATH}\\bin"
}
},
"linux": {
"program": "${workspaceFolder}/build/gui/obliteration"
},
"osx": {
"program": "${workspaceFolder}/build/gui/obliteration.app/Contents/MacOS/obliteration"
}
},
{
"name": "Slint",
"name": "GUI",
"type": "lldb",
"request": "launch",
"preLaunchTask": "Build",
Expand Down
11 changes: 11 additions & 0 deletions gui/src/rt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,14 @@ impl<T> ApplicationHandler<Event> for Runtime<T> {
_ => return,
}
}
WindowEvent::MouseInput {
device_id: dev,
state: st,
button: btn,
} => match self.dispatch_window(el, id, move |w| w.on_mouse_input(dev, st, btn)) {
Some(Err(e)) => RuntimeError::MouseInput(e),
_ => return,
},
WindowEvent::ScaleFactorChanged {
scale_factor: new,
inner_size_writer: sw,
Expand Down Expand Up @@ -283,6 +291,9 @@ pub enum RuntimeError {
#[error("couldn't handle cursor left")]
CursorLeft(#[source] Box<dyn Error + Send + Sync>),

#[error("couldn't handle mouse input")]
MouseInput(#[source] Box<dyn Error + Send + Sync>),

#[error("couldn't handle scale factor changed")]
ScaleFactorChanged(#[source] Box<dyn Error + Send + Sync>),

Expand Down
8 changes: 7 additions & 1 deletion gui/src/rt/window.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::error::Error;
use winit::dpi::{PhysicalPosition, PhysicalSize};
use winit::event::{DeviceId, InnerSizeWriter};
use winit::event::{DeviceId, ElementState, InnerSizeWriter, MouseButton};

/// Encapsulates winit window with window-specific logic.
///
Expand All @@ -14,6 +14,12 @@ pub trait RuntimeWindow {
pos: PhysicalPosition<f64>,
) -> Result<(), Box<dyn Error + Send + Sync>>;
fn on_cursor_left(&self, dev: DeviceId) -> Result<(), Box<dyn Error + Send + Sync>>;
fn on_mouse_input(
&self,
dev: DeviceId,
st: ElementState,
btn: MouseButton,
) -> Result<(), Box<dyn Error + Send + Sync>>;
fn on_scale_factor_changed(
&self,
new: f64,
Expand Down
35 changes: 33 additions & 2 deletions gui/src/ui/backend/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use crate::rt::RuntimeWindow;
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::platform::{PointerEventButton, Renderer, WindowAdapter, WindowEvent, WindowProperties};
use slint::{LogicalPosition, LogicalSize, PhysicalSize, PlatformError, WindowSize};
use std::any::Any;
use std::cell::Cell;
use std::error::Error;
use std::rc::Rc;
use winit::event::{DeviceId, InnerSizeWriter};
use winit::event::{DeviceId, ElementState, InnerSizeWriter, MouseButton};
use winit::window::WindowId;

/// Implementation of [`WindowAdapter`].
Expand All @@ -17,6 +17,7 @@ pub struct Window {
slint: slint::Window,
renderer: SkiaRenderer,
visible: Cell<Option<bool>>, // Wayland does not support this so we need to emulate it.
pointer: Cell<LogicalPosition>,
}

impl Window {
Expand All @@ -30,6 +31,7 @@ impl Window {
slint,
renderer,
visible: Cell::new(None),
pointer: Cell::default(),
}
}

Expand Down Expand Up @@ -68,6 +70,7 @@ impl RuntimeWindow for Window {

self.slint
.dispatch_event(WindowEvent::PointerMoved { position });
self.pointer.set(position);

Ok(())
}
Expand All @@ -77,6 +80,34 @@ impl RuntimeWindow for Window {
Ok(())
}

fn on_mouse_input(
&self,
_: DeviceId,
st: ElementState,
btn: MouseButton,
) -> Result<(), Box<dyn Error + Send + Sync>> {
// Map button.
let button = match btn {
MouseButton::Left => PointerEventButton::Left,
MouseButton::Right => PointerEventButton::Right,
MouseButton::Middle => PointerEventButton::Middle,
MouseButton::Back => PointerEventButton::Back,
MouseButton::Forward => PointerEventButton::Forward,
MouseButton::Other(_) => PointerEventButton::Other,
};

// Dispatch to Slint.
let position = self.pointer.get();
let ev = match st {
ElementState::Pressed => WindowEvent::PointerPressed { position, button },
ElementState::Released => WindowEvent::PointerReleased { position, button },
};

self.slint.dispatch_event(ev);

Ok(())
}

fn on_scale_factor_changed(
&self,
new: f64,
Expand Down

0 comments on commit b12a51a

Please sign in to comment.