From b1840080639322a7a86cabc41662ea17a09f0a0a Mon Sep 17 00:00:00 2001 From: Hodd Date: Mon, 19 Oct 2026 11:39:54 +0200 Subject: [PATCH 1/4] =?UTF-8?q?Layers=20footer:=20Pattern=E2=80=A6=20fill?= =?UTF-8?q?=20layer,=20filling=20an=20empty=20selected=20layer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Layers panel's fill/adjustment menu gains Pattern…, which opens the Pattern Fill dialog like Layer › New Fill Layer › Pattern…. As in Photoshop, a new Pattern Fill made while an empty pixel layer is selected turns that layer into the fill instead of adding one above it. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/pattern_cmds.rs | 12 ++++++++++++ crates/engine/src/pattern_fill_cmds.rs | 25 +++++++++++++++++++++++++ crates/engine/src/stamp_cmds.rs | 2 +- crates/ui-egui/src/panels.rs | 10 +++++++--- 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/pattern_cmds.rs b/crates/engine/src/pattern_cmds.rs index 0cb9052ceb1..14d65c3d2d7 100644 --- a/crates/engine/src/pattern_cmds.rs +++ b/crates/engine/src/pattern_cmds.rs @@ -317,6 +317,18 @@ pub(crate) fn insert_fill_layer(doc: &mut Document, active: &mut Option Result { } /// A plain, unlocked pixel layer with no pixels, mask or clipping: safe to stamp into. -fn is_empty_pixel_layer(doc: &Document, id: LayerId) -> bool { +pub(crate) fn is_empty_pixel_layer(doc: &Document, id: LayerId) -> bool { let Some(l) = doc.layer(id) else { return false }; let locks = doc.effective_locks(id); matches!(&l.content, LayerContent::Raster(s) if s.content_bounds().is_empty()) diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs index 9a1b07f2b7a..9e52d04255b 100644 --- a/crates/ui-egui/src/panels.rs +++ b/crates/ui-egui/src/panels.rs @@ -2706,6 +2706,9 @@ fn layers(app: &mut PhotocraftApp, ui: &mut egui::Ui) { if menu.item(ui, tl!("Gradient…")) { actions.push(("layer.newFillLayer.gradient".into(), json!({}))); } + if menu.item(ui, tl!("Pattern…")) { + actions.push(("layer.newFillLayer.pattern".into(), json!({}))); + } }); // Like Photoshop the button never replaces a mask (#2075): with a layer mask it adds a // vector mask, and with both it is greyed. @@ -2772,9 +2775,10 @@ fn layers(app: &mut PhotocraftApp, ui: &mut egui::Ui) { app.ui.dock.collapsed.retain(|g| *g != crate::dock::Group::Properties); continue; } - if p.is_null() || id == "layer.newFillLayer.solidColor" { - // Context-menu items and the Layers footer's Solid Color… action behave like - // their menu-bar twins. In particular, a new Solid Color fill opens its picker. + if p.is_null() || id == "layer.newFillLayer.solidColor" || id == photocraft_engine::pattern_fill_cmds::CREATE { + // Context-menu items and the Layers footer's Solid Color… and Pattern… actions behave + // like their menu-bar twins: a new Solid Color fill opens its picker, a new Pattern + // fill the Pattern Fill dialog. let ctx = ui.ctx().clone(); let params = if p.is_null() { json!({}) } else { p }; if let Err(e) = crate::menus::invoke(app, &ctx, &id, params) { From a02472d8c771758ba8470a8a79a75deeee32c470 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 19 Oct 2026 10:02:07 +0000 Subject: [PATCH 2/4] Fix the empty-layer pattern fill test: Document::layers is a field The new test called doc.layers() as a method; it is a Vec field, so the engine lib tests did not compile. Use .layers.len() and .layers[0]. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013E2P5znttiWsfgMEzVVQ9u --- crates/engine/src/pattern_fill_cmds.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/pattern_fill_cmds.rs b/crates/engine/src/pattern_fill_cmds.rs index 107625c45d6..908700fee0d 100644 --- a/crates/engine/src/pattern_fill_cmds.rs +++ b/crates/engine/src/pattern_fill_cmds.rs @@ -201,23 +201,23 @@ mod tests { s.execute("file.new", json!({"width": 40, "height": 30})).unwrap(); let empty = LayerId(s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap()); let before = s.active().unwrap().doc.clone(); - let count = s.active().unwrap().doc.layers().count(); + let count = s.active().unwrap().doc.layers.len(); let p = json!({"pattern": "Checkerboard"}); let shown = preview(&s, CREATE, &p).unwrap(); let id = s.execute(CREATE, p).unwrap()["layer"].as_u64().unwrap(); assert_eq!(id, empty.0, "the empty layer itself is filled"); - assert_eq!(s.active().unwrap().doc.layers().count(), count, "no layer is added"); + assert_eq!(s.active().unwrap().doc.layers.len(), count, "no layer is added"); assert!(matches!(fill(&s, empty), Fill::Pattern { .. })); assert_eq!(*s.active().unwrap().doc, shown, "the preview shows what OK commits"); s.undo(); assert_eq!(*s.active().unwrap().doc, *before); // A layer with pixels (the white Background) keeps them: the fill goes above it. - let bg = s.active().unwrap().doc.layers().next().unwrap().id; + let bg = s.active().unwrap().doc.layers[0].id; s.execute("layer.select", json!({"layer": bg.0})).unwrap(); let id = LayerId(s.execute(CREATE, json!({"pattern": "Checkerboard"})).unwrap()["layer"].as_u64().unwrap()); assert_ne!(id, bg); assert!(matches!(s.active().unwrap().doc.layer(bg).unwrap().content, LayerContent::Raster(_))); - assert_eq!(s.active().unwrap().doc.layers().count(), count + 1); + assert_eq!(s.active().unwrap().doc.layers.len(), count + 1); } #[test] From 70472fe58a4c10270f2ad084dc8df8d359dd5f86 Mon Sep 17 00:00:00 2001 From: Hodd Date: Mon, 19 Oct 2026 15:06:28 +0200 Subject: [PATCH 3/4] fixed layer name changing on previously named layers when adding new pattern fill from layer bottom panel --- crates/engine/src/pattern_cmds.rs | 11 +++++++++-- crates/engine/src/pattern_fill_cmds.rs | 7 +++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/pattern_cmds.rs b/crates/engine/src/pattern_cmds.rs index 14d65c3d2d7..4f44e6b4bfb 100644 --- a/crates/engine/src/pattern_cmds.rs +++ b/crates/engine/src/pattern_cmds.rs @@ -318,12 +318,14 @@ pub(crate) fn insert_fill_layer(doc: &mut Document, active: &mut Option bool { + name.strip_prefix("Layer ").is_some_and(|n| !n.is_empty() && n.bytes().all(|b| b.is_ascii_digit())) +} + fn new_fill_layer(s: &mut Session, p: &Value) -> Result { let pat = resolve_param(s, "layer.newFillLayer.pattern", p)?; let id = s.edit("New Pattern Fill Layer", |doc, active| insert_fill_layer(doc, active, &pat, p))?; diff --git a/crates/engine/src/pattern_fill_cmds.rs b/crates/engine/src/pattern_fill_cmds.rs index 908700fee0d..3842eb2f052 100644 --- a/crates/engine/src/pattern_fill_cmds.rs +++ b/crates/engine/src/pattern_fill_cmds.rs @@ -208,9 +208,16 @@ mod tests { assert_eq!(id, empty.0, "the empty layer itself is filled"); assert_eq!(s.active().unwrap().doc.layers.len(), count, "no layer is added"); assert!(matches!(fill(&s, empty), Fill::Pattern { .. })); + assert!(s.active().unwrap().doc.layer(empty).unwrap().name.starts_with("Pattern Fill"), "a default name follows the content"); assert_eq!(*s.active().unwrap().doc, shown, "the preview shows what OK commits"); s.undo(); assert_eq!(*s.active().unwrap().doc, *before); + // A name the user gave the layer is kept. + s.execute("layer.renameLayer", json!({"layer": empty.0, "name": "Sky"})).unwrap(); + s.execute(CREATE, json!({"pattern": "Checkerboard"})).unwrap(); + assert_eq!(s.active().unwrap().doc.layer(empty).unwrap().name, "Sky"); + s.undo(); + s.undo(); // A layer with pixels (the white Background) keeps them: the fill goes above it. let bg = s.active().unwrap().doc.layers[0].id; s.execute("layer.select", json!({"layer": bg.0})).unwrap(); From 3968e24cf03fec6e434c04096458682b53454978 Mon Sep 17 00:00:00 2001 From: David Mann Date: Thu, 29 Oct 2026 15:54:55 +0100 Subject: [PATCH 4/4] Integration: the empty-layer check honours the default pixel and video (#7716) `is_empty_pixel_layer` took any layer without stored tiles for empty, so a layer opaque only through its default pixel (as KRA imports it, `Surface::with_default`) or a blank video layer became the Pattern Fill (and the Stamp Visible target) instead of getting one above. It now needs a transparent default pixel (alpha only: a mode or profile conversion gives an empty layer's default pixel other colour bytes) and no video. Co-authored-by: Hodd <32705043+hodd1444@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/pattern_fill_cmds.rs | 39 ++++++++++++++++++ crates/engine/src/cmd/stamp_cmds.rs | 48 +++++++++++++++++++++- 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/pattern_fill_cmds.rs b/crates/engine/src/cmd/pattern_fill_cmds.rs index 96810dd059c..c59f2e4b99d 100644 --- a/crates/engine/src/cmd/pattern_fill_cmds.rs +++ b/crates/engine/src/cmd/pattern_fill_cmds.rs @@ -229,6 +229,45 @@ mod tests { assert_eq!(s.active().unwrap().doc.layers.len(), count + 1); } + /// Not empty: a layer opaque only through its default pixel (no tiles stored, as KRA imports + /// it) and a blank video layer. Both get a fill above and keep what they had. + #[test] + fn a_default_pixel_or_video_layer_is_not_taken_for_an_empty_one() { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 40, "height": 30})).unwrap(); + let opaque = LayerId(s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap()); + let doc = Arc::make_mut(&mut s.active_mut().unwrap().doc); + let l = doc.layer_mut(opaque).unwrap(); + let fmt = l.surface().unwrap().format(); + l.content = LayerContent::Raster(photocraft_raster::Surface::with_default(fmt, &[0.2, 0.4, 0.6, 1.0])); + let video = LayerId(s.execute("layer.videoLayers.newBlankVideoLayer", json!({})).unwrap()["layer"].as_u64().unwrap()); + for old in [opaque, video] { + s.execute("layer.select", json!({"layer": old.0})).unwrap(); + let before = s.active().unwrap().doc.layer(old).unwrap().clone(); + let count = s.active().unwrap().doc.layers.len(); + let id = LayerId(s.execute(CREATE, json!({"pattern": "Checkerboard"})).unwrap()["layer"].as_u64().unwrap()); + assert_ne!(id, old, "a fill above {old:?}"); + assert_eq!(s.active().unwrap().doc.layers.len(), count + 1); + assert_eq!(*s.active().unwrap().doc.layer(old).unwrap(), before, "{old:?} keeps its pixels and video"); + } + } + + /// An empty layer stays empty through a mode change (its default pixel stays transparent). + #[test] + fn an_empty_layer_is_still_the_fill_after_a_mode_change() { + for mode in ["image.mode.cmyk", "image.mode.lab"] { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 40, "height": 30})).unwrap(); + let empty = s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap(); + s.execute(mode, json!({})).unwrap(); + s.execute("layer.select", json!({"layer": empty})).unwrap(); + let count = s.active().unwrap().doc.layers.len(); + let id = s.execute(CREATE, json!({"pattern": "Checkerboard"})).unwrap()["layer"].as_u64().unwrap(); + assert_eq!(id, empty, "{mode}"); + assert_eq!(s.active().unwrap().doc.layers.len(), count, "{mode}"); + } + } + #[test] fn preview_matches_what_the_command_commits() { let (mut s, id) = session(8); diff --git a/crates/engine/src/cmd/stamp_cmds.rs b/crates/engine/src/cmd/stamp_cmds.rs index 04f3b4ba2fa..1f96831f7ec 100644 --- a/crates/engine/src/cmd/stamp_cmds.rs +++ b/crates/engine/src/cmd/stamp_cmds.rs @@ -82,11 +82,16 @@ fn stamp_visible(s: &mut Session, p: &Value) -> Result { Ok(json!({ "layer": id.0 })) } -/// A plain, unlocked pixel layer with no pixels, mask or clipping: safe to stamp into. +/// A plain, unlocked pixel layer with no pixels, mask, clipping or video: safe to stamp into. +/// Pixels it has only through a visible default pixel (no tiles stored) count as pixels. pub(crate) fn is_empty_pixel_layer(doc: &Document, id: LayerId) -> bool { let Some(l) = doc.layer(id) else { return false }; let locks = doc.effective_locks(id); - matches!(&l.content, LayerContent::Raster(s) if s.content_bounds().is_empty()) + // Only the default pixel's alpha counts: a mode or profile conversion keeps it transparent but + // gives its colour channels other values (no ink in CMYK, a neutral a/b in Lab). + matches!(&l.content, LayerContent::Raster(s) + if s.content_bounds().is_empty() && s.format().alpha && s.default_pixel().last().is_some_and(|a| *a <= 0.0)) + && l.video.is_none() && l.visible && l.mask.is_none() && l.vector_mask.is_none() @@ -277,6 +282,45 @@ mod tests { assert_eq!(ids(&s).len(), 5); } + /// A layer opaque only through its default pixel (no tiles, as KRA imports it) and a blank + /// video layer are not empty: the stamp goes into a new layer above them. + #[test] + fn stamp_visible_keeps_default_pixel_and_video_layers() { + let (mut s, ..) = session(8); + s.execute("layer.new.layer", json!({"name": "Opaque"})).unwrap(); + let opaque = s.active().unwrap().active_layer.unwrap(); + let l = std::sync::Arc::make_mut(&mut s.active_mut().unwrap().doc).layer_mut(opaque).unwrap(); + let fmt = l.surface().unwrap().format(); + l.content = LayerContent::Raster(photocraft_raster::Surface::with_default(fmt, &[0.0, 1.0, 0.0, 1.0])); + let video = LayerId(s.execute("layer.videoLayers.newBlankVideoLayer", json!({})).unwrap()["layer"].as_u64().unwrap()); + for old in [opaque, video] { + s.execute("layer.select", json!({"layer": old.0})).unwrap(); + let before = s.active().unwrap().doc.layer(old).unwrap().clone(); + let n = ids(&s).len(); + let r = s.execute("layer.stampVisible", json!({})).unwrap(); + assert_ne!(r["layer"].as_u64(), Some(old.0), "a new layer above {old:?}"); + assert_eq!(ids(&s).len(), n + 1); + assert_eq!(*s.active().unwrap().doc.layer(old).unwrap(), before, "{old:?} keeps its pixels and video"); + } + } + + /// An empty layer stays empty through a mode change, though its default pixel's colour + /// bytes change: the stamp still goes into it. + #[test] + fn stamp_visible_fills_an_empty_layer_after_a_mode_change() { + for mode in ["image.mode.cmyk", "image.mode.lab"] { + let (mut s, ..) = session(8); + s.execute("layer.new.layer", json!({"name": "Empty"})).unwrap(); + let empty = s.active().unwrap().active_layer.unwrap(); + s.execute(mode, json!({})).unwrap(); + s.execute("layer.select", json!({"layer": empty.0})).unwrap(); + let n = ids(&s).len(); + let r = s.execute("layer.stampVisible", json!({})).unwrap(); + assert_eq!(r["layer"].as_u64(), Some(empty.0), "{mode}"); + assert_eq!(ids(&s).len(), n, "{mode}"); + } + } + #[test] fn stamp_visible_respects_opacity_blend_and_groups() { let (mut s, bg, _, blue) = session(8);