diff --git a/crates/ui-egui/src/dialogs/sizing.rs b/crates/ui-egui/src/dialogs/sizing.rs index 360a2b75157..3f05834a888 100644 --- a/crates/ui-egui/src/dialogs/sizing.rs +++ b/crates/ui-egui/src/dialogs/sizing.rs @@ -13,8 +13,9 @@ use crate::theme::Tokens; const UNITS: [(&str, &str); 7] = [("px", "Pixels"), ("percent", "Percent"), ("in", "Inches"), ("cm", "Centimeters"), ("mm", "Millimeters"), ("pt", "Points"), ("pica", "Picas")]; -/// Width-only unit: columns of Preferences › Units & Rulers › Column Size, as in Photoshop's -/// Image Size and Canvas Size (height has no Columns). +/// Width-only unit of Image Size and Canvas Size (height has no Columns). Image Size measures it +/// in columns of Preferences › Units & Rulers › Column Size; Canvas Size in whole inches (see +/// [`canvas_columns_to_px`]). const COLUMNS: (&str, &str) = ("col", "Columns"); /// Decimal places a Columns field shows. @@ -50,6 +51,18 @@ fn columns_to_px(n: f64, res: f64, ppi: f64, (w, g): (f64, f64), relative: bool) pt / ppi * res.max(1.0) } +/// Canvas Size's Columns, measured in the reference application: one column is one inch at the +/// document resolution, whatever Column Size says, and only whole columns count (1.5 is 2; a +/// 1001 px canvas at 300 ppi shows as 3). Relative deltas the same way. +fn canvas_columns_to_px(n: f64, res: f64) -> f64 { + n.round() * res.max(1.0) +} + +/// Pixels → Canvas Size columns, rounded for display; the inverse of [`canvas_columns_to_px`]. +fn canvas_px_to_columns(px: f64, res: f64) -> f64 { + (px / res.max(1.0)).round() +} + /// Dialog unit for the Units & Rulers preference. fn pref_unit(u: photocraft_engine::prefs::Unit) -> &'static str { use photocraft_engine::prefs::Unit; @@ -221,6 +234,16 @@ fn dim_field(ui: &mut egui::Ui, f: &mut Map, key: &str, orig_key: let orig = num(f, orig_key); let px = num(f, key); let ppi = pt_per_in(f); + if unit == COLUMNS.0 && f.get("__command").and_then(Value::as_str) == Some("image.canvasSize") { + let mut v = canvas_px_to_columns(px, res) as f32; + let lim = (300000.0 / res.max(1.0)).ceil() as f32; + let changed = crate::widgets::value_field_prec(ui, &mut v, -lim..=lim, "", 90.0, 0).accessible_name(tl!("Width")).changed(); + if changed { + f.insert(key.into(), json!(canvas_columns_to_px(v as f64, res))); + } + let _ = id; + return changed; + } if unit == COLUMNS.0 { let cols = column_size(f); let relative = f.get("relative").and_then(Value::as_bool).unwrap_or(false); diff --git a/crates/ui-egui/src/dialogs/sizing/tests.rs b/crates/ui-egui/src/dialogs/sizing/tests.rs index b6022a57c89..bb7ba25e82a 100644 --- a/crates/ui-egui/src/dialogs/sizing/tests.rs +++ b/crates/ui-egui/src/dialogs/sizing/tests.rs @@ -112,7 +112,7 @@ fn columns_unit_is_width_only_and_follows_the_preference() { /// #6214: 2 columns typed into Image Size's Width at 300 ppi resize to 1550 px wide (the /// default 180 pt columns with 12 pt gutters), Height following the proportions; Canvas Size -/// takes the same width. +/// counts whole inches, so its 2 columns are 600 px. #[test] fn typed_columns_resize_the_document() { use egui_kittest::Harness; @@ -126,13 +126,48 @@ fn typed_columns_resize_the_document() { h.run_steps(3); type_width(&mut h, "2"); let f = fields(&h); - assert!((num(&f, "width") - 1550.0).abs() < 1e-6, "{command}: {f:?}"); + let px = if command == "image.imageSize" { 1550.0 } else { 600.0 }; + assert!((num(&f, "width") - px).abs() < 1e-6, "{command}: {f:?}"); crate::dialogs::confirm(h.state_mut(), id).unwrap(); - let want = if command == "image.imageSize" { photocraft_doc::Size::new(1550, 775) } else { photocraft_doc::Size::new(1550, 500) }; + let want = if command == "image.imageSize" { photocraft_doc::Size::new(1550, 775) } else { photocraft_doc::Size::new(600, 500) }; assert_eq!(h.state().session.active().unwrap().doc.size, want, "{command}"); } } +/// Canvas Size's Columns as measured in the reference application: a column is one inch at the +/// document resolution whatever Column Size says, and only whole columns count. At 300 ppi with +/// Column Size 360 pt / 24 pt: a 1001 px canvas shows as 3 columns, typed 1.5 is 2 columns +/// (600 px), and Relative +1 column adds 300 px. +#[test] +fn canvas_size_columns_are_whole_inches() { + use egui_kittest::Harness; + assert_eq!(canvas_px_to_columns(1001.0, 300.0), 3.0); + assert_eq!(canvas_px_to_columns(1001.0, 72.0), 14.0); + assert_eq!(canvas_columns_to_px(1.5, 72.0), 144.0); + assert_eq!(canvas_columns_to_px(-1.0, 300.0), -300.0); + assert!(canvas_px_to_columns(1000.0, 0.0).is_finite()); + for (relative, typed, want) in [(false, "1.5", 600), (true, "1", 1301)] { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.session.execute("file.new", json!({"width": 1001, "height": 500, "resolution": 300})).unwrap(); + app.session.execute("prefs.set", json!({"path": "unitsAndRulers.columnWidth", "value": 360})).unwrap(); + app.session.execute("prefs.set", json!({"path": "unitsAndRulers.gutter", "value": 24})).unwrap(); + let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_ui_state(|ui, app| crate::dialogs::show(app, ui.ctx()), app); + PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]); + let id = open(h.state_mut(), "image.canvasSize").unwrap(); + let f = &mut h.state_mut().ui.dialog_mut(id).unwrap().fields; + set_unit(f, "width", "col"); + if relative { + f.insert("relative".into(), json!(true)); + f.insert("width".into(), json!(0.0)); + f.insert("height".into(), json!(0.0)); + } + h.run_steps(3); + type_width(&mut h, typed); + crate::dialogs::confirm(h.state_mut(), id).unwrap(); + assert_eq!(h.state().session.active().unwrap().doc.size, photocraft_doc::Size::new(want, 500), "relative {relative}"); + } +} + #[test] fn dialog_maps_to_engine_command() { let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());