mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Contact Sheet II: OK right after a Units change sends the converted size
The size followed Units only when the dialog drew, so `ui.dialog.set units` and `ui.dialog.confirm` with no frame between them ran the old numbers in the new unit. The form's builder, which runs before each pass, OK and automation write, now converts too.
This commit is contained in:
1 parent
dd027048bd
commit
06bb3bf741
3 files changed
+43
-10
No files matched your search
@@ -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<u8> {
|
||||
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"));
|
||||
|
||||
@@ -40,7 +40,7 @@ pub(crate) fn rows(dir: String) -> Vec<Param> {
|
||||
.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<Param> {
|
||||
/// 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).
|
||||
|
||||
@@ -63,6 +63,9 @@ fn names(names: &[String]) -> Vec<(String, Label)> {
|
||||
fn open_form(app: &mut PhotocraftApp, key: &'static str, title: &str, rows: Vec<Param>) -> 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))
|
||||
})
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user