summaryrefslogtreecommitdiff
path: root/engine/src/windowing.rs
diff options
context:
space:
mode:
Diffstat (limited to 'engine/src/windowing.rs')
-rw-r--r--engine/src/windowing.rs144
1 files changed, 104 insertions, 40 deletions
diff --git a/engine/src/windowing.rs b/engine/src/windowing.rs
index b76d942..fee8755 100644
--- a/engine/src/windowing.rs
+++ b/engine/src/windowing.rs
@@ -154,6 +154,8 @@ fn update(
handle_app_messages(context, &mut actions, world);
+ update_externally_resized_windows(context, world);
+
if context.shared_state.thread_panicked.load(Ordering::Relaxed) {
cold_path();
return Err(error!("Windowing app thread panicked"));
@@ -204,7 +206,17 @@ fn handle_app_messages(context: &mut Context, actions: &mut Actions, world: &Wor
let window_id = WindowId::from_inner(winit_window.id());
- windows.insert(window_id, (winit_window, window_ent_id));
+ windows.insert(
+ window_id,
+ (
+ winit_window,
+ WindowMetadata {
+ pending_resize_req: None,
+ entity_id: window_ent_id,
+ flags: WindowFlags::empty(),
+ },
+ ),
+ );
tracing::info!(
window_id = ?window_id,
@@ -212,41 +224,24 @@ fn handle_app_messages(context: &mut Context, actions: &mut Actions, world: &Wor
"Window creation completed"
);
}
- MessageFromApp::Window(WindowMessage::Resized(
- window_id,
- new_window_size,
- )) => {
- tracing::trace!(
- window_id = ?window_id,
- "Received window resized message"
- );
+ MessageFromApp::Window(WindowMessage::Resized(window_id)) => {
+ let Some((_, window_metadata)) = windows.get_mut(window_id) else {
+ cold_path();
- 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) = world.get_entity(*window_ent_id) else {
- continue;
- };
- let Some(mut window) = window_ent.get_mut::<Window>() else {
continue;
};
- window.inner_size = new_window_size;
-
- window.set_changed();
+ window_metadata
+ .flags
+ .insert(WindowFlags::EXTERNALLY_RESIZED_SINCE_LAST_TICK);
}
MessageFromApp::Window(WindowMessage::CloseRequested(window_id)) => {
- let Some(window_ent_id) =
- windows.get(window_id).map(|(_, ent_id)| ent_id)
- else {
+ let Some((_, window_metadata)) = windows.get(window_id) else {
tracing::error!(
wid = ?window_id,
"Window does not exist in windowing context"
@@ -254,15 +249,13 @@ fn handle_app_messages(context: &mut Context, actions: &mut Actions, world: &Wor
continue;
};
- actions.remove_comps::<(Window,)>(*window_ent_id);
+ actions.remove_comps::<(Window,)>(window_metadata.entity_id);
}
MessageFromApp::Window(WindowMessage::ScaleFactorChanged(
window_id,
scale_factor,
)) => {
- let Some(window_ent_id) =
- windows.get(window_id).map(|(_, ent_id)| ent_id)
- else {
+ let Some((_, window_metadata)) = windows.get(window_id) else {
tracing::error!(
wid = ?window_id,
"Window does not exist in windowing context"
@@ -270,7 +263,7 @@ fn handle_app_messages(context: &mut Context, actions: &mut Actions, world: &Wor
continue;
};
- let Some(window_ent) = world.get_entity(*window_ent_id) else {
+ let Some(window_ent) = world.get_entity(window_metadata.entity_id) else {
continue;
};
@@ -286,6 +279,44 @@ fn handle_app_messages(context: &mut Context, actions: &mut Actions, world: &Wor
}
}
+fn update_externally_resized_windows(context: &mut Context, world: &World)
+{
+ for (winit_window, window_metadata) in context.windows.values_mut() {
+ if !window_metadata
+ .flags
+ .contains(WindowFlags::EXTERNALLY_RESIZED_SINCE_LAST_TICK)
+ {
+ continue;
+ }
+
+ window_metadata
+ .flags
+ .remove(WindowFlags::EXTERNALLY_RESIZED_SINCE_LAST_TICK);
+
+ let new_window_size = PhysicalSize::from(winit_window.inner_size());
+
+ let Some(window_ent) = world.get_entity(window_metadata.entity_id) else {
+ cold_path();
+ continue;
+ };
+
+ let Some(mut window) = window_ent.get_mut::<Window>() else {
+ cold_path();
+ continue;
+ };
+
+ window.set_changed();
+
+ window.real_inner_size = new_window_size;
+
+ if window_metadata.pending_resize_req == Some(new_window_size) {
+ window_metadata.pending_resize_req = None;
+ }
+
+ window.inner_size = new_window_size;
+ }
+}
+
fn update_inputs(
context: &Context,
keyboard: &mut Keyboard,
@@ -391,10 +422,10 @@ fn update_inputs(
fn handle_window_changed(
observe: Observe<'_, Pair<Changed, Window>>,
- context: Single<Context>,
+ mut context: Single<Context>,
)
{
- let Ok(context) = context.get() else {
+ let Ok(context) = context.get_mut() else {
unreachable!();
};
@@ -403,7 +434,8 @@ fn handle_window_changed(
let mut window = evt_match.get_ent_target_comp_mut();
- let Some((winit_window, _)) = context.windows.get(window.wid()) else {
+ let Some((winit_window, window_metadata)) = context.windows.get_mut(window.wid())
+ else {
tracing::error!(
wid = ?window.wid(),
entity_id = %window_ent_id,
@@ -412,10 +444,28 @@ fn handle_window_changed(
continue;
};
- let window_apply_results = window.apply(winit_window);
+ window.apply(winit_window);
- if let Err(actual_window_inner_size) = window_apply_results.inner_size_request {
- window.inner_size = actual_window_inner_size;
+ if window_metadata.pending_resize_req.is_none()
+ && window.inner_size != window.real_inner_size
+ {
+ match winit_window
+ .request_inner_size(winit::dpi::Size::Physical(window.inner_size.into()))
+ {
+ Some(applied_or_old_inner_size) => {
+ if applied_or_old_inner_size != window.inner_size.into()
+ || applied_or_old_inner_size != window.real_inner_size.into()
+ {
+ window.set_changed();
+ }
+
+ window.real_inner_size = applied_or_old_inner_size.into();
+ window.inner_size = window.real_inner_size;
+ }
+ None => {
+ window_metadata.pending_resize_req = Some(window.inner_size);
+ }
+ }
}
context.try_send_message_to_app(MessageToApp::SetWindowCursorGrabMode(
@@ -454,7 +504,7 @@ pub struct Context
{
shared_state: Arc<SharedState>,
display_handle: Option<OwnedDisplayHandle>,
- windows: IntMap<WindowId, (Arc<WinitWindow>, Uid)>,
+ windows: IntMap<WindowId, (Arc<WinitWindow>, WindowMetadata)>,
available_monitors: Vec<MonitorHandle>,
primary_monitor: Option<MonitorHandle>,
}
@@ -636,6 +686,21 @@ impl Drop for Context
}
}
+#[derive(Debug)]
+struct WindowMetadata
+{
+ pending_resize_req: Option<PhysicalSize<u32>>,
+ entity_id: Uid,
+ flags: WindowFlags,
+}
+
+bitflags! {
+ #[derive(Debug)]
+ struct WindowFlags: u8 {
+ const EXTERNALLY_RESIZED_SINCE_LAST_TICK = 1;
+ }
+}
+
fn app_thread_main(shared_state: Arc<SharedState>)
{
let shared_state_c = shared_state.clone();
@@ -733,7 +798,7 @@ enum MessageFromApp
enum WindowMessage
{
Created(Uid, Arc<WinitWindow>, WindowCreationAttributes),
- Resized(WindowId, PhysicalSize<u32>),
+ Resized(WindowId),
CloseRequested(WindowId),
ScaleFactorChanged(WindowId, f64),
}
@@ -1136,10 +1201,9 @@ impl ApplicationHandler for App
)
{
match event {
- WindowEvent::Resized(new_window_size) => {
+ WindowEvent::Resized(_) => {
self.send_message(MessageFromApp::Window(WindowMessage::Resized(
WindowId::from_inner(window_id),
- new_window_size.into(),
)));
}
WindowEvent::CloseRequested => {