diff --git a/crates/ui-egui/src/dialogs/contact_sheet_layout_tests.rs b/crates/ui-egui/src/dialogs/contact_sheet_layout_tests.rs index 9419544f1f..5d2dab2b22 100644 --- a/crates/ui-egui/src/dialogs/contact_sheet_layout_tests.rs +++ b/crates/ui-egui/src/dialogs/contact_sheet_layout_tests.rs @@ -144,6 +144,32 @@ fn switching_units_keeps_a_small_pixel_size() { }); } +/// `ui.dialog.set units` and OK with no frame between them run the sheet at the size the dialog showed. +#[test] +fn ok_right_after_a_units_change_keeps_the_size() { + crate::i18n::with_language(crate::i18n::Lang::EN, || { + let (mut h, id) = opened(); + let dir = tempfile::tempdir().unwrap(); + std::fs::write(dir.path().join("a.png"), png(40, 20)).unwrap(); + set(&mut h, id, "input", json!(dir.path().to_string_lossy())); + set(&mut h, id, "caption", json!(false)); + let ctx = h.ctx.clone(); + let app = h.state_mut(); + app.framework.dialogs.set_json_field(&ctx, id, "units", json!("pixels")).unwrap(); + crate::dialogs::testing::confirm(app, id).expect("OK is accepted"); + h.run_steps(4); + let sheet = &h.state().session.active().expect("a sheet").doc; + assert_eq!((sheet.size.width, sheet.size.height), (2400, 3000)); + }); +} + +/// A white PNG of `w` x `h` pixels. +fn png(w: u32, h: u32) -> Vec { + let mut image = photocraft_engine::Session::new(); + image.execute("file.new", json!({"width": w, "height": h, "background": "white"})).unwrap(); + photocraft_io::export(&image.active().unwrap().doc, "a.png", &Default::default()).unwrap().bytes +} + /// Width and Height take only numbers in their bounds: automation sets them through their own rows. #[test] fn width_and_height_refuse_a_value_their_rows_cannot_hold() { @@ -203,10 +229,7 @@ fn ok_runs_the_command_with_the_same_params() { assert_eq!((&p["depth"], &p["placeAcrossFirst"]), (&json!(16), &json!(false))); let dir = tempfile::tempdir().unwrap(); - let mut image = photocraft_engine::Session::new(); - image.execute("file.new", json!({"width": 40, "height": 20, "background": "white"})).unwrap(); - let png = photocraft_io::export(&image.active().unwrap().doc, "a.png", &Default::default()).unwrap().bytes; - std::fs::write(dir.path().join("a.png"), png).unwrap(); + std::fs::write(dir.path().join("a.png"), png(40, 20)).unwrap(); set(&mut h, id, "input", json!(dir.path().to_string_lossy())); set(&mut h, id, "caption", json!(false)); set(&mut h, id, "depth", json!("8")); diff --git a/crates/ui-egui/src/dialogs/contact_sheet_ui.rs b/crates/ui-egui/src/dialogs/contact_sheet_ui.rs index 9ffe319271..716a2f9b29 100644 --- a/crates/ui-egui/src/dialogs/contact_sheet_ui.rs +++ b/crates/ui-egui/src/dialogs/contact_sheet_ui.rs @@ -40,7 +40,7 @@ pub(crate) fn rows(dir: String) -> Vec { .tooltip(tl!("Images in subfolders will start on a new Contact Sheet")), Param::section(tl!("Document")), Param::new("units", tl!("Units"), ParamKind::choice(opts(&UNITS), Some("inches".into()))), - Param::custom(UNITS_SHOWN, Label::Empty, follow_units).full_width(), + Param::custom(UNITS_SHOWN, Label::Empty, units_row).full_width(), Param::new("width", tl!("Width"), ParamKind::number(Some(0.0), Some(300_000.0), 8.0, 0.5, 2)), Param::new("height", tl!("Height"), ParamKind::number(Some(0.0), Some(300_000.0), 10.0, 0.5, 2)), Param::number_with_units("resolution", tl!("Resolution"), 300.0, 1.0, 2, resolution).bounds(Some(1.0), Some(10_000.0)), @@ -63,11 +63,19 @@ pub(crate) fn rows(dir: String) -> Vec { /// The units Width and Height were last shown in, kept by [`follow_units`]. const UNITS_SHOWN: &str = "unitsShown"; +/// The row of [`follow_units`]: it shows nothing, so Units stays a plain list and the dialog remembers it with the +/// size. Following here converts in the pass that picked the unit. +fn units_row(ui: &mut egui::Ui, fields: &mut DialogFields, _: &mut CustomContext) -> egui::Response { + follow_units(fields); + ui.allocate_response(egui::Vec2::ZERO, egui::Sense::hover()) +} + /// Follows the Units list: switching it converts Width and Height at the resolution, as the command reads them, so -/// the sheet keeps its size: 8 x 10 in is 20.32 x 25.4 cm, and 2400 x 3000 px at 300 ppi. The row shows nothing; -/// Units stays a plain list, so the dialog remembers it with the size. -fn follow_units(ui: &mut egui::Ui, fields: &mut DialogFields, _: &mut CustomContext) -> egui::Response { - let units = fields.text("units").unwrap_or("inches").to_owned(); +/// the sheet keeps its size: 8 x 10 in is 20.32 x 25.4 cm, and 2400 x 3000 px at 300 ppi. The form's builder runs it +/// too, before each pass, OK and automation write, so an OK right after `ui.dialog.set units` sends the converted size. +pub(crate) fn follow_units(fields: &mut DialogFields) { + // The first build runs before the defaults are in: nothing to follow yet. + let Some(units) = fields.text("units").map(str::to_owned) else { return }; if let Some(shown) = fields.text(UNITS_SHOWN).filter(|shown| *shown != units).map(str::to_owned) { let ppi = fields.number("resolution").unwrap_or(300.0); let (from, to) = (unit_of(&shown), unit_of(&units)); @@ -81,7 +89,6 @@ fn follow_units(ui: &mut egui::Ui, fields: &mut DialogFields, _: &mut CustomCont if fields.text(UNITS_SHOWN) != Some(units.as_str()) { fields.set(UNITS_SHOWN, units); } - ui.allocate_response(egui::Vec2::ZERO, egui::Sense::hover()) } /// The unit a `units` value stands for, read as the command reads it (inches when unknown). diff --git a/crates/ui-egui/src/dialogs/file_forms.rs b/crates/ui-egui/src/dialogs/file_forms.rs index dcf6d2fc2d..4352296d22 100644 --- a/crates/ui-egui/src/dialogs/file_forms.rs +++ b/crates/ui-egui/src/dialogs/file_forms.rs @@ -63,6 +63,9 @@ fn names(names: &[String]) -> Vec<(String, Label)> { fn open_form(app: &mut PhotocraftApp, key: &'static str, title: &str, rows: Vec) -> DialogId { let title = Label::from(title.to_string()); params::open_param(app, key, DialogFields::new(), Value::Null, move |fields: &mut DialogFields| { + if key == CONTACT_SHEET { + crate::dialogs::contact_sheet_ui::follow_units(fields); + } ParamDialog::command(key, title.clone(), rows.clone()).values(Bind::bound(fields)) }) }