diff --git a/apps/solidcraft-web/src/linked.rs b/apps/solidcraft-web/src/linked.rs index 24e073d5d..f75d99446 100644 --- a/apps/solidcraft-web/src/linked.rs +++ b/apps/solidcraft-web/src/linked.rs @@ -15,6 +15,9 @@ type Reply = Result<(String, Vec), String>; pub struct Download { reply: Rc>>, pending: Option<(String, Vec)>, + /// The downloaded design being opened (a few features a frame, #10198): the answer to come, + /// the name it takes and the file it was read from. + opening: Option<(std::sync::mpsc::Receiver, String, String)>, } impl Download { @@ -33,13 +36,14 @@ impl Download { } pub fn tick(&mut self, app: &mut SolveApp, ctx: &egui::Context) { + self.opened(app); if let Some(reply) = self.reply.borrow_mut().take() { match reply { Ok(file) => self.pending = Some(file), Err(error) => app.set_status(format!("Could not open design link: {error}"), true), } } - if self.pending.is_none() || app.file_picker_busy() { + if self.pending.is_none() || self.opening.is_some() || app.file_picker_busy() { return; } if solidcraft_ui_egui::documents::any_dirty(app) { @@ -71,18 +75,33 @@ impl Download { } } let Some((name, bytes)) = self.pending.take() else { return }; - // The extension selects the design loader even when the URL has no extension. - let path = &format!("/linked/design.{}", solidcraft_engine::io::DESIGN_EXT); - vfs::insert(path, bytes); - app.open_path(path); - if app.session.path.as_deref() == Some(path.as_str()) { + // The extension selects the design loader even when the URL has no extension. Named + // after the link, as the progress card shows the file's name. + let file: String = name.chars().map(|c| if matches!(c, '/' | '\\') { '_' } else { c }).collect(); + let path = format!("/linked/{}.{}", if file.is_empty() { "design" } else { &file }, solidcraft_engine::io::DESIGN_EXT); + vfs::insert(&path, bytes); + // Opened as any design is, with its progress and Cancel; finished in `opened`. + self.opening = Some((app.open_path_answered(&path), name, path)); + self.opened(app); + } + + /// The downloaded design is in: it is an unsaved copy under the link's name. + fn opened(&mut self, app: &mut SolveApp) { + let Some((rx, _, _)) = &self.opening else { return }; + let (ok, cancelled) = match rx.try_recv() { + Ok(reply) => (reply["ok"].as_bool() == Some(true), reply["error"].as_str().is_some_and(|e| e.contains("cancelled"))), + Err(std::sync::mpsc::TryRecvError::Empty) => return, + Err(std::sync::mpsc::TryRecvError::Disconnected) => (false, false), + }; + let Some((_, name, path)) = self.opening.take() else { return }; + if ok && app.session.path.as_deref() == Some(path.as_str()) { app.session.path = None; app.session.doc_mut().name = name; app.session.mark_unsaved(); - } else { + } else if !ok && !cancelled { app.set_status("Could not open design link: the downloaded file is not a valid SolidCraft design", true); } - let _ = vfs::remove(path); + let _ = vfs::remove(&path); } } diff --git a/apps/solidcraft-web/src/main.rs b/apps/solidcraft-web/src/main.rs index 641cca8f6..ec3bd8ace 100644 --- a/apps/solidcraft-web/src/main.rs +++ b/apps/solidcraft-web/src/main.rs @@ -375,7 +375,8 @@ mod web { // Designs opened from the computer's disk (the files window): open them. let opened: Vec<_> = self.files.opened.borrow_mut().drain(..).collect(); for path in opened { - self.app.open_path(&path); + // A few features a frame, with progress and Cancel (#10198). + self.app.open_path_async(&path); self.files.panel.set(false); } // Recent designs whose file is gone are dropped from the list. @@ -416,7 +417,7 @@ mod web { path }; match intent { - Intent::Open => self.app.open_path(&path), + Intent::Open => self.app.open_path_async(&path), Intent::Insert | Intent::Theme => self.app.drop_path(&path), } self.files.panel.set(false); @@ -580,7 +581,7 @@ mod web { let path = format!("{}{name}", files::OPFS); match what { "open" => { - self.app.open_path(&path); + self.app.open_path_async(&path); self.files.panel.set(false); } "insert" => { diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 18426501b..51d3218b3 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -2345,6 +2345,18 @@ impl SolveApp { pub fn open_path_async(&mut self, path: &str) { file_load::begin(self, path, false); } + /// Open `path` as [`Self::open_path_async`] does, for a caller that has more to do once the + /// design is in (the web app's design links, #10198): the receiver gets `{"ok": true, ...}` + /// when it is, or `{"ok": false, "error"}`; it is dropped unanswered when the load is. + pub fn open_path_answered(&mut self, path: &str) -> std::sync::mpsc::Receiver { + let (tx, rx) = std::sync::mpsc::channel(); + let mut request = file_load::Request::new(path, file_load::Kind::Open); + request.reply = Some(tx); + if let Err(e) = file_load::begin_request(self, request) { + self.set_status(e, true); + } + rx + } pub fn insert_path_async(&mut self, path: &str) { file_load::begin(self, path, true); }