diff options
| author | HampusM <hampus@hampusmat.com> | 2026-06-30 19:25:56 +0200 |
|---|---|---|
| committer | HampusM <hampus@hampusmat.com> | 2026-06-30 19:25:56 +0200 |
| commit | 705daf36fee53beb0180ada3faf4a43509133395 (patch) | |
| tree | cf21d112ac38dade1bffdcee6c02125905181d35 /engine | |
| parent | d3b24c3812f9ae29fc019b3eaace3ea1b692d026 (diff) | |
feat(engine): add cursor_visible & cursor_grab_mode window creation attrs
Diffstat (limited to 'engine')
| -rw-r--r-- | engine/src/windowing.rs | 60 | ||||
| -rw-r--r-- | engine/src/windowing/window.rs | 36 |
2 files changed, 68 insertions, 28 deletions
diff --git a/engine/src/windowing.rs b/engine/src/windowing.rs index 8dba8f9..ea5b5f7 100644 --- a/engine/src/windowing.rs +++ b/engine/src/windowing.rs @@ -3,7 +3,7 @@ use std::env::consts::EXE_SUFFIX; use std::hint::cold_path; use std::panic::catch_unwind; use std::sync::atomic::{AtomicBool, Ordering}; -use std::sync::{Arc, Weak, Mutex}; +use std::sync::{Arc, Mutex, Weak}; use std::thread::Builder as ThreadBuilder; use crossbeam_queue::ArrayQueue; @@ -185,7 +185,7 @@ fn update_stuff( mouse.position = PhysicalPosition { x: absolute_mouse_pos.0, - y: absolute_mouse_pos.1 + y: absolute_mouse_pos.1, }; keyboard.set_text_keys(iter_array_queue(&context.shared_state.text_keys)); @@ -312,7 +312,6 @@ fn update_stuff( let err = err.take().expect("Not possible"); return Err(err.into()); - } Ok(()) @@ -416,7 +415,12 @@ impl Context fn try_send_message_to_app(&self, message: MessageToApp) { - if self.shared_state.message_to_app_queue.push(message).is_err() { + if self + .shared_state + .message_to_app_queue + .push(message) + .is_err() + { tracing::error!( "Failed to send message. Queue for messages to windowing app is full" ); @@ -451,7 +455,7 @@ impl Context }; let event_loop = match create_event_loop() { - Ok(event_loop) => event_loop, + Ok(event_loop) => event_loop, Err(err) => { return Err((app, AppThreadError::CreateEventLoop(err))); } @@ -459,17 +463,26 @@ impl Context event_loop.set_control_flow(EventLoopControlFlow::Poll); - event_loop.run_app(&mut app).map_err(|err| (app, AppThreadError::StartEventLoop(err)))?; + event_loop + .run_app(&mut app) + .map_err(|err| (app, AppThreadError::StartEventLoop(err)))?; Ok(()) }) { - Ok(Ok(())) => {}, + Ok(Ok(())) => {} Ok(Err((app, err))) => { - *app.shared_state.thread_err.try_lock().expect("Not possible") = Some(err); - app.shared_state.has_thread_err.store(true, Ordering::Relaxed); + *app.shared_state + .thread_err + .try_lock() + .expect("Not possible") = Some(err); + app.shared_state + .has_thread_err + .store(true, Ordering::Relaxed); } Err(_) => { - shared_state_c.thread_panicked.store(true, Ordering::Relaxed); + shared_state_c + .thread_panicked + .store(true, Ordering::Relaxed); } }; }) @@ -570,7 +583,7 @@ impl Default for SharedState is_dropped: AtomicBool::new(false), thread_panicked: AtomicBool::new(false), thread_err: Mutex::new(None), - has_thread_err: AtomicBool::new(false) + has_thread_err: AtomicBool::new(false), } } } @@ -596,9 +609,9 @@ impl App ); let winit_window = Arc::new( - match event_loop - .create_window(window_creation_attrs.clone().into_window_attrs()) - { + match event_loop.create_window( + window_creation_attrs.clone().into_window_attrs(), + ) { Ok(window) => window, Err(err) => { tracing::error!("Failed to create window: {err}"); @@ -607,9 +620,16 @@ impl App }, ); + window_creation_attrs.apply_extra_attrs_to_window(&winit_window); + self.windows.insert( WindowId::from_inner(winit_window.id()), - (Arc::downgrade(&winit_window), WindowSettings::default()), + ( + Arc::downgrade(&winit_window), + WindowSettings { + cursor_grab_mode: window_creation_attrs.cursor_grab_mode, + }, + ), ); self.send_message(MessageFromApp::WindowCreated( @@ -772,10 +792,12 @@ impl ApplicationHandler for App let window_size = window.inner_size(); - if let Err(err) = window.set_cursor_position(Position::Physical(PhysicalPosition { - x: window_size.width as i32 / 2, - y: window_size.height as i32 / 2, - })) { + if let Err(err) = + window.set_cursor_position(Position::Physical(PhysicalPosition { + x: window_size.width as i32 / 2, + y: window_size.height as i32 / 2, + })) + { cold_path(); tracing::error!( window_id=?window_id, diff --git a/engine/src/windowing/window.rs b/engine/src/windowing/window.rs index 93cb233..bbf7eb6 100644 --- a/engine/src/windowing/window.rs +++ b/engine/src/windowing/window.rs @@ -33,6 +33,10 @@ pub struct CreationAttributes pub position: Option<Position>, pub inner_size: Option<Size>, pub x_visual_id: Option<XVisualID>, + + // These do not have equivalents in winit::window::WindowAttributes + pub cursor_visible: bool, + pub cursor_grab_mode: CursorGrabMode, } macro_rules! gen_creation_attrs_with_fn { @@ -60,6 +64,9 @@ gen_creation_attrs_with_fn!(position, Option<Position>); gen_creation_attrs_with_fn!(inner_size, Option<Size>); gen_creation_attrs_with_fn!(x_visual_id, Option<XVisualID>); +gen_creation_attrs_with_fn!(cursor_visible, bool); +gen_creation_attrs_with_fn!(cursor_grab_mode, CursorGrabMode); + impl CreationAttributes { pub(crate) fn into_window_attrs(self) -> winit::window::WindowAttributes @@ -69,8 +76,10 @@ impl CreationAttributes .with_transparent(self.transparent) .with_maximized(self.maximized) .with_fullscreen(match self.fullscreen { - Some(Fullscreen::Borderless) => Some(winit::window::Fullscreen::Borderless(None)), - None => None + Some(Fullscreen::Borderless) => { + Some(winit::window::Fullscreen::Borderless(None)) + } + None => None, }) .with_visible(self.visible) .with_resizable(self.resizable); @@ -80,11 +89,18 @@ impl CreationAttributes #[cfg(target_os = "linux")] if let Some(visual_id) = self.x_visual_id { - return <winit::window::WindowAttributes as winit::platform::x11::WindowAttributesExtX11>::with_x11_visual(window_attrs, visual_id) + use winit::platform::x11::WindowAttributesExtX11; + + return window_attrs.with_x11_visual(visual_id); } window_attrs } + + pub(crate) fn apply_extra_attrs_to_window(&self, window: &winit::window::Window) + { + window.set_cursor_visible(self.cursor_visible); + } } impl Default for CreationAttributes @@ -100,7 +116,9 @@ impl Default for CreationAttributes resizable: true, position: None, inner_size: None, - x_visual_id: None + x_visual_id: None, + cursor_visible: true, + cursor_grab_mode: CursorGrabMode::None, } } } @@ -146,8 +164,8 @@ impl Window { Self { title: creation_attrs.title.clone().into_owned(), - cursor_visible: true, - cursor_grab_mode: CursorGrabMode::None, + cursor_visible: creation_attrs.cursor_visible, + cursor_grab_mode: creation_attrs.cursor_grab_mode, wid: Id::from_inner(winit_window.id()), inner_size: winit_window.inner_size().into(), scale_factor: winit_window.scale_factor(), @@ -162,9 +180,9 @@ impl Window let curr_inner_size = winit_window.inner_size().clone().into(); - let inner_size_request_result = match winit_window - .request_inner_size(winit::dpi::Size::Physical(self.inner_size.clone().into())) - { + let inner_size_request_result = match winit_window.request_inner_size( + winit::dpi::Size::Physical(self.inner_size.clone().into()), + ) { // The comparison of curr_inner_size is in case the user's windowing system // lies about using the requested inner size None if curr_inner_size == self.inner_size => Ok(()), |
