mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge pull request #18858
Co-authored-by: autocraft-bot[bot] <354215646+autocraft-bot[bot]@users.noreply.github.com>
This commit is contained in:
commit
3abee2f755
7 files changed
+235
-5
No files matched your search
@@ -87,6 +87,8 @@ pub mod layer_label_cmds;
|
||||
pub mod layer_list_cmds;
|
||||
pub mod layer_mask_props_cmds;
|
||||
pub mod layer_menu_cmds;
|
||||
#[cfg(test)]
|
||||
mod layer_merge_clipping_mask_tests;
|
||||
pub mod layer_multi_cmds;
|
||||
pub mod layer_nav_cmds;
|
||||
mod layer_params;
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
//! #18481: `Ctrl+E` (`layer.mergeLayers`) with the base of a clip group alone selected is Merge Clipping Mask: the base
|
||||
//! and the layers clipped to it become one layer with the base's identity, name, place, blend mode and opacity; the
|
||||
//! composite does not change, and clipped pixels outside the base are gone. Other layers, and the cases the issue did
|
||||
//! not measure, keep Merge Down.
|
||||
|
||||
use photocraft_doc::{BlendMode, LayerId};
|
||||
use photocraft_geom::Rect;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::Session;
|
||||
|
||||
/// `Top` (blue, wider than `Red`) clipped onto `Red`, `Masked` below, on a white Background; `Red` active.
|
||||
fn clip_group() -> (Session, LayerId) {
|
||||
let mut s = crate::session();
|
||||
s.execute("file.new", json!({"width": 60, "height": 40, "background": "#ffffff"})).unwrap();
|
||||
let fill = |s: &mut Session, name: &str, rect: Rect, color: [f32; 4]| {
|
||||
s.execute("layer.new.layer", json!({"name": name})).unwrap();
|
||||
s.edit("Paint", |doc, active| {
|
||||
doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap().fill_rect(rect, &color);
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
s.active().unwrap().active_layer.unwrap()
|
||||
};
|
||||
fill(&mut s, "Masked", Rect::new(0, 20, 60, 40), [0.0, 0.6, 0.0, 1.0]);
|
||||
let red = fill(&mut s, "Red", Rect::new(10, 10, 30, 30), [1.0, 0.0, 0.0, 1.0]);
|
||||
fill(&mut s, "Top", Rect::new(20, 0, 50, 25), [0.0, 0.0, 1.0, 0.7]);
|
||||
s.execute("layer.createClippingMask", json!({})).unwrap();
|
||||
s.execute("layer.select", json!({"layer": red.0})).unwrap();
|
||||
(s, red)
|
||||
}
|
||||
|
||||
fn names(s: &Session) -> Vec<String> {
|
||||
s.active().unwrap().doc.walk().into_iter().map(|(_, _, l)| l.name.clone()).collect()
|
||||
}
|
||||
|
||||
fn flat(s: &Session) -> Vec<[f32; 4]> {
|
||||
photocraft_compose::flatten(&s.active().unwrap().doc).into_px()
|
||||
}
|
||||
|
||||
fn max_diff(a: &[[f32; 4]], b: &[[f32; 4]]) -> f32 {
|
||||
a.iter().zip(b).flat_map(|(p, q)| (0..4).map(move |c| (p[c] - q[c]).abs())).fold(0.0, f32::max)
|
||||
}
|
||||
|
||||
fn undo_label(s: &Session) -> String {
|
||||
s.active().unwrap().history.undo_label().unwrap_or_default().to_string()
|
||||
}
|
||||
|
||||
/// The merged layer's pixel at canvas `(x, y)`.
|
||||
fn pixel(s: &Session, id: LayerId, x: i32, y: i32) -> Vec<f32> {
|
||||
s.active().unwrap().doc.layer(id).unwrap().surface().unwrap().pixel(x, y)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ctrl_e_on_a_clip_base_merges_the_clipped_layers_into_it() {
|
||||
let (mut s, red) = clip_group();
|
||||
let before = flat(&s);
|
||||
let steps = s.active().unwrap().history.past_len();
|
||||
|
||||
s.execute("layer.mergeLayers", json!({})).unwrap();
|
||||
|
||||
assert!(!names(&s).contains(&"Top".to_string()), "Top is merged: {:?}", names(&s));
|
||||
assert!(names(&s).contains(&"Masked".to_string()), "Masked is untouched: {:?}", names(&s));
|
||||
let d = s.active().unwrap();
|
||||
let merged = d.doc.layer(red).expect("the result keeps the base's id");
|
||||
assert_eq!(merged.name, "Red");
|
||||
assert!(!merged.clipped);
|
||||
assert_eq!(d.active_layer, Some(red));
|
||||
assert_eq!(d.history.past_len(), steps + 1, "one History state");
|
||||
assert_eq!(undo_label(&s), "Merge Clipping Mask");
|
||||
assert!(max_diff(&before, &flat(&s)) < 1e-3, "the composite does not change");
|
||||
|
||||
// Inside the base the blue is blended over the red; outside the base the clipped blue is gone.
|
||||
let inside = pixel(&s, red, 25, 15);
|
||||
assert!(inside[2] > inside[0] && inside[3] > 0.99, "{inside:?}");
|
||||
assert!(pixel(&s, red, 40, 5)[3] < 1e-3, "clipped pixels outside the base are dropped");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_result_keeps_the_bases_blend_mode_and_opacity() {
|
||||
let (mut s, red) = clip_group();
|
||||
s.execute("layer.setProps", json!({"blend": "Multiply", "opacity": 0.6})).unwrap();
|
||||
let before = flat(&s);
|
||||
|
||||
s.execute("layer.mergeLayers", json!({})).unwrap();
|
||||
|
||||
let merged = s.active().unwrap().doc.layer(red).unwrap().clone();
|
||||
assert_eq!(merged.blend, BlendMode::Multiply);
|
||||
assert!((merged.opacity - 0.6).abs() < 1e-3, "{}", merged.opacity);
|
||||
assert!(max_diff(&before, &flat(&s)) < 1e-3, "the composite does not change");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_layer_that_is_not_a_clip_base_still_merges_down() {
|
||||
// The clipped layer itself, and a layer with nothing clipped to it.
|
||||
for name in ["Top", "Masked"] {
|
||||
let (mut s, _) = clip_group();
|
||||
let id = s.active().unwrap().doc.walk().into_iter().find(|(_, _, l)| l.name == name).unwrap().2.id;
|
||||
s.execute("layer.select", json!({"layer": id.0})).unwrap();
|
||||
s.execute("layer.mergeLayers", json!({})).unwrap();
|
||||
assert_eq!(undo_label(&s), "Merge Down", "{name}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Not measured in the issue: these keep today's Merge Down.
|
||||
#[test]
|
||||
fn a_base_with_a_layer_mask_or_inside_a_group_keeps_merge_down() {
|
||||
let (mut s, _) = clip_group();
|
||||
s.execute("layer.layerMask.revealAll", json!({})).unwrap();
|
||||
s.execute("layer.mergeLayers", json!({})).unwrap();
|
||||
assert_eq!(undo_label(&s), "Merge Down", "a base with a layer mask");
|
||||
|
||||
let (mut s, red) = clip_group();
|
||||
let masked = s.active().unwrap().doc.walk().into_iter().find(|(_, _, l)| l.name == "Masked").unwrap().2.id;
|
||||
let top = s.active().unwrap().doc.walk().into_iter().find(|(_, _, l)| l.name == "Top").unwrap().2.id;
|
||||
s.execute("layer.select", json!({"layers": [masked.0, red.0, top.0]})).unwrap();
|
||||
s.execute("layer.groupLayers", json!({})).unwrap();
|
||||
assert!(s.active().unwrap().doc.layer(top).unwrap().clipped, "Top is still clipped inside the group");
|
||||
s.execute("layer.select", json!({"layer": red.0})).unwrap();
|
||||
s.execute("layer.mergeLayers", json!({})).unwrap();
|
||||
assert_eq!(undo_label(&s), "Merge Down", "a base inside a group");
|
||||
}
|
||||
|
||||
/// A base whose Fill, Blend If, Advanced Blending or excluded channels the merged layer could not carry keeps Merge
|
||||
/// Down, so the composite is never changed by Merge Clipping Mask.
|
||||
#[test]
|
||||
fn a_base_with_settings_the_result_cannot_carry_keeps_merge_down() {
|
||||
type Set = fn(&mut photocraft_doc::Layer);
|
||||
let cases: [(&str, Set); 4] = [
|
||||
("Fill 50 %", |l| l.fill_opacity = 0.5),
|
||||
("Blend If", |l| l.blend_if.ranges = vec![[photocraft_doc::BlendRange { black: [0, 0], white: [128, 128] }, photocraft_doc::BlendRange::FULL]]),
|
||||
("Blend Clipped Layers as Group off", |l| l.advanced.blend_clipped = false),
|
||||
("an excluded channel", |l| l.excluded_channels = 1),
|
||||
];
|
||||
for (case, set) in cases {
|
||||
let (mut s, red) = clip_group();
|
||||
s.edit("Set", |doc, _| {
|
||||
set(doc.layer_mut(red).unwrap());
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
s.execute("layer.mergeLayers", json!({})).unwrap();
|
||||
assert_eq!(undo_label(&s), "Merge Down", "{case}");
|
||||
}
|
||||
}
|
||||
@@ -31,9 +31,9 @@ pub use selection::top_level;
|
||||
use selection::{doc_state, find_layers, select_all_layers, select_linked};
|
||||
pub(crate) use selection::{mask_targets, recorded_step, reselect};
|
||||
pub use selection::{select, selected, set_selection};
|
||||
pub use structure::deletion_neighbours;
|
||||
pub(crate) use structure::{check_group_depth, relink_copies};
|
||||
pub use structure::{delete_selected, duplicate_selected, group_layers, set_visible_selected};
|
||||
pub use structure::{deletion_neighbours, merges_clipping_mask};
|
||||
use structure::{link_layers, lock_layers, merge_layers, rename_layer, reverse};
|
||||
|
||||
// ---------- enabled predicates ----------
|
||||
|
||||
@@ -228,7 +228,13 @@ fn group_layers_with(s: &mut Session, p: &Value, ids: &[LayerId], from_layers: b
|
||||
pub(super) fn merge_layers(s: &mut Session) -> Result<Value> {
|
||||
let sel = selected(s);
|
||||
if sel.len() < 2 {
|
||||
// ⌘E on a lone group is Photoshop's Merge Group (#3832); on a layer, Merge Down.
|
||||
// ⌘E on a lone clip base is Merge Clipping Mask (#18481); on a lone group, Photoshop's Merge Group (#3832); on a
|
||||
// layer, Merge Down.
|
||||
if let Some(&id) = sel.first()
|
||||
&& s.active().is_some_and(|d| merges_clipping_mask(&d.doc, id))
|
||||
{
|
||||
return merge_clipping_mask(s, id);
|
||||
}
|
||||
if let Some(&id) = sel.first()
|
||||
&& s.active().and_then(|d| d.doc.layer(id)).is_some_and(|l| matches!(l.content, LayerContent::Group(_)))
|
||||
{
|
||||
@@ -298,6 +304,66 @@ pub(super) fn merge_layers(s: &mut Session) -> Result<Value> {
|
||||
Ok(json!({ "layer": mid.0 }))
|
||||
}
|
||||
|
||||
/// `⌘E` on `id` alone is Merge Clipping Mask: a visible pixel-bearing top-level layer with layers clipped to it. A base
|
||||
/// inside a group, with a layer or vector mask or effects, a group or an adjustment layer as the base, and a hidden base
|
||||
/// were not measured (#18481) and keep Merge Down or Merge Group. So does a base whose Fill, Blend If, Advanced Blending
|
||||
/// or excluded channels the merged layer could not carry without changing the composite (review of #18858).
|
||||
pub fn merges_clipping_mask(doc: &Document, id: LayerId) -> bool {
|
||||
let Some(l) = doc.layer(id) else { return false };
|
||||
doc.path_of(id).is_some_and(|p| p.len() == 1)
|
||||
&& l.visible
|
||||
&& l.mask.is_none()
|
||||
&& l.vector_mask.is_none()
|
||||
&& l.effects.items.is_empty()
|
||||
&& l.fill_opacity >= 1.0
|
||||
&& l.blend_if.is_default()
|
||||
&& l.advanced.is_default()
|
||||
&& l.excluded_channels == 0
|
||||
&& !matches!(l.content, LayerContent::Group(_) | LayerContent::Adjustment(_))
|
||||
&& !crate::clip_group::clipped_to(doc, id).is_empty()
|
||||
}
|
||||
|
||||
/// Merge Clipping Mask: the base and the layers clipped to it become one layer with the base's id, name, place, blend mode,
|
||||
/// opacity, locks, colour label and link. Clipped pixels are blended inside the base's pixels and dropped outside it, so
|
||||
/// the composite does not change; hidden clipped layers are discarded, as Merge Layers does.
|
||||
fn merge_clipping_mask(s: &mut Session, base_id: LayerId) -> Result<Value> {
|
||||
s.edit("Merge Clipping Mask", |doc, active| {
|
||||
let clipped = crate::clip_group::clipped_to(doc, base_id);
|
||||
let base = doc.layer(base_id).ok_or(EngineError::NoLayer(base_id))?.clone();
|
||||
let format = doc.pixel_format();
|
||||
// The group alone, the base drawn plainly: its blend mode and opacity stay on the result.
|
||||
let mut plain = base.clone();
|
||||
plain.blend = photocraft_doc::BlendMode::Normal;
|
||||
plain.opacity = 1.0;
|
||||
let mut solo = Document::new("merge", doc.size, format.mode, format.sample);
|
||||
solo.icc_profile = doc.icc_profile.clone();
|
||||
let mut stack = vec![plain];
|
||||
stack.extend(clipped.iter().filter_map(|id| doc.layer(*id)).filter(|l| l.visible).cloned());
|
||||
solo.layers = stack.into();
|
||||
// A Background stays canvas-sized and opaque; any other layer keeps its pixels outside the canvas.
|
||||
let pixels = if doc.is_background(base_id) {
|
||||
crate::pixels::composite_layers(&solo, format, None)
|
||||
} else {
|
||||
crate::pixels::composite_layers_uncropped(&solo, PixelFormat::new(format.mode, format.sample, true))
|
||||
};
|
||||
let mut merged = Layer::new(base.name.clone(), LayerContent::Raster(pixels));
|
||||
merged.id = base_id;
|
||||
merged.blend = base.blend;
|
||||
merged.opacity = base.opacity;
|
||||
merged.locks = base.locks;
|
||||
merged.label = base.label;
|
||||
merged.link_group = base.link_group;
|
||||
for id in &clipped {
|
||||
doc.remove(*id);
|
||||
}
|
||||
*doc.layer_mut(base_id).ok_or(EngineError::NoLayer(base_id))? = merged;
|
||||
*active = Some(base_id);
|
||||
Ok(())
|
||||
})?;
|
||||
reselect(s, vec![base_id], Some(base_id));
|
||||
Ok(json!({ "layer": base_id.0 }))
|
||||
}
|
||||
|
||||
/// Next visible layer row below the deleted target, then rows above in reverse order.
|
||||
/// Group headers precede their children in the UI, unlike Document::walk's storage order.
|
||||
pub fn deletion_neighbours(doc: &Document, target: LayerId) -> Vec<LayerId> {
|
||||
|
||||
@@ -240,6 +240,14 @@ pub fn base_rows(app: &PhotocraftApp) -> Vec<Row> {
|
||||
{
|
||||
item.label = "Merge Group".into();
|
||||
}
|
||||
// And Merge Clipping Mask with just the base of a clip group selected (#18481).
|
||||
if let Some(st) = app.session.active()
|
||||
&& st.selected_layers().len() <= 1
|
||||
&& st.active_layer.is_some_and(|id| photocraft_engine::layer_multi_cmds::merges_clipping_mask(&st.doc, id))
|
||||
&& let Some(item) = find(&mut rows, "layer.mergeLayers")
|
||||
{
|
||||
item.label = "Merge Clipping Mask".into();
|
||||
}
|
||||
// Edit › Fade names the step it fades (#13803).
|
||||
if let Some(item) = find(&mut rows, "edit.fade") {
|
||||
item.label = photocraft_engine::edit_menu_cmds::fade_menu_label(&app.session);
|
||||
|
||||
@@ -64,14 +64,22 @@ pub(crate) fn layer_count_label(command: &str, multi: bool) -> Option<&'static s
|
||||
|
||||
/// The context menu entries for a layer's row: [`entries`], led on the Background's row
|
||||
/// (`background`, [`photocraft_doc::Document::is_background`]) by Layer from Background… and a
|
||||
/// separator, the Layer › New entry (#16764). A multi-selection keeps the plain entries.
|
||||
pub fn row_entries(l: &Layer, multi: bool, has_selection: bool, background: bool) -> Vec<Entry> {
|
||||
/// separator, the Layer › New entry (#16764). A multi-selection keeps the plain entries. The base of a clip group
|
||||
/// (`clip_base`, [`photocraft_engine::layer_multi_cmds::merges_clipping_mask`]) reads Merge Clipping Mask in place of
|
||||
/// Merge Down (#18481).
|
||||
pub fn row_entries(l: &Layer, multi: bool, has_selection: bool, background: bool, clip_base: bool) -> Vec<Entry> {
|
||||
let mut v: Vec<Entry> = Vec::new();
|
||||
if background && !multi {
|
||||
v.push(Some((tl!("Layer from Background…"), "layer.new.layerFromBackground")));
|
||||
v.push(None);
|
||||
}
|
||||
v.extend(entries(l, multi, has_selection));
|
||||
if clip_base
|
||||
&& !multi
|
||||
&& let Some(merge) = v.iter_mut().flatten().find(|e| e.1 == "layer.mergeDown")
|
||||
{
|
||||
*merge = (tl!("Merge Clipping Mask"), "layer.mergeLayers");
|
||||
}
|
||||
v
|
||||
}
|
||||
|
||||
|
||||
@@ -774,7 +774,8 @@ pub(crate) fn row_menu(app: &PhotocraftApp, tree: &PhotoLayers, target: &MenuTar
|
||||
MenuKind::Layer(id) => {
|
||||
let Some(l) = tree.layer(&id) else { return Vec::new() };
|
||||
let background = tree.doc().is_background(id);
|
||||
let mut out = commands(crate::layer_menu_ui::row_entries(l, target.on_selection, has_selection, background), target.on_selection);
|
||||
let clip_base = photocraft_engine::layer_multi_cmds::merges_clipping_mask(tree.doc(), id);
|
||||
let mut out = commands(crate::layer_menu_ui::row_entries(l, target.on_selection, has_selection, background, clip_base), target.on_selection);
|
||||
out.push(Entry::separator());
|
||||
out.push(Entry::new(RENAME.to_string(), tl!("Rename Layer…").to_string()));
|
||||
out
|
||||
|
||||
Reference in new issue
Block a user