From 48b031e4f3e8f60ea087fbc90ff232b3cdae876d Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 2 Nov 2022 13:22:56 +0100 Subject: [PATCH 01/12] Fix Markdown syntax in doc comment --- crates/fj-interop/src/status_report.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/fj-interop/src/status_report.rs b/crates/fj-interop/src/status_report.rs index 9405ff862..b78cf9238 100644 --- a/crates/fj-interop/src/status_report.rs +++ b/crates/fj-interop/src/status_report.rs @@ -11,7 +11,7 @@ pub struct StatusReport { } impl StatusReport { - /// Create a new ``StatusReport`` instance with a blank status + /// Create a new `StatusReport` instance with a blank status pub fn new() -> Self { Self::default() } From ebd655549f4bf761a7e9571d7bcc377220b512e2 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 2 Nov 2022 13:25:16 +0100 Subject: [PATCH 02/12] Move `StatusReport` to `fj-viewer` Since more code has been moved out of the main loop into separate threads, and communication has been replaced with channels, `StatusReport` isn't as widely used anymore. --- Cargo.lock | 2 +- crates/fj-interop/Cargo.toml | 1 - crates/fj-interop/src/lib.rs | 1 - crates/fj-viewer/Cargo.toml | 1 + crates/fj-viewer/src/gui.rs | 3 +-- crates/fj-viewer/src/lib.rs | 2 ++ crates/{fj-interop => fj-viewer}/src/status_report.rs | 0 crates/fj-viewer/src/viewer.rs | 5 ++--- crates/fj-window/src/run.rs | 3 +-- 9 files changed, 8 insertions(+), 10 deletions(-) rename crates/{fj-interop => fj-viewer}/src/status_report.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index 97dab07e7..ded72f909 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1177,7 +1177,6 @@ dependencies = [ name = "fj-interop" version = "0.22.0" dependencies = [ - "chrono", "fj-math", ] @@ -1236,6 +1235,7 @@ name = "fj-viewer" version = "0.22.0" dependencies = [ "bytemuck", + "chrono", "crossbeam-channel", "egui", "egui-wgpu", diff --git a/crates/fj-interop/Cargo.toml b/crates/fj-interop/Cargo.toml index cf6bf44e5..e40226a36 100644 --- a/crates/fj-interop/Cargo.toml +++ b/crates/fj-interop/Cargo.toml @@ -11,5 +11,4 @@ keywords.workspace = true categories.workspace = true [dependencies] -chrono = "0.4.22" fj-math.workspace = true diff --git a/crates/fj-interop/src/lib.rs b/crates/fj-interop/src/lib.rs index 450190fe1..cea11ceb3 100644 --- a/crates/fj-interop/src/lib.rs +++ b/crates/fj-interop/src/lib.rs @@ -18,4 +18,3 @@ pub mod debug; pub mod ext; pub mod mesh; pub mod processed_shape; -pub mod status_report; diff --git a/crates/fj-viewer/Cargo.toml b/crates/fj-viewer/Cargo.toml index 3ec99213f..8fca4af35 100644 --- a/crates/fj-viewer/Cargo.toml +++ b/crates/fj-viewer/Cargo.toml @@ -12,6 +12,7 @@ categories.workspace = true [dependencies] bytemuck = "1.12.2" +chrono = "0.4.22" crossbeam-channel = "0.5.6" egui = "0.19.0" egui-wgpu = "0.19.0" diff --git a/crates/fj-viewer/src/gui.rs b/crates/fj-viewer/src/gui.rs index 6c1fad6d3..1a5e5da5d 100644 --- a/crates/fj-viewer/src/gui.rs +++ b/crates/fj-viewer/src/gui.rs @@ -24,10 +24,9 @@ use crossbeam_channel::{Receiver, Sender}; #[cfg(not(target_arch = "wasm32"))] use rfd::FileDialog; -use fj_interop::status_report::StatusReport; use fj_math::{Aabb, Scalar}; -use crate::graphics::DrawConfig; +use crate::{graphics::DrawConfig, StatusReport}; struct GuiState { has_model: bool, diff --git a/crates/fj-viewer/src/lib.rs b/crates/fj-viewer/src/lib.rs index eea0499fb..b30df8506 100644 --- a/crates/fj-viewer/src/lib.rs +++ b/crates/fj-viewer/src/lib.rs @@ -19,6 +19,7 @@ mod graphics; mod gui; mod input; mod screen; +mod status_report; mod viewer; pub use self::{ @@ -27,5 +28,6 @@ pub use self::{ gui::Gui, input::{InputEvent, InputHandler}, screen::{NormalizedScreenPosition, Screen, ScreenSize}, + status_report::StatusReport, viewer::Viewer, }; diff --git a/crates/fj-interop/src/status_report.rs b/crates/fj-viewer/src/status_report.rs similarity index 100% rename from crates/fj-interop/src/status_report.rs rename to crates/fj-viewer/src/status_report.rs diff --git a/crates/fj-viewer/src/viewer.rs b/crates/fj-viewer/src/viewer.rs index c0520084f..803541608 100644 --- a/crates/fj-viewer/src/viewer.rs +++ b/crates/fj-viewer/src/viewer.rs @@ -1,8 +1,6 @@ use std::path::PathBuf; -use fj_interop::{ - processed_shape::ProcessedShape, status_report::StatusReport, -}; +use fj_interop::processed_shape::ProcessedShape; use fj_math::Aabb; use tracing::warn; @@ -11,6 +9,7 @@ use crossbeam_channel::{Receiver, Sender}; use crate::{ camera::FocusPoint, gui::Gui, Camera, DrawConfig, InputEvent, InputHandler, NormalizedScreenPosition, Renderer, RendererInitError, Screen, ScreenSize, + StatusReport, }; /// The Fornjot model viewer diff --git a/crates/fj-window/src/run.rs b/crates/fj-window/src/run.rs index 4bc91f38b..91776f10b 100644 --- a/crates/fj-window/src/run.rs +++ b/crates/fj-window/src/run.rs @@ -6,11 +6,10 @@ use std::{error, path::PathBuf}; use fj_host::{Host, Model, ModelEvent, Parameters}; -use fj_interop::status_report::StatusReport; use fj_operations::shape_processor::ShapeProcessor; use fj_viewer::{ InputEvent, NormalizedScreenPosition, RendererInitError, Screen, - ScreenSize, Viewer, + ScreenSize, StatusReport, Viewer, }; use futures::executor::block_on; use tracing::trace; From 5b2d75447ce908ec76f4bf61064ad3259b584640 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 2 Nov 2022 13:37:20 +0100 Subject: [PATCH 03/12] Update order of code `Gui` is the most important struct in this module. It should come first. --- crates/fj-viewer/src/gui.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/crates/fj-viewer/src/gui.rs b/crates/fj-viewer/src/gui.rs index 1a5e5da5d..dd3b121a9 100644 --- a/crates/fj-viewer/src/gui.rs +++ b/crates/fj-viewer/src/gui.rs @@ -28,16 +28,6 @@ use fj_math::{Aabb, Scalar}; use crate::{graphics::DrawConfig, StatusReport}; -struct GuiState { - has_model: bool, -} - -impl Default for GuiState { - fn default() -> Self { - Self { has_model: true } - } -} - /// The GUI pub struct Gui { context: egui::Context, @@ -375,3 +365,13 @@ pub struct Options { pub show_settings_ui: bool, pub show_inspection_ui: bool, } + +struct GuiState { + has_model: bool, +} + +impl Default for GuiState { + fn default() -> Self { + Self { has_model: true } + } +} From 373e3be303ae4023c7034ff06af2a2e22e98f57a Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 2 Nov 2022 13:39:29 +0100 Subject: [PATCH 04/12] Make `GuiState` public --- crates/fj-viewer/src/gui.rs | 6 ++++-- crates/fj-viewer/src/lib.rs | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/crates/fj-viewer/src/gui.rs b/crates/fj-viewer/src/gui.rs index dd3b121a9..eb6f7c51a 100644 --- a/crates/fj-viewer/src/gui.rs +++ b/crates/fj-viewer/src/gui.rs @@ -366,8 +366,10 @@ pub struct Options { pub show_inspection_ui: bool, } -struct GuiState { - has_model: bool, +/// The current status of the GUI +pub struct GuiState { + /// Indicates whether a model is currently available + pub has_model: bool, } impl Default for GuiState { diff --git a/crates/fj-viewer/src/lib.rs b/crates/fj-viewer/src/lib.rs index b30df8506..bab957c54 100644 --- a/crates/fj-viewer/src/lib.rs +++ b/crates/fj-viewer/src/lib.rs @@ -25,7 +25,7 @@ mod viewer; pub use self::{ camera::Camera, graphics::{DrawConfig, Renderer, RendererInitError}, - gui::Gui, + gui::{Gui, GuiState}, input::{InputEvent, InputHandler}, screen::{NormalizedScreenPosition, Screen, ScreenSize}, status_report::StatusReport, From 52f0a07ee2a8ca9f7dd4fa05d6f998d84fe077eb Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 2 Nov 2022 13:39:47 +0100 Subject: [PATCH 05/12] Update struct field name --- crates/fj-viewer/src/gui.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/crates/fj-viewer/src/gui.rs b/crates/fj-viewer/src/gui.rs index eb6f7c51a..ba29221f0 100644 --- a/crates/fj-viewer/src/gui.rs +++ b/crates/fj-viewer/src/gui.rs @@ -115,7 +115,7 @@ impl Gui { .ok(); match gui_event { - Some(_) => self.state.has_model = false, + Some(_) => self.state.model_available = false, None => break, }; } @@ -278,7 +278,7 @@ impl Gui { }) }); - if !self.state.has_model { + if !self.state.model_available { egui::Area::new("ask-model") .anchor(egui::Align2::CENTER_CENTER, [0_f32, -5_f32]) .show(&self.context, |ui| { @@ -297,7 +297,7 @@ impl Gui { .send(model_dir) .expect("Channel is disconnected"); - self.state.has_model = true; + self.state.model_available = true; } } }) @@ -369,11 +369,13 @@ pub struct Options { /// The current status of the GUI pub struct GuiState { /// Indicates whether a model is currently available - pub has_model: bool, + pub model_available: bool, } impl Default for GuiState { fn default() -> Self { - Self { has_model: true } + Self { + model_available: true, + } } } From 8951c0d068d9cde3ec9278c337c1ed89d29f9fdd Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 2 Nov 2022 13:41:38 +0100 Subject: [PATCH 06/12] Simplify argument --- crates/fj-viewer/src/viewer.rs | 2 +- crates/fj-window/src/run.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/fj-viewer/src/viewer.rs b/crates/fj-viewer/src/viewer.rs index 803541608..d7192bf53 100644 --- a/crates/fj-viewer/src/viewer.rs +++ b/crates/fj-viewer/src/viewer.rs @@ -127,7 +127,7 @@ impl Viewer { pub fn draw( &mut self, pixels_per_point: f32, - status: &mut StatusReport, + status: &StatusReport, egui_input: egui::RawInput, ) { let aabb = self diff --git a/crates/fj-window/src/run.rs b/crates/fj-window/src/run.rs index 91776f10b..57e8f30ba 100644 --- a/crates/fj-window/src/run.rs +++ b/crates/fj-window/src/run.rs @@ -234,7 +234,7 @@ pub fn run( let egui_input = egui_winit_state.take_egui_input(window.window()); - viewer.draw(pixels_per_point, &mut status, egui_input); + viewer.draw(pixels_per_point, &status, egui_input); } _ => {} } From 9674010757dc111ca5ed7115820f3fd712b4a1bc Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 2 Nov 2022 13:52:37 +0100 Subject: [PATCH 07/12] Pass `GuiState` into `Gui::update` --- crates/fj-viewer/src/graphics/renderer.rs | 10 +++----- crates/fj-viewer/src/gui.rs | 30 +++-------------------- crates/fj-viewer/src/viewer.rs | 13 +++++----- crates/fj-window/src/run.rs | 14 +++++------ 4 files changed, 20 insertions(+), 47 deletions(-) diff --git a/crates/fj-viewer/src/graphics/renderer.rs b/crates/fj-viewer/src/graphics/renderer.rs index 4912ab30d..c828bb478 100644 --- a/crates/fj-viewer/src/graphics/renderer.rs +++ b/crates/fj-viewer/src/graphics/renderer.rs @@ -1,6 +1,6 @@ use std::{io, mem::size_of, path::PathBuf}; -use crossbeam_channel::{Receiver, Sender}; +use crossbeam_channel::Sender; use thiserror::Error; use tracing::debug; use wgpu::util::DeviceExt as _; @@ -172,12 +172,8 @@ impl Renderer { }) } - pub(crate) fn init_gui( - &self, - event_rx: Receiver<()>, - event_tx: Sender, - ) -> Gui { - Gui::new(&self.device, self.surface_config.format, event_rx, event_tx) + pub(crate) fn init_gui(&self, event_tx: Sender) -> Gui { + Gui::new(&self.device, self.surface_config.format, event_tx) } /// Updates the geometry of the model being rendered. diff --git a/crates/fj-viewer/src/gui.rs b/crates/fj-viewer/src/gui.rs index ba29221f0..301d7564f 100644 --- a/crates/fj-viewer/src/gui.rs +++ b/crates/fj-viewer/src/gui.rs @@ -19,7 +19,7 @@ use std::path::PathBuf; #[cfg(not(target_arch = "wasm32"))] use std::env::current_dir; -use crossbeam_channel::{Receiver, Sender}; +use crossbeam_channel::Sender; #[cfg(not(target_arch = "wasm32"))] use rfd::FileDialog; @@ -33,16 +33,13 @@ pub struct Gui { context: egui::Context, render_pass: egui_wgpu::renderer::RenderPass, options: Options, - event_rx: Receiver<()>, event_tx: Sender, - state: GuiState, } impl Gui { pub(crate) fn new( device: &wgpu::Device, texture_format: wgpu::TextureFormat, - event_rx: Receiver<()>, event_tx: Sender, ) -> Self { // The implementation of the integration with `egui` is likely to need @@ -83,9 +80,7 @@ impl Gui { context, render_pass, options: Default::default(), - event_rx, event_tx, - state: Default::default(), } } @@ -94,6 +89,7 @@ impl Gui { &self.context } + #[allow(clippy::too_many_arguments)] pub(crate) fn update( &mut self, pixels_per_point: f32, @@ -102,24 +98,8 @@ impl Gui { aabb: &Aabb<3>, status: &StatusReport, line_drawing_available: bool, + state: GuiState, ) { - loop { - let gui_event = self - .event_rx - .try_recv() - .map_err(|err| { - if err.is_disconnected() { - panic!("Expected channel to never disconnect"); - } - }) - .ok(); - - match gui_event { - Some(_) => self.state.model_available = false, - None => break, - }; - } - self.context.set_pixels_per_point(pixels_per_point); self.context.begin_frame(egui_input); @@ -278,7 +258,7 @@ impl Gui { }) }); - if !self.state.model_available { + if !state.model_available { egui::Area::new("ask-model") .anchor(egui::Align2::CENTER_CENTER, [0_f32, -5_f32]) .show(&self.context, |ui| { @@ -296,8 +276,6 @@ impl Gui { self.event_tx .send(model_dir) .expect("Channel is disconnected"); - - self.state.model_available = true; } } }) diff --git a/crates/fj-viewer/src/viewer.rs b/crates/fj-viewer/src/viewer.rs index d7192bf53..b0e3c2621 100644 --- a/crates/fj-viewer/src/viewer.rs +++ b/crates/fj-viewer/src/viewer.rs @@ -4,12 +4,12 @@ use fj_interop::processed_shape::ProcessedShape; use fj_math::Aabb; use tracing::warn; -use crossbeam_channel::{Receiver, Sender}; +use crossbeam_channel::Sender; use crate::{ - camera::FocusPoint, gui::Gui, Camera, DrawConfig, InputEvent, InputHandler, - NormalizedScreenPosition, Renderer, RendererInitError, Screen, ScreenSize, - StatusReport, + camera::FocusPoint, gui::Gui, Camera, DrawConfig, GuiState, InputEvent, + InputHandler, NormalizedScreenPosition, Renderer, RendererInitError, + Screen, ScreenSize, StatusReport, }; /// The Fornjot model viewer @@ -43,11 +43,10 @@ impl Viewer { /// Construct a new instance of `Viewer` pub async fn new( screen: &impl Screen, - event_rx: Receiver<()>, event_tx: Sender, ) -> Result { let renderer = Renderer::new(screen).await?; - let gui = renderer.init_gui(event_rx, event_tx); + let gui = renderer.init_gui(event_tx); Ok(Self { camera: Camera::default(), @@ -129,6 +128,7 @@ impl Viewer { pixels_per_point: f32, status: &StatusReport, egui_input: egui::RawInput, + gui_state: GuiState, ) { let aabb = self .shape @@ -145,6 +145,7 @@ impl Viewer { &aabb, status, self.renderer.is_line_drawing_available(), + gui_state, ); if let Err(err) = self.renderer.draw( diff --git a/crates/fj-window/src/run.rs b/crates/fj-window/src/run.rs index 57e8f30ba..a5876a1e7 100644 --- a/crates/fj-window/src/run.rs +++ b/crates/fj-window/src/run.rs @@ -8,7 +8,7 @@ use std::{error, path::PathBuf}; use fj_host::{Host, Model, ModelEvent, Parameters}; use fj_operations::shape_processor::ShapeProcessor; use fj_viewer::{ - InputEvent, NormalizedScreenPosition, RendererInitError, Screen, + GuiState, InputEvent, NormalizedScreenPosition, RendererInitError, Screen, ScreenSize, StatusReport, Viewer, }; use futures::executor::block_on; @@ -30,15 +30,13 @@ pub fn run( shape_processor: ShapeProcessor, invert_zoom: bool, ) -> Result<(), Error> { - let (send_gui, gui_event_rx) = crossbeam_channel::bounded::<()>(1); let (gui_event_tx, recv_gui) = crossbeam_channel::bounded::(1); let mut status = StatusReport::new(); let event_loop = EventLoop::new(); let window = Window::new(&event_loop)?; - let mut viewer = - block_on(Viewer::new(&window, gui_event_rx, gui_event_tx))?; + let mut viewer = block_on(Viewer::new(&window, gui_event_tx))?; let mut held_mouse_button = None; @@ -47,8 +45,6 @@ pub fn run( let mut host = None; if let Some(model) = model { host = Some(Host::from_model(model)?); - } else { - send_gui.send(()).expect("Channel is disconnected"); } // Only handle resize events once every frame. This filters out spurious @@ -77,7 +73,6 @@ pub fn run( } Err(_) => { status.update_status("Error creating host."); - send_gui.send(()).expect("Channel is disconnected"); } } } @@ -234,7 +229,10 @@ pub fn run( let egui_input = egui_winit_state.take_egui_input(window.window()); - viewer.draw(pixels_per_point, &status, egui_input); + let gui_state = GuiState { + model_available: host.is_some(), + }; + viewer.draw(pixels_per_point, &status, egui_input, gui_state); } _ => {} } From ca4bb8e3d2a490b6ed00fa964e71ed6eaef0d69f Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 2 Nov 2022 13:53:47 +0100 Subject: [PATCH 08/12] Remove unused code --- crates/fj-viewer/src/gui.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/crates/fj-viewer/src/gui.rs b/crates/fj-viewer/src/gui.rs index 301d7564f..8692986d1 100644 --- a/crates/fj-viewer/src/gui.rs +++ b/crates/fj-viewer/src/gui.rs @@ -349,11 +349,3 @@ pub struct GuiState { /// Indicates whether a model is currently available pub model_available: bool, } - -impl Default for GuiState { - fn default() -> Self { - Self { - model_available: true, - } - } -} From 5a94aa8040708ddac65791aaac4cbb56582c2dd1 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 2 Nov 2022 13:56:07 +0100 Subject: [PATCH 09/12] Add `&StatusReport` to `GuiState` --- crates/fj-viewer/src/gui.rs | 18 +++++++++++------- crates/fj-viewer/src/viewer.rs | 4 +--- crates/fj-window/src/run.rs | 3 ++- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/crates/fj-viewer/src/gui.rs b/crates/fj-viewer/src/gui.rs index 8692986d1..86307be17 100644 --- a/crates/fj-viewer/src/gui.rs +++ b/crates/fj-viewer/src/gui.rs @@ -89,14 +89,12 @@ impl Gui { &self.context } - #[allow(clippy::too_many_arguments)] pub(crate) fn update( &mut self, pixels_per_point: f32, egui_input: egui::RawInput, config: &mut DrawConfig, aabb: &Aabb<3>, - status: &StatusReport, line_drawing_available: bool, state: GuiState, ) { @@ -250,10 +248,13 @@ impl Gui { egui::Area::new("fj-status-message").show(&self.context, |ui| { ui.group(|ui| { ui.add(egui::Label::new( - egui::RichText::new(format!("Status:{}", status.status())) - .monospace() - .color(egui::Color32::BLACK) - .background_color(egui::Color32::WHITE), + egui::RichText::new(format!( + "Status:{}", + state.status.status() + )) + .monospace() + .color(egui::Color32::BLACK) + .background_color(egui::Color32::WHITE), )) }) }); @@ -345,7 +346,10 @@ pub struct Options { } /// The current status of the GUI -pub struct GuiState { +pub struct GuiState<'a> { + /// Reference to the status messages + pub status: &'a StatusReport, + /// Indicates whether a model is currently available pub model_available: bool, } diff --git a/crates/fj-viewer/src/viewer.rs b/crates/fj-viewer/src/viewer.rs index b0e3c2621..9e3d47264 100644 --- a/crates/fj-viewer/src/viewer.rs +++ b/crates/fj-viewer/src/viewer.rs @@ -9,7 +9,7 @@ use crossbeam_channel::Sender; use crate::{ camera::FocusPoint, gui::Gui, Camera, DrawConfig, GuiState, InputEvent, InputHandler, NormalizedScreenPosition, Renderer, RendererInitError, - Screen, ScreenSize, StatusReport, + Screen, ScreenSize, }; /// The Fornjot model viewer @@ -126,7 +126,6 @@ impl Viewer { pub fn draw( &mut self, pixels_per_point: f32, - status: &StatusReport, egui_input: egui::RawInput, gui_state: GuiState, ) { @@ -143,7 +142,6 @@ impl Viewer { egui_input, &mut self.draw_config, &aabb, - status, self.renderer.is_line_drawing_available(), gui_state, ); diff --git a/crates/fj-window/src/run.rs b/crates/fj-window/src/run.rs index a5876a1e7..04ae03d85 100644 --- a/crates/fj-window/src/run.rs +++ b/crates/fj-window/src/run.rs @@ -230,9 +230,10 @@ pub fn run( egui_winit_state.take_egui_input(window.window()); let gui_state = GuiState { + status: &status, model_available: host.is_some(), }; - viewer.draw(pixels_per_point, &status, egui_input, gui_state); + viewer.draw(pixels_per_point, egui_input, gui_state); } _ => {} } From 4db3ddae2825b050aa1ad82f15f80df1b9f45a83 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 2 Nov 2022 13:58:52 +0100 Subject: [PATCH 10/12] Refactor --- crates/fj-window/src/run.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/crates/fj-window/src/run.rs b/crates/fj-window/src/run.rs index 04ae03d85..7a72c630a 100644 --- a/crates/fj-window/src/run.rs +++ b/crates/fj-window/src/run.rs @@ -42,10 +42,7 @@ pub fn run( let mut egui_winit_state = egui_winit::State::new(&event_loop); - let mut host = None; - if let Some(model) = model { - host = Some(Host::from_model(model)?); - } + let mut host = model.map(Host::from_model).transpose()?; // Only handle resize events once every frame. This filters out spurious // resize events that can lead to wgpu warnings. See this issue for some From cc8f86db0031bfbc6fe37af556dc672e0405e1f2 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 2 Nov 2022 14:07:56 +0100 Subject: [PATCH 11/12] Simplify returning data from GUI --- crates/fj-viewer/src/graphics/renderer.rs | 7 ++-- crates/fj-viewer/src/gui.rs | 18 ++++------ crates/fj-viewer/src/viewer.rs | 15 ++++---- crates/fj-window/src/run.rs | 43 +++++++++-------------- 4 files changed, 32 insertions(+), 51 deletions(-) diff --git a/crates/fj-viewer/src/graphics/renderer.rs b/crates/fj-viewer/src/graphics/renderer.rs index c828bb478..cea4a85ce 100644 --- a/crates/fj-viewer/src/graphics/renderer.rs +++ b/crates/fj-viewer/src/graphics/renderer.rs @@ -1,6 +1,5 @@ -use std::{io, mem::size_of, path::PathBuf}; +use std::{io, mem::size_of}; -use crossbeam_channel::Sender; use thiserror::Error; use tracing::debug; use wgpu::util::DeviceExt as _; @@ -172,8 +171,8 @@ impl Renderer { }) } - pub(crate) fn init_gui(&self, event_tx: Sender) -> Gui { - Gui::new(&self.device, self.surface_config.format, event_tx) + pub(crate) fn init_gui(&self) -> Gui { + Gui::new(&self.device, self.surface_config.format) } /// Updates the geometry of the model being rendered. diff --git a/crates/fj-viewer/src/gui.rs b/crates/fj-viewer/src/gui.rs index 86307be17..3e1216776 100644 --- a/crates/fj-viewer/src/gui.rs +++ b/crates/fj-viewer/src/gui.rs @@ -19,8 +19,6 @@ use std::path::PathBuf; #[cfg(not(target_arch = "wasm32"))] use std::env::current_dir; -use crossbeam_channel::Sender; - #[cfg(not(target_arch = "wasm32"))] use rfd::FileDialog; @@ -33,14 +31,12 @@ pub struct Gui { context: egui::Context, render_pass: egui_wgpu::renderer::RenderPass, options: Options, - event_tx: Sender, } impl Gui { pub(crate) fn new( device: &wgpu::Device, texture_format: wgpu::TextureFormat, - event_tx: Sender, ) -> Self { // The implementation of the integration with `egui` is likely to need // to change "significantly" depending on what architecture approach is @@ -80,7 +76,6 @@ impl Gui { context, render_pass, options: Default::default(), - event_tx, } } @@ -97,7 +92,7 @@ impl Gui { aabb: &Aabb<3>, line_drawing_available: bool, state: GuiState, - ) { + ) -> Option { self.context.set_pixels_per_point(pixels_per_point); self.context.begin_frame(egui_input); @@ -259,6 +254,8 @@ impl Gui { }) }); + let mut new_model_path = None; + if !state.model_available { egui::Area::new("ask-model") .anchor(egui::Align2::CENTER_CENTER, [0_f32, -5_f32]) @@ -272,16 +269,13 @@ impl Gui { .button(egui::RichText::new("Pick a model")) .clicked() { - let model_dir = show_file_dialog(); - if let Some(model_dir) = model_dir { - self.event_tx - .send(model_dir) - .expect("Channel is disconnected"); - } + new_model_path = show_file_dialog(); } }) }); } + + new_model_path } pub(crate) fn draw( diff --git a/crates/fj-viewer/src/viewer.rs b/crates/fj-viewer/src/viewer.rs index 9e3d47264..db03c42f4 100644 --- a/crates/fj-viewer/src/viewer.rs +++ b/crates/fj-viewer/src/viewer.rs @@ -4,8 +4,6 @@ use fj_interop::processed_shape::ProcessedShape; use fj_math::Aabb; use tracing::warn; -use crossbeam_channel::Sender; - use crate::{ camera::FocusPoint, gui::Gui, Camera, DrawConfig, GuiState, InputEvent, InputHandler, NormalizedScreenPosition, Renderer, RendererInitError, @@ -41,12 +39,9 @@ pub struct Viewer { impl Viewer { /// Construct a new instance of `Viewer` - pub async fn new( - screen: &impl Screen, - event_tx: Sender, - ) -> Result { + pub async fn new(screen: &impl Screen) -> Result { let renderer = Renderer::new(screen).await?; - let gui = renderer.init_gui(event_tx); + let gui = renderer.init_gui(); Ok(Self { camera: Camera::default(), @@ -128,7 +123,7 @@ impl Viewer { pixels_per_point: f32, egui_input: egui::RawInput, gui_state: GuiState, - ) { + ) -> Option { let aabb = self .shape .as_ref() @@ -137,7 +132,7 @@ impl Viewer { self.camera.update_planes(&aabb); - self.gui.update( + let new_model_path = self.gui.update( pixels_per_point, egui_input, &mut self.draw_config, @@ -154,5 +149,7 @@ impl Viewer { ) { warn!("Draw error: {}", err); } + + new_model_path } } diff --git a/crates/fj-window/src/run.rs b/crates/fj-window/src/run.rs index 7a72c630a..759a58f22 100644 --- a/crates/fj-window/src/run.rs +++ b/crates/fj-window/src/run.rs @@ -3,7 +3,7 @@ //! Provides the functionality to create a window and perform basic viewing //! with programmed models. -use std::{error, path::PathBuf}; +use std::error; use fj_host::{Host, Model, ModelEvent, Parameters}; use fj_operations::shape_processor::ShapeProcessor; @@ -30,13 +30,11 @@ pub fn run( shape_processor: ShapeProcessor, invert_zoom: bool, ) -> Result<(), Error> { - let (gui_event_tx, recv_gui) = crossbeam_channel::bounded::(1); - let mut status = StatusReport::new(); let event_loop = EventLoop::new(); let window = Window::new(&event_loop)?; - let mut viewer = block_on(Viewer::new(&window, gui_event_tx))?; + let mut viewer = block_on(Viewer::new(&window))?; let mut held_mouse_button = None; @@ -53,27 +51,6 @@ pub fn run( event_loop.run(move |event, _, control_flow| { trace!("Handling event: {:?}", event); - let gui_event = recv_gui - .try_recv() - .map_err(|err| { - if err.is_disconnected() { - panic!("Expected channel to never disconnect"); - } - }) - .ok(); - - if let Some(model_path) = gui_event { - let model = Model::new(model_path, Parameters::empty()).unwrap(); - match Host::from_model(model) { - Ok(new_host) => { - host = Some(new_host); - } - Err(_) => { - status.update_status("Error creating host."); - } - } - } - if let Some(host) = &host { loop { let events = host.events(); @@ -230,7 +207,21 @@ pub fn run( status: &status, model_available: host.is_some(), }; - viewer.draw(pixels_per_point, egui_input, gui_state); + let new_model_path = + viewer.draw(pixels_per_point, egui_input, gui_state); + + if let Some(model_path) = new_model_path { + let model = + Model::new(model_path, Parameters::empty()).unwrap(); + match Host::from_model(model) { + Ok(new_host) => { + host = Some(new_host); + } + Err(_) => { + status.update_status("Error creating host."); + } + } + } } _ => {} } From 4f51bc3792f508dc32ecaf8e7d8c199fff675a97 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 2 Nov 2022 14:13:05 +0100 Subject: [PATCH 12/12] Fix grammar --- crates/fj-host/src/host.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/fj-host/src/host.rs b/crates/fj-host/src/host.rs index 191423c5c..8a556e10b 100644 --- a/crates/fj-host/src/host.rs +++ b/crates/fj-host/src/host.rs @@ -12,7 +12,7 @@ impl Host { /// Create a new instance of `Host` /// /// This is only useful, if you want to continuously watch the model for - /// changes. If you don't just keep using `Model`. + /// changes. If you don't, just keep using `Model`. pub fn from_model(model: Model) -> Result { let watch_path = model.watch_path(); let evaluator = Evaluator::from_model(model);