From be7c448eb2c8dcc1e53bc1efa890affd9c148fe7 Mon Sep 17 00:00:00 2001 From: Devan Looches Date: Wed, 3 Aug 2022 08:53:41 -0700 Subject: [PATCH] Update status message and add it to the UI --- Cargo.lock | 3 ++ crates/fj-app/Cargo.toml | 4 ++ crates/fj-app/src/main.rs | 14 +++++-- crates/fj-host/Cargo.toml | 4 ++ crates/fj-host/src/lib.rs | 46 +++++++++++++++-------- crates/fj-viewer/src/graphics/renderer.rs | 6 +++ crates/fj-window/Cargo.toml | 4 ++ crates/fj-window/src/run.rs | 16 +++++--- 8 files changed, 72 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6996f7c3b7..2dec5f3af9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -846,6 +846,7 @@ dependencies = [ "fj", "fj-export", "fj-host", + "fj-interop", "fj-kernel", "fj-math", "fj-operations", @@ -872,6 +873,7 @@ version = "0.10.0" dependencies = [ "cargo_metadata", "fj", + "fj-interop", "libloading", "notify", "thiserror", @@ -958,6 +960,7 @@ name = "fj-window" version = "0.10.0" dependencies = [ "fj-host", + "fj-interop", "fj-operations", "fj-viewer", "futures", diff --git a/crates/fj-app/Cargo.toml b/crates/fj-app/Cargo.toml index b4c67eb74a..364da02a9e 100644 --- a/crates/fj-app/Cargo.toml +++ b/crates/fj-app/Cargo.toml @@ -55,6 +55,10 @@ path = "../fj-viewer" version = "0.10.0" path = "../fj-window" +[dependencies.fj-interop] +version = "0.10.0" +path = "../fj-interop" + [dependencies.serde] version = "1.0.140" features = ["derive"] diff --git a/crates/fj-app/src/main.rs b/crates/fj-app/src/main.rs index 6982fec487..5e90cad83d 100644 --- a/crates/fj-app/src/main.rs +++ b/crates/fj-app/src/main.rs @@ -20,6 +20,7 @@ use std::path::PathBuf; use anyhow::{anyhow, Context as _}; use fj_export::export; use fj_host::{Model, Parameters}; +use fj_interop::status_report::StatusReport; use fj_operations::shape_processor::ShapeProcessor; use fj_window::run::run; use tracing_subscriber::fmt::format; @@ -28,6 +29,7 @@ use tracing_subscriber::EnvFilter; use crate::{args::Args, config::Config}; fn main() -> anyhow::Result<()> { + let mut status = StatusReport::new(); // Respect `RUST_LOG`. If that's not defined or erroneous, log warnings and // above. // @@ -62,7 +64,13 @@ fn main() -> anyhow::Result<()> { }; if let Some(path) = args.export { - let shape = model.load_once(¶meters)?; + let shape = match model.load_once(¶meters, &mut status) { + Ok(shape) => shape, + Err(err) => { + status.update_status(err.to_string().as_str()); + return Err(anyhow!(err)); + } + }; let shape = shape_processor.process(&shape)?; export(&shape.mesh, &path)?; @@ -70,8 +78,8 @@ fn main() -> anyhow::Result<()> { return Ok(()); } - let watcher = model.load_and_watch(parameters)?; - run(watcher, shape_processor)?; + let watcher = model.load_and_watch(parameters, &mut status)?; + run(watcher, shape_processor, status)?; Ok(()) } diff --git a/crates/fj-host/Cargo.toml b/crates/fj-host/Cargo.toml index 164c1e1c28..c3fcc6d6c8 100644 --- a/crates/fj-host/Cargo.toml +++ b/crates/fj-host/Cargo.toml @@ -21,3 +21,7 @@ cargo_metadata = "0.15.0" [dependencies.fj] version = "0.10.0" path = "../fj" + +[dependencies.fj-interop] +version = "0.10.0" +path = "../fj-interop" diff --git a/crates/fj-host/src/lib.rs b/crates/fj-host/src/lib.rs index ad644be93d..8472eafe03 100644 --- a/crates/fj-host/src/lib.rs +++ b/crates/fj-host/src/lib.rs @@ -28,6 +28,7 @@ use std::{ thread, }; +use fj_interop::status_report::StatusReport; use notify::Watcher as _; use thiserror::Error; @@ -82,15 +83,18 @@ impl Model { pub fn load_once( &self, arguments: &Parameters, + status: &mut StatusReport, ) -> Result { let manifest_path = self.manifest_path.display().to_string(); - let status = Command::new("cargo") + status.update_status("Reloading model."); + let exit_status = Command::new("cargo") .arg("build") .args(["--manifest-path", &manifest_path]) .status()?; - if !status.success() { + if !exit_status.success() { + status.update_status("Error compiling model"); return Err(Error::Compile); } @@ -128,6 +132,7 @@ impl Model { pub fn load_and_watch( self, parameters: Parameters, + status: &mut StatusReport, ) -> Result { let (tx, rx) = mpsc::sync_channel(0); let tx2 = tx.clone(); @@ -260,22 +265,31 @@ impl Watcher { /// /// Returns `None`, if the model has not changed since the last time this /// method was called. - pub fn receive(&self) -> Option { + pub fn receive(&self, mut status: &mut StatusReport) -> Option { + status.update_status("Receiving updated shape..."); match self.channel.try_recv() { Ok(()) => { - let shape = match self.model.load_once(&self.parameters) { - Ok(shape) => shape, - Err(Error::Compile) => { - // It would be better to display an error in the UI, - // where the user can actually see it. Issue: - // https://github.com/hannobraun/fornjot/issues/30 - println!("Error compiling model"); - return None; - } - Err(err) => { - panic!("Error reloading model: {:?}", err); - } - }; + let shape = + match self.model.load_once(&self.parameters, &mut status) { + Ok(shape) => { + status.update_status("Updated shape."); + shape + } + Err(Error::Compile) => { + // It would be better to display an error in the UI, + // where the user can actually see it. Issue: + // https://github.com/hannobraun/fornjot/issues/30 + status.update_status("Error compiling model"); + return None; + } + Err(err) => { + status.update_status(&format!( + "Error reloading model: {:?}", + err + )); + panic!("Error reloading model: {:?}", err); + } + }; Some(shape) } diff --git a/crates/fj-viewer/src/graphics/renderer.rs b/crates/fj-viewer/src/graphics/renderer.rs index e33acd19ed..0f664e1baf 100644 --- a/crates/fj-viewer/src/graphics/renderer.rs +++ b/crates/fj-viewer/src/graphics/renderer.rs @@ -1,5 +1,6 @@ use std::{io, mem::size_of}; +use fj_interop::status_report::StatusReport; use fj_math::{Aabb, Point}; use thiserror::Error; use tracing::debug; @@ -297,6 +298,7 @@ impl Renderer { camera: &Camera, config: &mut DrawConfig, window: &egui_winit::winit::window::Window, + status: &StatusReport, ) -> Result<(), DrawError> { let aspect_ratio = self.surface_config.width as f64 / self.surface_config.height as f64; @@ -419,6 +421,10 @@ impl Renderer { egui::SidePanel::left("fj-left-panel").show(&self.egui.context, |ui| { ui.add_space(16.0); + ui.group(|ui| { + ui.label(format!("Status report:\n{}", status.status())) + }); + ui.add_space(16.0); ui.group(|ui| { ui.checkbox(&mut config.draw_model, "Render model") diff --git a/crates/fj-window/Cargo.toml b/crates/fj-window/Cargo.toml index 5fac566d04..c742cc56e1 100644 --- a/crates/fj-window/Cargo.toml +++ b/crates/fj-window/Cargo.toml @@ -29,3 +29,7 @@ path = "../fj-operations" [dependencies.fj-viewer] version = "0.10.0" path = "../fj-viewer" + +[dependencies.fj-interop] +version = "0.10.0" +path = "../fj-interop" diff --git a/crates/fj-window/src/run.rs b/crates/fj-window/src/run.rs index 67071390a1..e45adce85b 100644 --- a/crates/fj-window/src/run.rs +++ b/crates/fj-window/src/run.rs @@ -6,6 +6,7 @@ use std::error; use fj_host::Watcher; +use fj_interop::status_report::StatusReport; use fj_operations::shape_processor::ShapeProcessor; use fj_viewer::{ camera::Camera, @@ -30,6 +31,7 @@ use crate::window::{self, Window}; pub fn run( watcher: Watcher, shape_processor: ShapeProcessor, + mut status: StatusReport, ) -> Result<(), Error> { let event_loop = EventLoop::new(); let window = Window::new(&event_loop)?; @@ -49,7 +51,7 @@ pub fn run( event_loop.run(move |event, _, control_flow| { trace!("Handling event: {:?}", event); - if let Some(new_shape) = watcher.receive() { + if let Some(new_shape) = watcher.receive(&mut status) { match shape_processor.process(&new_shape) { Ok(new_shape) => { renderer.update_geometry( @@ -82,8 +84,6 @@ pub fn run( } } - // - if let Event::WindowEvent { event: window_event, .. @@ -164,15 +164,19 @@ pub fn run( }; } Event::MainEventsCleared => { + status.update_status("Redrawing..."); window.window().request_redraw(); } Event::RedrawRequested(_) => { if let (Some(shape), Some(camera)) = (&shape, &mut camera) { camera.update_planes(&shape.aabb); - if let Err(err) = - renderer.draw(camera, &mut draw_config, window.window()) - { + if let Err(err) = renderer.draw( + camera, + &mut draw_config, + window.window(), + &status, + ) { warn!("Draw error: {}", err); } }