picker: remove picker-side crop init and compute placement per node (#2499)

Remove picker-side crop initialization when selecting Crop mode in the
color picker, and instead let the engine compute the initial placement
crop per node using each selected layer's own dimensions. This avoids
stretching images when multiple differently sized layers are selected
at once.
This commit is contained in:
FueryS committed 2026-11-21 22:21:14 +05:30
1 parent a0539b4c3e
commit cfef5763f4
4 files changed
+112 -34

No files matched your search

+41 -10
View File
@@ -448,7 +448,7 @@ fn paint_set_in(s: &mut Session, p: &Value, fills: bool, to_regions: bool) -> Re
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() =>
if img.scale_mode == figcraft_color::ImageScaleMode::Crop =>
{
d.images.get(&img.image).map(|data| (f64::from(data.width), f64::from(data.height)))
}
@@ -460,28 +460,43 @@ 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}"))) };
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.scale_mode == figcraft_color::ImageScaleMode::Crop
{
img.crop = Some(img.placement_crop(iw, ih, nw, nh));
match &old.kind {
figcraft_color::PaintKind::Image(old_img) if old_img.image == img.image => {
if old_img.rotation != img.rotation {
img.crop = None;
img.crop = Some(img.placement_crop(iw, ih, nw, nh));
} else if img.crop.is_none() {
img.crop = Some(old_img.placement_crop(iw, ih, nw, nh));
} else if regions.len() > 1 && old_img.scale_mode == figcraft_color::ImageScaleMode::Crop {
img.crop = old_img.crop;
}
}
_ => {
if 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() =>
{
Some(figcraft_color::Paint { kind: figcraft_color::PaintKind::Image(img), .. }) if img.scale_mode == figcraft_color::ImageScaleMode::Crop => {
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)?;
for i in &ids {
let n = doc.get_mut(*i)?;
if fills {
crate::cmd::lists::take_character_fills(n);
}
@@ -495,9 +510,25 @@ fn paint_set_in(s: &mut Session, p: &Value, fills: bool, to_regions: bool) -> Re
edit_slot(slot);
if let Some((iw, ih)) = image_dims
&& let figcraft_color::PaintKind::Image(img) = &mut slot.kind
&& img.crop.is_none()
&& img.scale_mode == figcraft_color::ImageScaleMode::Crop
{
img.crop = Some(img.placement_crop(iw, ih, n.width, n.height));
match &old.kind {
figcraft_color::PaintKind::Image(old_img) if old_img.image == img.image => {
if old_img.rotation != img.rotation {
img.crop = None;
img.crop = Some(img.placement_crop(iw, ih, n.width, n.height));
} else if img.crop.is_none() {
img.crop = Some(old_img.placement_crop(iw, ih, n.width, n.height));
} else if ids.len() > 1 && old_img.scale_mode == figcraft_color::ImageScaleMode::Crop {
img.crop = old_img.crop;
}
}
_ => {
if 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 {
@@ -270,6 +270,71 @@ fn crop_mode_image_resizing_without_cmd_morphs_and_with_cmd_crops() {
}
}
#[test]
fn multi_layer_crop_mode_selection_computes_placement_per_node() {
use figcraft_doc::color::{ImageScaleMode, PaintKind};
let mut s = session();
let layer1 = image_layer(&mut s); // 200 x 100
let layer2 = id(&ex(&mut s, "shape.rectangle", json!({"x": 300, "y": 0, "width": 100, "height": 100})));
// Choose Crop mode on both layers simultaneously via fill.set with crop: None (as the picker does).
let mut fill = s.doc().unwrap().node(layer1).unwrap().fills[0].clone();
let PaintKind::Image(img) = &mut fill.kind else { panic!("image fill") };
img.scale_mode = ImageScaleMode::Crop;
img.crop = None;
ex(&mut s, "fill.set", json!({"ids": [layer1, layer2], "index": 0, "paint": fill}));
// Verify both layers received crop transforms tailored to their own dimensions, not stretched.
let n1 = s.doc().unwrap().node(layer1).unwrap().clone();
let n2 = s.doc().unwrap().node(layer2).unwrap().clone();
let PaintKind::Image(img1) = &n1.fills[0].kind else { panic!("image fill") };
let PaintKind::Image(img2) = &n2.fills[0].kind else { panic!("image fill") };
let crop1 = img1.crop.expect("layer1 crop initialized");
let crop2 = img2.crop.expect("layer2 crop initialized");
// layer1 (200x100) and layer2 (100x100) have different aspect ratios, so their crop transforms must differ.
assert_ne!(crop1, crop2, "differently sized layers must have distinct crop transforms");
// Both should match expected placement crop for their respective dimensions (iw=200, ih=100 from image_layer):
assert_eq!(crop1, [1.0, 0.0, 0.0, 1.0, 0.0, 0.0]);
assert_eq!(crop2, [0.5, 0.0, 0.0, 1.0, 0.25, 0.0]);
// Rotate 90° on the multi-selection (as the picker's Rotate button does):
let mut rot_fill = n1.fills[0].clone();
let PaintKind::Image(img) = &mut rot_fill.kind else { panic!("image fill") };
img.rotation = 1;
ex(&mut s, "fill.set", json!({"ids": [layer1, layer2], "index": 0, "paint": rot_fill}));
let n1_rot = s.doc().unwrap().node(layer1).unwrap().clone();
let n2_rot = s.doc().unwrap().node(layer2).unwrap().clone();
let PaintKind::Image(img1_rot) = &n1_rot.fills[0].kind else { panic!("image fill") };
let PaintKind::Image(img2_rot) = &n2_rot.fills[0].kind else { panic!("image fill") };
let crop1_rot = img1_rot.crop.expect("layer1 rotated crop initialized");
let crop2_rot = img2_rot.crop.expect("layer2 rotated crop initialized");
assert_ne!(crop1_rot, crop2_rot, "differently sized layers must have distinct rotated crop transforms");
assert_eq!(crop1_rot, [1.0, 0.0, 0.0, 0.25, 0.0, 0.375]);
assert_eq!(crop2_rot, [1.0, 0.0, 0.0, 0.5, 0.0, 0.25]);
// Shared filter edit on multi-selection in Crop mode (e.g. Exposure slider):
let mut filter_fill = n1_rot.fills[0].clone();
let PaintKind::Image(img) = &mut filter_fill.kind else { panic!("image fill") };
img.filters.exposure = 0.5;
ex(&mut s, "fill.set", json!({"ids": [layer1, layer2], "index": 0, "paint": filter_fill}));
let n1_filt = s.doc().unwrap().node(layer1).unwrap().clone();
let n2_filt = s.doc().unwrap().node(layer2).unwrap().clone();
let PaintKind::Image(img1_filt) = &n1_filt.fills[0].kind else { panic!("image fill") };
let PaintKind::Image(img2_filt) = &n2_filt.fills[0].kind else { panic!("image fill") };
// Both layers keep their distinct crop transforms; neither is overwritten by the other.
assert_eq!(img1_filt.crop, Some(crop1_rot));
assert_eq!(img2_filt.crop, Some(crop2_rot));
assert_eq!(img1_filt.filters.exposure, 0.5);
assert_eq!(img2_filt.filters.exposure, 0.5);
}
#[test]
fn command_crop_preserves_each_image_fill_and_leaves_tiles() {
use figcraft_doc::color::{Color, ImageScaleMode, Paint, PaintKind};
+3 -1
View File
@@ -272,7 +272,7 @@ fn crop_in_the_colour_picker_only_sets_the_mode_where_crop_image_does_not_apply(
assert_eq!(image_at(&u, layer, false, None).scale_mode, ImageScaleMode::Crop);
assert_eq!(mode(&u, layer), (ImageScaleMode::Fill, None), "the fill is untouched");
// Two image layers at once: both take the mode.
// Two image layers at once: both take the mode, with each layer's crop sized to its own dimensions.
let (mut u, layer, rect) = setup();
let paint = picture(&u, layer);
u.app.run("fill.set", json!({"ids": [rect], "index": 0, "paint": paint}));
@@ -280,6 +280,8 @@ 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![layer, rect] }, "two layers");
assert_eq!(mode(&u, layer).0, ImageScaleMode::Crop);
assert_eq!(mode(&u, rect).0, ImageScaleMode::Crop);
let (crop1, crop2) = (mode(&u, layer).1.unwrap(), mode(&u, rect).1.unwrap());
assert_ne!(crop1, crop2, "differently sized layers must have distinct placement crops without stretching");
// A region's own image fill in vector edit mode: the region's mode is set, the layer's image
// fill is untouched.
+3 -23
View File
@@ -760,21 +760,6 @@ fn blend_row(ui: &mut Ui, app: &mut FigApp, target: &PickerTarget, paint: &mut P
});
}
fn target_node_size(app: &FigApp, target: &PickerTarget) -> Option<(f64, f64)> {
let doc = app.session.doc().ok()?;
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.
fn image_controls(app: &mut FigApp, ui: &mut Ui, target: &PickerTarget, paint: &mut Paint, mut img: ImagePaint, t: &Tokens) {
ui.horizontal(|ui| {
@@ -788,14 +773,6 @@ fn image_controls(app: &mut FigApp, ui: &mut Ui, target: &PickerTarget, paint: &
(tr("Tile"), ImageScaleMode::Tile),
] {
if crate::right::menu_item(ui, l, "") {
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);
@@ -805,6 +782,9 @@ fn image_controls(app: &mut FigApp, ui: &mut Ui, target: &PickerTarget, paint: &
});
if icons::button(ui, Icon::Rotate, 24.0, false, tr("Rotate 90°")).clicked() {
img.rotation = (img.rotation + 1) % 4;
if img.scale_mode == ImageScaleMode::Crop {
img.crop = None;
}
paint.kind = PaintKind::Image(img.clone());
write(app, target, paint, false);
}