Skip to content

Commit

Permalink
Window: always size immediately instead of prior logic
Browse files Browse the repository at this point in the history
It seems that X11 doesn't always resize when requested, so
we can't rely on this.
  • Loading branch information
dhardy committed Feb 24, 2024
1 parent e67428d commit 7ae183c
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions crates/kas-core/src/app/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ struct WindowData<G: AppGraphicsBuilder, T: Theme<G::Shared>> {
theme_window: T::Window,
next_avail_frame_time: Instant,
queued_frame_time: Option<Instant>,
is_sized: bool,
}

/// Per-window data
Expand Down Expand Up @@ -122,7 +121,6 @@ impl<A: AppData, G: AppGraphicsBuilder, T: Theme<G::Shared>> Window<A, G, T> {

// Now that we have a scale factor, we may need to resize:
let scale_factor = window.scale_factor();
let apply_size;
if scale_factor != 1.0 {
let sf32 = scale_factor as f32;
state.shared.theme.update_window(&mut theme_window, sf32);
Expand All @@ -143,20 +141,18 @@ impl<A: AppData, G: AppGraphicsBuilder, T: Theme<G::Shared>> Window<A, G, T> {

if let Some(size) = window.request_inner_size(ideal) {
debug_assert_eq!(size, window.inner_size());
apply_size = true;
} else {
// We will receive WindowEvent::Resized
apply_size = false;
// We will receive WindowEvent::Resized and resize then.
// Unfortunately we can't rely on this since some platforms (X11)
// don't always behave as expected, thus we must resize now.
}
} else {
apply_size = true;
}

let size: Size = window.inner_size().cast();
log::info!(
"new: constructed with physical size {:?}, scale factor {}",
"Window::resume: constructed with physical size {:?}, scale factor {}",
size,
scale_factor
scale_factor,
);

#[cfg(all(wayland_platform, feature = "clipboard"))]
Expand All @@ -175,9 +171,7 @@ impl<A: AppData, G: AppGraphicsBuilder, T: Theme<G::Shared>> Window<A, G, T> {
// NOTE: usage of Arc is inelegant, but avoids lots of unsafe code
let window = Arc::new(window);
let mut surface = G::new_surface(&mut state.shared.draw.draw, window.clone())?;
if apply_size {
surface.do_resize(&mut state.shared.draw.draw, size);
}
surface.do_resize(&mut state.shared.draw.draw, size);

let winit_id = window.id();

Expand All @@ -193,12 +187,9 @@ impl<A: AppData, G: AppGraphicsBuilder, T: Theme<G::Shared>> Window<A, G, T> {
theme_window,
next_avail_frame_time: time,
queued_frame_time: Some(time),
is_sized: apply_size,
});

if apply_size {
self.apply_size(state, true);
}
self.apply_size(state, true);

log::trace!(target: "kas_perf::wgpu::window", "resume: {}µs", time.elapsed().as_micros());
Ok(winit_id)
Expand All @@ -221,6 +212,7 @@ impl<A: AppData, G: AppGraphicsBuilder, T: Theme<G::Shared>> Window<A, G, T> {
let Some(ref mut window) = self.window else {
return false;
};

match event {
WindowEvent::Moved(_) | WindowEvent::Destroyed => false,
WindowEvent::Resized(size) => {
Expand All @@ -245,7 +237,6 @@ impl<A: AppData, G: AppGraphicsBuilder, T: Theme<G::Shared>> Window<A, G, T> {
// NOTE: we could try resizing here in case the window is too
// small due to non-linear scaling, but it appears unnecessary.
window.solve_cache.invalidate_rule_cache();
window.is_sized = false;
false
}
WindowEvent::RedrawRequested => self.do_draw(state).is_err(),
Expand Down Expand Up @@ -275,6 +266,7 @@ impl<A: AppData, G: AppGraphicsBuilder, T: Theme<G::Shared>> Window<A, G, T> {
let Some(ref window) = self.window else {
return (Action::empty(), None);
};

let mut messages = MessageStack::new();
let action = self.ev_state.flush_pending(
&mut state.shared,
Expand Down Expand Up @@ -349,6 +341,7 @@ impl<A: AppData, G: AppGraphicsBuilder, T: Theme<G::Shared>> Window<A, G, T> {
let Some(ref window) = self.window else {
return None;
};

let widget = self.widget.as_node(&state.data);
let mut messages = MessageStack::new();
self.ev_state
Expand All @@ -368,6 +361,7 @@ impl<A: AppData, G: AppGraphicsBuilder, T: Theme<G::Shared>> Window<A, G, T> {
let Some(ref window) = self.window else {
return;
};

let mut messages = MessageStack::new();
self.ev_state
.with(&mut state.shared, window, &mut messages, |cx| {
Expand Down Expand Up @@ -453,7 +447,6 @@ impl<A: AppData, G: AppGraphicsBuilder, T: Theme<G::Shared>> Window<A, G, T> {
window.set_max_inner_size(Some(ideal));
};

window.is_sized = true;
window.set_visible(true);
window.request_redraw();
log::trace!(
Expand All @@ -471,10 +464,6 @@ impl<A: AppData, G: AppGraphicsBuilder, T: Theme<G::Shared>> Window<A, G, T> {
let Some(ref mut window) = self.window else {
return Ok(());
};
if !window.is_sized {
log::debug!("Ignoring redraw request before window is (re)sized.");
return Err(());
}

window.next_avail_frame_time = start + self.ev_state.config().frame_dur();

Expand Down

0 comments on commit 7ae183c

Please sign in to comment.