diff --git a/crates/ui-egui/src/crop_presets.rs b/crates/ui-egui/src/crop_presets.rs index 27fa24e06bf..e049fdfa8c6 100644 --- a/crates/ui-egui/src/crop_presets.rs +++ b/crates/ui-egui/src/crop_presets.rs @@ -172,8 +172,12 @@ pub fn dropdown(app: &mut PhotocraftApp, ui: &mut egui::Ui) { app.ui.tool_options.crop_ratio = k; // A size preset or Front Image fills W x H x Resolution (#2443). crop_size::chosen(app); + sync_custom_ratio_fields(app); + } + Some(Choice::User(name)) => { + select(app, &name); + sync_custom_ratio_fields(app); } - Some(Choice::User(name)) => select(app, &name), Some(Choice::New) => { open_new(app); } @@ -184,6 +188,13 @@ pub fn dropdown(app: &mut PhotocraftApp, ui: &mut egui::Ui) { } } +/// A newly chosen ratio fills the editable custom W/H fields (free Ratio clears them, #3188). +fn sync_custom_ratio_fields(app: &mut PhotocraftApp) { + if app.ui.tool_options.crop_ratio != crop_size::WHR { + crop_size::select_custom_ratio(&mut app.ui.tool_options); + } +} + /// Choose a saved crop preset: its fields fill the options bar (`tool.presets.select`, the same /// as clicking it in the Tool Presets panel). pub fn select(app: &mut PhotocraftApp, name: &str) { diff --git a/crates/ui-egui/src/crop_size.rs b/crates/ui-egui/src/crop_size.rs index d6203bad771..42b9675a58d 100644 --- a/crates/ui-egui/src/crop_size.rs +++ b/crates/ui-egui/src/crop_size.rs @@ -154,6 +154,36 @@ pub fn ratio(o: &ToolOptions, doc_dpi: f64) -> Option { Some(w / h).filter(|k| k.is_finite() && *k > 0.0) } +/// Editable W and H shown for a custom aspect ratio. An existing valid ratio is authoritative; +/// when either side is zero, use the in-progress values retained in ToolOptions. +pub fn custom_ratio_fields(o: &ToolOptions) -> (f32, f32) { + crate::chrome_ui::crop_ratio(&o.crop_ratio, 0.0, 0.0) + .filter(|(w, h)| w.is_finite() && h.is_finite() && *w > 0.0 && *h > 0.0 && *w <= 99_999.0 && *h <= 99_999.0) + .map(|(w, h)| (w as f32, h as f32)) + .unwrap_or((o.crop_custom_ratio_width, o.crop_custom_ratio_height)) +} + +/// Apply the selected non-WHR ratio to its editable W/H fields. Free Ratio starts empty. +pub fn select_custom_ratio(o: &mut ToolOptions) { + let sides = crate::chrome_ui::crop_ratio(&o.crop_ratio, 0.0, 0.0) + .filter(|(w, h)| w.is_finite() && h.is_finite() && *w > 0.0 && *h > 0.0 && *w <= 99_999.0 && *h <= 99_999.0) + .map(|(w, h)| (w as f32, h as f32)) + .unwrap_or((0.0, 0.0)); + (o.crop_custom_ratio_width, o.crop_custom_ratio_height) = sides; +} + +/// Keep custom ratio fields across frames. An incomplete pair means free Ratio until both sides +/// are positive. +pub fn set_custom_ratio_fields(o: &mut ToolOptions, width: f32, height: f32) { + o.crop_custom_ratio_width = width; + o.crop_custom_ratio_height = height; + if width.is_finite() && height.is_finite() && width > 0.0 && height > 0.0 { + o.crop_ratio = format!("{}:{}", crate::widgets::fmt_num(f64::from(width)), crate::widgets::fmt_num(f64::from(height))); + } else { + o.crop_ratio.clear(); + } +} + /// `image.crop`'s target params for the pending crop: `targetWidth`/`targetHeight` (whole px) when /// W and H are set, and `resolution` (ppi) when the resolution is; the Image Interpolation /// preference resamples. @@ -306,6 +336,34 @@ mod tests { } } + #[test] + fn custom_ratio_keeps_the_first_side_until_the_second_is_entered() { + let mut options = ToolOptions::default(); + select_custom_ratio(&mut options); + set_custom_ratio_fields(&mut options, 4.0, 0.0); + + assert_eq!(options.crop_ratio, "", "an incomplete ratio stays unconstrained"); + assert_eq!(custom_ratio_fields(&options), (4.0, 0.0), "first entry survives the next frame"); + + let (width, _) = custom_ratio_fields(&options); + set_custom_ratio_fields(&mut options, width, 3.0); + assert_eq!(options.crop_ratio, "4:3"); + assert_eq!(custom_ratio_fields(&options), (4.0, 3.0)); + let (width, height) = crate::chrome_ui::crop_ratio(&options.crop_ratio, 0.0, 0.0).unwrap_or_default(); + assert_eq!(width / height, 4.0 / 3.0); + } + + #[test] + fn selecting_free_ratio_clears_previous_custom_dimensions() { + let mut options = ToolOptions { crop_ratio: "16:9".into(), ..ToolOptions::default() }; + select_custom_ratio(&mut options); + assert_eq!(custom_ratio_fields(&options), (16.0, 9.0)); + + options.crop_ratio.clear(); + select_custom_ratio(&mut options); + assert_eq!(custom_ratio_fields(&options), (0.0, 0.0)); + } + #[test] fn lengths_parse_with_their_units() { assert_eq!(parse_length("4 in", Unit::Pixels), Some((4.0, Unit::Inches))); diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs index 285e7ed92d7..eceada624a5 100644 --- a/crates/ui-egui/src/panels.rs +++ b/crates/ui-egui/src/panels.rs @@ -1154,16 +1154,16 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) { if o.crop_ratio == crate::crop_size::WHR { crate::crop_size::fields(o, ui); } else { - // Custom ratio fields (Photoshop shows the preset's numbers here). - let (mut rw, mut rh) = crate::chrome_ui::crop_ratio(&o.crop_ratio, 0.0, 0.0).map_or((0.0, 0.0), |(w, h)| (w as f32, h as f32)); + // Keep partial edits in ToolOptions so the first side survives a frame. + let (mut rw, mut rh) = crate::crop_size::custom_ratio_fields(o); let cw = widgets::value_field(ui, &mut rw, 0.0..=99_999.0, "", 54.0).changed(); let swap = icons::button(ui, "arrow-left-right", 22.0, false, tl!("Swaps height and width")).clicked(); let ch = widgets::value_field(ui, &mut rh, 0.0..=99_999.0, "", 54.0).changed(); if swap { std::mem::swap(&mut rw, &mut rh); } - if (cw || ch || swap) && rw > 0.0 && rh > 0.0 { - o.crop_ratio = format!("{}:{}", widgets::fmt_num(rw as f64), widgets::fmt_num(rh as f64)); + if cw || ch || swap { + crate::crop_size::set_custom_ratio_fields(o, rw, rh); } } widgets::vline(ui, 22.0); diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs index 6e9c5316110..52c98a628c1 100644 --- a/crates/ui-egui/src/state.rs +++ b/crates/ui-egui/src/state.rs @@ -545,6 +545,12 @@ pub struct ToolOptions { /// Delete Cropped Pixels. #[serde(default)] pub crop_ratio: String, + /// In-progress custom crop ratio fields. Kept separately because entering one side before the + /// other must survive a frame while the ratio itself remains unconstrained. + #[serde(default)] + pub crop_custom_ratio_width: f32, + #[serde(default)] + pub crop_custom_ratio_height: f32, #[serde(default = "yes")] pub crop_delete: bool, /// Crop W x H x Resolution (`crop_ratio` = "whr", #2443): width and height as typed lengths @@ -719,6 +725,8 @@ impl Default for ToolOptions { move_target: default_move_target(), move_show_transform: false, crop_ratio: String::new(), + crop_custom_ratio_width: 0.0, + crop_custom_ratio_height: 0.0, crop_delete: true, crop_width: String::new(), crop_height: String::new(),