diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index 38626a8842..6224b271da 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -692,7 +692,7 @@ pub(crate) fn field_set(app: &mut VectorcraftApp, field: &str, old: Option<&Valu return composite_fonts::field_set(d, field); } if let Some(d) = app.ui.dialog.as_mut().filter(|d| d.kind == save_pdf::KIND || d.kind == save_pdf::PRESET_KIND) { - return save_pdf::field_set(d, field, old); + return save_pdf::field_set(d, field); } if let Some(d) = app.ui.dialog.as_mut().filter(|d| d.kind == eps_options::KIND) { eps_options::field_set(d, field); diff --git a/crates/ui-egui/src/dialogs/save_pdf.rs b/crates/ui-egui/src/dialogs/save_pdf.rs index 32dab2a445..85ff601db0 100644 --- a/crates/ui-egui/src/dialogs/save_pdf.rs +++ b/crates/ui-egui/src/dialogs/save_pdf.rs @@ -754,9 +754,13 @@ fn link_bleed(d: &mut Dialog, on: bool) { } } -/// Side `side` of the custom bleed was edited to `value`: linked, every side takes it. +/// Side `side` of the custom bleed was edited to `value`: linked, every side takes it. The link +/// keeps the state it had before the edit, so an unlinked edit that makes the sides equal leaves +/// it off. fn edit_bleed(d: &mut Dialog, side: usize, value: f64) { - if bleed_linked(d) { + let linked = bleed_linked(d); + d.fields.insert(BLEED_LINKED.into(), json!(linked)); + if linked { for p in BLEED_SIDES { set(d, p, json!(value)); } @@ -766,9 +770,9 @@ fn edit_bleed(d: &mut Dialog, side: usize, value: f64) { } /// `ui.dialog.set` set `field` of Save PDF or the PDF preset editor: `__bleedLinked` links or -/// unlinks the bleed as its button does, and must be true or false; a new `bleed` drops the link's -/// state, so the link follows the values set. -pub(super) fn field_set(d: &mut Dialog, field: &str, old: Option<&Value>) -> Result<(), String> { +/// unlinks the bleed as its button does, and must be true or false; every write of `bleed`, the +/// same values included, drops the link's state, so the link follows the values set. +pub(super) fn field_set(d: &mut Dialog, field: &str) -> Result<(), String> { match field { BLEED_LINKED => match d.fields.get(BLEED_LINKED).cloned() { Some(Value::Bool(on)) => { @@ -777,7 +781,7 @@ pub(super) fn field_set(d: &mut Dialog, field: &str, old: Option<&Value>) -> Res } v => Err(format!("{BLEED_LINKED} must be true or false, not {}", v.unwrap_or(Value::Null))), }, - "bleed" if d.fields.get("bleed") != old => { + "bleed" => { d.fields.remove(BLEED_LINKED); Ok(()) } diff --git a/crates/ui-egui/src/dialogs/save_pdf/tests.rs b/crates/ui-egui/src/dialogs/save_pdf/tests.rs index 10cfa57ce0..1f04c8c912 100644 --- a/crates/ui-egui/src/dialogs/save_pdf/tests.rs +++ b/crates/ui-egui/src/dialogs/save_pdf/tests.rs @@ -633,6 +633,30 @@ fn ui_dialog_set_links_the_bleed() { assert!(!d.fields.contains_key(BLEED_LINKED) && bleed_linked(d), "equal values set are linked"); set(&mut app, "bleed", json!({"top": 1, "bottom": 2, "left": 1, "right": 1})).unwrap(); assert!(!bleed_linked(app.ui.dialog.as_ref().unwrap()), "unequal values set are unlinked"); + + // Writing the same `bleed` again also lets the link follow its values; a `__bleedLinked` set + // after it in the same request still decides. + set(&mut app, "bleed", json!({"top": 1, "bottom": 1, "left": 1, "right": 1})).unwrap(); + set(&mut app, BLEED_LINKED, json!(false)).unwrap(); + set(&mut app, "bleed", json!({"top": 1, "bottom": 1, "left": 1, "right": 1})).unwrap(); + assert!(bleed_linked(app.ui.dialog.as_ref().unwrap()), "the same equal values set again are linked"); + set(&mut app, BLEED_LINKED, json!(false)).unwrap(); + let d = app.ui.dialog.as_ref().unwrap(); + assert!(!bleed_linked(d) && bleed_of(d) == [1.0; 4], "a later __bleedLinked unlinks them"); +} + +/// An unlinked edit that makes the four sides equal leaves the link off, so the next edit +/// changes one side only. +#[test] +fn an_unlinked_edit_that_evens_the_sides_keeps_the_link_off() { + let (mut app, _, _) = app(); + app.run("ui.savePdfDialog", json!({"bleed": {"top": 3, "bottom": 5, "left": 3, "right": 3}})).unwrap(); + let d = app.ui.dialog.as_mut().unwrap(); + assert!(!bleed_linked(d)); + edit_bleed(d, 1, 3.0); + assert!(!bleed_linked(d), "the link stays off"); + edit_bleed(d, 2, 6.0); + assert_eq!(bleed_of(d), [3.0, 3.0, 6.0, 3.0], "the next edit changes one side"); } #[test]