Merge PR #7716: Add Pattern Fill option to Layer Panel Footer

This commit is contained in:
David Mann committed 2026-10-29 16:26:56 +01:00
commit 010ab858e7
4 files changed
+144 -6

No files matched your search

+19
View File
@@ -348,11 +348,30 @@ pub(crate) fn insert_fill_layer(
let fill = Fill::Pattern { name: pat.name.clone(), scale, id: pat.id.clone(), angle, link, phase };
let mut l = Layer::new(doc.next_layer_name("Pattern Fill"), LayerContent::Fill(fill));
crate::commands::mask_new_layer(doc, &mut l, p, "layer.newFillLayer.pattern", default_mask)?;
// Like Photoshop, an empty pixel layer selected becomes the fill layer instead of getting one
// above it; it keeps its place, opacity and blending, and a name the user gave it.
if let Some(id) = *active
&& crate::stamp_cmds::is_empty_pixel_layer(doc, id)
{
let target = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?;
if is_default_layer_name(&target.name) {
target.name = l.name;
}
target.content = l.content;
target.mask = l.mask;
target.vector_mask = l.vector_mask;
return Ok(id);
}
let id = crate::layer_place::insert_new(doc, *active, l, crate::layer_place::Place::Auto);
*active = Some(id);
Ok(id)
}
/// A name new layers get ("Layer 3"), so not one the user chose.
fn is_default_layer_name(name: &str) -> bool {
name.strip_prefix("Layer ").is_some_and(|n| !n.is_empty() && n.bytes().all(|b| b.is_ascii_digit()))
}
/// `layer.newFillLayer.pattern`: a Pattern Fill layer above the active one, masked as the Layers panel's Use Default Masks on Fill Layers option says.
fn new_fill_layer(s: &mut Session, p: &Value) -> Result<Value> {
let pat = resolve_param(s, "layer.newFillLayer.pattern", p)?;
@@ -197,6 +197,77 @@ mod tests {
}
}
#[test]
fn an_empty_pixel_layer_becomes_the_fill_other_layers_get_one_above() {
let mut s = Session::new();
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.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.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();
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.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);
+47 -3
View File
@@ -82,11 +82,16 @@ fn stamp_visible(s: &mut Session, p: &Value) -> Result<Value> {
Ok(json!({ "layer": id.0 }))
}
/// 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 {
/// 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);
+7 -3
View File
@@ -460,6 +460,9 @@ pub(super) 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.
@@ -534,9 +537,10 @@ pub(super) 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) {