Crop: the Info panel shows the crop frame's ratio (#3145)

Under W and H, while the Crop tool shows a frame, a Ratio line gives the
frame's aspect: reduced to whole numbers when both are small (4:3, 16:9,
4:5), else a decimal against 1 on the short side (1.413:1, 1:1.505), so
a photo's ratio can be matched as #3145 asks. Photoshop's Info panel has
no ratio line; this goes beyond it at the reporter's request.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Cristi RaduandClaude Opus 5.5 committed 2026-10-31 10:14:38 +02:00
1 parent 3bdbb940b7
commit 78356601d5
3 files changed
+51 -1

No files matched your search

+10 -1
View File
@@ -40,7 +40,10 @@ pub(super) fn info_panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
let fy = move |px: f64| units_y.format(px, dpi, size.height as f64);
let sel = st.doc.selection.as_ref().map(photocraft_compose::bounds::content_bounds).map(|r| [r.width() as f64, r.height() as f64]);
// With the Crop tool, W and H are the pending frame's, as in Photoshop (#3145).
let wh = crate::crop_ui::info_size(app).or(sel);
let crop = crate::crop_ui::info_size(app);
let wh = crop.or(sel);
// And the frame's ratio, to match another photo's (#3145).
let ratio = crop.and_then(crate::crop_ui::ratio_label);
let pos = hover_pixel(app);
// While a guide is dragged (#5728), the Info panel shows its position in one axis only, even
// when the pointer is outside the canvas (ruler drags do not update `hover_doc` there).
@@ -79,6 +82,12 @@ pub(super) fn info_panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
row(&mut cols[0], "Y:", guide_y.map(|p| fy(p - origin[1])).or_else(|| pos.map(|p| fy(p.1 as f64 - origin[1]))).unwrap_or_else(dash));
row(&mut cols[1], "W:", wh.map_or_else(dash, |s| fx(s[0])));
row(&mut cols[1], "H:", wh.map_or_else(dash, |s| fy(s[1])));
if let Some(r) = ratio {
cols[1].horizontal(|ui| {
ui.label(RichText::new(format!("{}:", tl!("Ratio"))).color(t.text_dim).size(11.5));
ui.label(RichText::new(r).font(mono.clone()).color(t.text));
});
}
});
widgets::hairline(ui);
ui.label(
+25
View File
@@ -676,6 +676,31 @@ pub fn info_size(app: &PhotocraftApp) -> Option<[f64; 2]> {
frame_size(app.ui.crop_rect?)
}
/// The Info panel's ratio for a frame of `size` (whole px, as [`info_size`] gives): reduced to
/// whole numbers when both are small ("4:3", "16:9", "1:1"), else as a decimal against 1 on the
/// short side ("1.413:1", "1:1.5"), so a photo's ratio can be matched (#3145).
pub fn ratio_label(size: [f64; 2]) -> Option<String> {
let [w, h] = size;
if !(w.is_finite() && h.is_finite() && w >= 1.0 && h >= 1.0) {
return None;
}
// Whole px from `frame_size`; `as` saturates for huge values and the gcd stays positive.
let (a, b) = (w.round() as u64, h.round() as u64);
let (mut x, mut y) = (a, b);
while y != 0 {
(x, y) = (y, x % y);
}
let (rw, rh) = (a / x.max(1), b / x.max(1));
if rw <= 32 && rh <= 32 {
return Some(format!("{rw}:{rh}"));
}
let trim = |v: f64| {
let s = format!("{v:.3}");
s.trim_end_matches('0').trim_end_matches('.').to_string()
};
Some(if w >= h { format!("{}:1", trim(w / h)) } else { format!("1:{}", trim(h / w)) })
}
/// The Crop tool owns the arrow keys and X: a pending frame and no gesture in progress.
pub fn takes_keys(app: &PhotocraftApp) -> bool {
app.ui.tool == Tool::Crop && app.crop.drag.is_none() && app.ui.transform.is_none() && app.ui.crop_rect.is_some_and(|r| r.iter().all(|v| v.is_finite()))
+16
View File
@@ -1163,3 +1163,19 @@ fn info_panel_shows_the_pending_frame_size() {
app.ui.tool = Tool::Move;
assert_eq!(info_size(&app), None, "another tool shows the selection instead");
}
/// #3145: the Info panel's crop ratio, reduced when small, else a decimal against 1.
#[test]
fn ratio_label_reduces_or_shows_a_decimal() {
assert_eq!(ratio_label([400.0, 300.0]).as_deref(), Some("4:3"));
assert_eq!(ratio_label([1920.0, 1080.0]).as_deref(), Some("16:9"));
assert_eq!(ratio_label([500.0, 500.0]).as_deref(), Some("1:1"));
assert_eq!(ratio_label([800.0, 1000.0]).as_deref(), Some("4:5"));
assert_eq!(ratio_label([1413.0, 1000.0]).as_deref(), Some("1.413:1"));
assert_eq!(ratio_label([200.0, 301.0]).as_deref(), Some("1:1.505"));
assert_eq!(ratio_label([1000.0, 1.0]).as_deref(), Some("1000:1"));
// Broken or absurd sizes never panic.
assert_eq!(ratio_label([f64::NAN, 1.0]), None);
assert_eq!(ratio_label([0.0, 1.0]), None);
assert!(ratio_label([f64::MAX, 1.0]).is_some());
}