VectorCraft: An open-source, vector art tool written by scratch in pure Rust
Part of #861: a file the user can't open is reported in a dialog
bflatasticc19873e2204d
diff --git a/apps/vectorcraft/src/main.rs b/apps/vectorcraft/src/main.rsindex f446c543d..f7b6771d5 100644--- a/apps/vectorcraft/src/main.rs+++ b/apps/vectorcraft/src/main.rs@@ -118,12 +118,11 @@ fn end_before_teardown(frames: u64) {}/// Open files handed to the app (command line, macOS Finder and Dock) as documents. A file that-/// can't be opened is reported in the status bar and on stderr; the others still open.+/// can't be opened is reported to the user and on stderr; the others still open.fn open_files(app: &mut VectorcraftApp, files: Vec<String>) {for f in files {- if let Err(e) = vectorcraft_ui_egui::io::open_path(app, &f) {+ if let Err(e) = vectorcraft_ui_egui::io::open_reporting(app, &f) {eprintln!("vectorcraft: {f}: {e}");- app.status(format!("Couldn't open {}: {e}", fileio::file_name(&f)));}}}diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex 04392757f..f0dccfa3d 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -1850,10 +1850,9 @@ fn recent(app: &mut VectorcraftApp, ui: &mut Ui) {crate::icons::icon(ui, "history", 16.0, t.icon);let r = ui.add(egui::Button::new(egui::RichText::new(&name).color(t.accent).size(13.0)).frame(false)).on_hover_text(&path);ui.add(egui::Label::new(egui::RichText::new(&folder).size(12.0).color(t.text_dim)).truncate());- if r.clicked()- && let Err(e) = crate::io::open_path(app, &path)- {- app.status(e);+ if r.clicked() {+ // A failure is reported to the user there.+ let _ = crate::io::open_reporting(app, &path);}});}diff --git a/crates/ui-egui/src/dialogs/confirm.rs b/crates/ui-egui/src/dialogs/confirm.rsindex 4e02feb00..ba1668748 100644--- a/crates/ui-egui/src/dialogs/confirm.rs+++ b/crates/ui-egui/src/dialogs/confirm.rs@@ -36,6 +36,17 @@ pub fn ask(app: &mut VectorcraftApp, message: &str, detail: &str, command: &str,app.ui.dialog = Some(Dialog::new(KIND, fields));}+/// The dialog kind of a message with a Close button only ([`tell`]).+pub const MESSAGE: &str = "message";++pub(super) const MESSAGE_SPEC: DialogSpec = DialogSpec { ok: None, ..SPEC };++/// Tell the user `message` (with an optional `detail` line), both in the UI language, in a dialog+/// they close.+pub fn tell(app: &mut VectorcraftApp, message: &str, detail: &str) {+ app.ui.dialog = Some(Dialog::new(MESSAGE, json!({"message": message, "detail": detail})));+}+/// Closes before running, so a dialog the command opens stays open.fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result<Value, String> {let params = d.fields.get("__params").cloned().unwrap_or_else(|| json!({}));diff --git a/crates/ui-egui/src/dialogs/mod.rs b/crates/ui-egui/src/dialogs/mod.rsindex c1ecc219a..c0cf2d66b 100644--- a/crates/ui-egui/src/dialogs/mod.rs+++ b/crates/ui-egui/src/dialogs/mod.rs@@ -216,6 +216,7 @@ registry! {FindFont: ["findFont"] => DialogSpec::window(crate::find_font::show, |app, _| crate::find_font::confirm(app)),SwatchOptions: [swatch_options::KIND] => swatch_options::SPEC,Confirm: [confirm::KIND] => confirm::SPEC,+ Message: [confirm::MESSAGE] => confirm::MESSAGE_SPEC,NewSwatch: [new_swatch::KIND] => new_swatch::SPEC,NewColorGroup: [new_color_group::KIND] => new_color_group::SPEC,GradientStop: ["gradientStop"] => gradient_stop::SPEC,diff --git a/crates/ui-egui/src/i18n/es.tsv b/crates/ui-egui/src/i18n/es.tsvindex 5e550b498..116a0d88e 100644--- a/crates/ui-egui/src/i18n/es.tsv+++ b/crates/ui-egui/src/i18n/es.tsv@@ -686,6 +686,7 @@Divide Vertically Into Dividir verticalmente enDo Not Move No moverDo you want to save the changes you made to “{name}”? ¿Quieres guardar los cambios hechos en “{name}”?+ Can't open “{name}”. No se puede abrir «{name}».Document DocumentoDocument Color Mode Modo de color del documentoDocument Info Información del documentodiff --git a/crates/ui-egui/src/i18n/fr.tsv b/crates/ui-egui/src/i18n/fr.tsvindex 58d52e353..138fc7e9d 100644--- a/crates/ui-egui/src/i18n/fr.tsv+++ b/crates/ui-egui/src/i18n/fr.tsv@@ -688,6 +688,7 @@Divide Vertically Into Diviser verticalement enDo Not Move Ne pas déplacerDo you want to save the changes you made to “{name}”? Voulez-vous enregistrer les modifications apportées à « {name} » ?+ Can't open “{name}”. Impossible d’ouvrir « {name} ».Document DocumentDocument Color Mode Mode colorimétrique du documentDocument Info Informations sur le documentdiff --git a/crates/ui-egui/src/i18n/it.tsv b/crates/ui-egui/src/i18n/it.tsvindex 7ee24bd84..c6f75cd22 100644--- a/crates/ui-egui/src/i18n/it.tsv+++ b/crates/ui-egui/src/i18n/it.tsv@@ -686,6 +686,7 @@Divide Vertically Into Dividi verticalmente inDo Not Move Non spostareDo you want to save the changes you made to “{name}”? Vuoi salvare le modifiche apportate a “{name}”?+ Can't open “{name}”. Impossibile aprire «{name}».Document DocumentoDocument Color Mode Metodo colore documentoDocument Info Informazioni documentodiff --git a/crates/ui-egui/src/i18n/ja.tsv b/crates/ui-egui/src/i18n/ja.tsvindex 1dc6a3406..17954c92e 100644--- a/crates/ui-egui/src/i18n/ja.tsv+++ b/crates/ui-egui/src/i18n/ja.tsv@@ -1122,6 +1122,7 @@Divide Slices スライスを分割Divide Vertically Into 垂直方向に分割Do you want to save the changes you made to “{name}”? 「{name}」への変更を保存しますか?+ Can't open “{name}”. 「{name}」を開けません。Document ドキュメントDocument Ink Options ドキュメントのインキオプションDocument JSON ドキュメント JSONdiff --git a/crates/ui-egui/src/i18n/pt-br.tsv b/crates/ui-egui/src/i18n/pt-br.tsvindex 64c7b2af4..5563671b5 100644--- a/crates/ui-egui/src/i18n/pt-br.tsv+++ b/crates/ui-egui/src/i18n/pt-br.tsv@@ -636,6 +636,7 @@Divide Slices… Dividir Fatias…Divide Vertically Into Dividir Verticalmente EmDo you want to save the changes you made to “{name}”? Deseja salvar as alterações feitas em “{name}”?+ Can't open “{name}”. Não é possível abrir “{name}”.Document DocumentoDocument Color Mode Modo de Cor do DocumentoDocument Info Informações do Documentodiff --git a/crates/ui-egui/src/i18n/ru.tsv b/crates/ui-egui/src/i18n/ru.tsvindex 68dd631b3..57c7a2f7f 100644--- a/crates/ui-egui/src/i18n/ru.tsv+++ b/crates/ui-egui/src/i18n/ru.tsv@@ -684,6 +684,7 @@Divide Vertically Into Разделить по вертикали наDo Not Move Не перемещатьDo you want to save the changes you made to “{name}”? Сохранить изменения, внесённые в «{name}»?+ Can't open “{name}”. Не удаётся открыть «{name}».Document ДокументDocument Color Mode Цветовой режим документаDocument Info Сведения о документеdiff --git a/crates/ui-egui/src/i18n/uk.tsv b/crates/ui-egui/src/i18n/uk.tsvindex de22dc225..bd73f4e22 100644--- a/crates/ui-egui/src/i18n/uk.tsv+++ b/crates/ui-egui/src/i18n/uk.tsv@@ -680,6 +680,7 @@Divide Vertically Into Розділити по вертикалі наDo Not Move Не переміщатиDo you want to save the changes you made to “{name}”? Зберегти зміни, внесені до «{name}»?+ Can't open “{name}”. Не вдається відкрити «{name}».Document ДокументDocument Color Mode Колірний режим документаDocument Info Відомості про документdiff --git a/crates/ui-egui/src/i18n/zh-hans.tsv b/crates/ui-egui/src/i18n/zh-hans.tsvindex b1021820f..69cf99610 100644--- a/crates/ui-egui/src/i18n/zh-hans.tsv+++ b/crates/ui-egui/src/i18n/zh-hans.tsv@@ -666,6 +666,7 @@Divide Vertically Into 垂直分割为Do Not Move 不移动Do you want to save the changes you made to “{name}”? 要存储对“{name}”所做的更改吗?+ Can't open “{name}”. 无法打开“{name}”。Document 文档Document Color Mode 文档颜色模式Document Info 文档信息diff --git a/crates/ui-egui/src/i18n/zh-hant.tsv b/crates/ui-egui/src/i18n/zh-hant.tsvindex 177b1869d..df3715acc 100644--- a/crates/ui-egui/src/i18n/zh-hant.tsv+++ b/crates/ui-egui/src/i18n/zh-hant.tsv@@ -684,6 +684,7 @@Divide Vertically Into 垂直分割為Do Not Move 不移動Do you want to save the changes you made to “{name}”? 要儲存對「{name}」所做的變更嗎?+ Can't open “{name}”. 無法開啟「{name}」。Document 文件Document Color Mode 文件色彩模式Document Info 文件資訊diff --git a/crates/ui-egui/src/io.rs b/crates/ui-egui/src/io.rsindex 7716e52f4..5e94601fa 100644--- a/crates/ui-egui/src/io.rs+++ b/crates/ui-egui/src/io.rs@@ -97,7 +97,24 @@ pub fn open_dialog(app: &mut VectorcraftApp) -> Result<(), String> {return Ok(());}let path = pick_open(app, &FilePick { filters: fileio::open_filters().collect(), ..Default::default() })?;- open_path(app, &path).map(|_| ())+ open_reporting(app, &path).map(|_| ())+}++/// A file the user asked to open (File › Open, Open Recent, the Home screen, a drop, the Finder)+/// couldn't be: say so in a dialog as well as the status bar, which is easily missed (#861).+pub fn report_open_error(app: &mut VectorcraftApp, name: &str, e: &str) {+ app.status(format!("Couldn't open {name}: {e}"));+ let message = crate::i18n::fmt(tl!("Can't open “{name}”."), &[("name", name)]);+ dialogs::confirm::tell(app, &message, &crate::i18n::message(crate::i18n::current(), e));+}++/// [`open_path`] for a file the user asked for, a failure reported ([`report_open_error`]).+pub fn open_reporting(app: &mut VectorcraftApp, path: &str) -> Result<Value, String> {+ let r = open_path(app, path);+ if let Err(e) = &r {+ report_open_error(app, &fileio::file_name(path), e);+ }+ r}fn read(app: &VectorcraftApp, path: &str) -> Result<Vec<u8>, String> {@@ -699,6 +716,24 @@ mod tests {(VectorcraftApp::new(Session::new(), services), written)}+ /// #861: a file the user opens that can't be opened is reported in a dialog they close, not+ /// only in the status bar; a file an agent opens by path only answers with the error.+ #[test]+ fn a_file_the_user_cant_open_is_reported_in_a_dialog() {+ let (mut app, _) = app();+ app.services.read = Some(Box::new(|_: &str| Err("no such file".into())));+ assert!(open_reporting(&mut app, "/docs/gone.ai").is_err());+ let d = app.ui.dialog.take().expect("a dialog says so");+ assert_eq!(+ (d.kind.as_str(), d.str("message"), d.str("detail")),+ (dialogs::confirm::MESSAGE, "Can't open “gone.ai”.".to_string(), "no such file".to_string())+ );+ assert!(app.ui.status.contains("gone.ai"));+ // An agent's `file.open {path}` gets the error back and no dialog.+ assert!(app.run("file.open", json!({"path": "/docs/gone.ai"})).is_err());+ assert!(app.ui.dialog.is_none());+ }+fn bytes_of(app: &mut VectorcraftApp, cmd: &str, p: Value) -> Vec<u8> {let v = app.session.execute(cmd, &p).unwrap();vectorcraft_format::base64_decode(v["dataBase64"].as_str().unwrap()).unwrap()diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex 6c3ccdff2..b5a71ba75 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -706,7 +706,7 @@ impl VectorcraftApp {self.services.inbox.as_ref().map(|q| std::mem::take(&mut *q.lock().unwrap_or_else(|e| e.into_inner()))).unwrap_or_default();for (name, bytes) in arrived {if let Err(e) = io::open_bytes(self, &name, &bytes, None) {- self.status(format!("Couldn't open {name}: {e}"));+ io::report_open_error(self, &name, &e);}}place::drain(self);diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rsindex e53eedf6b..a725fa21e 100644--- a/crates/ui-egui/src/menus.rs+++ b/crates/ui-egui/src/menus.rs@@ -884,7 +884,7 @@ pub fn run_ui_command(app: &mut VectorcraftApp, id: &str, p: &Value) -> Option<R"file.revert" if p.get("confirmed").and_then(Value::as_bool) != Some(true) => io::ask_revert(app),"file.reveal" => io::reveal(app),id if id.starts_with("file.openRecent") => match recent_slot(app, id).cloned() {- Some(path) => io::open_path(app, &path),+ Some(path) => io::open_reporting(app, &path),None => Err("no such recent file".into()),},"type.findFont" => {diff --git a/crates/ui-egui/src/place.rs b/crates/ui-egui/src/place.rsindex 13e7d9f4e..468c2dce9 100644--- a/crates/ui-egui/src/place.rs+++ b/crates/ui-egui/src/place.rs@@ -357,8 +357,10 @@ pub fn drop_files(app: &mut VectorcraftApp, mut files: Vec<(DropTarget, DropFile}),},};- if let Err(e) = r {- app.status(if target == DropTarget::Open { format!("Couldn't open {name}: {e}") } else { format!("Couldn't place {name}: {e}") });+ match r {+ Err(e) if target == DropTarget::Open => io::report_open_error(app, &name, &e),+ Err(e) => app.status(format!("Couldn't place {name}: {e}")),+ Ok(()) => {}}}}