diff --git a/crates/engine-core/src/cmd/channel_cmds/refs.rs b/crates/engine-core/src/cmd/channel_cmds/refs.rs index f42cdc3af2..cbb524cd26 100644 --- a/crates/engine-core/src/cmd/channel_cmds/refs.rs +++ b/crates/engine-core/src/cmd/channel_cmds/refs.rs @@ -258,6 +258,18 @@ pub(super) fn source(s: &Session, p: &Value, cmd: &str, gray: bool) -> Result String { + format!("__sourceLayer/{index}") +} + +/// The Channel row of a source document that is not the target. +fn channel_key(index: usize) -> String { + format!("__sourceChannel/{index}") +} +use crate::PhotocraftApp; +use craft::ui::dialog::DialogId; +use craft::ui::patterns::form_dialogs::ParamKind; +use serde_json::{Value, json}; + +const APPLY: &str = "image.applyImage"; + +fn run(app: &mut PhotocraftApp, id: &str, params: Value) { + app.run(id, params).unwrap_or_else(|e| panic!("{id}: {e}")); +} + +/// Fills the rectangle `x, y, w, h` of the active layer with `color`. +fn fill(app: &mut PhotocraftApp, rect: [u32; 4], color: &str) { + run(app, "select.rect", json!({"x": rect[0], "y": rect[1], "width": rect[2], "height": rect[3]})); + run(app, "edit.fill", json!({"color": color})); + run(app, "select.deselect", json!({})); +} + +fn five_documents() -> (Box, egui::Context) { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + run(&mut app, "file.new", json!({"width": 300, "height": 200, "name": "other"})); + run(&mut app, "file.new", json!({"width": 400, "height": 300, "mode": "gray", "background": "#000000", "name": "gray"})); + fill(&mut app, [200, 0, 200, 300], "#ffffff"); + run(&mut app, "file.new", json!({"width": 400, "height": 300, "depth": 16, "background": "#404040", "name": "rgb16"})); + run(&mut app, "file.new", json!({"width": 400, "height": 300, "background": "#808080", "name": "source"})); + run(&mut app, "layer.new.layer", json!({"name": "S1"})); + fill(&mut app, [200, 0, 200, 300], "#ffffff"); + run(&mut app, "file.new", json!({"width": 400, "height": 300, "background": "#c86432", "name": "target"})); + fill(&mut app, [200, 0, 200, 300], "#3296fa"); + run(&mut app, "select.rect", json!({"x": 0, "y": 0, "width": 200, "height": 150})); + run(&mut app, "select.saveSelection", json!({"name": "Alpha 1"})); + run(&mut app, "select.deselect", json!({})); + run(&mut app, "layer.new.layer", json!({"name": "Top"})); + run(&mut app, "layer.new.layer", json!({"name": "Masked"})); + run(&mut app, "layer.select", json!({"name": "Background"})); + app.sync_views(); + let ctx = app.ctx().clone(); + (app, ctx) +} + +fn frame(app: &mut PhotocraftApp, ctx: &egui::Context) { + let mut output = ctx.run_ui(Default::default(), |ui| { + let mut frame = eframe::Frame::_new_kittest(); + app.logic_frame(ui.ctx(), &mut frame); + app.ui_frame(ui, &mut frame); + }); + output.textures_delta.clear(); +} + +fn open(app: &mut PhotocraftApp, ctx: &egui::Context) -> DialogId { + let reply = crate::menus::invoke(app, ctx, APPLY, json!({})).unwrap(); + let want = reply["dialog"].as_str().expect("the reply names a dialog").to_owned(); + let id = app.framework.dialogs.open_dialogs().into_iter().map(|d| d.id).find(|id| id.value().to_string() == want).expect("the dialog is open"); + frame(app, ctx); + id +} + +fn fields(app: &PhotocraftApp, id: DialogId) -> Value { + app.framework.dialogs.open_dialogs().into_iter().find(|open| open.id == id).and_then(|open| open.fields).expect("an open dialog") +} + +fn set(app: &mut PhotocraftApp, ctx: &egui::Context, id: DialogId, key: &str, value: Value) { + app.framework.dialogs.set_json_field(ctx, id, key, value).unwrap_or_else(|e| panic!("{key}: {e}")); + // The form brings its derived keys up to date at its next pass. + frame(app, ctx); +} + +/// The `(id, label)` options of choice row `key`. +fn options(app: &mut PhotocraftApp, key: &str) -> Vec<(String, String)> { + let (params, _) = params_for(app, APPLY).expect("Apply Image has a form"); + match ¶ms.iter().find(|p| p.key == key).unwrap_or_else(|| panic!("no row {key}")).kind { + ParamKind::Choice { options, .. } => options.iter().map(|(id, label)| (id.clone(), label.as_str().to_owned())).collect(), + other => panic!("{key} is not a choice: {other:?}"), + } +} + +fn ids(app: &mut PhotocraftApp, key: &str) -> Vec { + options(app, key).into_iter().map(|(id, _)| id).collect() +} + +fn index_of(app: &PhotocraftApp, name: &str) -> usize { + app.session.documents().iter().position(|st| st.doc.name == name).unwrap_or_else(|| panic!("no document {name}")) +} + +/// The active layer's 8-bit RGB at `(x, y)`. +fn rgb(app: &PhotocraftApp, x: i32, y: i32) -> [u8; 3] { + let st = app.session.active().unwrap(); + let p = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().pixel(x, y); + [0, 1, 2].map(|c| (p[c] * 255.0).round() as u8) +} + +#[test] +fn source_lists_same_size_and_depth_documents_and_defaults_to_the_target() { + let (mut app, ctx) = five_documents(); + let names: Vec = options(&mut app, SOURCE).into_iter().map(|(_, name)| name).collect(); + assert_eq!(names, ["gray", "source", "target"], "not other (size), not rgb16 (bit depth)"); + let d = open(&mut app, &ctx); + assert_eq!(fields(&app, d)[SOURCE], index_of(&app, "target").to_string()); +} + +#[test] +fn another_target_lists_its_own_size_and_depth() { + let (mut app, _) = five_documents(); + // rgb16 as the target: only itself. + let rgb16 = index_of(&app, "rgb16"); + run(&mut app, "document.activate", json!({"document": rgb16})); + let names: Vec = options(&mut app, SOURCE).into_iter().map(|(_, name)| name).collect(); + assert_eq!(names, ["rgb16"]); + // gray as the target: the RGB documents of its size and depth too. + let gray = index_of(&app, "gray"); + run(&mut app, "document.activate", json!({"document": gray})); + let names: Vec = options(&mut app, SOURCE).into_iter().map(|(_, name)| name).collect(); + assert_eq!(names, ["gray", "source", "target"]); +} + +#[test] +fn layer_and_channel_follow_the_chosen_document() { + let (mut app, ctx) = five_documents(); + let (gray, source) = (index_of(&app, "gray"), index_of(&app, "source")); + // The target's own rows: Merged, then its layers top to bottom. + assert_eq!(ids(&mut app, "sourceLayer"), ["merged", "Masked", "Top", "Background"]); + assert_eq!(ids(&mut app, &layer_key(source)), ["merged", "S1", "Background"]); + assert_eq!(ids(&mut app, &channel_key(source)), ["RGB", "Red", "Green", "Blue"]); + // A single-layer document lists only Background, with no Merged entry. + assert_eq!(ids(&mut app, &layer_key(gray)), ["Background"]); + assert_eq!(ids(&mut app, &channel_key(gray)), ["Gray"]); + // Choosing a document shows its rows and hides the target's. + let d = open(&mut app, &ctx); + set(&mut app, &ctx, d, SOURCE, json!(source.to_string())); + let shown = fields(&app, d); + assert_eq!(shown["__sourceIsTarget"], false); + assert_eq!(shown[format!("__sourceShown/{source}")], true); + assert_eq!(shown[format!("__sourceShown/{gray}")], false); + assert_eq!(shown["source"], json!({"document": source, "layer": "merged", "channel": "RGB", "invert": false})); + let target = index_of(&app, "target"); + set(&mut app, &ctx, d, SOURCE, json!(target.to_string())); + let shown = fields(&app, d); + assert_eq!(shown["__sourceIsTarget"], true); + assert!(shown.get("source").is_none(), "the target sends the flat keys: {shown}"); +} + +#[test] +fn the_target_line_names_the_target() { + let (mut app, _) = five_documents(); + let (params, _) = params_for(&mut app, APPLY).unwrap(); + let row = params.iter().find(|p| p.key == "__target").expect("a Target row"); + assert_eq!(row.label.as_str(), "Target"); + match &row.kind { + ParamKind::ReadOnlyText { text, .. } => assert_eq!(text.as_str(), "target"), + other => panic!("the Target row is static text: {other:?}"), + } +} + +#[test] +fn ok_applies_the_chosen_source_document() { + let (mut app, ctx) = five_documents(); + let source = index_of(&app, "source"); + let d = open(&mut app, &ctx); + set(&mut app, &ctx, d, "blending", json!("normal")); + set(&mut app, &ctx, d, SOURCE, json!(source.to_string())); + crate::dialogs::confirm(&mut app, &ctx, d).unwrap(); + // "source" Merged: 128 on the left, white on the right. + assert_eq!(rgb(&app, 50, 50), [128, 128, 128]); + assert_eq!(rgb(&app, 300, 50), [255, 255, 255]); + run(&mut app, "edit.undo", json!({})); + // Its Background alone (128 everywhere), inverted. + let d = open(&mut app, &ctx); + set(&mut app, &ctx, d, &layer_key(source), json!("Background")); + set(&mut app, &ctx, d, "sourceInvert", json!(true)); + crate::dialogs::confirm(&mut app, &ctx, d).unwrap(); + assert_eq!(rgb(&app, 50, 50), [127, 127, 127]); + assert_eq!(rgb(&app, 300, 50), [127, 127, 127]); +} + +#[test] +fn ok_with_the_mask_of_the_issue() { + let (mut app, ctx) = five_documents(); + let (source, target) = (index_of(&app, "source"), index_of(&app, "target")); + for (invert, at_50, at_300) in [(false, [128, 128, 128], [50, 150, 250]), (true, [200, 100, 50], [255, 255, 255])] { + let d = open(&mut app, &ctx); + set(&mut app, &ctx, d, "blending", json!("normal")); + set(&mut app, &ctx, d, SOURCE, json!(source.to_string())); + set(&mut app, &ctx, d, "__mask", json!(true)); + set(&mut app, &ctx, d, "__maskSource", json!(target.to_string())); + set(&mut app, &ctx, d, &format!("__maskChannel/{target}"), json!("Alpha 1")); + set(&mut app, &ctx, d, "__maskInvert", json!(invert)); + crate::dialogs::confirm(&mut app, &ctx, d).unwrap(); + assert_eq!(rgb(&app, 50, 50), at_50, "invert {invert}"); + assert_eq!(rgb(&app, 300, 50), at_300, "invert {invert}"); + run(&mut app, "edit.undo", json!({})); + } +} + +#[test] +fn a_source_of_another_bit_depth_is_refused_and_changes_nothing() { + let (mut app, _) = five_documents(); + let rgb16 = index_of(&app, "rgb16"); + let revision = app.session.active().unwrap().revision; + for params in [ + json!({"source": {"document": rgb16}, "blending": "normal"}), + json!({"sourceDocument": rgb16, "blending": "normal"}), + json!({"mask": {"document": rgb16, "channel": "RGB"}, "blending": "normal"}), + ] { + let error = app.session.execute(APPLY, params.clone()).expect_err("another bit depth is refused").to_string(); + assert!(error.contains("bit depth"), "{params}: {error}"); + assert_eq!(app.session.active().unwrap().revision, revision, "{params} changed the target"); + } + assert_eq!(rgb(&app, 50, 50), [200, 100, 50]); + // The same bit depth in another mode still applies. + let gray = index_of(&app, "gray"); + app.session.execute(APPLY, json!({"source": {"document": gray}, "blending": "normal"})).unwrap(); + assert_eq!(rgb(&app, 300, 50), [255, 255, 255]); +} diff --git a/crates/ui-egui/src/menus/view_cmds/dialogs.rs b/crates/ui-egui/src/menus/view_cmds/dialogs.rs index 56a1434162..889008d1a5 100644 --- a/crates/ui-egui/src/menus/view_cmds/dialogs.rs +++ b/crates/ui-egui/src/menus/view_cmds/dialogs.rs @@ -79,6 +79,7 @@ fn derive(id: &str, fields: &mut DialogFields) { artboard_name_options::derive(fields); } if id == "image.applyImage" { + apply_image_source_derive(fields); apply_image_mask_derive(fields); } } @@ -629,25 +630,25 @@ fn channel_form(app: &PhotocraftApp, id: &str) -> Option<(Vec, DialogFiel ] } "image.applyImage" => { - // Source layer: Merged, then the pixel layers top to bottom (the engine takes - // the topmost layer of a name), shown by their own names. - let mut layers = vec!["merged".to_string()]; - for (_, _, l) in d.walk().into_iter().rev() { - if l.surface().is_some() && !layers.contains(&l.name) { - layers.push(l.name.clone()); - } - } + // The target's own Layer and Channel rows; another source document has its own pair (#14480). + let (layers, layer) = apply_image_layers(d); + let on_target = |row: Param| row.visible_when(SOURCE_IS_TARGET, true); + let mut rows = apply_image_source_rows(app); // Scale and Offset only affect Add and Subtract. - let mut rows = vec![ - choice("sourceLayer", tl!("Layer"), channels(&layers), "merged"), - choice("sourceChannel", tl!("Source Channel"), channels(&load), &chans[0]), + rows.extend([ + on_target(choice("sourceLayer", tl!("Layer"), channels(&layers), &layer)), + on_target(choice("sourceChannel", tl!("Source Channel"), channels(&load), &chans[0])), + ]); + rows.extend(apply_image_other_source_rows(app)); + rows.extend([ Param::new("sourceInvert", tl!("Invert"), ParamKind::bool(false)), + Param::read_only_text("__target", tl!("Target"), d.name.clone()), choice("blending", tl!("Blending"), blend_modes(), "multiply"), Param::new("opacity", tl!("Opacity"), ParamKind::number(Some(0.0), Some(100.0), 100.0, 1.0, 0)), Param::new("scale", tl!("Scale"), ParamKind::number(Some(0.001), Some(2.0), 1.0, 0.1, 3)), Param::new("offset", tl!("Offset"), ParamKind::int(Some(-255), Some(255), 0)), Param::new("preserveTransparency", tl!("Preserve Transparency"), ParamKind::bool(false)), - ]; + ]); rows.extend(apply_image_mask_rows(app)); rows } @@ -669,6 +670,78 @@ fn channel_form(app: &PhotocraftApp, id: &str) -> Option<(Vec, DialogFiel Some((rows, DialogFields::new())) } +/// Apply Image's Source (#14480): the index of one of [`apply_image_documents`], the target by default. +const SOURCE_DOCUMENT: &str = "__sourceDocument"; +/// Shows the target's own Layer and Channel rows (`sourceLayer`, `sourceChannel`): the target is the chosen Source. +const SOURCE_IS_TARGET: &str = "__sourceIsTarget"; + +/// The Layer row of source document `index` when it is not the target. +fn source_layer_key(index: usize) -> String { + format!("__sourceLayer/{index}") +} + +/// The Channel row of source document `index` when it is not the target. +fn source_channel_key(index: usize) -> String { + format!("__sourceChannel/{index}") +} + +/// Shows the Layer and Channel rows of source document `index`. +fn source_shown_key(index: usize) -> String { + format!("__sourceShown/{index}") +} + +/// Apply Image's Source row (#14480): the documents of [`apply_image_documents`], the target first chosen. +fn apply_image_source_rows(app: &PhotocraftApp) -> Vec { + let names: Vec<(String, Label)> = apply_image_documents(app).iter().map(|(i, d)| (i.to_string(), Label::from(d.name.clone()))).collect(); + let target = app.session.active_index().map(|i| i.to_string()).unwrap_or_default(); + vec![choice(SOURCE_DOCUMENT, tl!("Source"), names, &target)] +} + +/// The Layer and Channel rows of each source document other than the target (#14480): its Layer list (as the +/// target's) and its channels (the composite, the colour channels of a colour document, the alpha channels). +fn apply_image_other_source_rows(app: &PhotocraftApp) -> Vec { + let target = app.session.active_index(); + let mut rows = Vec::new(); + for (i, doc) in apply_image_documents(app).into_iter().filter(|(i, _)| Some(*i) != target) { + let (layers, layer) = apply_image_layers(doc); + let mode = doc.pixel_format().mode; + let mut list = vec![photocraft_engine::channel_cmds::composite_name(mode).to_string()]; + if mode.color_channels() > 1 { + list.extend(photocraft_engine::channel_cmds::color_names(mode).iter().map(|n| n.to_string())); + } + list.extend(doc.channels.iter().map(|c| c.name.clone())); + let composite = list.first().cloned().unwrap_or_default(); + rows.push(choice(&source_layer_key(i), tl!("Layer"), channels(&layers), &layer).visible_when(source_shown_key(i), true)); + rows.push(choice(&source_channel_key(i), tl!("Channel"), channels(&list), &composite).visible_when(source_shown_key(i), true)); + } + rows +} + +/// Brings the Source group's shell keys and the source parameter up to date: the target as Source shows its own +/// Layer and Channel rows, which send the flat `sourceLayer` / `sourceChannel` / `sourceInvert` as before; another +/// document shows its own pair and sends `source: {document, layer, channel, invert}`, which the command reads before +/// the flat keys. +fn apply_image_source_derive(fields: &mut DialogFields) { + let chosen = fields.text(SOURCE_DOCUMENT).and_then(|s| s.parse::().ok()); + let others: Vec = fields.iter().filter_map(|(k, _)| k.strip_prefix("__sourceLayer/").and_then(|i| i.parse().ok())).collect(); + for &i in &others { + fields.set(source_shown_key(i), chosen == Some(i)); + } + fields.remove("source"); + let other = chosen.filter(|i| others.contains(i)); + fields.set(SOURCE_IS_TARGET, other.is_none()); + let Some(i) = other else { return }; + let (Some(layer), Some(channel)) = (fields.text(&source_layer_key(i)).map(str::to_owned), fields.text(&source_channel_key(i)).map(str::to_owned)) else { + return; + }; + let invert = fields.bool("sourceInvert") == Some(true); + fields.set("source", FieldValue::Json(json!({"document": i, "layer": layer, "channel": channel, "invert": invert}))); +} + +#[cfg(test)] +#[path = "../../dialogs/apply_image_source_document_tests.rs"] +mod apply_image_source_document_tests; + /// Apply Image's Mask... checkbox (#14489): off by default; ticked, it shows the mask group below it. const MASK_ON: &str = "__mask"; /// The mask group's Source: the index of one of [`apply_image_documents`]. @@ -698,7 +771,11 @@ fn mask_shown_key(index: usize) -> String { pub(crate) fn apply_image_documents(app: &PhotocraftApp) -> Vec<(usize, &photocraft_doc::Document)> { let Some(target) = app.session.active() else { return Vec::new() }; let (size, depth) = (target.doc.size, target.doc.depth); - app.session.documents().iter().enumerate().filter(|(_, st)| st.doc.size == size && st.doc.depth == depth).map(|(i, st)| (i, &*st.doc)).collect() + let mut documents: Vec<(usize, &photocraft_doc::Document)> = + app.session.documents().iter().enumerate().filter(|(_, st)| st.doc.size == size && st.doc.depth == depth).map(|(i, st)| (i, &*st.doc)).collect(); + // Document ids are handed out as documents open, so they keep the opening order when tabs are dragged. + documents.sort_by_key(|(_, d)| d.id.0); + documents } /// The Layer list of a source document and its default: Merged, then its pixel layers top to bottom (the engine takes