EPS Options: Enter on Custom… opens the options without confirming them

- A dialog's Enter no longer confirms a dialog that its body opened in
  the same frame, so Enter on the focused Custom… button opens Custom
  Transparency Flattener Options instead of confirming them at once.
- With an option out of range or of the wrong type, changing another
  option in Custom Transparency Flattener Options or Flatten
  Transparency writes only that option, so the values entered stay and
  OK still refuses them.
This commit is contained in:
dotCooCoo
2026-11-16 08:16:36 -08:00
parent be27667228
commit cfd0a8b1e4
4 changed files with 149 additions and 5 deletions
+4 -1
View File
@@ -828,6 +828,9 @@ pub fn show(app: &mut VectorcraftApp, ctx: &egui::Context) {
if after_panel && !ctx.input(|i| i.key_down(egui::Key::Enter) || i.key_pressed(egui::Key::Enter)) {
d.fields.remove(AFTER_PANEL);
}
// A body can open another dialog in its place (EPS Options' Custom…): this frame's Enter
// doesn't confirm that one.
let opened = d.kind.clone();
let heading = (spec.heading)(&d);
let revolves = revolve_gizmo::active(&d);
if revolves {
@@ -927,7 +930,7 @@ pub fn show(app: &mut VectorcraftApp, ctx: &egui::Context) {
if ok_clicked && !rejected && !refused_during_click {
ok = true;
}
if spec.ok.is_some() && ok_enabled && !after_panel && !rejected && crate::shortcuts::pressed_anew(ctx, egui::Key::Enter) {
if spec.ok.is_some() && ok_enabled && !after_panel && !rejected && d.kind == opened && crate::shortcuts::pressed_anew(ctx, egui::Key::Enter) {
ok = true;
}
if discard {
+5 -2
View File
@@ -197,13 +197,16 @@ 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);
let mut o = current.clone().unwrap_or_default();
// 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;
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_options(&mut d.fields, &o);
super::flatten::put_changed_options(&mut d.fields, &shown, &o);
}
if let Err(e) = &current {
ui.label(egui::RichText::new(e).color(Tokens::get(ui.ctx()).text_dim).size(11.5));
+53 -2
View File
@@ -48,6 +48,32 @@ pub(crate) fn put_options(fields: &mut Map<String, Value>, 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<String, Value>, 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<String, Value>, base: &FlattenOptions) -> FlattenOptions {
let Ok(Value::Object(mut v)) = serde_json::to_value(base) else { return base.clone() };
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::<FlattenOptions>(Value::Object(v.clone())).is_err() {
v.insert(k.clone(), old);
}
}
serde_json::from_value(Value::Object(v)).unwrap_or_else(|_| base.clone())
}
/// The options the fields hold (a `preset` not loaded yet: its own).
fn options(d: &Dialog, saved: &[FlattenerPreset]) -> Result<FlattenOptions, String> {
if d.str("preset") != d.str(APPLIED) {
@@ -101,9 +127,11 @@ fn body(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool {
save_row(app, ui, d);
}
ui.add_space(10.0);
let mut o = current.clone().unwrap_or_default();
// 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_options(&mut d.fields, &o);
put_changed_options(&mut d.fields, &shown, &o);
}
if let Err(e) = &current {
ui.label(egui::RichText::new(e).color(Tokens::get(ui.ctx()).text_dim).size(11.5));
@@ -404,4 +432,27 @@ mod tests {
assert!(super::super::confirm(&mut app).unwrap_err().contains("resolutions"));
assert!(app.ui.dialog.is_some());
}
/// Changing a switch beside a value out of range keeps the values entered.
#[test]
fn a_switch_changed_beside_a_bad_value_keeps_the_values_entered() {
let mut app = scene();
app.run("ui.flattenTransparencyDialog", json!({})).unwrap();
let d = app.ui.dialog.as_mut().unwrap();
d.fields.insert("balance".into(), json!(20));
d.fields.insert("gradientPpi".into(), json!(9000));
d.fields.insert("lineArtPpi".into(), json!("many"));
let mut f = super::super::tests_eps::Frames::new();
let (_, at) = f.control(&mut app, "Convert All Text to Outlines", egui::accesskit::Role::CheckBox);
let before = app.ui.dialog.as_ref().unwrap().bool("textToOutlines");
f.click(&mut app, at);
let d = app.ui.dialog.as_ref().unwrap();
assert_eq!(d.bool("textToOutlines"), !before, "the switch changes");
assert_eq!((d.f64("balance", 0.0), d.f64("gradientPpi", 0.0)), (20.0, 9000.0), "the other values stay as entered");
assert_eq!(d.fields["lineArtPpi"], json!("many"), "a value of the wrong type stays to be refused");
assert!(super::super::confirm(&mut app).is_err());
let d = app.ui.dialog.as_mut().unwrap();
d.fields.insert("lineArtPpi".into(), json!(300));
assert!(super::super::confirm(&mut app).unwrap_err().contains("resolutions"));
}
}
+87
View File
@@ -169,6 +169,93 @@ fn bad_custom_flattener_options_keep_the_dialog_open() {
assert_eq!((d.fields["flattener"]["preserveOverprints"].as_bool(), d.fields["flattener"]["preserveAlpha"].as_bool()), (Some(true), Some(false)));
}
/// Headless frames of the dialog layer with screen-reader output, input events and a clock.
pub(super) struct Frames {
ctx: egui::Context,
time: f64,
}
impl Frames {
pub(super) fn new() -> Self {
let ctx = egui::Context::default();
ctx.enable_accesskit();
crate::theme::install_fonts(&ctx);
Self { ctx, time: 0.0 }
}
fn run(&mut self, app: &mut VectorcraftApp, events: Vec<egui::Event>) -> egui::FullOutput {
self.time += 0.1;
let screen = egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1280.0, 900.0));
let input = egui::RawInput { events, time: Some(self.time), screen_rect: Some(screen), ..Default::default() };
let mut out = self.ctx.run_ui(input, |ui| show(app, ui.ctx()));
out.textures_delta.clear();
out
}
/// The screen-reader node of the control of `role` named `label`, with its center.
pub(super) fn control(&mut self, app: &mut VectorcraftApp, label: &str, role: egui::accesskit::Role) -> (egui::accesskit::NodeId, egui::Pos2) {
self.run(app, vec![]);
let out = self.run(app, vec![]);
let nodes = &out.platform_output.accesskit_update.as_ref().expect("an accessibility tree").nodes;
let (id, n) =
nodes.iter().find(|(_, n)| n.label() == Some(label) && n.role() == role).unwrap_or_else(|| panic!("no {role:?} named {label:?}"));
let b = n.bounds().expect("bounds");
(*id, egui::pos2((b.x0 + b.x1) as f32 * 0.5, (b.y0 + b.y1) as f32 * 0.5))
}
pub(super) fn click(&mut self, app: &mut VectorcraftApp, at: egui::Pos2) {
for pressed in [true, false] {
let button = egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE };
self.run(app, vec![egui::Event::PointerMoved(at), button]);
}
}
}
/// Enter on the focused Custom… button opens the custom options and does not also confirm them.
#[test]
fn enter_on_the_custom_button_opens_the_custom_options() {
use egui::accesskit::{Action, ActionRequest, Role, TreeId};
let (mut app, _) = app(1);
app.run("flattener.presets.save", json!({"name": "Mine", "balance": 0})).unwrap();
app.run("ui.epsOptionsDialog", json!({"path": "/out/a.eps", "flattenerPreset": "Mine"})).unwrap();
let mut f = Frames::new();
let (node, _) = f.control(&mut app, "Custom…", Role::Button);
f.run(
&mut app,
vec![egui::Event::AccessKitActionRequest(ActionRequest { action: Action::Focus, target_node: node, target_tree: TreeId::ROOT, data: None })],
);
let enter = |pressed| egui::Event::Key { key: egui::Key::Enter, physical_key: None, pressed, repeat: false, modifiers: Default::default() };
f.run(&mut app, vec![enter(true)]);
f.run(&mut app, vec![enter(false)]);
assert_eq!(kind(&app), Some(eps_options::CUSTOM_KIND), "the custom options stay open");
let parent = super::parent(app.ui.dialog.as_ref().unwrap()).unwrap();
assert_eq!(parent.str("flattenerPreset"), "Mine", "EPS Options keep their preset");
}
/// With a value out of range, the editor shows the values entered, and changing a switch keeps
/// them, so OK still refuses the bad value.
#[test]
fn a_switch_changed_beside_a_bad_value_keeps_the_values_entered() {
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, "balance", json!(20));
set(&mut app, "gradientPpi", json!(9000));
set(&mut app, "lineArtPpi", json!("many"));
let mut f = Frames::new();
let (_, at) = f.control(&mut app, "Convert All Text to Outlines", egui::accesskit::Role::CheckBox);
let before = app.ui.dialog.as_ref().unwrap().bool("textToOutlines");
f.click(&mut app, at);
let d = app.ui.dialog.as_ref().unwrap();
assert_eq!(d.bool("textToOutlines"), !before, "the switch changes");
assert_eq!((d.f64("balance", 0.0), d.f64("gradientPpi", 0.0)), (20.0, 9000.0), "the other values stay as entered");
assert_eq!(d.fields["lineArtPpi"], json!("many"), "a value of the wrong type stays to be refused");
assert!(confirm(&mut app).is_err());
set(&mut app, "lineArtPpi", json!(300));
assert!(confirm(&mut app).unwrap_err().contains("resolutions"));
}
#[test]
fn the_eps_options_dialog_needs_a_document() {
let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default());