From a0539b4c3e554f5f1b744e439b375d55e26fbdf7 Mon Sep 17 00:00:00 2001 From: FueryS Date: Fri, 20 Nov 2026 00:50:03 +0530 Subject: [PATCH] M1: image fill in Crop mode morphs and squishes on normal resize (#2499) Choosing Crop in the color picker's scale mode dropdown initializes the image's placement crop without closing the picker or entering interactive crop mode on the canvas. Resizing the layer normally without Cmd keeps the stored crop transform unchanged, stretching/squishing the image fill to fit the layer (Figma parity). Holding Cmd while resizing continues to crop the image without moving pixels. Tests: transform_behaviour_tests::crop_mode_image_resizing_without_cmd_morphs_and_with_cmd_crops, crop_image_tests. --- crates/engine/src/cmd/edit/props.rs | 29 ++++++++++ .../engine/src/transform_behaviour_tests.rs | 53 +++++++++++++++++++ crates/ui-egui/src/canvas/handles.rs | 4 +- crates/ui-egui/src/crop_image_tests.rs | 50 +++++------------ crates/ui-egui/src/widgets/colorpicker.rs | 44 ++++++++------- 5 files changed, 118 insertions(+), 62 deletions(-) diff --git a/crates/engine/src/cmd/edit/props.rs b/crates/engine/src/cmd/edit/props.rs index 5bd6710af..0706abcdf 100644 --- a/crates/engine/src/cmd/edit/props.rs +++ b/crates/engine/src/cmd/edit/props.rs @@ -445,6 +445,15 @@ fn paint_set_in(s: &mut Session, p: &Value, fills: bool, to_regions: bool) -> Re } }; if let Some((id, regions)) = fill_regions(s, cmd, p, &ids)?.filter(|_| fills && to_regions) { + let (nw, nh) = s.doc().ok().and_then(|d| d.node(id)).map_or((100.0, 100.0), |n| (n.width, n.height)); + let image_dims = s.doc().ok().and_then(|d| match &new_paint { + Some(figcraft_color::Paint { kind: figcraft_color::PaintKind::Image(img), .. }) + if img.scale_mode == figcraft_color::ImageScaleMode::Crop && img.crop.is_none() => + { + d.images.get(&img.image).map(|data| (f64::from(data.width), f64::from(data.height))) + } + _ => None, + }); edit_region_fills(s.doc_mut()?, id, ®ions, |list| { if list.is_empty() { list.push(figcraft_color::Paint::solid(figcraft_color::Color::DEFAULT_SHAPE)); @@ -452,10 +461,24 @@ fn paint_set_in(s: &mut Session, p: &Value, fills: bool, to_regions: bool) -> Re let at = idx.unwrap_or(list.len().saturating_sub(1)); let Some(slot) = list.get_mut(at) else { return Err(bad(cmd, format!("no paint at index {at}"))) }; edit_slot(slot); + if let Some((iw, ih)) = image_dims + && let figcraft_color::PaintKind::Image(img) = &mut slot.kind + && img.crop.is_none() + { + img.crop = Some(img.placement_crop(iw, ih, nw, nh)); + } Ok(()) })?; return ok(); } + let image_dims = s.doc().ok().and_then(|d| match &new_paint { + Some(figcraft_color::Paint { kind: figcraft_color::PaintKind::Image(img), .. }) + if img.scale_mode == figcraft_color::ImageScaleMode::Crop && img.crop.is_none() => + { + d.images.get(&img.image).map(|data| (f64::from(data.width), f64::from(data.height))) + } + _ => None, + }); let doc = s.doc_mut()?; for i in ids { let n = doc.get_mut(i)?; @@ -470,6 +493,12 @@ fn paint_set_in(s: &mut Session, p: &Value, fills: bool, to_regions: bool) -> Re let Some(slot) = list.get_mut(at) else { return Err(bad(cmd, format!("no paint at index {at}"))) }; let old = slot.clone(); edit_slot(slot); + if let Some((iw, ih)) = image_dims + && let figcraft_color::PaintKind::Image(img) = &mut slot.kind + && img.crop.is_none() + { + img.crop = Some(img.placement_crop(iw, ih, n.width, n.height)); + } crate::cmd::lists::rebind_edited_paint(n, fills, at, &old); if fills { n.styles.fill = None; diff --git a/crates/engine/src/transform_behaviour_tests.rs b/crates/engine/src/transform_behaviour_tests.rs index 3c37bf6c0..06bdb2a50 100644 --- a/crates/engine/src/transform_behaviour_tests.rs +++ b/crates/engine/src/transform_behaviour_tests.rs @@ -217,6 +217,59 @@ fn ordinary_resize_and_scale_keep_image_fill_settings() { } } +#[test] +fn crop_mode_image_resizing_without_cmd_morphs_and_with_cmd_crops() { + use figcraft_doc::color::{ImageScaleMode, PaintKind}; + let mut s = session(); + let layer = image_layer(&mut s); + // Set the image fill mode to Crop without an explicit crop: fill.set initializes placement crop. + let mut fills = s.doc().unwrap().node(layer).unwrap().fills.clone(); + let PaintKind::Image(img) = &mut fills[0].kind else { panic!("image fill") }; + img.scale_mode = ImageScaleMode::Crop; + img.crop = None; + ex(&mut s, "fill.set", json!({"ids": [layer], "index": 0, "paint": fills[0]})); + + // Verify fill.set initialized img.crop with placement crop. + let initial_node = s.doc().unwrap().node(layer).unwrap().clone(); + let PaintKind::Image(img) = &initial_node.fills[0].kind else { panic!("image fill") }; + let Some(initial_crop) = img.crop else { panic!("crop initialized") }; + + let before = figcraft_render::render_node(s.doc().unwrap(), layer, 1.0).unwrap(); + assert_eq!((before.width(), before.height()), (200, 100)); + + // 1. Resize without Cmd (normal resize from bottom edge: 100 -> 50). + // The layer height shrinks to 50, but img.crop stays unchanged, stretching/squishing the image. + s.pointer(&ev(PointerKind::Down, Point::new(100.0, 100.0), NONE)).unwrap(); + s.pointer(&ev(PointerKind::Move, Point::new(100.0, 50.0), NONE)).unwrap(); + s.pointer(&ev(PointerKind::Up, Point::new(100.0, 50.0), NONE)).unwrap(); + assert_eq!(size(&s, layer), (200.0, 50.0)); + + let n = s.doc().unwrap().node(layer).unwrap().clone(); + let PaintKind::Image(img) = &n.fills[0].kind else { panic!("image fill") }; + assert_eq!(img.crop, Some(initial_crop), "normal resize leaves crop transform unchanged"); + + let morphed = figcraft_render::render_node(s.doc().unwrap(), layer, 1.0).unwrap(); + assert_eq!((morphed.width(), morphed.height()), (200, 50)); + + // 2. Resize with Cmd from right edge: 200 -> 100. + // The layer width shrinks to 100, and img.crop is updated so the pixels stay stationary on page (cropping). + s.pointer(&ev(PointerKind::Down, Point::new(200.0, 25.0), CMD)).unwrap(); + s.pointer(&ev(PointerKind::Move, Point::new(100.0, 25.0), CMD)).unwrap(); + s.pointer(&ev(PointerKind::Up, Point::new(100.0, 25.0), CMD)).unwrap(); + assert_eq!(size(&s, layer), (100.0, 50.0)); + + let n = s.doc().unwrap().node(layer).unwrap().clone(); + let PaintKind::Image(img) = &n.fills[0].kind else { panic!("image fill") }; + assert_ne!(img.crop, Some(initial_crop), "Cmd resize updates crop transform"); + + let cropped = figcraft_render::render_node(s.doc().unwrap(), layer, 1.0).unwrap(); + assert_eq!((cropped.width(), cropped.height()), (100, 50)); + // The remaining 100x50 pixels match the top-left 100x50 of morphed (pixels stayed still in layer space). + for y in 0..50usize { + assert_eq!(&cropped.data()[y * 400..(y + 1) * 400], &morphed.data()[y * 800..y * 800 + 400], "row {y}: stationary cropped pixels"); + } +} + #[test] fn command_crop_preserves_each_image_fill_and_leaves_tiles() { use figcraft_doc::color::{Color, ImageScaleMode, Paint, PaintKind}; diff --git a/crates/ui-egui/src/canvas/handles.rs b/crates/ui-egui/src/canvas/handles.rs index 0a300c279..b53ce3bdd 100644 --- a/crates/ui-egui/src/canvas/handles.rs +++ b/crates/ui-egui/src/canvas/handles.rs @@ -697,8 +697,8 @@ pub fn input(app: &mut FigApp, ctx: &egui::Context, pos: Pos2) -> Input { } } if let Some((id, fills, index, _, rect)) = crop_target(app) { - // Crop mode: the layer's own resize handles crop it (the pointer events carry Cmd/Ctrl). - if app.ui.crop.is_some() && on_resize_handle(app, pos) { + // The layer's own resize handles resize/crop the layer, not the crop handles. + if on_resize_handle(app, pos) { return Input::Ignored; } let Some(c) = crop_corners(app, id, rect) else { return Input::Ignored }; diff --git a/crates/ui-egui/src/crop_image_tests.rs b/crates/ui-egui/src/crop_image_tests.rs index 05435d856..16e762cad 100644 --- a/crates/ui-egui/src/crop_image_tests.rs +++ b/crates/ui-egui/src/crop_image_tests.rs @@ -141,11 +141,11 @@ fn choose_mode(u: &mut Ui, shown: &str, item: &str) { u.frame(vec![]); } -/// Crop chosen as the image fill's mode in the colour picker enters crop mode, as Crop image does -/// (#2150): the picker closes, the picture keeps its place, and a corner handle then crops it -/// with the layer still selected. The other modes only set the mode. +/// Crop chosen as the image fill's mode in the colour picker sets the mode with a placement crop +/// (#2499): the picker stays open, crop mode on the canvas does not start, and dragging a corner +/// handle without Cmd resizes the layer and morphs/squishes the image. #[test] -fn crop_chosen_in_the_colour_picker_enters_crop_mode() { +fn crop_chosen_in_the_colour_picker_sets_mode_and_morphs_on_resize() { let (mut u, layer, _) = setup(); u.app.ui.picker = Some(crate::colorpicker::PickerTarget::Paint { fills: true, index: 0, ids: vec![layer] }); for _ in 0..4 { @@ -155,8 +155,8 @@ fn crop_chosen_in_the_colour_picker_enters_crop_mode() { assert_eq!(mode(&u, layer), (ImageScaleMode::Fit, None)); assert!(u.app.ui.picker.is_some() && u.app.ui.crop.is_none(), "Fit is only a mode"); choose_mode(&mut u, "Fit", "Crop"); - assert_eq!(u.app.ui.crop, Some((layer, 0)), "crop mode on the image fill"); - assert!(u.app.ui.picker.is_none(), "crop mode takes the canvas from the picker"); + assert_eq!(u.app.ui.crop, None, "Crop in picker does not enter canvas crop mode"); + assert!(u.app.ui.picker.is_some(), "picker stays open"); // The picture keeps its place: the crop is the one Crop image makes of the fitted image. let (m, Some(before)) = mode(&u, layer) else { panic!("a crop where the picture was") }; assert_eq!(m, ImageScaleMode::Crop); @@ -169,29 +169,18 @@ fn crop_chosen_in_the_colour_picker_enters_crop_mode() { assert_eq!(mode(&menu, other), (ImageScaleMode::Fit, None)); menu.app.run("image.crop", json!({})); assert_eq!(mode(&menu, other), (ImageScaleMode::Crop, Some(before))); - // The bottom right corner, dragged in, crops the picture to the top left 3/4 of the layer. + // Close the picker to drag the canvas resize handles. + u.app.ui.picker = None; + u.frame(vec![]); + // Dragging the bottom right corner without Cmd resizes the layer to 150x75 and morphs/squishes the image. let (from, to) = (u.app.to_screen(200.0, 100.0), u.app.to_screen(150.0, 75.0)); u.drag_to(from, to); u.button(to, false); assert_eq!(u.app.session.selection(), vec![layer], "the layer stays selected"); - assert_eq!(u.app.ui.crop, Some((layer, 0)), "still cropping"); let n = u.app.session.doc().unwrap().node(layer).unwrap().clone(); assert!((n.width - 150.0).abs() < 1e-6 && (n.height - 75.0).abs() < 1e-6, "{} x {}", n.width, n.height); let (_, Some(after)) = mode(&u, layer) else { panic!("a crop") }; - assert!((after[0] - before[0] * 0.75).abs() < 1e-9 && (after[3] - before[3] * 0.75).abs() < 1e-9, "{before:?} -> {after:?}"); - assert!((after[4] - before[4]).abs() < 1e-9 && (after[5] - before[5]).abs() < 1e-9, "the top left stays: {before:?} -> {after:?}"); - // The picture's own corner, now outside the layer at (200, 100), scales the picture inside the - // layer, which keeps its size and its selection. - let (from, to) = (u.app.to_screen(200.0, 100.0), u.app.to_screen(240.0, 120.0)); - u.drag_to(from, to); - u.button(to, false); - assert_eq!(u.app.session.selection(), vec![layer], "the layer stays selected"); - assert_eq!(u.app.ui.crop, Some((layer, 0)), "still cropping"); - let n = u.app.session.doc().unwrap().node(layer).unwrap().clone(); - assert!((n.width - 150.0).abs() < 1e-6 && (n.height - 75.0).abs() < 1e-6, "the layer keeps its size: {} x {}", n.width, n.height); - let (_, Some(scaled)) = mode(&u, layer) else { panic!("a crop") }; - // The picture grew from 200 x 100 to 240 x 120: the layer shows 5/6 of what it showed. - assert!((scaled[0] - after[0] * 5.0 / 6.0).abs() < 1e-6 && (scaled[3] - after[3] * 5.0 / 6.0).abs() < 1e-6, "{after:?} -> {scaled:?}"); + assert_eq!(after, before, "normal resize without Cmd leaves crop transform unchanged to morph/squish"); } /// The reporter's sequence (#2307): the colour picker open on an image fill held in Crop mode @@ -206,12 +195,7 @@ fn crop_from_the_colour_picker_then_a_drag_on_the_image_moves_the_crop_not_the_l u.frame(vec![]); } choose_mode(&mut u, "Fill", "Crop"); - // Crop mode on the canvas ended (a click away from the image); the picker opens on the Crop fill. - u.app.ui.crop = None; - u.app.ui.picker = Some(crate::colorpicker::PickerTarget::Paint { fills: true, index: 0, ids: vec![layer] }); - for _ in 0..4 { - u.frame(vec![]); - } + assert!(u.app.ui.picker.is_some()); let (_, Some(before)) = mode(&u, layer) else { panic!("a crop") }; let at = u.app.session.doc().unwrap().node(layer).unwrap().transform.as_coeffs(); let from = u.app.to_screen(100.0, 50.0); @@ -267,7 +251,7 @@ fn image_at(u: &Ui, id: NodeId, fills: bool, region: Option) -> figcraft_ /// fill have nothing for Crop image to crop either. #[test] fn crop_in_the_colour_picker_only_sets_the_mode_where_crop_image_does_not_apply() { - use crate::colorpicker::{PickerTarget, crop_on_canvas}; + use crate::colorpicker::PickerTarget; let picture = |u: &Ui, id: NodeId| u.app.session.doc().unwrap().node(id).unwrap().fills[0].clone(); // Opens the picker on `target`, chooses Crop and checks crop mode stays off. let crop_only_sets_the_mode = |u: &mut Ui, target: PickerTarget, what: &str| { @@ -313,12 +297,4 @@ fn crop_in_the_colour_picker_only_sets_the_mode_where_crop_image_does_not_apply( crop_only_sets_the_mode(&mut u, PickerTarget::Paint { fills: true, index: 0, ids: vec![vector] }, "a region"); assert_eq!(image_at(&u, vector, true, Some(1)).scale_mode, ImageScaleMode::Crop); assert_eq!(u.app.session.doc().unwrap().node(vector).unwrap().fills, layer_fills, "the layer's fills are untouched"); - - // A fill that isn't an image, and targets that aren't a layer's paint, have no crop on the canvas. - let (u, layer, rect) = setup(); - assert_eq!(crop_on_canvas(&u.app, &PickerTarget::Paint { fills: true, index: 0, ids: vec![layer] }), Some((layer, 0))); - assert_eq!(crop_on_canvas(&u.app, &PickerTarget::Paint { fills: true, index: 0, ids: vec![rect] }), None, "a solid fill"); - assert_eq!(crop_on_canvas(&u.app, &PickerTarget::Paint { fills: true, index: 1, ids: vec![layer] }), None, "no fill there"); - assert_eq!(crop_on_canvas(&u.app, &PickerTarget::PageBackground), None); - assert_eq!(crop_on_canvas(&u.app, &PickerTarget::Style { id: "S:1".into(), index: 0 }), None); } diff --git a/crates/ui-egui/src/widgets/colorpicker.rs b/crates/ui-egui/src/widgets/colorpicker.rs index 662e4ba24..2d9119d93 100644 --- a/crates/ui-egui/src/widgets/colorpicker.rs +++ b/crates/ui-egui/src/widgets/colorpicker.rs @@ -760,19 +760,19 @@ fn blend_row(ui: &mut Ui, app: &mut FigApp, target: &PickerTarget, paint: &mut P }); } -/// The layer and fill index to crop on the canvas (`image.crop`) when Crop is chosen for `target`: -/// one layer's image fill as the Fill section shows it (a text's characters' shared fills -/// included). Strokes, several layers at once and the regions selected in vector edit mode have -/// no crop mode, so for them Crop only sets the paint's mode. -pub(crate) fn crop_on_canvas(app: &FigApp, target: &PickerTarget) -> Option<(NodeId, usize)> { - let PickerTarget::Paint { fills: true, index, ids } = target else { return None }; - let [id] = ids[..] else { return None }; - if figcraft_engine::vectoredit::selected_region_fills(&app.session, id).is_some() { - return None; - } +fn target_node_size(app: &FigApp, target: &PickerTarget) -> Option<(f64, f64)> { let doc = app.session.doc().ok()?; - let shown = figcraft_engine::cmd::lists::character_fills(doc.node(id)?)?; - matches!(shown.get(*index)?.kind, PaintKind::Image(_)).then_some((id, *index)) + match target { + PickerTarget::Paint { ids, .. } | PickerTarget::Effect { ids, .. } => { + let n = doc.node(*ids.first()?)?; + Some((n.width, n.height)) + } + PickerTarget::TextRange { id, .. } => { + let n = doc.node(*id)?; + Some((n.width, n.height)) + } + _ => None, + } } /// The image paint's scale mode, rotation, colour profile, presets and filter sliders. @@ -788,19 +788,17 @@ fn image_controls(app: &mut FigApp, ui: &mut Ui, target: &PickerTarget, paint: & (tr("Tile"), ImageScaleMode::Tile), ] { if crate::right::menu_item(ui, l, "") { - match crop_on_canvas(app, target).filter(|_| m == ImageScaleMode::Crop) { - // Crop for a layer's own image fill is Crop image (#2150): the picture keeps - // its place and crop mode takes the canvas, closing the picker, so its - // handles crop it as they do when crop mode comes from the menu. - Some((id, index)) => { - app.run("image.crop", json!({"ids": [id], "index": index})); - } - None => { - img.scale_mode = m; - paint.kind = PaintKind::Image(img.clone()); - write(app, target, paint, false); + if m == ImageScaleMode::Crop && img.crop.is_none() { + let size = target_node_size(app, target); + let doc = app.session.doc().ok(); + let dims = doc.as_ref().and_then(|d| d.images.get(&img.image)).map(|d| (f64::from(d.width), f64::from(d.height))); + if let (Some((w, h)), Some((iw, ih))) = (size, dims) { + img.crop = Some(img.placement_crop(iw, ih, w, h)); } } + img.scale_mode = m; + paint.kind = PaintKind::Image(img.clone()); + write(app, target, paint, false); ui.close(); } }