From d89a8856cc58d936fd34c592b97b2608ea89bcbc Mon Sep 17 00:00:00 2001 From: David Mann Date: Wed, 2 Dec 2026 05:41:35 +0100 Subject: [PATCH] Crop: Fill goes back to its default when the active layer cannot use the synthesised fill (#17687) With Fill set to the synthesised fill, making a smart object, an adjustment, type or shape layer, or the Background among other layers active puts Fill back on the default entry, and it stays there when a pixel layer is active again. Several selected layers keep today's behaviour. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/app/commands.rs | 5 + crates/ui-egui/src/tools.rs | 2 + .../src/tools/crop_fill_layer_switch_tests.rs | 102 ++++++++++++++++++ crates/ui-egui/src/tools/crop_ui.rs | 13 +++ 4 files changed, 122 insertions(+) create mode 100644 crates/ui-egui/src/tools/crop_fill_layer_switch_tests.rs diff --git a/crates/ui-egui/src/app/commands.rs b/crates/ui-egui/src/app/commands.rs index 9222714853..d92a482738 100644 --- a/crates/ui-egui/src/app/commands.rs +++ b/crates/ui-egui/src/app/commands.rs @@ -366,6 +366,11 @@ impl PhotocraftApp { { self.ui.tool_options.crop.fill = craft::draw::ui::crop::CropFill::Background; } + // So does Content-Aware Fill once a layer that can't use it is active (a smart object, a type layer, the + // Background among other layers); it stays on the default after a pixel layer is active again (#17687). + if self.ui.tool_options.crop.fill == craft::draw::ui::crop::CropFill::ContentAware && crate::crop_ui::content_aware_fill_greyed(self) { + self.ui.tool_options.crop.fill = craft::draw::ui::crop::CropFill::Background; + } let same_document = match (&self.path_document, &document) { (Some((old_id, old_revision, old_source)), Some((id, revision, source))) => old_id == id && (revision > old_revision || old_source.ptr_eq(source)), (None, None) => true, diff --git a/crates/ui-egui/src/tools.rs b/crates/ui-egui/src/tools.rs index 1650755d10..c90a652d6a 100644 --- a/crates/ui-egui/src/tools.rs +++ b/crates/ui-egui/src/tools.rs @@ -65,6 +65,8 @@ mod gradient_mode_greyed_tests; #[cfg(test)] mod count_show_extras_tests; #[cfg(test)] +mod crop_fill_layer_switch_tests; +#[cfg(test)] mod crop_layers_panel_commit_tests; #[cfg(test)] mod crop_mode_tests; diff --git a/crates/ui-egui/src/tools/crop_fill_layer_switch_tests.rs b/crates/ui-egui/src/tools/crop_fill_layer_switch_tests.rs new file mode 100644 index 0000000000..3b52a97877 --- /dev/null +++ b/crates/ui-egui/src/tools/crop_fill_layer_switch_tests.rs @@ -0,0 +1,102 @@ +//! #17687: Fill: Content-Aware Fill goes back to the default entry once a layer that can't use it becomes active (a +//! smart object, an adjustment layer, the Background among other layers, #17686), and stays there when a pixel layer is +//! active again. Switching between pixel layers keeps it; several selected layers keep today's behaviour. + +use craft::draw::ui::crop::CropFill; +use photocraft_doc::{Color, ColorMode, Document, SampleType, Size}; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::state::Tool; + +/// 200 × 100: Background, "A" and "B" (pixel layers) and "Smart" (a smart object, active); the Crop tool current. +fn app() -> Box { + let mut app = lone_background(); + for name in ["A", "B", "Smart"] { + app.run("layer.new.layer", json!({"name": name})).unwrap(); + } + app.run("edit.fill", json!({"color": "#ff0000"})).unwrap(); + app.run("layer.smartObjects.convertToSmartObject", json!({})).unwrap(); + app +} + +/// A document of one Background layer, the Crop tool current. +fn lone_background() -> Box { + let doc = Document::with_background("crop", Size::new(200, 100), ColorMode::Rgb, SampleType::U8, Color::WHITE); + let mut s = photocraft_engine::Session::new(); + s.add_document(doc, None); + let mut app = PhotocraftApp::new(s, crate::Services::default()); + app.ui.tool = Tool::Crop; + app +} + +fn id(app: &PhotocraftApp, name: &str) -> u64 { + let st = app.session.active().unwrap(); + st.doc.layers.iter().find(|l| l.name == name).map(|l| l.id.0).unwrap_or_else(|| panic!("no layer {name}")) +} + +/// Makes `name` the active (and only selected) layer through `layer.select`. +fn select(app: &mut PhotocraftApp, name: &str) { + let layer = id(app, name); + app.run("layer.select", json!({"layer": layer})).unwrap(); +} + +fn fill(app: &PhotocraftApp) -> CropFill { + app.ui.tool_options.crop.fill +} + +/// Content-Aware Fill chosen on pixel layer "A". +fn content_aware_on_a() -> Box { + let mut app = app(); + select(&mut app, "A"); + app.ui.tool_options.crop.fill = CropFill::ContentAware; + app +} + +#[test] +fn a_smart_object_becoming_active_resets_fill_and_it_stays_after_going_back() { + let mut app = content_aware_on_a(); + select(&mut app, "Smart"); + assert_eq!(fill(&app), CropFill::Background, "the smart object can't use Content-Aware Fill"); + select(&mut app, "A"); + assert_eq!(fill(&app), CropFill::Background, "back on the pixel layer, Fill stays on the default entry"); +} + +#[test] +fn switching_between_pixel_layers_keeps_content_aware_fill() { + let mut app = content_aware_on_a(); + select(&mut app, "B"); + assert_eq!(fill(&app), CropFill::ContentAware); + select(&mut app, "A"); + assert_eq!(fill(&app), CropFill::ContentAware); +} + +#[test] +fn the_background_among_other_layers_and_an_adjustment_layer_reset_fill() { + let mut app = content_aware_on_a(); + select(&mut app, "Background"); + assert_eq!(fill(&app), CropFill::Background, "the Background while other layers exist"); + + let mut app = content_aware_on_a(); + app.run("layer.newAdjustmentLayer.invert", json!({})).unwrap(); + assert_eq!(fill(&app), CropFill::Background, "a new adjustment layer becomes active"); +} + +#[test] +fn the_only_background_keeps_content_aware_fill() { + let mut app = lone_background(); + app.ui.tool_options.crop.fill = CropFill::ContentAware; + let background = id(&app, "Background"); + app.run("layer.select", json!({"layer": background})).unwrap(); + assert_eq!(fill(&app), CropFill::ContentAware); +} + +/// Several selected layers are not measured: Fill keeps its entry, as before. +#[test] +fn several_selected_layers_keep_content_aware_fill() { + let mut app = content_aware_on_a(); + let smart = id(&app, "Smart"); + app.run("layer.select", json!({"layer": smart, "mode": "add"})).unwrap(); + assert!(app.session.active().unwrap().selected_layers().len() > 1); + assert_eq!(fill(&app), CropFill::ContentAware); +} diff --git a/crates/ui-egui/src/tools/crop_ui.rs b/crates/ui-egui/src/tools/crop_ui.rs index bfe2324999..fe3e70f5d3 100644 --- a/crates/ui-egui/src/tools/crop_ui.rs +++ b/crates/ui-egui/src/tools/crop_ui.rs @@ -292,6 +292,19 @@ pub(crate) fn grown_fill(app: &PhotocraftApp, doc: &photocraft_doc::Document) -> fills.then_some(app.session.tools.background) } +/// The active layer can't take Fill: Content-Aware Fill: it is not a plain pixel layer, or it is the Background while +/// other layers exist (#17686). `false` without a document, without an active layer and with several layers selected, +/// which keep the entry as it is. +pub(crate) fn content_aware_fill_greyed(app: &PhotocraftApp) -> bool { + let Some(st) = app.session.active() else { return false }; + let Some(layer) = st.active_layer.and_then(|id| st.doc.layer(id)) else { return false }; + if st.selected_layers().len() > 1 { + return false; + } + let background = st.doc.background().is_some_and(|bg| bg.id == layer.id); + !matches!(layer.content, photocraft_doc::LayerContent::Raster(_)) || (background && st.doc.layer_count() > 1) +} + /// Show Cropped Area is off with the Crop tool showing a frame: nothing outside it shows. pub(crate) fn hides_canvas_edge(app: &PhotocraftApp) -> bool { app.ui.tool == Tool::Crop && frame(app).is_some() && !app.ui.tool_options.crop.shield.shows_cropped_area()