Skip to content

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

Crop tool: custom ratios keep three decimals, 1.414 : 1 no longer becomes 1.4 : 1 (fixes #6487) (#6503)

Marco Arnulfo6a7fc88c79d8
diff --git a/crates/ui-egui/src/crop_presets.rs b/crates/ui-egui/src/crop_presets.rs
index e049fdfa8..e30b0d1cf 100644
--- a/crates/ui-egui/src/crop_presets.rs
+++ b/crates/ui-egui/src/crop_presets.rs
@@ -109,7 +109,7 @@ pub fn savable(o: &ToolOptions) -> Option<(Map<String, Value>, String)> {
if !(a.is_finite() && b.is_finite() && a > 0.0 && b > 0.0) {
return None;
}
- let name = format!("{} : {}", crate::widgets::fmt_num(a), crate::widgets::fmt_num(b));
+ let name = format!("{} : {}", crop_size::fmt_ratio(a), crop_size::fmt_ratio(b));
let mut fields = Map::new();
fields.insert("crop_ratio".into(), json!(o.crop_ratio));
Some((fields, name))
diff --git a/crates/ui-egui/src/crop_size.rs b/crates/ui-egui/src/crop_size.rs
index 28a6a0e12..b30cdd8c4 100644
--- a/crates/ui-egui/src/crop_size.rs
+++ b/crates/ui-egui/src/crop_size.rs
@@ -146,6 +146,14 @@ pub fn ratio(o: &ToolOptions, doc_dpi: f64) -> Option<f64> {
Some(w / h).filter(|k| k.is_finite() && *k > 0.0)
}
+/// Decimals a custom ratio side keeps and shows: 1.414 : 1 (ISO A sizes) as in Photoshop (#6487).
+pub const RATIO_DECIMALS: u32 = 3;
+
+/// A ratio side as stored in a `crop_ratio` key ("1.414", "16").
+pub fn fmt_ratio(v: f64) -> String {
+ crate::widgets::fmt_num_prec(v, RATIO_DECIMALS)
+}
+
/// 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, doc_size: (f64, f64)) -> (f32, f32) {
@@ -170,7 +178,7 @@ 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)));
+ o.crop_ratio = format!("{}:{}", fmt_ratio(f64::from(width)), fmt_ratio(f64::from(height)));
} else {
o.crop_ratio.clear();
}
@@ -347,6 +355,15 @@ mod tests {
assert_eq!(width / height, 4.0 / 3.0);
}
+ #[test]
+ fn custom_ratio_keeps_three_decimals() {
+ // #6487: 1.414 : 1 (ISO A sizes) stays 1.414, not 1.4, as in Photoshop.
+ let mut options = ToolOptions::default();
+ set_custom_ratio_fields(&mut options, 1.414, 1.0);
+ assert_eq!(options.crop_ratio, "1.414:1");
+ assert_eq!(custom_ratio_fields(&options, (0.0, 0.0)), (1.414, 1.0));
+ }
+
#[test]
fn selecting_free_ratio_clears_previous_custom_dimensions() {
let mut options = ToolOptions { crop_ratio: "16:9".into(), ..ToolOptions::default() };
diff --git a/crates/ui-egui/src/crop_ui.rs b/crates/ui-egui/src/crop_ui.rs
index 78f8d44b5..f1df63ff1 100644
--- a/crates/ui-egui/src/crop_ui.rs
+++ b/crates/ui-egui/src/crop_ui.rs
@@ -645,7 +645,7 @@ pub fn swapped(r: [f64; 4]) -> [f64; 4] {
/// 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<String> {
let (w, h) = crate::chrome_ui::crop_ratio(key, doc_w, doc_h).filter(|(w, h)| w.is_finite() && h.is_finite() && *w > 0.0 && *h > 0.0)?;
- Some(format!("{}:{}", crate::widgets::fmt_num(h), crate::widgets::fmt_num(w)))
+ Some(format!("{}:{}", crate::crop_size::fmt_ratio(h), crate::crop_size::fmt_ratio(w)))
}
/// X: swaps the pending frame between portrait and landscape about its centre, keeping its angle,
@@ -1949,6 +1949,7 @@ mod tests {
app.ui.tool_options.crop_ratio = String::new();
assert!(swap_orientation(&mut app));
assert_eq!(app.ui.tool_options.crop_ratio, "", "no ratio stays none");
+ assert_eq!(swapped_ratio("1.414:1", 1.0, 1.0).as_deref(), Some("1:1.414"), "decimals survive the swap (#6487)");
assert_eq!(swapped_ratio("0:5", 1.0, 1.0), None);
assert_eq!(swapped_ratio("nonsense", 1.0, 1.0), None);
// No frame, or another tool: X is Switch Foreground and Background Colors.
diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs
index 479355415..7f2eadd08 100644
--- a/crates/ui-egui/src/panels.rs
+++ b/crates/ui-egui/src/panels.rs
@@ -1279,9 +1279,10 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
// Keep partial edits in ToolOptions so the first side survives a frame;
// Original Ratio shows the document's numbers, as Photoshop does.
let (mut rw, mut rh) = crate::crop_size::custom_ratio_fields(o, doc_size);
- let cw = widgets::value_field(ui, &mut rw, 0.0..=99_999.0, "", 54.0).changed();
+ let decimals = crate::crop_size::RATIO_DECIMALS;
+ let cw = widgets::value_field_prec(ui, &mut rw, 0.0..=99_999.0, "", 54.0, decimals).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();
+ 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);
}
@@ -6217,6 +6218,40 @@ mod compact_visual_regressions {
}
}
+ #[test]
+ fn the_crop_ratio_fields_show_three_decimals() {
+ // #6487: a 1.414 : 1 ratio reads 1.414 in the bar, not 1.4.
+ let ctx = egui::Context::default();
+ crate::theme::install_fonts(&ctx);
+ let Some(kind) = crate::theme::ThemeKind::ALL.into_iter().find(|k| {
+ crate::theme::apply(&ctx, *k);
+ Tokens::get(&ctx).pro
+ }) else {
+ return;
+ };
+ crate::theme::apply(&ctx, kind);
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());
+ app.ui.tool = Tool::Crop;
+ app.ui.tool_options.crop_ratio = "1.414:1".into();
+ let screen = Rect::from_min_size(egui::Pos2::ZERO, vec2(1600.0, 120.0));
+ let mut out = ctx.run_ui(input_at_scale(screen, 1.0), |ui| options_bar(&mut app, ui));
+ for _ in 0..3 {
+ out.textures_delta.clear();
+ out = ctx.run_ui(input_at_scale(screen, 1.0), |ui| options_bar(&mut app, ui));
+ }
+ out.textures_delta.clear();
+ let texts: Vec<&str> = out
+ .shapes
+ .iter()
+ .filter_map(|s| match &s.shape {
+ egui::Shape::Text(t) => Some(t.galley.job.text.as_str()),
+ _ => None,
+ })
+ .collect();
+ assert!(texts.contains(&"1.414"), "{texts:?}");
+ assert_eq!(app.ui.tool_options.crop_ratio, "1.414:1", "showing the ratio leaves it alone");
+ }
+
#[test]
fn the_narrow_options_bar_preference_shortens_the_bar() {
assert_eq!(options_bar_height(true, false), 36.0);