Skip to content

Commit

Permalink
Move more logic into fj-viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
hannobraun committed Nov 15, 2024
1 parent 532187d commit 466efa2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
14 changes: 10 additions & 4 deletions crates/fj-viewer/src/viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
/// The Fornjot model viewer
pub struct Viewer {
current_screen_size: ScreenSize,
new_screen_size: Option<ScreenSize>,

camera: Camera,
cursor: Option<NormalizedScreenPosition>,
Expand All @@ -27,6 +28,7 @@ impl Viewer {

Ok(Self {
current_screen_size: screen.size(),
new_screen_size: None,
camera: Camera::default(),
cursor: None,
draw_config: DrawConfig::default(),
Expand Down Expand Up @@ -84,10 +86,7 @@ impl Viewer {
/// Handle the screen being resized
pub fn on_screen_resize(&mut self, new_size: ScreenSize) {
self.current_screen_size = new_size;
if new_size.is_valid() {
// We should only supply valid screen sizes to the renderer.
self.renderer.handle_resize(new_size);
}
self.new_screen_size = Some(new_size);
}

/// Compute and store a focus point, unless one is already stored
Expand All @@ -111,6 +110,13 @@ impl Viewer {
return;
}

if let Some(new_size) = self.new_screen_size.take() {
// We should only supply valid screen sizes to the renderer. But
// `self.current_screen_size` has already been updated, and we're
// checking if that's valid above. No need to check again.
self.renderer.handle_resize(new_size);
}

let aabb = self
.model
.as_ref()
Expand Down
10 changes: 1 addition & 9 deletions crates/fj-window/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ pub fn display(model: Model, invert_zoom: bool) -> Result<(), Error> {
window: None,
viewer: None,
held_mouse_button: None,
new_size: None,
};

event_loop.run_app(&mut display_state)?;
Expand Down Expand Up @@ -58,7 +57,6 @@ struct DisplayState {
window: Option<Window>,
viewer: Option<Viewer>,
held_mouse_button: Option<MouseButton>,
new_size: Option<ScreenSize>,
}

impl ApplicationHandler for DisplayState {
Expand Down Expand Up @@ -127,7 +125,7 @@ impl ApplicationHandler for DisplayState {
width: size.width,
height: size.height,
};
self.new_size = Some(size);
viewer.on_screen_resize(size);
}
WindowEvent::MouseInput { state, button, .. } => match state {
ElementState::Pressed => {
Expand All @@ -141,12 +139,6 @@ impl ApplicationHandler for DisplayState {
},
WindowEvent::MouseWheel { .. } => viewer.add_focus_point(),
WindowEvent::RedrawRequested => {
// Only do a screen resize once per frame. This protects against
// spurious resize events that cause issues with the renderer.
if let Some(size) = self.new_size.take() {
viewer.on_screen_resize(size);
}

viewer.draw();
}
_ => {}
Expand Down

0 comments on commit 466efa2

Please sign in to comment.