From f8763e2d5a05cbdbf9bd062bd6c6fae858631f61 Mon Sep 17 00:00:00 2001 From: vngocvan Date: Tue, 27 Oct 2026 18:38:32 +0700 Subject: [PATCH] Fix Crop Clear retaining custom proportions --- crates/ui-egui/src/tools/crop_size.rs | 21 +++++++++++++ crates/ui-egui/tests/it/crop_default_mode.rs | 31 ++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/crates/ui-egui/src/tools/crop_size.rs b/crates/ui-egui/src/tools/crop_size.rs index ee1b939e15..190ca1bf33 100644 --- a/crates/ui-egui/src/tools/crop_size.rs +++ b/crates/ui-egui/src/tools/crop_size.rs @@ -232,6 +232,7 @@ pub fn clear_ratio(o: &mut ToolOptions) { clear(o); } else { o.crop_ratio.clear(); + select_custom_ratio(o); } } @@ -385,6 +386,26 @@ mod tests { assert_eq!(width / height, 4.0 / 3.0); } + #[test] + fn clear_ratio_discards_complete_and_partial_ratio_fields() { + for (width, height) in [(16.0, 9.0), (4.0, 0.0), (0.0, 3.0)] { + let mut options = ToolOptions::default(); + set_custom_ratio_fields(&mut options, width, height); + clear_ratio(&mut options); + assert!(options.crop_ratio.is_empty()); + assert_eq!(custom_ratio_fields(&options, (300.0, 200.0)), (0.0, 0.0)); + set_custom_ratio_fields(&mut options, 5.0, 0.0); + assert!(options.crop_ratio.is_empty(), "entering a new width must not reuse the old height"); + assert_eq!(custom_ratio_fields(&options, (300.0, 200.0)), (5.0, 0.0)); + } + let mut options = whr("4 in", "6 in", "300", PX_PER_IN); + clear_ratio(&mut options); + assert_eq!( + (options.crop_ratio.as_str(), options.crop_width.as_str(), options.crop_height.as_str(), options.crop_resolution.as_str()), + (WHR, "", "", "") + ); + } + #[test] fn custom_ratio_keeps_three_decimals() { // #6487: 1.414 : 1 (ISO A sizes) stays 1.414, not 1.4, as in Photoshop. diff --git a/crates/ui-egui/tests/it/crop_default_mode.rs b/crates/ui-egui/tests/it/crop_default_mode.rs index 7c426410db..5ef478fb50 100644 --- a/crates/ui-egui/tests/it/crop_default_mode.rs +++ b/crates/ui-egui/tests/it/crop_default_mode.rs @@ -238,3 +238,34 @@ fn a_resize_keeps_the_box_centre_after_a_space_pan() { let b = screen_box(&h); assert!(mid(b).distance(mid(box0)) < 0.5, "still where it was: {:?} vs {:?}", mid(b), mid(box0)); } + +#[test] +fn clear_removes_the_displayed_crop_proportions_without_changing_the_frame() { + let _gpu = photocraft_testkit::gpu_lock(); + let Some(mut h) = setup() else { return }; + let o = &mut h.state_mut().ui.tool_options; + o.crop_ratio = "16:9".into(); + o.crop_custom_ratio_width = 16.0; + o.crop_custom_ratio_height = 9.0; + h.run_steps(3); + assert!(h.query_all_by_value("16").next().is_some()); + assert!(h.query_all_by_value("9").next().is_some()); + let frame = h.state().ui.crop_rect; + let save = |h: &mut Harness, name: &str| { + if let Some(dir) = std::env::var_os("CRAFT_UI_VISUAL_FIXTURES") { + let dir = std::path::PathBuf::from(dir); + std::fs::create_dir_all(&dir).unwrap(); + h.render().unwrap().save(dir.join(name)).unwrap(); + } + }; + save(&mut h, "crop-clear-before.png"); + h.query_all_by_label("Clear").min_by(|a, b| a.rect().top().total_cmp(&b.rect().top())).expect("Crop options bar Clear").click(); + h.run_steps(3); + save(&mut h, "crop-clear-after.png"); + let o = &h.state().ui.tool_options; + assert!(o.crop_ratio.is_empty()); + assert_eq!((o.crop_custom_ratio_width, o.crop_custom_ratio_height), (0.0, 0.0)); + assert!(h.query_all_by_value("16").next().is_none()); + assert!(h.query_all_by_value("9").next().is_none()); + assert_eq!(h.state().ui.crop_rect, frame); +}