diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs index be80f832bbc..b1b787ac338 100644 --- a/crates/ui-egui/src/panels.rs +++ b/crates/ui-egui/src/panels.rs @@ -33,7 +33,7 @@ mod dock; pub mod histogram_panel; mod history; pub mod history_menu_ui; -mod info; +pub(crate) mod info; mod layer_drag; pub mod layer_filter_ui; pub mod layer_menu_ui; diff --git a/crates/ui-egui/src/panels/info.rs b/crates/ui-egui/src/panels/info.rs index 0be26c9d42c..f3a6572081e 100644 --- a/crates/ui-egui/src/panels/info.rs +++ b/crates/ui-egui/src/panels/info.rs @@ -23,6 +23,12 @@ pub(crate) fn pointer_sample(app: &mut PhotocraftApp, (x, y): (i32, i32)) -> Opt Some(v) } +/// The Info panel's W and H for a size of `wh` px (the selection's or the pending crop frame's): +/// both go through the ruler unit's formatter at `dpi`, percent of the document's `extent`. +pub(crate) fn wh_readout(ur: &photocraft_engine::prefs::UnitsAndRulers, dpi: f64, extent: [f64; 2], wh: [f64; 2]) -> [String; 2] { + [ur.format(wh[0], dpi, extent[0]), ur.format(wh[1], dpi, extent[1])] +} + /// Photoshop's Info panel: colour under the pointer (RGB and CMYK), position, selection size. pub(super) fn info_panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) { let t = Tokens::get(ui.ctx()); @@ -37,6 +43,7 @@ pub(super) fn info_panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) { let dpi = st.doc.resolution_dpi as f64; let fx = move |px: f64| units.format(px, dpi, size.width as f64); let units_y = app.session.prefs().units_and_rulers.clone(); + let units_wh = app.session.prefs().units_and_rulers.clone(); 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). @@ -80,8 +87,9 @@ pub(super) fn info_panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) { // Measured from the rulers' zero point (#2955). row(&mut cols[0], "X:", guide_x.map(|p| fx(p - origin[0])).or_else(|| pos.map(|p| fx(p.0 as f64 - origin[0]))).unwrap_or_else(dash)); 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]))); + let [w, h] = wh.map_or_else(|| [dash(), dash()], |s| wh_readout(&units_wh, dpi, [size.width as f64, size.height as f64], s)); + row(&mut cols[1], "W:", w); + row(&mut cols[1], "H:", h); if let Some(r) = ratio { cols[1].horizontal(|ui| { ui.label(RichText::new(format!("{}:", tl!("Ratio"))).color(t.text_dim).size(11.5)); diff --git a/crates/ui-egui/src/tools/crop_ui/tests.rs b/crates/ui-egui/src/tools/crop_ui/tests.rs index dbc55e6c147..c066aed788a 100644 --- a/crates/ui-egui/src/tools/crop_ui/tests.rs +++ b/crates/ui-egui/src/tools/crop_ui/tests.rs @@ -1179,3 +1179,72 @@ fn ratio_label_reduces_or_shows_a_decimal() { assert_eq!(ratio_label([0.0, 1.0]), None); assert!(ratio_label([f64::MAX, 1.0]).is_some()); } + +/// #3145 review: the ratio of a frame that is 16:9 only after rounding to whole px, of a frame +/// near 16:9 but not on it, and of degenerate frames (under a pixel, or a line). +#[test] +fn ratio_label_rounds_and_rejects_degenerate_frames() { + assert_eq!(ratio_label([1920.4, 1079.6]).as_deref(), Some("16:9"), "rounded to whole px first"); + assert_eq!(ratio_label([1366.0, 768.0]).as_deref(), Some("1.779:1"), "near 16:9 is not 16:9"); + assert_eq!(ratio_label([1.0, 1.0]).as_deref(), Some("1:1")); + assert_eq!(ratio_label([0.4, 100.0]), None, "under a pixel wide"); + assert_eq!(ratio_label([100.0, 0.0]), None, "a line"); + assert_eq!(ratio_label([-16.0, -9.0]), None, "negative"); + assert_eq!(ratio_label([f64::INFINITY, 9.0]), None); + // A frame `frame_size` floors to 1 px stays labelled, never a division by zero. + let tiny = frame_size([0.0, 0.0, 0.2, 0.4]).unwrap(); + assert_eq!(ratio_label(tiny).as_deref(), Some("1:1")); +} + +/// #3145 review: the crop frame's W and H use the Info panel's formatter for the ruler unit, +/// the same one the selection's go through. +#[test] +fn info_size_is_shown_in_the_ruler_unit() { + use crate::panels::info::wh_readout; + use photocraft_engine::prefs::{Unit, UnitsAndRulers}; + let mut app = app(SampleType::U8); + app.ui.crop_rect = Some([0.0, 0.0, 144.0, 72.0]); + let wh = info_size(&app).unwrap(); + let mut ur = UnitsAndRulers::default(); + let extent = [200.0, 100.0]; + ur.rulers = Unit::Pixels; + assert_eq!(wh_readout(&ur, 72.0, extent, wh), ["144", "72"]); + ur.rulers = Unit::Inches; + assert_eq!(wh_readout(&ur, 72.0, extent, wh), ["2", "1"]); + assert_eq!(wh_readout(&ur, 144.0, extent, wh), ["1", "0.5"], "at the document's resolution"); + ur.rulers = Unit::Centimeters; + assert_eq!(wh_readout(&ur, 72.0, extent, wh), ["5.08", "2.54"]); + ur.rulers = Unit::Percent; + assert_eq!(wh_readout(&ur, 72.0, extent, wh), ["72", "72"], "W of the width, H of the height"); +} + +/// #3145 review: after the crop is cancelled or committed, or the tool is left, W and H are the +/// selection's again. With the Crop tool still picked, the new default frame is the selection's +/// bounds (or the canvas), as in Photoshop, so the panel shows the same size. +#[test] +fn info_size_returns_to_the_selection_after_cancel_or_commit() { + let mut app = app(SampleType::U8); + app.run("select.rect", json!({"x": 20, "y": 10, "width": 80, "height": 40})).unwrap(); + // Cancel: no frame, then the new default frame frames the selection. + ensure_frame(&mut app); + app.ui.crop_rect = Some([10.0, 10.0, 50.0, 40.0]); + assert_eq!(info_size(&app), Some([40.0, 30.0])); + cancel(&mut app); + assert_eq!(info_size(&app), None, "cancelled: the panel falls back to the selection"); + ensure_frame(&mut app); + assert_eq!(info_size(&app), Some([80.0, 40.0]), "the new frame is the selection's bounds"); + // Commit: the document is cropped and the frame is gone. + app.run("select.deselect", json!({})).unwrap(); + cancel(&mut app); + drag(&mut app, &[[10.0, 10.0], [50.0, 40.0]], NONE); + assert_eq!(info_size(&app), Some([40.0, 30.0])); + crate::canvas::commit_crop(&mut app); + assert_eq!(app.session.active().unwrap().doc.size, Size::new(40, 30)); + assert_eq!(info_size(&app), None, "committed: no frame"); + ensure_frame(&mut app); + assert_eq!(info_size(&app), Some([40.0, 30.0]), "the new frame is the cropped canvas"); + // Leaving the tool: the panel shows the selection, whatever the frame. + app.ui.tool = Tool::Move; + ensure_frame(&mut app); + assert_eq!(info_size(&app), None); +}