Cursor position is now obtained through egui.

This commit is contained in:
Filipe Rodrigues 2025-09-16 12:47:38 +01:00
parent 56323b1df6
commit 4382b631f2
Signed by: zenithsiz
SSH Key Fingerprint: SHA256:Mb5ppb3Sh7IarBO/sBTXLHbYEOz37hJAlslLQPPAPaU
3 changed files with 11 additions and 19 deletions

View File

@ -152,9 +152,9 @@ impl ApplicationHandler<AppEvent> for WinitApp {
}
fn window_event(&mut self, _event_loop: &ActiveEventLoop, window_id: WindowId, event: WindowEvent) {
#[expect(clippy::single_match, reason = "We'll add more in the future")]
match event {
WindowEvent::Resized(size) => self.shared.last_resize.store(Some(Resize { size })),
WindowEvent::CursorMoved { position, .. } => self.shared.cursor_pos.store(Some(position)),
_ => (),
}
@ -203,7 +203,6 @@ impl WinitApp {
let shared = Shared {
event_loop_proxy,
last_resize: AtomicCell::new(None),
cursor_pos: AtomicCell::new(None),
wgpu,
panels_renderer_shared,
displays,
@ -377,14 +376,9 @@ async fn paint_egui(
menu: &mut Menu,
window_geometry: Rect<i32, u32>,
) -> Result<(Vec<egui::ClippedPrimitive>, egui::TexturesDelta), AppError> {
// Adjust cursor pos to account for the scale factor
let scale_factor = window.scale_factor();
let cursor_pos = shared.cursor_pos.load();
let full_output_fut =
egui_painter.draw(window, async |ctx| {
// Draw the menu
let cursor_pos = cursor_pos.map(|cursor_pos| cursor_pos.cast::<f32>().to_logical(scale_factor));
tokio::task::block_in_place(|| {
menu.draw(
ctx,
@ -396,17 +390,17 @@ async fn paint_egui(
&shared.metrics,
&shared.window_monitor_names,
&shared.event_loop_proxy,
cursor_pos,
window_geometry,
)
});
// Then go through all panels checking for interactions with their geometries
let Some(cursor_pos) = shared.cursor_pos.load() else {
// TODO: Should this be done here and not somewhere else?
let Some(pointer_pos) = ctx.input(|input| input.pointer.latest_pos()) else {
return Ok(());
};
let cursor_pos = Point2::new(cursor_pos.x as i32, cursor_pos.y as i32);
let pointer_pos = Point2::new(pointer_pos.x as i32, pointer_pos.y as i32);
shared
.panels
.for_each(async |panel| {
@ -416,7 +410,7 @@ async fn paint_egui(
// If we're over an egui area, or none of the geometries are underneath the cursor, skip the panel
if ctx.is_pointer_over_area() ||
!display.geometries.iter().any(|&geometry| {
PanelGeometry::geometry_on(geometry, window_geometry).contains(cursor_pos)
PanelGeometry::geometry_on(geometry, window_geometry).contains(pointer_pos)
}) {
return;
}

View File

@ -28,7 +28,7 @@ use {
sync::{Arc, nonpoison::Mutex},
},
strum::IntoEnumIterator,
winit::{dpi::LogicalPosition, event_loop::EventLoopProxy},
winit::event_loop::EventLoopProxy,
zsw_util::{AppError, DurationDisplay, Rect},
zsw_wgpu::Wgpu,
};
@ -64,18 +64,17 @@ impl Menu {
metrics: &Metrics,
window_monitor_names: &WindowMonitorNames,
event_loop_proxy: &EventLoopProxy<AppEvent>,
cursor_pos: Option<LogicalPosition<f32>>,
window_geometry: Rect<i32, u32>,
) {
// Create the window
let mut egui_window = egui::Window::new("Menu");
// Open it at the mouse if pressed
if let Some(cursor_pos) = cursor_pos &&
!ctx.is_pointer_over_area() &&
ctx.input(|input| input.pointer.button_clicked(egui::PointerButton::Secondary))
if !ctx.is_pointer_over_area() &&
ctx.input(|input| input.pointer.button_clicked(egui::PointerButton::Secondary)) &&
let Some(pointer_pos) = ctx.input(|input| input.pointer.latest_pos())
{
egui_window = egui_window.current_pos(egui::pos2(cursor_pos.x, cursor_pos.y));
egui_window = egui_window.current_pos(pointer_pos);
self.open = true;
}

View File

@ -14,7 +14,7 @@ use {
},
crossbeam::atomic::AtomicCell,
std::sync::Arc,
winit::{dpi::PhysicalPosition, event_loop::EventLoopProxy},
winit::event_loop::EventLoopProxy,
zsw_wgpu::Wgpu,
};
@ -24,7 +24,6 @@ pub struct Shared {
pub event_loop_proxy: EventLoopProxy<AppEvent>,
pub last_resize: AtomicCell<Option<Resize>>,
pub cursor_pos: AtomicCell<Option<PhysicalPosition<f64>>>,
pub wgpu: Wgpu,
pub panels_renderer_shared: PanelsRendererShared,