diff --git a/crates/ui-egui/src/dialogs/eps_options.rs b/crates/ui-egui/src/dialogs/eps_options.rs index 0d14b3cac4..0eb1853065 100644 --- a/crates/ui-egui/src/dialogs/eps_options.rs +++ b/crates/ui-egui/src/dialogs/eps_options.rs @@ -197,17 +197,12 @@ fn overprints_kept(d: &Dialog) -> bool { /// format (no alpha) and by EPS Options' Overprints. fn custom_body(_app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool { let current = custom_options(d); - // Values refused stay as entered, so that changing another option keeps them. - let mut o = current.clone().unwrap_or_else(|_| super::flatten::entered_options(&d.fields, &FlattenOptions::default())); - let kept = o.preserve_overprints; + // Values refused stay as entered, so that changing another option keeps them. The Preserve + // switches can't be set here, so the fields keep their own values of them. + let (mut o, unread) = super::flatten::shown_options(&d.fields, ¤t); o.preserve_alpha = false; - o.preserve_overprints = kept && overprints_kept(d); - let mut shown = o.clone(); - shown.preserve_overprints = kept; - if super::flatten::options_editor_fixed_preserve(ui, "eps-flattener", &mut o) { - o.preserve_overprints = kept; - super::flatten::put_changed_options(&mut d.fields, &shown, &o); - } + o.preserve_overprints &= overprints_kept(d); + super::flatten::fields_editor(ui, "eps-flattener", &mut d.fields, &mut o, &unread, true); if let Err(e) = ¤t { ui.label(egui::RichText::new(e).color(Tokens::get(ui.ctx()).text_dim).size(11.5)); } diff --git a/crates/ui-egui/src/dialogs/flatten.rs b/crates/ui-egui/src/dialogs/flatten.rs index e9731efe4b..3ae84e0f53 100644 --- a/crates/ui-egui/src/dialogs/flatten.rs +++ b/crates/ui-egui/src/dialogs/flatten.rs @@ -48,30 +48,51 @@ pub(crate) fn put_options(fields: &mut Map, o: &FlattenOptions) { } } -/// Write into the option fields only the options that differ between `shown` and `edited`, so -/// that a value the editor could not show stays in the fields as it was entered. -pub(crate) fn put_changed_options(fields: &mut Map, shown: &FlattenOptions, edited: &FlattenOptions) { - let (Ok(Value::Object(old)), Ok(Value::Object(new))) = (serde_json::to_value(shown), serde_json::to_value(edited)) else { return }; - for (k, v) in new { - if old.get(&k) != Some(&v) { - fields.insert(k, v); - } - } -} - /// The options `fields` hold over `base` as they were entered, values out of range included, for -/// the editor to show while [`FlattenOptions::from_params_with`] refuses them. A value of the wrong -/// type shows as `base`'s. -pub(crate) fn entered_options(fields: &Map, base: &FlattenOptions) -> FlattenOptions { - let Ok(Value::Object(mut v)) = serde_json::to_value(base) else { return base.clone() }; +/// the editor to show while [`FlattenOptions::from_params_with`] refuses them, and the keys of the +/// values of the wrong type, which take `base`'s value here and show blank in the editor. +pub(crate) fn entered_options(fields: &Map, base: &FlattenOptions) -> (FlattenOptions, Vec) { + let Ok(Value::Object(mut v)) = serde_json::to_value(base) else { return (base.clone(), Vec::new()) }; + let mut unread = Vec::new(); for (k, val) in fields { let Some(slot) = v.get_mut(k) else { continue }; let old = std::mem::replace(slot, val.clone()); if serde_json::from_value::(Value::Object(v.clone())).is_err() { v.insert(k.clone(), old); + unread.push(k.clone()); + } + } + (serde_json::from_value(Value::Object(v)).unwrap_or_else(|_| base.clone()), unread) +} + +/// The options a dialog's `fields` hold, for [`fields_editor`]: `current` when they are valid, else +/// the values entered over the default preset's, with the keys of the values of the wrong type. +pub(crate) fn shown_options(fields: &Map, current: &Result) -> (FlattenOptions, Vec) { + match current { + Ok(o) => (o.clone(), Vec::new()), + Err(_) => entered_options(fields, &FlattenOptions::default()), + } +} + +/// [`options_editor`] on the option fields of a dialog: `o` and `unread` from [`shown_options`]. +/// Each option whose control was set is written into `fields`, also when it equals the value shown, +/// so that it replaces a value of the wrong type; the other fields stay as they were entered. With +/// `fixed_preserve`, the two Preserve switches show but can't be changed. +pub(crate) fn fields_editor( + ui: &mut egui::Ui, + id: &str, + fields: &mut Map, + o: &mut FlattenOptions, + unread: &[String], + fixed_preserve: bool, +) { + let set = editor(ui, id, o, true, fixed_preserve, unread); + let Ok(Value::Object(m)) = serde_json::to_value(&*o) else { return }; + for k in set { + if let Some(v) = m.get(k) { + fields.insert(k.into(), v.clone()); } } - serde_json::from_value(Value::Object(v)).unwrap_or_else(|_| base.clone()) } /// The options the fields hold (a `preset` not loaded yet: its own). @@ -128,11 +149,8 @@ fn body(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool { } ui.add_space(10.0); // Values refused stay as entered, so that changing another option keeps them. - let mut o = current.clone().unwrap_or_else(|_| entered_options(&d.fields, &FlattenOptions::default())); - let shown = o.clone(); - if options_editor(ui, "flatten-dialog", &mut o, true) { - put_changed_options(&mut d.fields, &shown, &o); - } + let (mut o, unread) = shown_options(&d.fields, ¤t); + fields_editor(ui, "flatten-dialog", &mut d.fields, &mut o, &unread, false); if let Err(e) = ¤t { ui.label(egui::RichText::new(e).color(Tokens::get(ui.ctx()).text_dim).size(11.5)); } @@ -208,18 +226,19 @@ const FIELD: f32 = 56.0; /// panel edit them: the raster/vector balance, both resolutions and the six switches. Returns true /// when one changed. pub(crate) fn options_editor(ui: &mut egui::Ui, id: &str, o: &mut FlattenOptions, enabled: bool) -> bool { - editor(ui, id, o, enabled, false) -} - -/// [`options_editor`] with Preserve Alpha Transparency and Preserve Overprints and Spot Colors -/// shown but not editable (the file format or another control sets them). -pub(crate) fn options_editor_fixed_preserve(ui: &mut egui::Ui, id: &str, o: &mut FlattenOptions) -> bool { - editor(ui, id, o, true, true) -} - -fn editor(ui: &mut egui::Ui, id: &str, o: &mut FlattenOptions, enabled: bool, fixed_preserve: bool) -> bool { - let t = Tokens::get(ui.ctx()); let before = o.clone(); + editor(ui, id, o, enabled, false, &[]); + *o != before +} + +/// The option controls, with the value fields of the options named in `unread` blank. With +/// `fixed_preserve`, Preserve Alpha Transparency and Preserve Overprints and Spot Colors show but +/// can't be changed (the file format or another control sets them). Returns the keys of the +/// options whose controls were set, also to the value they showed. +fn editor(ui: &mut egui::Ui, id: &str, o: &mut FlattenOptions, enabled: bool, fixed_preserve: bool, unread: &[String]) -> Vec<&'static str> { + let t = Tokens::get(ui.ctx()); + let mut set = Vec::new(); + let field_value = |key: &str, v: f64| (!unread.iter().any(|k| k == key)).then_some(v); let dim = |ui: &mut egui::Ui, s: &str| { ui.label(egui::RichText::new(tl!(s)).color(t.text_dim).size(11.5)); }; @@ -241,22 +260,28 @@ fn editor(ui: &mut egui::Ui, id: &str, o: &mut FlattenOptions, enabled: bool, fi ); if let Some(b) = x.map(|x| (x as f64 * 100.0).round()).or(keyed) { o.balance = b; + set.push("balance"); } dim(ui, tl!("Vectors")); + let value = field_value("balance", o.balance); if let Some(v) = - widgets::range_field_named(ui, (id, "balance"), Some(tl!("Raster/Vector Balance:")), o.balance, 0.0..=100.0, "", 0, FIELD - 12.0) + widgets::range_field_named(ui, (id, "balance"), Some(tl!("Raster/Vector Balance:")), value, 0.0..=100.0, "", 0, FIELD - 12.0) { o.balance = v; + set.push("balance"); } }); ui.add_space(4.0); egui::Grid::new((id, "resolutions")).num_columns(2).spacing([8.0, 4.0]).show(ui, |ui| { - for (label, key) in [(tl!("Line Art and Text Resolution:"), 0), (tl!("Gradient and Mesh Resolution:"), 1)] { + for (label, key, name) in + [(tl!("Line Art and Text Resolution:"), 0, "lineArtPpi"), (tl!("Gradient and Mesh Resolution:"), 1, "gradientPpi")] + { widgets::dim_label(ui, label); ui.horizontal(|ui| { let v = if key == 0 { &mut o.line_art_ppi } else { &mut o.gradient_ppi }; - if let Some(n) = widgets::range_field_named(ui, (id, key), Some(label), *v, 1.0..=2400.0, "", 0, FIELD) { + if let Some(n) = widgets::range_field_named(ui, (id, key), Some(label), field_value(name, *v), 1.0..=2400.0, "", 0, FIELD) { *v = n; + set.push(name); } dim(ui, "ppi"); }); @@ -264,21 +289,22 @@ fn editor(ui: &mut egui::Ui, id: &str, o: &mut FlattenOptions, enabled: bool, fi } }); ui.add_space(4.0); - let switches: [(&str, &mut bool, bool); 6] = [ - (tl!("Convert All Text to Outlines"), &mut o.text_to_outlines, false), - (tl!("Convert All Strokes to Outlines"), &mut o.strokes_to_outlines, false), - (tl!("Clip Complex Regions"), &mut o.clip_complex_regions, false), - (tl!("Anti-alias Rasters"), &mut o.anti_alias, false), - (tl!("Preserve Alpha Transparency"), &mut o.preserve_alpha, fixed_preserve), - (tl!("Preserve Overprints and Spot Colors"), &mut o.preserve_overprints, fixed_preserve), + let switches: [(&str, &mut bool, bool, &'static str); 6] = [ + (tl!("Convert All Text to Outlines"), &mut o.text_to_outlines, false, "textToOutlines"), + (tl!("Convert All Strokes to Outlines"), &mut o.strokes_to_outlines, false, "strokesToOutlines"), + (tl!("Clip Complex Regions"), &mut o.clip_complex_regions, false, "clipComplexRegions"), + (tl!("Anti-alias Rasters"), &mut o.anti_alias, false, "antiAlias"), + (tl!("Preserve Alpha Transparency"), &mut o.preserve_alpha, fixed_preserve, "preserveAlpha"), + (tl!("Preserve Overprints and Spot Colors"), &mut o.preserve_overprints, fixed_preserve, "preserveOverprints"), ]; - for (label, v, fixed) in switches { + for (label, v, fixed, key) in switches { if widgets::check(ui, label, *v, enabled && !fixed) { *v = !*v; + set.push(key); } } }); - *o != before + set } #[cfg(test)] @@ -455,4 +481,19 @@ mod tests { d.fields.insert("lineArtPpi".into(), json!(300)); assert!(super::super::confirm(&mut app).unwrap_err().contains("resolutions")); } + + /// A value of the wrong type shows blank, and typing the default preset's value into its + /// field replaces it, so OK flattens. + #[test] + fn the_default_value_typed_over_a_value_of_the_wrong_type_replaces_it() { + let mut app = scene(); + app.run("ui.flattenTransparencyDialog", json!({})).unwrap(); + app.ui.dialog.as_mut().unwrap().fields.insert("lineArtPpi".into(), json!("many")); + let default = FlattenOptions::default().line_art_ppi; + let mut f = super::super::tests_eps::Frames::new(); + f.type_into(&mut app, "Line Art and Text Resolution:", &default.to_string()); + assert_eq!(app.ui.dialog.as_ref().unwrap().fields["lineArtPpi"], json!(default)); + super::super::confirm(&mut app).unwrap(); + assert!(app.ui.dialog.is_none() && !transparent_left(&app)); + } } diff --git a/crates/ui-egui/src/dialogs/tests_eps.rs b/crates/ui-egui/src/dialogs/tests_eps.rs index eddde431b1..5cd2efe24c 100644 --- a/crates/ui-egui/src/dialogs/tests_eps.rs +++ b/crates/ui-egui/src/dialogs/tests_eps.rs @@ -209,6 +209,17 @@ impl Frames { self.run(app, vec![egui::Event::PointerMoved(at), button]); } } + + /// Click the text field named `label`, type `text` over its text and press Tab. + pub(super) fn type_into(&mut self, app: &mut VectorcraftApp, label: &str, text: &str) { + let (_, at) = self.control(app, label, egui::accesskit::Role::TextInput); + self.click(app, at); + self.run(app, vec![egui::Event::Text(text.into())]); + let tab = |pressed| egui::Event::Key { key: egui::Key::Tab, physical_key: None, pressed, repeat: false, modifiers: Default::default() }; + self.run(app, vec![tab(true)]); + self.run(app, vec![tab(false)]); + self.run(app, vec![]); + } } /// Enter on the focused Custom… button opens the custom options and does not also confirm them. @@ -256,6 +267,24 @@ fn a_switch_changed_beside_a_bad_value_keeps_the_values_entered() { assert!(confirm(&mut app).unwrap_err().contains("resolutions")); } +/// A value of the wrong type shows blank, and typing the default preset's value into its field +/// replaces it, so OK takes the options. +#[test] +fn the_default_value_typed_over_a_value_of_the_wrong_type_replaces_it() { + let (mut app, _) = app(1); + app.run("ui.epsOptionsDialog", json!({"path": "/out/a.eps"})).unwrap(); + frame(&mut app); + open_custom(&mut app); + set(&mut app, "lineArtPpi", json!("many")); + let default = vectorcraft_engine::cmd::FlattenOptions::default().line_art_ppi; + let mut f = Frames::new(); + f.type_into(&mut app, "Line Art and Text Resolution:", &default.to_string()); + assert_eq!(app.ui.dialog.as_ref().unwrap().fields["lineArtPpi"], json!(default)); + confirm(&mut app).unwrap(); + assert_eq!(kind(&app), Some("epsOptions")); + assert_eq!(app.ui.dialog.as_ref().unwrap().fields["flattener"]["lineArtPpi"].as_f64(), Some(default)); +} + #[test] fn the_eps_options_dialog_needs_a_document() { let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default());