From a523c4308703c41215c8688c1c9bf1eb3908ddac Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 30 Oct 2025 15:52:13 +0100 Subject: feat(engine): add scale factor to Window struct --- engine/src/windowing.rs | 30 ++++++++++++++++++++++++++++++ engine/src/windowing/window.rs | 16 ++++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) (limited to 'engine/src') diff --git a/engine/src/windowing.rs b/engine/src/windowing.rs index 69adae9..2bfdb31 100644 --- a/engine/src/windowing.rs +++ b/engine/src/windowing.rs @@ -195,6 +195,29 @@ fn update_stuff( actions.remove_comps::<(Window,)>(*window_ent_id); } + MessageFromApp::WindowScaleFactorChanged(window_id, scale_factor) => { + let Some(window_ent_id) = + windows.get(&window_id).map(|(_, ent_id)| ent_id) + else { + tracing::error!( + wid = ?window_id, + "Window does not exist in windowing context" + ); + continue; + }; + + let Some(window_ent) = entity_obtainer.get_entity(*window_ent_id) else { + continue; + }; + + let Some(mut window) = window_ent.get_mut::() else { + continue; + }; + + window.set_scale_factor(scale_factor); + + window.set_changed(); + } MessageFromApp::KeyboardKeyStateChanged(key, key_state) => { keyboard.set_key_state(key, key_state); } @@ -405,6 +428,7 @@ enum MessageFromApp WindowCreated(Uid, Arc, WindowCreationAttributes), WindowResized(WindowId, Dimens), WindowCloseRequested(WindowId), + WindowScaleFactorChanged(WindowId, f64), KeyboardKeyStateChanged(Key, KeyState), MouseMoved { @@ -605,6 +629,12 @@ impl ApplicationHandler for App self.focused_window_id = None; } } + WindowEvent::ScaleFactorChanged { scale_factor, inner_size_writer: _ } => { + self.send_message(MessageFromApp::WindowScaleFactorChanged( + WindowId::from_inner(window_id), + scale_factor, + )); + } _ => {} } } diff --git a/engine/src/windowing/window.rs b/engine/src/windowing/window.rs index 79b2102..627bdec 100644 --- a/engine/src/windowing/window.rs +++ b/engine/src/windowing/window.rs @@ -87,11 +87,12 @@ pub struct CreationReady; #[non_exhaustive] pub struct Window { - wid: Id, pub title: Cow<'static, str>, pub cursor_visible: bool, pub cursor_grab_mode: CursorGrabMode, + wid: Id, inner_size: Dimens, + scale_factor: f64, } impl Window @@ -106,17 +107,23 @@ impl Window &self.inner_size } + pub fn scale_factor(&self) -> f64 + { + self.scale_factor + } + pub(crate) fn new( winit_window: &winit::window::Window, creation_attrs: &CreationAttributes, ) -> Self { Self { - wid: Id::from_inner(winit_window.id()), title: creation_attrs.title().to_string().into(), cursor_visible: true, cursor_grab_mode: CursorGrabMode::None, + wid: Id::from_inner(winit_window.id()), inner_size: winit_window.inner_size().into(), + scale_factor: winit_window.scale_factor(), } } @@ -130,6 +137,11 @@ impl Window { self.inner_size = inner_size; } + + pub(crate) fn set_scale_factor(&mut self, scale_factor: f64) + { + self.scale_factor = scale_factor; + } } #[derive(Debug, Component)] -- cgit v1.2.3-18-g5258