From 99b5d92725ab60cc5b4b4dbe3c840b8c0724664e Mon Sep 17 00:00:00 2001 From: David Mann Date: Mon, 19 Oct 2026 13:54:43 +0200 Subject: [PATCH 1/4] Crop: the options-bar swap button turns the frame instead of shrinking it (#7738) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With a ratio set, the ⇄ button only swapped the ratio fields; apply_ratio then fitted the swapped ratio inside the old, unturned frame and shrank it on every click. The button now turns the frame about its centre first, as X does. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/crop_ui.rs | 35 +++++++++++++++++++++++++++++++++++ crates/ui-egui/src/panels.rs | 5 +++++ 2 files changed, 40 insertions(+) diff --git a/crates/ui-egui/src/crop_ui.rs b/crates/ui-egui/src/crop_ui.rs index f1df63ff14..74b803dd7a 100644 --- a/crates/ui-egui/src/crop_ui.rs +++ b/crates/ui-egui/src/crop_ui.rs @@ -641,6 +641,14 @@ pub fn swapped(r: [f64; 4]) -> [f64; 4] { [c[0] - hh, c[1] - hw, c[0] + hh, c[1] + hw] } +/// The options bar's ⇄ with a ratio set: the frame turns with the ratio about its centre, as X +/// does, so fitting the swapped ratio afterwards doesn't shrink it (#7738). +pub(crate) fn turn_frame(app: &mut PhotocraftApp) { + if let Some(r) = app.ui.crop_rect.filter(|_| takes_keys(app) && preset_ratio(app).is_some()) { + keyed(app, swapped(r)); + } +} + /// The options-bar ratio `key` with its sides swapped (what the bar's ⇄ button does), `None` when /// no ratio is set; "original" becomes the document's size turned on its side. pub fn swapped_ratio(key: &str, doc_w: f64, doc_h: f64) -> Option { @@ -1970,6 +1978,33 @@ mod tests { assert!(!swap_orientation(&mut app)); } + /// #7738: with a ratio set, the options bar's ⇄ turns the frame with the ratio, as X does, + /// instead of fitting the swapped ratio inside the old frame; a second click brings it back. + #[test] + fn the_swap_button_turns_the_ratio_frame() { + use egui_kittest::kittest::Queryable; + let mut a = app(SampleType::U8); + a.ui.tool_options.crop_ratio = "16:9".into(); + a.ui.crop_rect = Some([60.0, 27.5, 140.0, 72.5]); + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1600.0, 60.0)).build_ui_state( + |ui, app: &mut PhotocraftApp| { + if ui.ctx().fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) { + crate::panels::options_bar(app, ui); + } + }, + a, + ); + PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ProMedium); + h.run_steps(4); + let near = |a: Option<[f64; 4]>, b: [f64; 4]| a.is_some_and(|a| a.iter().zip(b).all(|(x, y)| (x - y).abs() < 1e-9)); + h.get_by_label("Swaps height and width").click(); + h.run_steps(2); + assert!(near(h.state().ui.crop_rect, [77.5, 10.0, 122.5, 90.0]), "45 x 80, not shrunk: {:?}", h.state().ui.crop_rect); + h.get_by_label("Swaps height and width").click(); + h.run_steps(2); + assert!(near(h.state().ui.crop_rect, [60.0, 27.5, 140.0, 72.5]), "and back: {:?}", h.state().ui.crop_rect); + } + /// #1919: W × H beside the pointer while the frame is drawn or resized (its own size when /// turned), not while it moves or turns or between gestures. #[test] diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs index 9a1b07f2b7..5c5fe57004 100644 --- a/crates/ui-egui/src/panels.rs +++ b/crates/ui-egui/src/panels.rs @@ -1276,6 +1276,7 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) { // Ratios, sizes, saved presets, New / Delete Crop Preset… (#2443, #1919). crate::crop_presets::dropdown(app, ui); let o = &mut app.ui.tool_options; + let mut turn = false; if o.crop_ratio == crate::crop_size::WHR { crate::crop_size::fields(o, ui); } else { @@ -1288,6 +1289,7 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) { let ch = widgets::value_field_prec(ui, &mut rh, 0.0..=99_999.0, "", 54.0, decimals).changed(); if swap { std::mem::swap(&mut rw, &mut rh); + turn = true; } if cw || ch || swap { crate::crop_size::set_custom_ratio_fields(o, rw, rh); @@ -1315,6 +1317,9 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) { &[(false, tl!("Background (Default)")), (true, tl!("Content-Aware Fill"))], 150.0, ); + if turn { + crate::crop_ui::turn_frame(app); + } if crate::crop_ui::preset_ratio(app) != before_ratio { crate::crop_ui::apply_ratio(app); } From e465970a3a1679e4d0d148c529f730f0919c07ff Mon Sep 17 00:00:00 2001 From: David Mann Date: Tue, 20 Oct 2026 16:58:24 +0200 Subject: [PATCH 2/4] Crop: the default-mode swap test expects the swap button to turn the ratio frame, as X does (#7738) Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/tests/crop_default_mode.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/crates/ui-egui/tests/crop_default_mode.rs b/crates/ui-egui/tests/crop_default_mode.rs index 58d9709e99..88916b94e1 100644 --- a/crates/ui-egui/tests/crop_default_mode.rs +++ b/crates/ui-egui/tests/crop_default_mode.rs @@ -138,18 +138,17 @@ fn the_swap_button_and_x_swap_the_ratio_about_the_centre() { let before = mid(screen_box(&h)); h.get_by_label("Swaps height and width").click(); h.run_steps(3); + // Both turn the frame instead of fitting the turned ratio inside it (#7057, #7738). let button = h.state().ui.crop_rect.unwrap(); assert_eq!(h.state().ui.tool_options.crop_ratio, "2:3"); - assert!(((button[2] - button[0]) / (button[3] - button[1]) - 2.0 / 3.0).abs() < 1e-10); - assert!(button[0] >= original[0] && button[2] <= original[2] && button[1] >= original[1] && button[3] <= original[3]); + assert_eq!(button, [70.0, -20.0, 230.0, 220.0]); assert!(mid(screen_box(&h)).distance(before) < 0.01); h.state_mut().ui.crop_rect = Some(original); h.state_mut().ui.tool_options.crop_ratio = "3:2".into(); h.run_steps(3); h.key_press(Key::X); h.run_steps(3); - // X turns the frame instead of fitting the turned ratio inside it (#7057). - assert_eq!(h.state().ui.crop_rect, Some([70.0, -20.0, 230.0, 220.0])); + assert_eq!(h.state().ui.crop_rect, Some(button)); assert!(mid(screen_box(&h)).distance(before) < 0.01); } From 6cd724a10ffbad47417d694ae5fe606d96af0374 Mon Sep 17 00:00:00 2001 From: David Mann Date: Wed, 21 Oct 2026 12:30:47 +0200 Subject: [PATCH 3/4] Address CodeRabbit review: swapping a square default crop frame leaves it untouched The options-bar swap and X no longer mark an unchanged square frame as edited, so leaving the tool drops it instead of cropping. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/crop_ui.rs | 61 ++++++++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 12 deletions(-) diff --git a/crates/ui-egui/src/crop_ui.rs b/crates/ui-egui/src/crop_ui.rs index 712ae46e6a..a2bbfa44f5 100644 --- a/crates/ui-egui/src/crop_ui.rs +++ b/crates/ui-egui/src/crop_ui.rs @@ -644,7 +644,11 @@ pub fn swapped(r: [f64; 4]) -> [f64; 4] { /// The options bar's ⇄ with a ratio set: the frame turns with the ratio about its centre, as X /// does, so fitting the swapped ratio afterwards doesn't shrink it (#7738). pub(crate) fn turn_frame(app: &mut PhotocraftApp) { - if let Some(r) = app.ui.crop_rect.filter(|_| takes_keys(app) && preset_ratio(app).is_some()) { + // A square frame doesn't change, so an untouched default frame stays one (and isn't + // committed on leaving the tool). + if let Some(r) = app.ui.crop_rect.filter(|_| takes_keys(app) && preset_ratio(app).is_some()) + && swapped(r) != r + { keyed(app, swapped(r)); } } @@ -668,8 +672,11 @@ pub fn swap_orientation(app: &mut PhotocraftApp) -> bool { { app.ui.tool_options.crop_ratio = k; } - // The frame turns first: fitting the swapped ratio inside the old frame would shrink it. - keyed(app, swapped(r)); + // The frame turns first: fitting the swapped ratio inside the old frame would shrink it. A + // square frame doesn't change, so an untouched default frame stays one. + if swapped(r) != r { + keyed(app, swapped(r)); + } if preset_ratio(app).is_some() { apply_ratio(app); } else { @@ -2010,10 +2017,46 @@ mod tests { /// instead of fitting the swapped ratio inside the old frame; a second click brings it back. #[test] fn the_swap_button_turns_the_ratio_frame() { + use egui_kittest::kittest::Queryable; + let a = app(SampleType::U8); + let mut h = swap_button_harness(a, "16:9", [60.0, 27.5, 140.0, 72.5]); + let near = |a: Option<[f64; 4]>, b: [f64; 4]| a.is_some_and(|a| a.iter().zip(b).all(|(x, y)| (x - y).abs() < 1e-9)); + h.get_by_label("Swaps height and width").click(); + h.run_steps(2); + assert!(near(h.state().ui.crop_rect, [77.5, 10.0, 122.5, 90.0]), "45 x 80, not shrunk: {:?}", h.state().ui.crop_rect); + h.get_by_label("Swaps height and width").click(); + h.run_steps(2); + assert!(near(h.state().ui.crop_rect, [60.0, 27.5, 140.0, 72.5]), "and back: {:?}", h.state().ui.crop_rect); + } + + /// A square default frame doesn't turn, so ⇄ leaves it untouched: not edited, and leaving the + /// tool drops it instead of cropping. + #[test] + fn the_swap_button_leaves_a_square_default_frame_untouched() { use egui_kittest::kittest::Queryable; let mut a = app(SampleType::U8); - a.ui.tool_options.crop_ratio = "16:9".into(); - a.ui.crop_rect = Some([60.0, 27.5, 140.0, 72.5]); + a.crop.default_frame = true; + a.crop.editing = false; + let mut h = swap_button_harness(a, "1:1", [50.0, 0.0, 150.0, 100.0]); + h.get_by_label("Swaps height and width").click(); + h.run_steps(2); + assert_eq!(h.state().ui.crop_rect, Some([50.0, 0.0, 150.0, 100.0])); + assert!(h.state().crop.default_frame && !h.state().crop.editing, "still the untouched default frame"); + // X, the same swap from the keyboard. + let mut a = app(SampleType::U8); + a.crop.default_frame = true; + a.crop.editing = false; + a.crop.frame_for = a.session.active_index().zip(a.session.active()).map(|(i, st)| (i, st.doc.size.width, st.doc.size.height)); + a.ui.tool_options.crop_ratio = "1:1".into(); + a.ui.crop_rect = Some([50.0, 0.0, 150.0, 100.0]); + assert!(swap_orientation(&mut a)); + assert_eq!(a.ui.crop_rect, Some([50.0, 0.0, 150.0, 100.0])); + assert!(a.crop.default_frame && !a.crop.editing, "X keeps it untouched too"); + } + + fn swap_button_harness(mut a: PhotocraftApp, ratio: &str, rect: [f64; 4]) -> egui_kittest::Harness<'static, PhotocraftApp> { + a.ui.tool_options.crop_ratio = ratio.into(); + a.ui.crop_rect = Some(rect); let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1600.0, 60.0)).build_ui_state( |ui, app: &mut PhotocraftApp| { if ui.ctx().fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) { @@ -2024,13 +2067,7 @@ mod tests { ); PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ProMedium); h.run_steps(4); - let near = |a: Option<[f64; 4]>, b: [f64; 4]| a.is_some_and(|a| a.iter().zip(b).all(|(x, y)| (x - y).abs() < 1e-9)); - h.get_by_label("Swaps height and width").click(); - h.run_steps(2); - assert!(near(h.state().ui.crop_rect, [77.5, 10.0, 122.5, 90.0]), "45 x 80, not shrunk: {:?}", h.state().ui.crop_rect); - h.get_by_label("Swaps height and width").click(); - h.run_steps(2); - assert!(near(h.state().ui.crop_rect, [60.0, 27.5, 140.0, 72.5]), "and back: {:?}", h.state().ui.crop_rect); + h } /// #1919: W × H beside the pointer while the frame is drawn or resized (its own size when From 849d88129f650f71300d0431e8b76f37c05b756d Mon Sep 17 00:00:00 2001 From: David Mann Date: Wed, 21 Oct 2026 20:23:10 +0200 Subject: [PATCH 4/4] Address CodeRabbit review: the W x H x Resolution swap button turns the frame too With a size preset the options bar's swap button only swapped W and H, and the ratio change then fitted the turned size inside the old frame, shrinking it, while X already turned the frame. Now both turn it about its centre, as with a ratio preset and as in Photoshop. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/crop_size.rs | 8 ++++---- crates/ui-egui/src/crop_ui.rs | 31 +++++++++++++++++++++++++++++++ crates/ui-egui/src/panels.rs | 3 ++- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/crop_size.rs b/crates/ui-egui/src/crop_size.rs index d219a7a50c..1b93c108a0 100644 --- a/crates/ui-egui/src/crop_size.rs +++ b/crates/ui-egui/src/crop_size.rs @@ -292,11 +292,10 @@ fn text_field(ui: &mut egui::Ui, id: &str, value: &mut String, hint: &str, width } /// The W x H x Resolution fields of the options bar: W, ⇄, H, the resolution and its unit. -pub fn fields(o: &mut ToolOptions, ui: &mut egui::Ui) { +/// Returns true when ⇄ swapped W and H, so the Crop tool turns its frame with them. +pub fn fields(o: &mut ToolOptions, ui: &mut egui::Ui) -> bool { text_field(ui, "crop-width", &mut o.crop_width, tl!("Width"), 64.0, normalize_length); - if crate::icons::button(ui, "arrow-left-right", 22.0, false, tl!("Swaps height and width")).clicked() { - swap(o); - } + let swapped = crate::icons::button(ui, "arrow-left-right", 22.0, false, tl!("Swaps height and width")).clicked() && swap(o); text_field(ui, "crop-height", &mut o.crop_height, tl!("Height"), 64.0, normalize_length); text_field(ui, "crop-resolution", &mut o.crop_resolution, tl!("Resolution"), 64.0, normalize_resolution); let units = [(PX_PER_IN.to_string(), "px/in"), (PX_PER_CM.to_string(), "px/cm")]; @@ -308,6 +307,7 @@ pub fn fields(o: &mut ToolOptions, ui: &mut egui::Ui) { o.crop_resolution = crate::widgets::fmt_num2(v); } } + swapped } #[cfg(test)] diff --git a/crates/ui-egui/src/crop_ui.rs b/crates/ui-egui/src/crop_ui.rs index 48c0c9b84c..2969d47e3e 100644 --- a/crates/ui-egui/src/crop_ui.rs +++ b/crates/ui-egui/src/crop_ui.rs @@ -2117,6 +2117,37 @@ mod tests { assert!(near(h.state().ui.crop_rect, [60.0, 27.5, 140.0, 72.5]), "and back: {:?}", h.state().ui.crop_rect); } + /// With W x H x Resolution the bar's ⇄ swaps W and H and turns the frame with them, as X + /// does, instead of fitting the turned size inside the old frame. + #[test] + fn the_swap_button_turns_a_size_preset_frame() { + use egui_kittest::kittest::Queryable; + let whr = |a: &mut PhotocraftApp| { + let o = &mut a.ui.tool_options; + (o.crop_width, o.crop_height, o.crop_resolution) = ("1600 px".into(), "900 px".into(), "72".into()); + }; + let mut a = app(SampleType::U8); + whr(&mut a); + let mut h = swap_button_harness(a, crate::crop_size::WHR, [60.0, 27.5, 140.0, 72.5]); + let near = |a: Option<[f64; 4]>, b: [f64; 4]| a.is_some_and(|a| a.iter().zip(b).all(|(x, y)| (x - y).abs() < 1e-9)); + h.get_by_label("Swaps height and width").click(); + h.run_steps(2); + let o = &h.state().ui.tool_options; + assert_eq!((o.crop_ratio.as_str(), o.crop_width.as_str(), o.crop_height.as_str()), (crate::crop_size::WHR, "900 px", "1600 px")); + assert!(near(h.state().ui.crop_rect, [77.5, 10.0, 122.5, 90.0]), "45 x 80, not shrunk: {:?}", h.state().ui.crop_rect); + h.get_by_label("Swaps height and width").click(); + h.run_steps(2); + assert!(near(h.state().ui.crop_rect, [60.0, 27.5, 140.0, 72.5]), "and back: {:?}", h.state().ui.crop_rect); + // X does the same. + let mut a = app(SampleType::U8); + whr(&mut a); + a.ui.tool_options.crop_ratio = crate::crop_size::WHR.into(); + a.ui.crop_rect = Some([60.0, 27.5, 140.0, 72.5]); + assert!(swap_orientation(&mut a)); + assert_eq!((a.ui.tool_options.crop_width.as_str(), a.ui.tool_options.crop_height.as_str()), ("900 px", "1600 px")); + assert!(near(a.ui.crop_rect, [77.5, 10.0, 122.5, 90.0]), "X: 45 x 80, not shrunk: {:?}", a.ui.crop_rect); + } + /// A square default frame doesn't turn, so ⇄ leaves it untouched: not edited, and leaving the /// tool drops it instead of cropping. #[test] diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs index 979f19b237..770a95789a 100644 --- a/crates/ui-egui/src/panels.rs +++ b/crates/ui-egui/src/panels.rs @@ -1335,7 +1335,8 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) { let mut turn = false; let mut ratio_base = None; if o.crop_ratio == crate::crop_size::WHR { - crate::crop_size::fields(o, ui); + // ⇄ turns the frame with W and H, as with a ratio. + turn = crate::crop_size::fields(o, ui); } else { // Keep partial edits in ToolOptions so the first side survives a frame; // Original Ratio shows the document's numbers, as Photoshop does.