Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Properties / New Document: tiny sizes in physical units no longer resize the document (#5661) (#5662)

David Mann1025914b0e3e
diff --git a/crates/ui-egui/src/doc_props_ui.rs b/crates/ui-egui/src/doc_props_ui.rs
index 8c10b8c44..fbdbc9a95 100644
--- a/crates/ui-egui/src/doc_props_ui.rs
+++ b/crates/ui-egui/src/doc_props_ui.rs
@@ -100,7 +100,7 @@ fn committed_field(ui: &mut egui::Ui, id: &str, current: f32, u: crate::layer_pr
// Shown in the ruler unit (cm when the rulers are in centimeters); 100% is the current size.
let extent = f64::from(current);
let mut v = ui.data(|d| d.get_temp::<f32>(key)).unwrap_or_else(|| u.shown(extent, extent));
- let r = widgets::value_field(ui, &mut v, 0.01..=300_000.0, u.unit.suffix(), 74.0);
+ let r = widgets::value_field(ui, &mut v, 0.0..=300_000.0, u.unit.suffix(), 74.0);
if r.dragged() || r.has_focus() {
ui.data_mut(|d| d.insert_temp(key, v));
return None;
@@ -318,6 +318,30 @@ mod tests {
assert_eq!(canvas_resize_params((2400, 1500), Some(2400.2), None, false), None);
}
+ /// #5661: a 1 x 1 px document at 300 ppi is 0.0033 in wide. With the rulers in inches the
+ /// Width/Height fields clamped that to 0.01 in (3 px) and resized the document unasked.
+ #[test]
+ fn tiny_document_in_inches_is_not_resized_by_showing_it() {
+ use egui_kittest::Harness;
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ app.run("file.new", json!({"width": 1, "height": 1, "resolution": 300, "background": "white"})).unwrap();
+ app.session.prefs.edit(|p| p.units_and_rulers.rulers = photocraft_engine::prefs::Unit::Inches);
+ let undo = app.session.active().unwrap().history.entries().len();
+ let mut h = Harness::builder().with_size(vec2(320.0, 600.0)).build_ui_state(
+ |ui, app: &mut PhotocraftApp| {
+ if ui.ctx().fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) {
+ properties(app, ui);
+ }
+ },
+ app,
+ );
+ PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]);
+ h.run_steps(3);
+ let st = h.state().session.active().unwrap();
+ assert_eq!((st.doc.size.width, st.doc.size.height), (1, 1));
+ assert_eq!(st.history.entries().len(), undo, "no history step");
+ }
+
#[test]
fn document_dropdowns_map_to_live_mode_commands() {
for id in MODES.iter().map(|m| m.2).chain(DEPTHS.iter().map(|d| d.2)) {
diff --git a/crates/ui-egui/src/new_doc_ui.rs b/crates/ui-egui/src/new_doc_ui.rs
index c9bf902c6..e615620d4 100644
--- a/crates/ui-egui/src/new_doc_ui.rs
+++ b/crates/ui-egui/src/new_doc_ui.rs
@@ -633,7 +633,7 @@ pub fn body(app: &mut crate::PhotocraftApp, ui: &mut egui::Ui, f: &mut Map<Strin
small_label(ui, tl!("Width"));
ui.horizontal(|ui| {
let mut w = shown_size(f, "width", 1920.0, &unit, ppi);
- if widgets::value_field_units(ui, &mut w, 0.01..=300_000.0, 110.0, widgets::unit_decimals(&unit), units).changed() {
+ if widgets::value_field_units(ui, &mut w, 0.0..=300_000.0, 110.0, widgets::unit_decimals(&unit), units).changed() {
set_size(f, "width", w, &unit, ppi);
}
let opts: Vec<(String, &str)> = UNITS.iter().map(|u| (u.0.to_string(), u.1)).collect();
@@ -645,7 +645,7 @@ pub fn body(app: &mut crate::PhotocraftApp, ui: &mut egui::Ui, f: &mut Map<Strin
small_label(ui, tl!("Height"));
ui.horizontal(|ui| {
let mut h = shown_size(f, "height", 1080.0, &unit, ppi);
- if widgets::value_field_units(ui, &mut h, 0.01..=300_000.0, 110.0, widgets::unit_decimals(&unit), units).changed() {
+ if widgets::value_field_units(ui, &mut h, 0.0..=300_000.0, 110.0, widgets::unit_decimals(&unit), units).changed() {
set_size(f, "height", h, &unit, ppi);
}
ui.add_space(6.0);
@@ -796,6 +796,30 @@ mod tests {
assert_eq!(shown_size(&f, "width", 1920.0, "mm", 72.0), to_unit(300_000.0, "mm", 72.0));
}
+ /// #5661: 1 px at 300 ppi is 0.0033 in. Showing it in inches clamped the field to 0.01 in and
+ /// turned the width into 3 px without an edit.
+ #[test]
+ fn tiny_size_in_inches_is_not_changed_by_showing_it() {
+ use egui_kittest::Harness;
+ let app = crate::PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ let mut f = crate::state::UiState::new_document_fields();
+ for (k, v) in [("width", json!(1)), ("height", json!(1)), ("resolution", json!(300.0)), ("__unit", json!("in"))] {
+ f.insert(k.into(), v);
+ }
+ let mut h = Harness::builder().with_size(vec2(1200.0, 800.0)).build_ui_state(
+ |ui, (app, f): &mut (crate::PhotocraftApp, Map<String, Value>)| {
+ if ui.ctx().fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) {
+ body(app, ui, f);
+ }
+ },
+ (app, f),
+ );
+ crate::PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]);
+ h.run_steps(3);
+ let p = command_params(&h.state().1);
+ assert_eq!((p["width"].as_u64(), p["height"].as_u64()), (Some(1), Some(1)));
+ }
+
/// #1147: the Orientation buttons swap the typed values too: 841 x 1189 mm becomes 1189 x 841.
#[test]
fn orientation_swap_keeps_the_typed_values() {