From 9b0fc2e249c7a56be0acfbf454699a92d26e68c5 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 18 Nov 2026 18:24:51 -0800 Subject: [PATCH] HP-GL Options: check and list pens from what the export writes - Export Selection to HP-GL checks the selection and starts Pen Table from its pens, as the export writes it, and `exportSelection` is no longer remembered for the next whole-document export. - Pen Table with options or an artboard choice the export refuses keeps One Pen per Color and shows the error, instead of starting from one black pen that corrected options would keep. `automatic_pens` returns that error. - `automatic_pens` refuses `rect`, as `check` and the export do. - A save panel shown in line that is canceled keeps the dialog open, and a canceled save panel keeps the options remembered before. --- crates/engine/src/cmd/fileio/hpgl.rs | 18 +-- crates/engine/src/cmd/fileio/tests_hpgl.rs | 43 +++++++- crates/ui-egui/src/dialogs/export_as.rs | 2 +- crates/ui-egui/src/dialogs/hpgl_options.rs | 99 ++++++++++++----- crates/ui-egui/src/dialogs/tests_hpgl.rs | 121 +++++++++++++++++++-- docs/control-protocol.md | 12 +- docs/mcp.md | 6 +- 7 files changed, 245 insertions(+), 56 deletions(-) diff --git a/crates/engine/src/cmd/fileio/hpgl.rs b/crates/engine/src/cmd/fileio/hpgl.rs index 137915f8ec..bf98457771 100644 --- a/crates/engine/src/cmd/fileio/hpgl.rs +++ b/crates/engine/src/cmd/fileio/hpgl.rs @@ -98,18 +98,15 @@ fn hex(v: &Value) -> Option<[u8; 3]> { pub fn check(doc: &Document, p: &Value) -> std::result::Result<(), String> { let run = || -> Result<()> { parse(p)?; - let f = super::format("hpgl").ok_or_else(|| bad(C, "no HP-GL format"))?; - // The export refuses `rect` for HP-GL. - super::encode::page_rect(f, p)?; with_plotted(doc, p, |_, _| Ok(())) }; run().map_err(|e| e.to_string()) } /// The pen table an export of `doc` with the options and artboard choice in `p`, and no `pens`, -/// uses: pen 1's color first, as `#rrggbb` colors. Empty when the options or the artboard choice -/// are refused, or nothing is plotted. -pub fn automatic_pens(doc: &Document, p: &Value) -> Vec { +/// uses: pen 1's color first, as `#rrggbb` colors. Empty when nothing is plotted; an error when +/// the options or the artboard choice are refused. +pub fn automatic_pens(doc: &Document, p: &Value) -> std::result::Result, String> { let run = || -> Result> { let mut q = p.clone(); if let Some(o) = q.as_object_mut() { @@ -122,14 +119,17 @@ pub fn automatic_pens(doc: &Document, p: &Value) -> Vec { Ok(plots.first().map(|o| o.pens.clone()).unwrap_or_default()) }) }; - run().unwrap_or_default().iter().map(|[r, g, b]| format!("#{r:02x}{g:02x}{b:02x}")).collect() + let pens = run().map_err(|e| e.to_string())?; + Ok(pens.iter().map(|[r, g, b]| format!("#{r:02x}{g:02x}{b:02x}")).collect()) } /// Run `f` on the document an export of `doc` with `p` plots and the regions of its plots: with /// `useArtboards: false`, a copy of `doc` whose one artboard is the bounds of the visible art. -/// Like the export, it leaves out an opacity-mask or symbol editing layer, and measures the bounds -/// without the art a plot draws nothing for. +/// Like the export, it refuses `rect`, leaves out an opacity-mask or symbol editing layer, and +/// measures the bounds without the art a plot draws nothing for. fn with_plotted(doc: &Document, p: &Value, f: impl FnOnce(&Document, Vec) -> Result) -> Result { + let format = super::format("hpgl").ok_or_else(|| bad(C, "no HP-GL format"))?; + super::encode::page_rect(format, p)?; let doc = &*doc.without_edit_modes(); let use_artboards = super::encode::use_artboards(C, p)?; if use_artboards == Some(false) diff --git a/crates/engine/src/cmd/fileio/tests_hpgl.rs b/crates/engine/src/cmd/fileio/tests_hpgl.rs index db1bf8f538..9a74b8513b 100644 --- a/crates/engine/src/cmd/fileio/tests_hpgl.rs +++ b/crates/engine/src/cmd/fileio/tests_hpgl.rs @@ -580,15 +580,18 @@ fn export_options_are_checked() { fn automatic_pens_list_the_plotted_art_s_colors_in_pen_order() { let s = two_boards(); let doc = &s.doc().unwrap().doc; - let pens = |p: Value| hpgl::automatic_pens(doc, &p); + let pens = |p: Value| hpgl::automatic_pens(doc, &p).unwrap(); assert_eq!(pens(json!({})), ["#000000"], "the first artboard"); assert_eq!(pens(json!({"artboard": 1})), ["#ff0000"]); assert_eq!(pens(json!({"useArtboards": true})), ["#000000", "#ff0000"], "one numbering for every file"); assert_eq!(pens(json!({"useArtboards": false})), ["#000000", "#ff0000"], "the art's bounds"); // The pens given and the path order are not the export's own pens. assert_eq!(pens(json!({"useArtboards": true, "pens": ["#00ff00"], "sortPaths": true})), ["#000000", "#ff0000"]); - // A refused artboard choice: no pens. - assert!(pens(json!({"artboard": 5})).is_empty()); + // A refused artboard choice or option is an error, not an empty table. + assert!(hpgl::automatic_pens(doc, &json!({"artboard": 5})).is_err()); + assert!(hpgl::automatic_pens(doc, &json!({"curveTolerance": 20})).unwrap_err().contains("`curveTolerance`")); + // So is an area of the page, which the export and the check refuse for HP-GL. + assert!(hpgl::automatic_pens(doc, &json!({"rect": [0, 0, 10, 10]})).unwrap_err().contains("rect applies to raster formats")); // Art outside every artboard is plotted only with the art's bounds. let mut off = Session::new(); @@ -597,8 +600,38 @@ fn automatic_pens_list_the_plotted_art_s_colors_in_pen_order() { off.execute("paint.setStroke", &json!({"color": "#0000ff"})).unwrap(); off.execute("shape.rectangle", &json!({"x": 500, "y": 500, "width": 30, "height": 30})).unwrap(); let doc = &off.doc().unwrap().doc; - assert!(hpgl::automatic_pens(doc, &json!({})).is_empty()); - assert_eq!(hpgl::automatic_pens(doc, &json!({"useArtboards": false})), ["#0000ff"]); + assert_eq!(hpgl::automatic_pens(doc, &json!({})), Ok(vec![])); + assert_eq!(hpgl::automatic_pens(doc, &json!({"useArtboards": false})), Ok(vec!["#0000ff".to_string()])); +} + +/// A placed document opened with only its preview is plotted with its file's art (the plot reads +/// the file), so the check and the pens of a dialog measure that art as the export does. +#[test] +fn a_placed_document_s_pens_are_those_of_its_file_s_art() { + use crate::tests_links::{Folder, open, save, session, write}; + let dir = Folder::new("hpgl-placed"); + let src = dir.file("logo.vectorcraft"); + let mut d = vectorcraft_doc::Document::new(100.0, 50.0); + let layer = d.layers[0].id; + let id = d.alloc_id(); + let red = vectorcraft_color::Paint::solid(vectorcraft_color::Color::rgb8(255, 0, 0)); + let look = vectorcraft_doc::Appearance::basic(red, vectorcraft_color::Paint::None, 0.0); + let rect = vectorcraft_geom::shapes::rectangle(vectorcraft_geom::Rect::new(0.0, 0.0, 100.0, 50.0)); + d.insert(Some(layer), 0, vectorcraft_doc::Node::path(id, rect, look)).unwrap(); + write(&src, &vectorcraft_format::save_file(&d)); + let mut s = session(); + s.execute("file.place", &json!({"path": src, "at": [200, 150]})).unwrap(); + let poster = dir.file("poster.vectorcraft"); + save(&mut s, &poster); + open(&mut s, &poster); + let doc = &s.doc().unwrap().doc; + assert!(doc.images.values().any(vectorcraft_doc::ImageBlob::is_proxy), "opened with the preview only"); + for p in [json!({}), json!({"useArtboards": false})] { + assert_eq!(hpgl::automatic_pens(doc, &p), Ok(vec!["#ff0000".to_string()]), "{p}"); + assert_eq!(hpgl::check(doc, &p), Ok(()), "{p}"); + } + let plot = String::from_utf8(bytes(&s.execute("document.export", &json!({"format": "hpgl"})).unwrap())).unwrap(); + assert!(plot.contains("SP1;") && plot.contains("PD"), "{plot}"); } /// `insideFirst` draws the square inside the other one first, though it was made last. diff --git a/crates/ui-egui/src/dialogs/export_as.rs b/crates/ui-egui/src/dialogs/export_as.rs index 94aab72ec1..60701aae13 100644 --- a/crates/ui-egui/src/dialogs/export_as.rs +++ b/crates/ui-egui/src/dialogs/export_as.rs @@ -234,7 +234,7 @@ fn options_or_write( } } let plugin = plugin.filter(|p| !p.manifest().params.is_empty()); - let dialog = plugin.is_none() && (f.raster || matches!(f.id, "dxf" | "eps")); + let dialog = plugin.is_none() && (f.raster || matches!(f.id, "dxf" | "eps" | "hpgl")); if selection { // An options dialog keeps the mark as a field of its own, which OK hands on. params[if dialog { SELECTION_FIELD } else { io::EXPORT_SELECTION }] = json!(true); diff --git a/crates/ui-egui/src/dialogs/hpgl_options.rs b/crates/ui-egui/src/dialogs/hpgl_options.rs index 1544dc2556..5bbc6b3e6b 100644 --- a/crates/ui-egui/src/dialogs/hpgl_options.rs +++ b/crates/ui-egui/src/dialogs/hpgl_options.rs @@ -5,8 +5,10 @@ //! Fields: `document.export` params (`format: hpgl`, `path?`, the artboard choice) and the HP-GL //! options (`hpglVersion`, `curveTolerance`, `origin`, `pens`, `sortPaths`, `insideFirst`). OK //! remembers the options for next time and writes the file (asking where when there is no path); -//! an option or artboard choice the export refuses keeps the dialog open. A typed Curve Tolerance -//! is kept within 0.01 to 10 points. +//! an option or artboard choice the export refuses, or a save panel shown in line that is canceled, +//! keeps the dialog open. A canceled save panel keeps the options remembered before. Opened by Export +//! Selection, it checks and writes the selection alone. A typed Curve Tolerance is kept within 0.01 +//! to 10 points. use serde_json::{Map, Value, json}; use vectorcraft_cad::hpgl::write::{DEFAULT_TOLERANCE, MAX_PENS, TOLERANCE_RANGE}; @@ -32,11 +34,14 @@ pub(super) const SPEC: DialogSpec = DialogSpec { }; /// Fields that are not remembered between exports (they belong to one export). -const PER_EXPORT: [&str; 7] = ["format", "path", "useArtboards", "range", "artboard", "artboards", "selectedOnly"]; +const PER_EXPORT: [&str; 8] = ["format", "path", "useArtboards", "range", "artboard", "artboards", "selectedOnly", io::EXPORT_SELECTION]; /// The pen table set aside while One Pen per Color is chosen, so Pen Table shows it again. const SET_ASIDE: &str = "__pens"; +/// Why Pen Table could not start from the art's pens, shown under the Pens row. +const PEN_ERROR: &str = "__penError"; + /// The Origin menu's names, in [`Origin::ALL`] order. pub(crate) const ORIGIN_NAMES: [&str; 2] = ["Bottom Left", "Center"]; @@ -97,14 +102,22 @@ fn pens(app: &VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) { let table = d.fields.get("pens").is_some_and(Value::is_array); widgets::label_row(ui, tl!("Pens:"), LABEL, |ui| { if widgets::radio(ui, tl!("One Pen per Color"), !table, true) { - set_pen_table(d, false, Vec::new); + let _ = set_pen_table(d, false, || Ok(Vec::new())); + d.fields.remove(PEN_ERROR); } ui.add_space(12.0); if widgets::radio(ui, tl!("Pen Table"), table, true) { - let params = super::form::params(d); - set_pen_table(d, true, || automatic_pens(app, ¶ms)); + let shown = d.clone(); + match set_pen_table(d, true, || automatic_pens(app, &shown)) { + Ok(()) => d.fields.remove(PEN_ERROR), + Err(e) => d.fields.insert(PEN_ERROR.into(), json!(e)), + }; } }); + if let Some(e) = d.fields.get(PEN_ERROR).and_then(Value::as_str) { + let t = crate::theme::Tokens::get(ui.ctx()); + ui.label(egui::RichText::new(crate::i18n::message(crate::i18n::current(), e)).color(t.error)); + } // The table as the radio buttons left it, so a row edit committed this frame can't undo a switch. // A table longer than a plot takes (set through `ui.dialog.set`) draws no rows: OK refuses it. let Some(mut list) = d.fields.get("pens").and_then(Value::as_array).filter(|a| a.len() <= MAX_PENS).cloned() else { return }; @@ -151,29 +164,50 @@ fn pens(app: &VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) { } } -/// The pens an export with the dialog's `params` and no pen table would get: its options, artboard -/// choice and source art (the selection with Export Selected Art Only). -pub(super) fn automatic_pens(app: &VectorcraftApp, params: &Value) -> Vec { - let Some(st) = app.session.active() else { return Vec::new() }; - fileio::export_source(st, params).map(|doc| fileio::hpgl::automatic_pens(&doc, params)).unwrap_or_default() +/// The pens an export from dialog `d` with no pen table would get: its options, artboard choice and +/// source art (the selection with Export Selected Art Only, or when Export Selection opened `d`); +/// an error when there is no document or the export refuses those. +pub(super) fn automatic_pens(app: &VectorcraftApp, d: &Dialog) -> Result, String> { + let st = app.session.active().ok_or("no document")?; + let (doc, params) = source(st, d)?; + fileio::hpgl::automatic_pens(&doc, ¶ms) +} + +/// The document an export from dialog `d` writes and the export params it reads, as +/// [`io::export`] takes them ([`io::take_selection`]): with Export Selection the selection alone, +/// without the artboard choice, else the whole document or, with Export Selected Art Only, its +/// selected objects. +fn source<'a>(st: &'a vectorcraft_engine::DocState, d: &Dialog) -> Result<(std::borrow::Cow<'a, vectorcraft_doc::Document>, Value), String> { + let (selection, params) = io::take_selection(&super::export_as::exported(d, super::form::params(d)))?; + if !selection { + let doc = fileio::export_source(st, ¶ms).map_err(|e| e.to_string())?; + return Ok((doc, params)); + } + let (doc, _) = fileio::selection_doc(st, "document.exportSelection", false).map_err(|e| e.to_string())?; + Ok((std::borrow::Cow::Owned(doc), params)) } /// Choose Pen Table (`on`) or One Pen per Color. Pen Table shows the table set aside when One Pen /// per Color was chosen, else the pens the export would give the art (`automatic`), else one black -/// pen; One Pen per Color sets the table aside. -pub(super) fn set_pen_table(d: &mut Dialog, on: bool, automatic: impl FnOnce() -> Vec) { +/// pen when that art draws nothing; when `automatic` fails the dialog is left as it was and the +/// error returned. One Pen per Color sets the table aside. +pub(super) fn set_pen_table(d: &mut Dialog, on: bool, automatic: impl FnOnce() -> Result, String>) -> Result<(), String> { let table = d.fields.get("pens").filter(|v| v.is_array()).cloned(); match (on, table) { (true, None) => { - let kept = d.fields.remove(SET_ASIDE).filter(|v| v.as_array().is_some_and(|a| !a.is_empty())); - let list = kept.unwrap_or_else(|| { - let mut pens = automatic(); - pens.truncate(MAX_PENS); - if pens.is_empty() { - pens.push("#000000".into()); + let kept = d.fields.get(SET_ASIDE).filter(|v| v.as_array().is_some_and(|a| !a.is_empty())).cloned(); + let list = match kept { + Some(list) => list, + None => { + let mut pens = automatic()?; + pens.truncate(MAX_PENS); + if pens.is_empty() { + pens.push("#000000".into()); + } + json!(pens) } - json!(pens) - }); + }; + d.fields.remove(SET_ASIDE); d.fields.insert("pens".into(), list); } (false, Some(list)) => { @@ -182,6 +216,7 @@ pub(super) fn set_pen_table(d: &mut Dialog, on: bool, automatic: impl FnOnce() - } _ => {} } + Ok(()) } /// A typed pen color (`ff0000` or `#FF0000`) as the table holds it, `#ff0000`; `None` unless it is @@ -206,11 +241,25 @@ fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result { let params = super::form::params(d); let st = app.session.active().ok_or("no document")?; // A bad option or artboard choice, nothing selected, or no art to export keeps the dialog open. - let source = fileio::export_source(st, ¶ms).map_err(|e| e.to_string())?; - fileio::hpgl::check(&source, ¶ms)?; + let (source, checked) = source(st, d)?; + fileio::hpgl::check(&source, &checked)?; let remembered = params.as_object().map(|o| o.iter().filter(|(k, _)| !PER_EXPORT.contains(&k.as_str())).map(|(k, v)| (k.clone(), v.clone()))); - app.ui.hpgl_options = Value::Object(remembered.into_iter().flatten().collect()); + let before = std::mem::replace(&mut app.ui.hpgl_options, Value::Object(remembered.into_iter().flatten().collect())); app.ui.dialog = None; let path = d.fields.get("path").and_then(Value::as_str).map(str::to_string); - io::export(app, Some("hpgl"), path, ¶ms) + let waiting = app.picks.is_waiting(); + let r = io::export(app, Some("hpgl"), path, &super::export_as::exported(d, params)); + if !waiting && app.picks.is_waiting() { + // A save panel shown off the UI thread confirms the dialog again when it answers, which + // remembers the options then; until it does, and when it is canceled, the options + // remembered before stay. + app.ui.hpgl_options = before; + } else if r.is_err() && app.ui.dialog.is_none() { + // An export that stops before writing (a save panel that was canceled or could not be + // shown) leaves the dialog open over the options remembered before. A question that a + // save panel asked stays open. + app.ui.hpgl_options = before; + app.ui.dialog = Some(d.clone()); + } + r } diff --git a/crates/ui-egui/src/dialogs/tests_hpgl.rs b/crates/ui-egui/src/dialogs/tests_hpgl.rs index dba59afea2..77e4071f74 100644 --- a/crates/ui-egui/src/dialogs/tests_hpgl.rs +++ b/crates/ui-egui/src/dialogs/tests_hpgl.rs @@ -107,36 +107,137 @@ fn bad_hpgl_options_keep_the_dialog_open() { #[test] fn pen_table_starts_from_the_art_s_pens_and_comes_back() { let (mut app, _) = app(1); - let automatic = fileio::hpgl::automatic_pens(&app.session.active().unwrap().doc, &json!({})); + let automatic = fileio::hpgl::automatic_pens(&app.session.active().unwrap().doc, &json!({})).unwrap(); assert_eq!(automatic.len(), 1, "the ellipse's fill is one pen"); app.run("ui.hpglOptionsDialog", json!({})).unwrap(); + let none = || -> Result, String> { Ok(Vec::new()) }; let d = app.ui.dialog.as_mut().unwrap(); - hpgl_options::set_pen_table(d, true, || automatic.clone()); + hpgl_options::set_pen_table(d, true, || Ok(automatic.clone())).unwrap(); assert_eq!(d.fields["pens"], json!(automatic)); d.fields.insert("pens".into(), json!(["#00ff00", "#0000ff"])); - hpgl_options::set_pen_table(d, false, Vec::new); + hpgl_options::set_pen_table(d, false, none).unwrap(); assert!(d.fields["pens"].is_null()); + // The table set aside is not an export option. + assert_eq!(d.fields["__pens"], json!(["#00ff00", "#0000ff"])); + assert!(form::params(d).get("__pens").is_none()); // Choosing One Pen per Color again keeps the table set aside. - hpgl_options::set_pen_table(d, false, Vec::new); - hpgl_options::set_pen_table(d, true, || panic!("the table set aside comes back")); + hpgl_options::set_pen_table(d, false, none).unwrap(); + hpgl_options::set_pen_table(d, true, || panic!("the table set aside comes back")).unwrap(); assert_eq!(d.fields["pens"], json!(["#00ff00", "#0000ff"])); // Nothing set aside and nothing drawn: one black pen. d.fields.insert("pens".into(), serde_json::Value::Null); d.fields.remove("__pens"); - hpgl_options::set_pen_table(d, true, Vec::new); + hpgl_options::set_pen_table(d, true, none).unwrap(); assert_eq!(d.fields["pens"], json!(["#000000"])); - // The set-aside table is not an export option. - assert!(form::params(d).get("__pens").is_none()); frame(&mut app); } +/// Pen Table with options the export refuses keeps One Pen per Color and says why, rather than +/// starting from one black pen that the corrected options would keep. +#[test] +fn pen_table_with_refused_options_keeps_one_pen_per_color() { + let (mut app, _) = app(1); + app.run("ui.hpglOptionsDialog", json!({"curveTolerance": 20})).unwrap(); + let d = app.ui.dialog.clone().unwrap(); + let e = hpgl_options::automatic_pens(&app, &d).unwrap_err(); + assert!(e.contains("`curveTolerance`"), "{e}"); + let d = app.ui.dialog.as_mut().unwrap(); + assert_eq!(hpgl_options::set_pen_table(d, true, || Err(e.clone())), Err(e)); + assert!(d.fields["pens"].is_null(), "One Pen per Color stays chosen"); + frame(&mut app); + // With the tolerance corrected, Pen Table starts from the art's pen. + set(&mut app, "curveTolerance", json!(0.1)); + let d = app.ui.dialog.clone().unwrap(); + let pens = hpgl_options::automatic_pens(&app, &d).unwrap(); + let d = app.ui.dialog.as_mut().unwrap(); + hpgl_options::set_pen_table(d, true, || Ok(pens.clone())).unwrap(); + assert_eq!(d.fields["pens"], json!(pens)); + assert_eq!(pens.len(), 1, "the ellipse's fill is one pen"); +} + +/// Export Selection to HP-GL checks, lists the pens of and writes the selection alone, and the next +/// whole-document export is not a selection export. +#[test] +fn export_selection_to_hpgl_uses_the_selection_and_is_not_remembered() { + let (mut app, written) = app(1); + // A blue outline that is the selection; the ellipse stays out of the file. + app.run("shape.rectangle", json!({"x": 40, "y": 30, "width": 10, "height": 5})).unwrap(); + app.run("paint.setFill", json!({"none": true})).unwrap(); + app.run("paint.setStroke", json!({"color": "#0000ff"})).unwrap(); + app.services.pick_save = Some(Box::new(|_: &crate::FilePick| Some("/out/sel.plt".to_string()))); + app.run("document.exportSelection", json!({})).unwrap(); + assert_eq!(kind(&app), Some("hpglOptions")); + let d = app.ui.dialog.clone().unwrap(); + assert!(d.bool(export_as::SELECTION_FIELD) && !d.fields.contains_key(crate::io::EXPORT_SELECTION)); + assert_eq!(hpgl_options::automatic_pens(&app, &d).unwrap(), ["#0000ff"], "the selection's pen only"); + // An artboard choice the whole document would refuse is not this export's. + set(&mut app, "artboard", json!(5)); + frame(&mut app); + confirm(&mut app).unwrap(); + assert!(app.ui.dialog.is_none()); + assert_eq!(names(&written), ["/out/sel.plt"]); + let plot = String::from_utf8_lossy(&written.borrow()[0].1).into_owned(); + assert_eq!(plot.matches("PD").count(), 1, "the rectangle alone: {plot}"); + assert!(app.ui.hpgl_options.get(export_as::SELECTION_FIELD).is_none()); + // A selection export asked for by its param is not remembered either. + app.run("ui.hpglOptionsDialog", json!({"path": "/out/b.plt", "exportSelection": true})).unwrap(); + confirm(&mut app).unwrap(); + assert!(app.ui.hpgl_options.get(crate::io::EXPORT_SELECTION).is_none()); + app.run("select.none", json!({})).unwrap(); + app.run("ui.hpglOptionsDialog", json!({"path": "/out/c.plt"})).unwrap(); + confirm(&mut app).unwrap(); + assert_eq!(names(&written).last().map(String::as_str), Some("/out/c.plt"), "the whole document, with nothing selected"); +} + +/// A save panel that is canceled keeps HP-GL Options open, with the options remembered before. +#[test] +fn a_canceled_save_panel_keeps_hpgl_options_open() { + let (mut app, written) = app(1); + app.ui.hpgl_options = json!({"origin": "center"}); + app.services.pick_save = Some(Box::new(|_: &crate::FilePick| None)); + app.run("ui.hpglOptionsDialog", json!({})).unwrap(); + set(&mut app, "origin", json!("bottomLeft")); + frame(&mut app); + assert!(confirm(&mut app).is_err()); + assert_eq!(kind(&app), Some("hpglOptions")); + assert_eq!(app.ui.dialog.as_ref().unwrap().str("origin"), "bottomLeft"); + assert_eq!(app.ui.hpgl_options, json!({"origin": "center"})); + assert!(written.borrow().is_empty()); + app.services.pick_save = Some(Box::new(|_: &crate::FilePick| Some("/out/d.plt".to_string()))); + confirm(&mut app).unwrap(); + assert_eq!(names(&written), ["/out/d.plt"]); + assert_eq!(app.ui.hpgl_options["origin"], "bottomLeft"); +} + +/// A save panel shown off the UI thread leaves the options remembered before until it answers: +/// canceled, they stay; answered, the export remembers the dialog's. +#[test] +fn an_off_thread_save_panel_remembers_hpgl_options_when_it_answers() { + let (mut app, _) = app(1); + let (shown, written) = crate::tests_picks::off_the_ui_thread(&mut app.services); + let poll = |app: &mut VectorcraftApp| crate::picks::poll(app, &egui::Context::default()); + app.ui.hpgl_options = json!({"origin": "center"}); + for answer in [&[][..], &["/out/e.plt"][..]] { + app.run("ui.hpglOptionsDialog", json!({})).unwrap(); + set(&mut app, "origin", json!("bottomLeft")); + frame(&mut app); + let _ = confirm(&mut app); + assert_eq!(shown.requests().len(), 1, "the save panel is showing"); + assert_eq!(app.ui.hpgl_options, json!({"origin": "center"}), "not remembered while the panel is open"); + shown.answer(answer); + poll(&mut app); + } + assert_eq!(*written.borrow(), ["/out/e.plt"], "the canceled panel wrote nothing"); + assert_eq!(app.ui.hpgl_options["origin"], "bottomLeft"); +} + /// The pens Pen Table starts from are those of what this export plots: the art's bounds without /// Use Artboards (art outside the artboard included), the selection with Export Selected Art Only, /// and only the art on the artboard with Use Artboards. #[test] fn pen_table_starts_from_the_pens_of_what_the_export_plots() { let (mut app, _) = app(1); - let on_board = fileio::hpgl::automatic_pens(&app.session.active().unwrap().doc, &json!({})); + let on_board = fileio::hpgl::automatic_pens(&app.session.active().unwrap().doc, &json!({})).unwrap(); assert_eq!(on_board.len(), 1, "the ellipse's fill is one pen"); // A blue outline outside the artboard (the paint applies to the new rectangle, the selection). app.run("shape.rectangle", json!({"x": 500, "y": 500, "width": 30, "height": 30})).unwrap(); @@ -147,7 +248,7 @@ fn pen_table_starts_from_the_pens_of_what_the_export_plots() { frame(&mut app); confirm(&mut app).unwrap(); assert_eq!(kind(&app), Some("hpglOptions")); - let seed = |app: &VectorcraftApp| hpgl_options::automatic_pens(app, &form::params(app.ui.dialog.as_ref().unwrap())); + let seed = |app: &VectorcraftApp| hpgl_options::automatic_pens(app, app.ui.dialog.as_ref().unwrap()).unwrap(); assert_eq!(seed(&app), [on_board[0].as_str(), "#0000ff"], "the art's bounds"); // The rectangle is the selection. set(&mut app, "selectedOnly", json!(true)); diff --git a/docs/control-protocol.md b/docs/control-protocol.md index fe47dc138e..b3ab28fb7c 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -885,10 +885,14 @@ HP-GL Options: Export As… with the HP-GL format (`file.exportAs {format: "hpgl (`bottomLeft`, `center`), `pens` (null for One Pen per Color, or the pen table's `"#rrggbb"` colors, pen 1's first), `sortPaths` and `insideFirst`, plus `path` and the artboard choice (`useArtboards`, `range`). A typed Curve Tolerance is kept within 0.01 to 10. Choosing Pen Table fills the table with the table set aside when One Pen per -Color was chosen, else with the pens One Pen per Color gives the art this export plots (its artboards or the art's -bounds), or one black pen when that art draws nothing. `ui.dialog.confirm` checks the options and the artboard choice -(one the export refuses keeps the dialog open), remembers the options for the next HP-GL Options and writes the -`.plt` file(s), asking for a path when there is none (one per artboard with Use Artboards). +Color was chosen, else with the pens One Pen per Color gives the art this export plots (its artboards, the art's +bounds, or the selection from Export Selection or with `selectedOnly`), or one black pen when that art draws nothing; +when the export refuses the options or the artboard choice, `pens` stays null and the dialog shows the error. +`ui.dialog.confirm` checks the options and the artboard choice (one the export refuses keeps the dialog open), +remembers the options for the next HP-GL Options (not `exportSelection`) and writes the `.plt` file(s), asking for a +path when there is none (one per artboard with Use Artboards). A canceled save dialog writes nothing and keeps the +options remembered before; a save dialog shown in line (not as a sheet or off the UI thread) also keeps HP-GL +Options open. TIFF, BMP and Targa Options: Export As… with the TIFF, BMP or Targa format (`file.exportAs {format: "tiff"}`, then the save dialog) opens `tiffOptions`, `bmpOptions` or `tgaOptions`: the raster rows of PNG Options (`ppi`, diff --git a/docs/mcp.md b/docs/mcp.md index 2151a49c85..2fa3f1892b 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -9740,8 +9740,10 @@ In the app, Export As… with the HP-GL format opens HP-GL Options (`ui.hpglOpti Curve Tolerance, Origin (Bottom Left or Center), Pens (One Pen per Color, or Pen Table with a row per pen to type its color, remove it or add one), Draw the Nearest Path Next (`sortPaths`) and Draw Inner Paths First (`insideFirst`). Pen Table starts from the pens One Pen per Color gives the art this export plots (the chosen -artboards, or the bounds of the art), or one black pen when that art draws nothing. OK checks the options and the -artboard choice, and remembers the options for the next export. +artboards, the bounds of the art, or the selection with Export Selection or Export Selected Art Only), or one black +pen when that art draws nothing. When the export refuses the options or the artboard choice, One Pen per Color stays +chosen and the dialog shows why. OK checks the options and the artboard choice, and remembers the options for the +next export; a canceled save panel writes nothing and keeps the options remembered before. ## TIFF, BMP and Targa export