diff --git a/crates/ui-egui/src/canvas/document_display.rs b/crates/ui-egui/src/canvas/document_display.rs index 052581d13d..c108ed81a6 100644 --- a/crates/ui-egui/src/canvas/document_display.rs +++ b/crates/ui-egui/src/canvas/document_display.rs @@ -392,6 +392,12 @@ pub(crate) fn show(app: &mut PhotocraftApp, ui: &mut egui::Ui, view: &mut View, let r = frame.turned_bounds(); PixelRect::new(r.x0.floor() as i32, r.y0.floor() as i32, r.x1.ceil() as i32, r.y1.ceil() as i32) }); + // What a crop that grows the canvas adds to a Background previews in its fill colour, as on the monitor. + let region_fill = region.and_then(|_| crate::crop_ui::grown_fill(app, doc)).and_then(|c| { + let (display, _) = super::display::canvas_display(app, doc, output); + let one = photocraft_compose::Buffer::filled(DRect::from_xywh(0, 0, 1, 1), [c[0], c[1], c[2], 1.0]); + super::display::display_image(display.as_deref(), &one).pixels.first().copied() + }); let app: &PhotocraftApp = app; let (gpu, composite) = match prepared { Some(Prepared::Gpu(picture)) => (Some(picture), None), @@ -401,7 +407,7 @@ pub(crate) fn show(app: &mut PhotocraftApp, ui: &mut egui::Ui, view: &mut View, let overlay = overlay.map(|prep| prep.source(app)); let bounds = gpu.as_ref().map(GpuPicture::bounds).or_else(|| composite.as_ref().map(|c| c.bounds())).unwrap_or_else(|| doc.bounds()); let mapping = (bounds != doc.bounds()).then(|| placement(bounds)); - let frame = Frame { checker: checker.as_ref(), edge, mapping, pattern, region }; + let frame = Frame { checker: checker.as_ref(), edge, mapping, pattern, region, region_fill }; if let Some(picture) = &gpu { frame.apply(RasterDisplay::painted(label.clone(), size, picture.clone().into_paint())).picture().show_at(ui, body.rect); } else if let Some(composite) = &composite { @@ -578,6 +584,8 @@ struct Frame<'c> { mapping: Option, pattern: bool, region: Option, + /// The colour of the region past the document (a crop's grown Background), over the checkerboard. + region_fill: Option, } impl Frame<'_> { @@ -595,6 +603,10 @@ impl Frame<'_> { Some(region) => display.extent(Extent::Region(region)), None => display, }; + let display = match self.region_fill { + Some(fill) => display.region_fill(fill), + None => display, + }; if self.pattern { display.extent(Extent::Tiled) } else { display } } } diff --git a/crates/ui-egui/src/tools/crop_ui.rs b/crates/ui-egui/src/tools/crop_ui.rs index 1235377484..cda684c4de 100644 --- a/crates/ui-egui/src/tools/crop_ui.rs +++ b/crates/ui-egui/src/tools/crop_ui.rs @@ -230,9 +230,19 @@ pub fn frame(app: &PhotocraftApp) -> Option { app.canvas_tools.crop.tool.frame() } -/// The canvas shows the pixels past its edges under the shield. +/// The canvas shows the pixels past its edges under the shield. Default mode only: in Classic Mode the frame lies over +/// the pasteboard and the layers stop at the canvas edge, as in Photoshop. pub(crate) fn shows_beyond_canvas(app: &PhotocraftApp) -> bool { - app.ui.tool == Tool::Crop && app.canvas_tools.crop.tool.shows_beyond_canvas() + app.ui.tool == Tool::Crop && app.ui.tool_options.crop.image_mode() && app.canvas_tools.crop.tool.shows_beyond_canvas() +} + +/// The colour the area a crop adds past the canvas previews in (`shows_beyond_canvas`), as the commit fills it: the +/// background colour on a visible Background with Delete Cropped Pixels on and Fill: Background. `None` previews +/// transparency (Delete off turns the Background into a layer; Content-Aware Fill makes its own pixels). +pub(crate) fn grown_fill(app: &PhotocraftApp, doc: &photocraft_doc::Document) -> Option<[f32; 4]> { + let crop = &app.ui.tool_options.crop; + let fills = crop.delete_pixels && crop.fill == CropFill::Background && doc.background().is_some_and(|layer| layer.visible); + fills.then_some(app.session.tools.background) } /// Show Cropped Area is off with the Crop tool showing a frame: nothing outside it shows. diff --git a/crates/ui-egui/src/tools/crop_ui/tests.rs b/crates/ui-egui/src/tools/crop_ui/tests.rs index 95a1ea379f..11d8883160 100644 --- a/crates/ui-egui/src/tools/crop_ui/tests.rs +++ b/crates/ui-egui/src/tools/crop_ui/tests.rs @@ -417,3 +417,44 @@ fn drawing_a_new_crop_snaps_its_corner_not_the_default_frame() { assert!((frame.rect.x1 - 300.0).abs() < 1e-6, "classic {classic}: {frame:?}"); } } + +/// #16185: as in Photoshop, the pixels past the canvas show while a frame is edited in the default mode only; in Classic +/// Mode the frame lies over the pasteboard and the layers stop at the canvas edge. +#[test] +fn classic_mode_shows_nothing_past_the_canvas() { + let mut app = app(SampleType::U8); + drag(&mut app, &[[10.0, 10.0], [50.0, 40.0]], NONE); + assert!(tool(&mut app).shows_beyond_canvas(), "the drawn frame is being edited"); + assert!(!crop_ui::shows_beyond_canvas(&app), "Classic Mode: no preview past the canvas"); + app.ui.tool_options.crop.shield.classic_mode = false; + assert!(crop_ui::shows_beyond_canvas(&app), "default mode: the canvas shows past its edges"); +} + +/// #16185: the area a crop adds past the canvas previews in the colour the commit fills a Background's new area with, +/// and as transparency where the commit leaves it transparent. +#[test] +fn the_grown_area_previews_in_the_background_colour_the_crop_fills_it_with() { + let mut app = app(SampleType::U8); + app.ui.tool_options.crop.shield.classic_mode = false; + let red = [1.0, 0.0, 0.0, 1.0]; + app.session.tools.background = red; + let doc = (*app.session.active().unwrap().doc).clone(); + assert_eq!(crop_ui::grown_fill(&app, &doc), Some(red), "Delete Cropped Pixels on, Fill: Background"); + + app.ui.tool_options.crop.delete_pixels = false; + assert_eq!(crop_ui::grown_fill(&app, &doc), None, "Delete off turns the Background into a layer: transparency"); + app.ui.tool_options.crop.delete_pixels = true; + + app.ui.tool_options.crop.fill = CropFill::ContentAware; + assert_eq!(crop_ui::grown_fill(&app, &doc), None, "Content-Aware Fill makes its own pixels"); + app.ui.tool_options.crop.fill = CropFill::Background; + + let mut hidden = doc.clone(); + hidden.layers[0].visible = false; + assert_eq!(crop_ui::grown_fill(&app, &hidden), None, "a hidden Background shows nothing"); + + app.session.execute("layer.new.layerFromBackground", json!({})).unwrap(); + let layered = (*app.session.active().unwrap().doc).clone(); + assert!(layered.background().is_none()); + assert_eq!(crop_ui::grown_fill(&app, &layered), None, "no Background: the other layers stay transparent there"); +}