diff --git a/crates/ui-egui/src/crop_image_tests.rs b/crates/ui-egui/src/crop_image_tests.rs index 9f802aee2..68ed04a81 100644 --- a/crates/ui-egui/src/crop_image_tests.rs +++ b/crates/ui-egui/src/crop_image_tests.rs @@ -193,3 +193,82 @@ fn crop_chosen_in_the_colour_picker_enters_crop_mode() { // 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:?}"); } + +/// The first paint, an image, of `id`'s fills (`fills`) or strokes, or of region `region` of a vector. +fn image_at(u: &Ui, id: NodeId, fills: bool, region: Option) -> figcraft_color::ImagePaint { + let n = u.app.session.doc().unwrap().node(id).unwrap(); + let list = match region { + Some(r) => match &n.kind { + figcraft_doc::NodeKind::Vector { network } => network.regions[r].fills.clone().unwrap(), + _ => panic!("not a vector"), + }, + None if fills => n.fills.clone(), + None => n.strokes.clone(), + }; + match &list[0].kind { + PaintKind::Image(img) => img.clone(), + k => panic!("not an image: {k:?}"), + } +} + +/// Crop chosen in the picker for a paint that has no crop mode on the canvas (#2150) — a stroke, +/// several layers' fills at once, or a region's fill in vector edit mode — only sets the paint's +/// mode: crop mode doesn't start and the picker stays open. Targets that aren't one layer's image +/// 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}; + 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| { + u.app.ui.picker = Some(target.clone()); + for _ in 0..4 { + u.frame(vec![]); + } + choose_mode(u, "Fill", "Crop"); + assert_eq!(u.app.ui.crop, None, "{what}: no crop mode"); + assert_eq!(u.app.ui.picker, Some(target), "{what}: the picker stays open"); + }; + + // The layer's image as its stroke: the stroke's mode is set, the fill is left alone. + let (mut u, layer, _) = setup(); + let paint = picture(&u, layer); + u.app.run("stroke.add", json!({"ids": [layer], "paint": paint})); + crop_only_sets_the_mode(&mut u, PickerTarget::Paint { fills: false, index: 0, ids: vec![layer] }, "a stroke"); + 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. + let (mut u, layer, rect) = setup(); + let paint = picture(&u, layer); + u.app.run("fill.set", json!({"ids": [rect], "index": 0, "paint": paint})); + u.app.run("select.set", json!({"ids": [layer, rect]})); + 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); + + // A region's own image fill in vector edit mode: the region's mode is set, the layer's image + // fill is untouched. + let (u0, layer, _) = setup(); + let paint = picture(&u0, layer); + let mut s = Session::new(); + let vector = crate::region_fill_rows_tests::two_region_vector(&mut s); + // The layer's own fill is an image too, so cropping it instead would be possible, and wrong. + s.execute("fill.set", &json!({"ids": [vector.0], "index": 0, "paint": paint})).unwrap(); + s.execute("vector.edit.begin", &json!({"id": vector.0})).unwrap(); + s.execute("vector.edit.select", &json!({"regions": [1]})).unwrap(); + s.execute("fill.set", &json!({"ids": [vector.0], "index": 0, "paint": paint})).unwrap(); + let layer_fills = s.doc().unwrap().node(vector).unwrap().fills.clone(); + let mut u = Ui::new(s); + 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 848a0c8d3..6aa0c4915 100644 --- a/crates/ui-egui/src/widgets/colorpicker.rs +++ b/crates/ui-egui/src/widgets/colorpicker.rs @@ -764,7 +764,7 @@ fn blend_row(ui: &mut Ui, app: &mut FigApp, target: &PickerTarget, paint: &mut P /// 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. -fn crop_on_canvas(app: &FigApp, target: &PickerTarget) -> Option<(NodeId, usize)> { +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() {