diff --git a/crates/ui-egui/src/file_asks.rs b/crates/ui-egui/src/file_asks.rs index 2b249d1c27..18d05eaf08 100644 --- a/crates/ui-egui/src/file_asks.rs +++ b/crates/ui-egui/src/file_asks.rs @@ -5,11 +5,13 @@ use std::path::PathBuf; +#[cfg(not(target_arch = "wasm32"))] use craft::dialogs::ui::UnavailableWords; use craft::host::{FileFilter, Files, Host, PickId, PickKind, PickRequest, Picked}; use craft::ui::dialog::{DialogId, DialogResult, PromptChoice, StandardPrompt}; // PLAN: assumed re-export paths for `HostedParamDialog`, `PathRequest` and `Env`. use craft::ui::patterns::form_dialogs::HostedParamDialog; +#[cfg(not(target_arch = "wasm32"))] use craft::ui::runtime::env::Env; use craft::ui::runtime::requests::PathRequest; use serde_json::{Map, Value, json}; @@ -263,7 +265,11 @@ pub(crate) fn poll(app: &mut VectorcraftApp) { match &picked { Picked::Cancelled => continue, Picked::Unavailable(why) => { + // The desktop says what gives it a file dialog; the browser has none for this kind of pick. + #[cfg(not(target_arch = "wasm32"))] app.status(Env::of_root(&ctx).words::().sentence(why)); + #[cfg(target_arch = "wasm32")] + app.status(why.clone()); continue; } _ => {} diff --git a/crates/ui-egui/src/io/read_only_save.rs b/crates/ui-egui/src/io/read_only_save.rs index 3ef43b35b4..91a4a259ba 100644 --- a/crates/ui-egui/src/io/read_only_save.rs +++ b/crates/ui-egui/src/io/read_only_save.rs @@ -4,7 +4,9 @@ use std::path::PathBuf; use serde_json::{Value, json}; -use vectorcraft_engine::cmd::fileio::{self, SaveMode, SavePlan}; +use vectorcraft_engine::cmd::fileio::SaveMode; +#[cfg(not(target_arch = "wasm32"))] +use vectorcraft_engine::cmd::fileio::{self, SavePlan}; use crate::VectorcraftApp; @@ -16,6 +18,7 @@ pub(super) struct Retry { } impl Retry { + #[cfg(not(target_arch = "wasm32"))] pub(super) fn new(plan: &SavePlan, folder: Option) -> Self { let name = plan.path.as_deref().map(fileio::file_name).unwrap_or_else(|| plan.name.clone()); let mut params = json!({ "format": plan.format.id, "options": plan.options }); @@ -40,6 +43,7 @@ impl Retry { let name = &self.name; app.status(format!("Cannot write to the location of “{name}”; choose where to save it")); // The failed save is the flow's running write: it ends before the picker opens. + #[cfg(not(target_arch = "wasm32"))] app.saves.release(uid); Some(super::save::save_suggesting(app, SaveMode::SaveAs, &self.params, false, Some((&self.name, self.directory)))) } diff --git a/crates/ui-egui/src/io/save.rs b/crates/ui-egui/src/io/save.rs index 45a0f19c2d..c380b6c53c 100644 --- a/crates/ui-egui/src/io/save.rs +++ b/crates/ui-egui/src/io/save.rs @@ -1,10 +1,18 @@ use std::collections::HashMap; -use std::path::{Path, PathBuf}; +use std::path::PathBuf; +// The save panel's flow is the desktop's (craft-dialogs is native only); the web downloads. +#[cfg(not(target_arch = "wasm32"))] +use std::path::Path; +#[cfg(not(target_arch = "wasm32"))] use craft::dialogs::save::{SAVE_IN_PROGRESS, SaveDocument, SaveFlow, SaveStep, SaveTarget}; -use craft::host::{FileFilter, Files, PickRequest, Picked, Wake}; +#[cfg(not(target_arch = "wasm32"))] +use craft::host::Files; +use craft::host::{FileFilter, PickRequest, Picked, Wake}; use craft::platform::watch::FileWatch; -use craft::ui::dialog::{DialogId, DialogResult, PromptChoice, StandardPrompt}; +use craft::ui::dialog::DialogId; +#[cfg(not(target_arch = "wasm32"))] +use craft::ui::dialog::{DialogResult, PromptChoice, StandardPrompt}; use super::export::file_url; use super::open::note_recent; @@ -18,6 +26,7 @@ use crate::file_asks::{self, Owner}; pub(crate) type Done = Box)>; /// What [`pump`] needs to go on with a document's save once its flow answers. +#[cfg(not(target_arch = "wasm32"))] struct SaveDone { mode: SaveMode, params: Value, @@ -31,11 +40,11 @@ struct SaveDone { retry: Option, /// The changed-on-disk question, while it is open. question: Option, - then: Option, } /// The saves of the app: one [`SaveFlow`] per document, and what the flows need to ask and write. pub(crate) struct Saves { + #[cfg(not(target_arch = "wasm32"))] flows: HashMap>, /// The version of each document's file the app last read or wrote. pub(crate) watch: FileWatch, @@ -43,51 +52,66 @@ pub(crate) struct Saves { pub(crate) written_beside: HashMap>, /// The files the OK of the replace question allows the save it runs again to replace. pub(crate) replace_files_ok: Vec, + #[cfg(not(target_arch = "wasm32"))] done: HashMap, + /// What each document's save tells once it is written, failed or cancelled ([`save_reporting`]). + then: HashMap, } impl Saves { /// No save in progress; `wake` runs a frame when the watcher has news. pub(crate) fn new(wake: Wake) -> Self { Self { + #[cfg(not(target_arch = "wasm32"))] flows: HashMap::new(), watch: FileWatch::new(wake), written_beside: HashMap::new(), replace_files_ok: Vec::new(), + #[cfg(not(target_arch = "wasm32"))] done: HashMap::new(), + then: HashMap::new(), } } + #[cfg(not(target_arch = "wasm32"))] fn start(&mut self, files: &dyn Files, document: SaveDocument, target: SaveTarget) -> SaveStep { let Self { flows, watch, .. } = self; flows.entry(document.key).or_default().start(files, watch, document, target) } /// Frees the document's flow after a write that wrote nothing. + #[cfg(not(target_arch = "wasm32"))] pub(super) fn release(&mut self, uid: u64) { if let Some(flow) = self.flows.get_mut(&uid) { flow.failed(&uid); } } + + /// A write's end on the document's flow: the flow is free again, and the watcher knows the new + /// version of a file the document took. + #[cfg(not(target_arch = "wasm32"))] + fn written(&mut self, uid: u64, retargets: bool, out: &Result) { + let written = out.as_ref().ok().filter(|v| v.get("cancelled").is_none() && v.get("pending").is_none()); + let flow = self.flows.entry(uid).or_default(); + match written.and_then(|v| v["path"].as_str()) { + Some(path) if flow.is_busy() || retargets => flow.written(&mut self.watch, uid, Path::new(path)), + _ => flow.failed(&uid), + } + } } /// Calls the `then` of the document's save, once. fn terminate(app: &mut VectorcraftApp, uid: u64, r: Result) { - if let Some(then) = app.saves.done.remove(&uid).and_then(|d| d.then) { + #[cfg(not(target_arch = "wasm32"))] + app.saves.done.remove(&uid); + if let Some(then) = app.saves.then.remove(&uid) { then(app, r); } } -/// A save's end: the flow is free again (and the watcher knows the new version of a file the -/// document took), and the caller that waits for it is told, unless a question or a fallback -/// picker goes on with it. -fn finish(app: &mut VectorcraftApp, uid: u64, retargets: bool, out: &Result) { - let written = out.as_ref().ok().filter(|v| v.get("cancelled").is_none() && v.get("pending").is_none()); - let flow = app.saves.flows.entry(uid).or_default(); - match written.and_then(|v| v["path"].as_str()) { - Some(path) if flow.is_busy() || retargets => flow.written(&mut app.saves.watch, uid, Path::new(path)), - _ => flow.failed(&uid), - } +/// A save's end: the caller that waits for it is told, unless a question or a fallback picker +/// goes on with it. +fn finish(app: &mut VectorcraftApp, uid: u64, out: &Result) { if !matches!(out, Ok(v) if v.get("pending").is_some()) { terminate(app, uid, out.clone()); } @@ -96,6 +120,7 @@ fn finish(app: &mut VectorcraftApp, uid: u64, retargets: bool, out: &Result = app.saves.flows.iter().filter(|(_, f)| f.is_busy()).map(|(uid, _)| *uid).collect(); + #[cfg(not(target_arch = "wasm32"))] for uid in busy { if !app.session.documents().iter().any(|d| d.uid == uid) { terminate(app, uid, Err(file_asks::closed().into())); @@ -165,10 +182,13 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) { app.status(e); } } + #[cfg(target_arch = "wasm32")] + let _ = app; } /// The step the document's flow has reached since the last pass: its question's answer, else its /// pick. +#[cfg(not(target_arch = "wasm32"))] fn next_step(app: &mut VectorcraftApp, uid: u64) -> Option> { if let Some(id) = app.saves.done.get(&uid).and_then(|d| d.question) { let answer = app.dialogs()?.with(|host| host.take_by_id::(id))?; @@ -186,6 +206,7 @@ fn next_step(app: &mut VectorcraftApp, uid: u64) -> Option> { flows.get_mut(&uid)?.take_pick(app.host.files(), watch) } +#[cfg(not(target_arch = "wasm32"))] /// Runs `step` of the document `uid`'s save; a failure frees the flow and tells the caller. fn run_step(app: &mut VectorcraftApp, uid: u64, step: SaveStep) -> Result { let r = proceed(app, step); @@ -196,6 +217,7 @@ fn run_step(app: &mut VectorcraftApp, uid: u64, step: SaveStep) -> Result) -> Result { match step { SaveStep::Picking => Ok(json!({ "pending": "fileDialog" })), @@ -217,6 +239,7 @@ fn proceed(app: &mut VectorcraftApp, step: SaveStep) -> Result { mode: SaveMode, @@ -255,6 +279,7 @@ struct Start<'a> { retrying: bool, } +#[cfg(not(target_arch = "wasm32"))] /// Starts the active document's save on its flow: the picker, or the changed-on-disk check of /// its own file, then the write. fn start_flow(app: &mut VectorcraftApp, first: &SavePlan, s: Start<'_>) -> Result { @@ -281,7 +306,6 @@ fn start_flow(app: &mut VectorcraftApp, first: &SavePlan, s: Start<'_>) -> Resul if matches!(step, SaveStep::Busy) { return Err(SAVE_IN_PROGRESS.english.into()); } - let then = app.saves.done.remove(&uid).and_then(|d| d.then); let done = SaveDone { mode: s.mode, params: s.params.clone(), @@ -291,7 +315,6 @@ fn start_flow(app: &mut VectorcraftApp, first: &SavePlan, s: Start<'_>) -> Resul retrying: s.retrying, retry: s.retry, question: None, - then, }; app.saves.done.insert(uid, done); run_step(app, uid, step) @@ -664,6 +687,7 @@ pub(crate) fn save_current(app: &mut VectorcraftApp, p: &Value) -> Result) -> Result { remember_view(app); let first = plan(app, SaveMode::Save, &acknowledged(p))?; @@ -695,6 +719,12 @@ pub(super) fn save_current_in(app: &mut VectorcraftApp, p: &Value, folder: Optio start_flow(app, &first, start) } +/// The web has no save panel to fall back on: Save downloads as [`save`] does. +#[cfg(target_arch = "wasm32")] +pub(super) fn save_current_in(app: &mut VectorcraftApp, p: &Value, _folder: Option) -> Result { + save(app, SaveMode::Save, p, true) +} + /// File → Save, Save As…, Save a Copy…, Save as Template… and the options dialogs' OK. `p` is the /// engine command's `{path?, format?, options?, svg?}`. /// @@ -738,22 +768,24 @@ pub(super) fn save_suggesting( return Ok(dialogs::save_options::open(app, mode.command(), first.format, &first.name, true)); } let retrying = suggestion.is_some(); - let (name, directory) = match suggestion { - Some((name, directory)) => (name.to_string(), directory), - None => (first.name.clone(), first.folder.clone().map(PathBuf::from)), - }; - // The web downloads under the suggested name: no panel. - if is_web(app) { - let picked = with_picked_type(&name, &name, None, first.format, mode); - return finish_picked(app, mode, p, ask_options, &picked, retrying); + let name = suggestion.as_ref().map_or_else(|| first.name.clone(), |(name, _)| (*name).to_string()); + // The desktop asks with the save panel; the web downloads under the suggested name. + #[cfg(not(target_arch = "wasm32"))] + if !is_web(app) { + let directory = match suggestion { + _ if mode == SaveMode::Template => fileio::templates_dialog_folder(&app.session.prefs).map(PathBuf::from), + Some((_, directory)) => directory, + None => first.folder.clone().map(PathBuf::from), + }; + let target = match mode { + SaveMode::Save => SaveTarget::InPlace, + SaveMode::SaveAs => SaveTarget::As, + SaveMode::Copy | SaveMode::Template => SaveTarget::Copy, + }; + return start_flow(app, &first, Start { mode, params: p, ask_options, target, retry: None, name, directory, retrying }); } - let directory = if mode == SaveMode::Template { fileio::templates_dialog_folder(&app.session.prefs).map(PathBuf::from) } else { directory }; - let target = match mode { - SaveMode::Save => SaveTarget::InPlace, - SaveMode::SaveAs => SaveTarget::As, - SaveMode::Copy | SaveMode::Template => SaveTarget::Copy, - }; - start_flow(app, &first, Start { mode, params: p, ask_options, target, retry: None, name, directory, retrying }) + let picked = with_picked_type(&name, &name, None, first.format, mode); + finish_picked(app, mode, p, ask_options, &picked, retrying) } /// The rest of a save to the path the save panel's answer was made into: the options dialog of @@ -845,8 +877,8 @@ pub(super) fn ask_before_losing(app: &mut VectorcraftApp, path: &str, command: & let st = app.session.active()?; let what = fileio::losses_summary(fileio::overwrite_losses(st, path, params)?); // The question's Cancel has no step of the flow to answer: the OK runs the command again. - let uid = st.uid; - app.saves.release(uid); + #[cfg(not(target_arch = "wasm32"))] + app.saves.release(st.uid); let name = fileio::file_name(path); let message = crate::i18n::fmt(tl!("Replace “{name}”, the file this document was opened from?"), &[("name", &name)]); let detail = crate::i18n::fmt( @@ -1130,7 +1162,9 @@ fn write_plan( let then = move |app: &mut VectorcraftApp, r: Result| { let out = outcome(app, r); if !handed.get() { - finish(app, uid, retargets, &out); + #[cfg(not(target_arch = "wasm32"))] + app.saves.written(uid, retargets, &out); + finish(app, uid, &out); } out };