SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Web app: restore and recover a large design without freezing the page, with progress and Cancel (#12578)
Daniel9387a1b93cbe
diff --git a/apps/solidcraft-web/src/keep.rs b/apps/solidcraft-web/src/keep.rsindex 80e182c6a0..df1868db3c 100644--- a/apps/solidcraft-web/src/keep.rs+++ b/apps/solidcraft-web/src/keep.rs@@ -173,6 +173,35 @@ pub fn is_open(open: Option<&str>, wanted: &str) -> bool {open == Some(wanted)}+/// What follows once a design restored from a kept copy, or recovered, is open: it opens in+/// stages, a few features a frame with progress and Cancel (#10198), so this waits for the answer.+#[derive(Clone, Debug, PartialEq, Eq)]+pub enum AfterOpen {+ /// A kept copy (#10220): it is its own design again (saving goes to `path`, it is named+ /// `name`), with its changes unsaved; then the copy `id` goes when `drop` says so.+ KeptCopy { opened: String, id: String, path: Option<String>, name: String, drop: bool },+ /// The saved design offered beside a kept copy: the copy `id` goes when `drop` says so.+ KeptSaved { opened: String, id: String, drop: bool },+ /// Unsaved work from the page's last visit: it belongs to `path` again, named `name`, unsaved.+ Recovery { opened: String, path: Option<String>, name: String },+}++impl AfterOpen {+ /// The file the open reads.+ pub fn opened(&self) -> &str {+ match self {+ AfterOpen::KeptCopy { opened, .. } | AfterOpen::KeptSaved { opened, .. } | AfterOpen::Recovery { opened, .. } => opened,+ }+ }+}++/// Whether the open of `wanted` answered with `reply` put it in: the answer says so, and it is+/// the design now open (`open`: the open design's path). A refusal, a Cancel or another file+/// leaves the copy or the recovered work offered.+pub fn opened(reply: &Value, open: Option<&str>, wanted: &str) -> bool {+ reply["ok"].as_bool() == Some(true) && is_open(open, wanted)+}+/// Whether leaving the page asks through the browser's "leave site?" box: there is unsaved work,/// and either the user still wants to be asked or it isn't all kept in the browser yet (with the/// keep preference off it never is, so there the box always stays).@@ -224,6 +253,20 @@ pub fn answers(entry: &Entry, saved_exists: bool) -> Vec<Answer> {mod tests {use super::*;+ /// A restored or recovered design opens in stages (#10198): only an answer that it is in, for+ /// the file asked for, counts; a refusal, a Cancel or another design open leaves it offered.+ #[test]+ fn a_staged_restore_counts_only_when_its_file_is_in() {+ let copy = "/opfs/.kept/a.sc";+ assert!(opened(&json!({"ok": true}), Some(copy), copy));+ assert!(!opened(&json!({"ok": false, "error": "cancelled"}), Some(copy), copy));+ assert!(!opened(&json!({"ok": false, "error": "not a design"}), None, copy));+ assert!(!opened(&json!({"ok": true}), Some("/opfs/other.sc"), copy));+ assert!(!opened(&json!({}), Some(copy), copy));+ let after = AfterOpen::KeptCopy { opened: copy.into(), id: "a".into(), path: None, name: "Bracket".into(), drop: true };+ assert_eq!(after.opened(), copy);+ }+fn entry(id: &str, changed_at: f64, path: Option<&str>) -> Entry {Entry { id: id.into(), name: "Bracket".into(), path: path.map(str::to_string), changed_at }}diff --git a/apps/solidcraft-web/src/main.rs b/apps/solidcraft-web/src/main.rsindex 629c24cda6..57a929aa5e 100644--- a/apps/solidcraft-web/src/main.rs+++ b/apps/solidcraft-web/src/main.rs@@ -382,6 +382,9 @@ mod web {keep_track: std::collections::HashMap<u64, Track>,/// Every unsaved design's kept copy is current (and the preference is on).all_kept: bool,+ /// A kept copy or recovered work being opened, in stages with progress and Cancel+ /// (#10198): the answer to come and what follows it ([`Shell::restored`]).+ restoring: Option<(std::sync::mpsc::Receiver<serde_json::Value>, keep::AfterOpen)>,}impl Shell {@@ -633,7 +636,7 @@ mod web {/// (when it has a file) and Discard.fn keep_prompt(&mut self, ctx: &egui::Context) {let offered = self.files.keep_offered.borrow().clone();- if offered.is_empty() || !self.files.ready.get() {+ if offered.is_empty() || !self.files.ready.get() || self.restoring.is_some() {return;}let now = (js_sys::Date::now() / 1000.0) as u64;@@ -673,24 +676,21 @@ mod web {return;}};- self.app.open_path(©);- if self.app.session.path.as_deref() != Some(copy.as_str()) {- // Not opened (the status says why): it stays offered.- return;- }- // It is its own design again (saving goes to its file), with its changes unsaved.- self.app.session.path = entry.path.clone();- self.app.session.doc_mut().name = entry.name.clone();- self.app.session.mark_unsaved();- self.app.home.open = false;+ // Finished in `restored` once it is in.+ let after = keep::AfterOpen::KeptCopy {+ opened: copy.clone(),+ id: entry.id.clone(),+ path: entry.path.clone(),+ name: entry.name.clone(),+ drop: plan.drop_copy,+ };+ self.restore(©, after);+ return;}if let Some(path) = &plan.open_saved {if vfs::exists(path) {- self.app.open_path(path);- if !keep::is_open(self.app.session.path.as_deref(), path) {- // Not opened (the status says why): the copy stays offered.- return;- }+ self.restore(path, keep::AfterOpen::KeptSaved { opened: path.clone(), id: entry.id.clone(), drop: plan.drop_copy });+ return;} else {// A file on the computer: the browser asks for access again, in this click.// The copy goes once the file has opened, not before: if access is refused,@@ -708,8 +708,62 @@ mod web {}}if plan.drop_copy {- self.files.keep_offered.borrow_mut().retain(|e| e.id != entry.id);- files::remove_kept(&self.files, &entry.id);+ self.drop_kept(&entry.id);+ }+ }++ fn drop_kept(&self, id: &str) {+ self.files.keep_offered.borrow_mut().retain(|e| e.id != id);+ files::remove_kept(&self.files, id);+ }++ /// Open a kept copy, its saved design or recovered work as any design opens, a few+ /// features a frame with progress and Cancel (#10198), so a large design doesn't freeze the+ /// page; `after` follows once it is in ([`Self::restored`]).+ fn restore(&mut self, path: &str, after: keep::AfterOpen) {+ self.restoring = Some((self.app.open_path_answered(path), after));+ self.restored();+ }++ /// What follows a restore that is in: the design gets back its file and name, and the+ /// copy or the recovered work it came from goes. One that didn't open (refused or+ /// cancelled; the status says why) stays offered.+ fn restored(&mut self) {+ let Some((rx, _)) = &self.restoring else { return };+ let reply = match rx.try_recv() {+ Ok(reply) => reply,+ Err(std::sync::mpsc::TryRecvError::Empty) => return,+ Err(std::sync::mpsc::TryRecvError::Disconnected) => serde_json::json!({"ok": false}),+ };+ let Some((_, after)) = self.restoring.take() else { return };+ if !keep::opened(&reply, self.app.session.path.as_deref(), after.opened()) {+ return;+ }+ match after {+ keep::AfterOpen::KeptCopy { id, path, name, drop, .. } => {+ // It is its own design again (saving goes to its file), with its changes unsaved.+ self.app.session.path = path;+ self.app.session.doc_mut().name = name;+ self.app.session.mark_unsaved();+ self.app.home.open = false;+ if drop {+ self.drop_kept(&id);+ }+ }+ keep::AfterOpen::KeptSaved { id, drop, .. } => {+ if drop {+ self.drop_kept(&id);+ }+ }+ keep::AfterOpen::Recovery { path, name, .. } => {+ // It belongs to its own file again (saving goes there); it is unsaved.+ self.app.session.path = path;+ self.app.session.doc_mut().name = name;+ self.app.session.mark_unsaved();+ self.app.home.open = false;+ *self.files.recovery.borrow_mut() = None;+ files::autosave(&self.files, None);+ }}}@@ -917,6 +971,9 @@ mod web {/// Offer the autosave an earlier session left.fn recovery_prompt(&mut self, ctx: &egui::Context) {+ if self.restoring.is_some() {+ return;+ }let Some((name, path)) = self.files.recovery.borrow().clone() else { return };let mut answer = None;let _ = solidcraft_ui_egui::ui_kit::Window::new(egui::Id::unique("sc_browser_recovery"), "restore", "Recover unsaved work?")@@ -941,15 +998,10 @@ mod web {return;}};- self.app.open_path(&recovery);- if self.app.session.path.as_deref() != Some(recovery.as_str()) {- return;- }- // It belongs to its own file again (saving goes there); it is unsaved.- self.app.session.path = path;- self.app.session.doc_mut().name = name;- self.app.session.mark_unsaved();- self.app.home.open = false;+ // Finished in `restored` once it is in; until then the prompt stays away.+ let after = keep::AfterOpen::Recovery { opened: recovery.clone(), path, name };+ self.restore(&recovery, after);+ return;}*self.files.recovery.borrow_mut() = None;files::autosave(&self.files, None);@@ -1093,6 +1145,7 @@ mod web {}fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {self.linked.tick(&mut self.app, ui.ctx());+ self.restored();UNSAVED.with(|u| u.set(self.asks_before_unload()));if self.launch.embed {ui_kit::Container::new(ui.ctx(), ui_kit::Surface::Chrome).edge("embed_header", ui_kit::Edge::Top).show(ui, |ui| {@@ -1306,6 +1359,7 @@ mod web {other_tab_prefs,keep_track: Default::default(),all_kept: false,+ restoring: None,}))}),)diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex be1e20a8e4..aadd490e79 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -2689,11 +2689,18 @@ impl SolveApp {self.set_status(text, false);}- pub fn open_path(&mut self, path: &str) {+ /// Open the design (or file) at `path` here and now: it is read and evaluated before this+ /// returns, which blocks the thread for a large design. The app opens through+ /// [`Self::open_path_async`] or [`Self::open_path_answered`] (progress and Cancel, #10198);+ /// this is for tests.+ #[cfg(test)]+ pub(crate) fn open_path(&mut self, path: &str) {self.open_path_with(path, &Default::default());}- /// [`Self::open_path`] with the import options of the request (`units` of an STL or OBJ file).+ /// Open the design (or file) at `path` here and now, with the import options of the request+ /// (`units` of an STL or OBJ file). The web file loader uses it for its own formats that have+ /// no staged open.pub fn open_path_with(&mut self, path: &str, options: &serde_json::Map<String, Value>) {match documents::reuse_path(self, path) {Ok(true) => return,diff --git a/crates/ui-egui/tests/stand_up_offer.rs b/crates/ui-egui/tests/stand_up_offer.rsindex cbe7d92cb6..b802168e64 100644--- a/crates/ui-egui/tests/stand_up_offer.rs+++ b/crates/ui-egui/tests/stand_up_offer.rs@@ -51,8 +51,8 @@ fn opened_boxes(size: [f64; 3], corners: &[[f64; 3]], programs: &str, prepare: istd::fs::write(&path, text.replace("'SolidCraft','SolidCraft'", programs)).unwrap();let mut h = Harness::new();prepare(&mut h);- h.app.open_path(&path);- h.frames(3);+ h.app.open_path_async(&path);+ h.settle();h}