diff --git a/crates/ui-egui/src/dialogs/rasterize_prompt.rs b/crates/ui-egui/src/dialogs/rasterize_prompt.rs index ae1bc659cd..8b8236d4a9 100644 --- a/crates/ui-egui/src/dialogs/rasterize_prompt.rs +++ b/crates/ui-egui/src/dialogs/rasterize_prompt.rs @@ -6,7 +6,8 @@ //! The prompt is a `StandardPrompt` on the dialog host (`ui.inspect` shows it, `ui.dialog.confirm` //! and `ui.dialog.cancel` answer it). OK runs the matching `layer.rasterize.*` command and nothing //! else: the click that opened the prompt is not replayed, as in Photoshop (#8806), so the next -//! click paints. Cancel does nothing. +//! click paints. Cancel does nothing. The Patch tool's prompt is the exception (#12121): its drag is +//! over when that prompt opens, so OK also applies the `paint.patch` the drag worked out. //! //! A filter on a type layer asks the same question (#4303), with Photoshop's Convert to Smart //! Object / Rasterize / Cancel: the layer is converted or rasterized, then the filter is chosen @@ -30,12 +31,17 @@ pub enum Kind { } /// What the prompt asked about: rasterize `layer` (a `kind` layer); with `filter`, then run that -/// filter and its params on it. +/// filter and its params on it; with `patch`, then run `paint.patch` with those params (#12121). // Whether Convert is offered is fixed when the prompt opens (the button exists or not), so the tag does not carry it. pub(crate) struct Rasterize { kind: Kind, + /// The document `layer` was taken from. A document read back from a file keeps the layer ids + /// stored in it, so one number can name a layer of another open document; see + /// [`active_doc`]. + doc: u64, layer: u64, filter: Option<(String, Value)>, + patch: Option, } impl Kind { @@ -113,6 +119,34 @@ pub fn intercept(app: &mut PhotocraftApp, tool: Tool, x: f64, y: f64, pressure: true } +/// The document a prompt is opened for, as the id of the active document. The tag carries numbers, +/// and `Session::add_document` only re-picks a document id, so two documents read from files can hold +/// the same layer id (`photocraft_format` keeps the ids stored in a native file). `u64::MAX` means no +/// document: nothing matches it, so the answer refuses. See [`stale_document`]. +fn active_doc(app: &PhotocraftApp) -> u64 { + app.session.active().map_or(u64::MAX, |st| st.doc.id.0) +} + +/// The refusal the writing answers share. The tag names its target by number, and a document read +/// back from a file keeps the layer ids stored in it, so that number can name a layer of another +/// open document: when the active document is not the one the question was opened for, the answer +/// writes in neither of them (#12121). +fn stale_document(app: &PhotocraftApp, doc: u64) -> Option { + (active_doc(app) != doc).then(|| "the document this question was about is no longer the active document, so nothing was rasterized".to_owned()) +} + +/// Opens the same question for a Patch drag on a `layer` that must be rasterized first: OK rasterizes +/// it and then runs `paint.patch` with `params`, the params the finished drag had worked out, so the +/// patch lands on the rasterized layer (#12121). Cancel does nothing. +pub fn open_patch(app: &mut PhotocraftApp, kind: Kind, layer: photocraft_doc::LayerId, params: Value) -> DialogId { + let prompt = StandardPrompt::custom(tl!(kind.message()).to_owned()) + .title(tl!("PhotoCraft").to_owned()) + .button(PromptChoice::Proceed, tl!("OK").to_owned()) + .button(PromptChoice::Cancel, tl!("Cancel").to_owned()) + .tag(Rasterize { kind, doc: active_doc(app), layer: layer.0, filter: None, patch: Some(params) }); + alerts::open_prompt(app, prompt).unwrap_or_else(|open| open) +} + /// Opens the prompt for `layer`; OK rasterizes it, and the id of the prompt already open comes back /// when there is one. `tool` and `at` (x, y, pressure) name the press that opened it; the press is /// never replayed, so they change nothing. @@ -121,7 +155,7 @@ pub fn open(app: &mut PhotocraftApp, kind: Kind, layer: photocraft_doc::LayerId, .title(tl!("PhotoCraft").to_owned()) .button(PromptChoice::Proceed, tl!("OK").to_owned()) .button(PromptChoice::Cancel, tl!("Cancel").to_owned()) - .tag(Rasterize { kind, layer: layer.0, filter: None }); + .tag(Rasterize { kind, doc: active_doc(app), layer: layer.0, filter: None, patch: None }); alerts::open_prompt(app, prompt).unwrap_or_else(|open| open) } @@ -164,8 +198,10 @@ pub fn intercept_filter(app: &mut PhotocraftApp, id: &str, params: &Value) -> Op } let prompt = prompt.button(PromptChoice::Cancel, tl!("Cancel").to_owned()).tag(Rasterize { kind: Kind::Type, + doc: active_doc(app), layer: layer.0, filter: Some((id.to_owned(), params.clone())), + patch: None, }); let dialog = alerts::open_prompt(app, prompt).unwrap_or_else(|open| open); Some(Ok(json!({"dialog": dialog.value()}))) @@ -173,13 +209,17 @@ pub fn intercept_filter(app: &mut PhotocraftApp, id: &str, params: &Value) -> Op /// The `PROMPTS` entry: Proceed rasterizes, Alternate converts to a Smart Object (the filter prompt /// only), Cancel does nothing. The filter of a filter prompt runs at once, as the menu would, now -/// that the layer can take it. `false` for another family's answer. +/// that the layer can take it. A patch prompt's Proceed runs `paint.patch` on the layer it just +/// rasterized (#12121). Every answer that writes first refuses when the active document is not the +/// one the question was opened for ([`stale_document`]). `false` for another family's answer. pub(crate) fn answer(app: &mut PhotocraftApp, ctx: &egui::Context, answer: &mut PromptAnswer) -> bool { - let Some(Rasterize { kind, layer, filter }) = answer.take_tag::() else { return false }; - let result = match (answer.choice, filter) { - (PromptChoice::Proceed, None) => rasterize(app, kind, layer), - (PromptChoice::Proceed | PromptChoice::Alternate, Some((filter, params))) => { - convert_for_filter(app, ctx, answer.choice == PromptChoice::Alternate, &filter, params, layer) + let Some(Rasterize { kind, doc, layer, filter, patch }) = answer.take_tag::() else { return false }; + let result = match (answer.choice, filter, patch) { + (PromptChoice::Proceed, None, None) => rasterize(app, kind, doc, layer), + // The Patch drag had already ended when this prompt opened, so its params re-run here. + (PromptChoice::Proceed, None, Some(params)) => rasterize_then_patch(app, kind, doc, layer, params), + (PromptChoice::Proceed | PromptChoice::Alternate, Some((filter, params)), None) => { + convert_for_filter(app, ctx, answer.choice == PromptChoice::Alternate, &filter, params, doc, layer) } _ => return true, }; @@ -188,7 +228,10 @@ pub(crate) fn answer(app: &mut PhotocraftApp, ctx: &egui::Context, answer: &mut } /// OK: rasterize the layer. The click that opened the prompt does not paint. -fn rasterize(app: &mut PhotocraftApp, kind: Kind, layer: u64) -> Result { +fn rasterize(app: &mut PhotocraftApp, kind: Kind, doc: u64, layer: u64) -> Result { + if let Some(why) = stale_document(app, doc) { + return Err(why); + } let revision = app.session.active().map(|st| st.revision); let r = app.run(kind.command(), json!({ "layer": layer }))?; // A painting tool's type prompt names its step as the reference does (#12888); Layer › @@ -203,8 +246,27 @@ fn rasterize(app: &mut PhotocraftApp, kind: Kind, layer: u64) -> Result Result { + // `rasterize` answers for the document the question was opened for, so a patch can never follow + // it into another document whose layers happen to carry the same ids (#12121). + let rasterized = rasterize(app, kind, doc, layer)?; + // The patch params name no layer: run as they stand, its pixels go to whatever is active when OK + // comes. The rasterize above settled the layer the question was about, so a different active + // layer now refuses the patch instead of painting somewhere else (#12121). + if app.session.active().and_then(|st| st.active_layer).map(|id| id.0) != Some(layer) { + return Err("the layer this question was about is no longer the active layer, so the patch was not applied".into()); + } + let patch = app.run("paint.patch", params)?; + Ok(json!({ "rasterized": rasterized, "patched": patch })) +} + /// The filter prompt's answer: convert (`smart`) or rasterize the layer, then run the filter. -fn convert_for_filter(app: &mut PhotocraftApp, ctx: &egui::Context, smart: bool, filter: &str, params: Value, layer: u64) -> Result { +fn convert_for_filter(app: &mut PhotocraftApp, ctx: &egui::Context, smart: bool, filter: &str, params: Value, doc: u64, layer: u64) -> Result { + if let Some(why) = stale_document(app, doc) { + return Err(why); + } let command = if smart { "layer.smartObjects.convertToSmartObject" } else { Kind::Type.command() }; let converted = app.run(command, json!({ "layer": layer }))?; crate::menus::invoke(app, ctx, filter, params)?; diff --git a/crates/ui-egui/src/dialogs/rasterize_prompt/tests.rs b/crates/ui-egui/src/dialogs/rasterize_prompt/tests.rs index 51ba8c87ea..176bcc374f 100644 --- a/crates/ui-egui/src/dialogs/rasterize_prompt/tests.rs +++ b/crates/ui-egui/src/dialogs/rasterize_prompt/tests.rs @@ -183,6 +183,61 @@ fn a_stale_prompt_fails_gracefully() { assert_eq!(past(&app), before); } +/// Review round: the prompt names its layer by number, and a document read back from a file keeps the +/// layer ids stored in it, so two open documents can hold the same number. A brush press in one +/// document whose question is answered while another document is active writes in neither of them. +#[test] +fn a_document_switch_while_the_prompt_is_up_rasterizes_nothing() { + let mut app = app_with(Kind::Type); + app.ui.tool = Tool::Brush; + let mine = app.session.active_index().unwrap(); + let text = app.session.active().unwrap().active_layer.unwrap(); + click(&mut app, 30.0, 20.0); + let id = prompt(&app).expect("the press opened the prompt"); + // A second document made from the bytes of the first: its layers keep the ids stored in the file, + // so the same number names a Type layer there too, and that layer is active there. + let bytes = photocraft_format::save_to_bytes(&app.session.active().unwrap().doc, &Default::default()).unwrap(); + let twin = photocraft_format::load_from_bytes(&bytes).unwrap(); + assert_eq!(twin.layer(text).map(|l| l.content.kind_name()), Some("Type"), "both documents hold a Type layer {}", text.0); + app.session.add_document(twin, None); + let other = app.session.active_index().unwrap(); + assert_ne!(other, mine, "the second document is the active one now"); + assert_eq!(app.session.documents()[other].active_layer, Some(text), "the other document's active layer carries the same id"); + let (before_other, before_mine) = (app.session.documents()[other].revision, app.session.documents()[mine].revision); + answer(&mut app, id); + assert!(app.ui.status_error, "OK says why nothing ran: {}", app.ui.status); + // Both documents are read live, after the answer. + assert_eq!(app.session.documents()[other].revision, before_other, "no history step in the document that is active now"); + assert_eq!(app.session.documents()[other].doc.layer(text).unwrap().content.kind_name(), "Type", "its Type layer stays editable"); + assert_eq!(app.session.documents()[mine].revision, before_mine, "the document the question was about is untouched"); + assert_eq!(app.session.documents()[mine].doc.layer(text).unwrap().content.kind_name(), "Type", "and keeps its text"); +} + +/// The Filter menu's prompt keeps its question inside the document it was opened for for the same +/// reason: OK would otherwise rasterize or convert a same-id layer of another open document and run +/// the filter on that one. +#[test] +fn the_filter_prompt_refuses_a_document_that_is_no_longer_active() { + let mut app = app_with(Kind::Type); + let mine = app.session.active_index().unwrap(); + let text = app.session.active().unwrap().active_layer.unwrap(); + menu(&mut app, BLUR).unwrap(); + let id = prompt(&app).expect("the filter asks first"); + let bytes = photocraft_format::save_to_bytes(&app.session.active().unwrap().doc, &Default::default()).unwrap(); + let twin = photocraft_format::load_from_bytes(&bytes).unwrap(); + assert_eq!(twin.layer(text).map(|l| l.content.kind_name()), Some("Type"), "both documents hold a Type layer {}", text.0); + app.session.add_document(twin, None); + let other = app.session.active_index().unwrap(); + let (before_other, before_mine) = (app.session.documents()[other].revision, app.session.documents()[mine].revision); + answer(&mut app, id); + assert!(app.ui.status_error, "OK says why nothing ran: {}", app.ui.status); + assert_eq!(app.session.documents()[other].revision, before_other, "no history step in the document that is active now"); + assert_eq!(app.session.documents()[other].doc.layer(text).unwrap().content.kind_name(), "Type", "its Type layer stays editable"); + assert!(filter_dialog(&app, BLUR).is_none(), "Gaussian Blur did not open on the other document"); + assert_eq!(app.session.documents()[mine].revision, before_mine, "the document the question was about is untouched"); + assert_eq!(app.session.documents()[mine].doc.layer(text).unwrap().content.kind_name(), "Type", "and keeps its text"); +} + #[test] fn the_control_channel_sees_and_confirms_the_prompt() { let mut app = app_with(Kind::Shape); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 126c75ddaa..cce1b96da7 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -743,6 +743,8 @@ mod eyedropper_context_menu_tests; #[cfg(test)] mod menu_automate_groups_tests; #[cfg(test)] +mod patch_rasterize_prompt_tests; +#[cfg(test)] mod patch_shift_constrain_tests; #[cfg(test)] mod paths_scroll_tests; diff --git a/crates/ui-egui/src/patch_rasterize_prompt_tests.rs b/crates/ui-egui/src/patch_rasterize_prompt_tests.rs new file mode 100644 index 0000000000..71819b1b6b --- /dev/null +++ b/crates/ui-egui/src/patch_rasterize_prompt_tests.rs @@ -0,0 +1,323 @@ +//! #12121: dragging the Patch selection on a Type layer asks the rasterize question, as the +//! painting tools do, instead of refusing in the status bar. OK rasterizes the layer and applies +//! the patch the drag worked out; Cancel changes nothing. A lasso drag with the Patch tool still +//! only makes the selection. Shape layers, Content-Aware mode and a targeted mask are *not +//! measured* in the issue and keep today's behaviour, pinned here. + +use craft::ui::dialog::DialogId; +use egui::Modifiers; +use serde_json::{Value, json}; + +use crate::PhotocraftApp; +use crate::canvas::{ToolEvent, tool_event}; +use crate::dialogs::alerts; +use crate::rasterize_prompt::Rasterize; +use crate::state::Tool; + +/// A 200 x 120 document whose active layer holds `kind` content (a Type, Shape, Smart Object or +/// fill layer, or plain pixels), the Patch tool active, and a hard 40 x 30 rectangle selected at +/// 40,40. +fn app(kind: &str) -> Box { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + // The rasterize step and the patch settle in the pass that asks for them. + app.background_jobs = false; + app.run("file.new", json!({"width": 200, "height": 120, "background": "white"})).unwrap(); + match kind { + "Type" => app.run("type.create", json!({"x": 20, "y": 60, "text": "Hello world"})).map(|_| ()), + "Shape" => app.run("shape.create", json!({"kind": "rect", "rect": [20, 40, 90, 70], "fill": "#ff0000"})).map(|_| ()), + "Smart Object" => { + app.run("layer.new.layer", json!({})).unwrap(); + app.run("edit.fill", json!({"color": "#00ff00"})).unwrap(); + app.run("layer.smartObjects.convertToSmartObject", json!({})).map(|_| ()) + } + "Fill" => app.run("layer.newFillLayer.solidColor", json!({"color": "#00ff00"})).map(|_| ()), + _ => Ok(()), + } + .unwrap(); + app.run("select.rect", json!({"x": 40, "y": 40, "width": 40, "height": 30, "mode": "replace"})).unwrap(); + // The layer's own pixels, not its mask. + app.ui.mask_target = false; + app.sync_mask_targets(); + app.ui.tool = Tool::Patch; + app.ui.tool_options.patch_content_aware = false; + app +} + +/// Drags the patch from inside the selection at 55,50 to 115,50: the offset 60,0. +fn drag_patch(app: &mut PhotocraftApp) { + let m = Modifiers::NONE; + tool_event(app, ToolEvent::Down { x: 55.0, y: 50.0, pressure: 1.0 }, m); + tool_event(app, ToolEvent::Move { x: 80.0, y: 50.0, pressure: 1.0 }, m); + tool_event(app, ToolEvent::Move { x: 115.0, y: 50.0, pressure: 1.0 }, m); + tool_event(app, ToolEvent::Up { x: 115.0, y: 50.0 }, m); +} + +/// The open rasterize prompt, whichever family opened it. +fn prompt(app: &PhotocraftApp) -> Option { + alerts::prompt_tagged::(app) +} + +/// OK on the open prompt, as `ui.dialog.confirm` does: the answer acts inside the call. +fn answer(app: &mut PhotocraftApp, id: DialogId) { + let ctx = app.ctx().clone(); + crate::dialogs::confirm(app, &ctx, id).unwrap(); +} + +/// The params of every `paint.patch` in the History. +fn patch_params(app: &PhotocraftApp) -> Vec { + app.session.journal.iter().filter(|(id, _)| id == "paint.patch").map(|(_, p)| p.clone()).collect() +} + +fn past(app: &PhotocraftApp) -> usize { + app.session.active().unwrap().history.past_len() +} + +/// The active layer's content, as "Type", "Shape", "Pixel" ... +fn active_content(app: &PhotocraftApp) -> &'static str { + let st = app.session.active().unwrap(); + st.doc.layer(st.active_layer.unwrap()).unwrap().content.kind_name() +} + +/// The selected area as [x, y, width, height], for the Destination mode's moved selection. +fn selection_bounds(app: &PhotocraftApp) -> [i32; 4] { + let sel = app.session.active().unwrap().doc.selection.clone().unwrap(); + let b = sel.content_bounds(); + [b.x0, b.y0, b.x1 - b.x0, b.y1 - b.y0] +} + +/// Done when 1: the drag on a Type layer asks instead of refusing; nothing happens until the answer. +#[test] +fn a_patch_drag_on_a_type_layer_asks_to_rasterize_first() { + let mut app = app("Type"); + let before = (past(&app), selection_bounds(&app)); + drag_patch(&mut app); + prompt(&app).unwrap_or_else(|| panic!("the patch drag opened no prompt; status: {}", app.ui.status)); + assert!(!app.ui.status_error, "the question stands in place of the status-bar refusal: {}", app.ui.status); + assert_eq!(active_content(&app), "Type", "the layer waits for the answer"); + assert_eq!(past(&app), before.0, "nothing is recorded before the answer"); + assert_eq!(selection_bounds(&app), before.1, "the selection is where the drag left it"); + assert!(patch_params(&app).is_empty(), "no patch ran"); + assert!(!crate::patch_preview::showing(&app), "the healed preview is off the canvas once the question is up"); + // A second drag while the question is up does not stack another prompt. + drag_patch(&mut app); + assert_eq!(app.framework.dialogs.open_dialogs().len(), 1, "{:?}", app.framework.dialogs.open_dialogs()); +} + +/// Done when 1: OK rasterizes the layer and applies the patch the drag worked out. Destination +/// mode also moves the selection, which proves the patch ran without depending on the glyphs. +#[test] +fn ok_rasterizes_the_layer_and_applies_the_patch() { + let mut app = app("Type"); + app.ui.tool_options.patch_mode = "destination".into(); + let before = (past(&app), selection_bounds(&app)); + drag_patch(&mut app); + let id = prompt(&app).expect("the prompt"); + answer(&mut app, id); + assert!(!app.ui.status_error, "{}", app.ui.status); + assert_eq!(active_content(&app), "Pixel", "OK rasterizes the type layer"); + assert_eq!(past(&app), before.0 + 2, "rasterize, then the patch"); + assert_eq!(patch_params(&app), vec![json!({"offset": [60, 0], "mode": "destination", "target": "pixels"})]); + let [x, y, w, h] = before.1; + assert_eq!(selection_bounds(&app), [x + 60, y, w, h], "the patch ran on the rasterized layer"); + assert!(app.session.active().unwrap().doc.layer(app.session.active().unwrap().active_layer.unwrap()).unwrap().surface().is_some()); + // One undo takes the patch back and keeps the rasterized layer. + app.session.undo(); + assert_eq!(active_content(&app), "Pixel"); + assert_eq!(selection_bounds(&app), [x, y, w, h]); + app.session.undo(); + assert_eq!(active_content(&app), "Type"); +} + +/// Done when 1: Cancel leaves the Type layer, the selection and the History exactly as they were. +#[test] +fn cancel_changes_nothing() { + let mut app = app("Type"); + let before = (past(&app), app.session.active().unwrap().doc.clone()); + drag_patch(&mut app); + let id = prompt(&app).expect("the prompt"); + let ctx = app.ctx().clone(); + crate::dialogs::cancel(&mut app, &ctx, id).unwrap(); + assert!(prompt(&app).is_none()); + assert!(app.framework.dialogs.is_empty()); + assert_eq!(past(&app), before.0); + assert_eq!(*app.session.active().unwrap().doc, *before.1, "the document is untouched"); + assert_eq!(active_content(&app), "Type"); + assert!(patch_params(&app).is_empty()); +} + +/// Done when 2: a lasso drag with the Patch tool on a Type layer only makes the selection. +#[test] +fn a_lasso_drag_makes_the_selection_without_asking() { + let mut app = app("Type"); + let m = Modifiers::NONE; + // Outside the selection, so this is a lasso and not a patch drag. + tool_event(&mut app, ToolEvent::Down { x: 120.0, y: 80.0, pressure: 1.0 }, m); + for [x, y] in [[180.0, 80.0], [180.0, 110.0], [150.0, 110.0]] { + tool_event(&mut app, ToolEvent::Move { x, y, pressure: 1.0 }, m); + } + tool_event(&mut app, ToolEvent::Up { x: 150.0, y: 110.0 }, m); + assert!(prompt(&app).is_none(), "the outline makes a selection, it does not paint"); + assert!(patch_params(&app).is_empty()); + assert!(!app.ui.status_error, "{}", app.ui.status); + let sel = app.session.active().unwrap().doc.selection.clone().unwrap(); + assert!(sel.sample_channel(160, 95, 0) > 0.0, "the lasso replaced the selection"); + assert_eq!(sel.sample_channel(55, 50, 0), 0.0, "the rectangle is gone"); + assert_eq!(active_content(&app), "Type"); +} + +/// Not measured: a patch of the type layer's own mask paints no pixels, so it still runs unasked. +#[test] +fn a_type_layers_mask_still_takes_the_patch() { + let mut app = app("Type"); + app.run("layer.layerMask.revealAll", json!({})).unwrap(); + app.ui.mask_target = true; + app.sync_mask_targets(); + let before = past(&app); + drag_patch(&mut app); + assert!(prompt(&app).is_none(), "the mask needs no rasterizing"); + assert!(!app.ui.status_error, "{}", app.ui.status); + assert_eq!(patch_params(&app), vec![json!({"offset": [60, 0], "mode": "source", "target": "mask"})]); + assert_eq!(past(&app), before + 1, "the mask patch is one step"); + assert_eq!(active_content(&app), "Type", "the layer stays editable"); +} + +/// Not measured: Content-Aware mode on a Type layer keeps today's status-bar refusal. +#[test] +fn content_aware_mode_on_a_type_layer_still_refuses() { + let mut app = app("Type"); + app.ui.tool_options.patch_content_aware = true; + let before = past(&app); + drag_patch(&mut app); + assert!(prompt(&app).is_none(), "Content-Aware mode on a type layer was not measured"); + assert!(app.ui.status_error, "the refusal is still reported: {}", app.ui.status); + assert!(app.ui.status.contains("Type layer"), "unexpected refusal: {}", app.ui.status); + assert!(patch_params(&app).is_empty()); + assert_eq!(past(&app), before); + assert_eq!(active_content(&app), "Type"); +} + +/// Not measured: a Shape, Smart Object or fill layer keeps today's status-bar refusal, no prompt. A +/// fill layer carries a mask, so the engine's own check lets it through and its surface lookup is +/// what says "not a pixel layer"; the wording differs from the other two and is pinned as it is. +#[test] +fn the_other_layer_kinds_still_refuse() { + for (kind, words) in + [("Shape", "a Shape layer, not a pixel layer"), ("Smart Object", "a Smart Object layer, not a pixel layer"), ("Fill", "not a pixel layer")] + { + let mut app = app(kind); + let before = past(&app); + drag_patch(&mut app); + assert!(prompt(&app).is_none(), "{kind}: no prompt was measured for it"); + assert!(app.ui.status_error, "{kind}: the refusal is still reported: {}", app.ui.status); + assert!(app.ui.status.contains(words), "{kind}: unexpected refusal: {}", app.ui.status); + assert!(patch_params(&app).is_empty(), "{kind}"); + assert_eq!(past(&app), before, "{kind}"); + assert_eq!(active_content(&app), kind, "{kind} stays editable"); + } +} + +/// Review round: a Type layer that is hidden, or sits in a hidden group, keeps today's refusal. The +/// engine never writes there, so a prompt would only let OK destroy the text for no patch. +#[test] +fn a_hidden_type_layer_is_refused_without_a_prompt() { + for group in [false, true] { + let mut app = app("Type"); + let text = app.session.active().unwrap().active_layer.unwrap(); + if group { + let wrapped = app.run("layer.new.group", json!({"name": "Wrapped"})).unwrap()["layer"].as_u64().unwrap(); + app.run("layer.moveTo", json!({"layer": text.0, "target": wrapped, "position": "into"})).unwrap(); + app.run("layer.hideLayers", json!({"layer": wrapped})).unwrap(); + } else { + app.run("layer.hideLayers", json!({"layer": text.0})).unwrap(); + } + let what = if group { "type layer in a hidden group" } else { "hidden type layer" }; + assert_eq!(app.session.active().unwrap().active_layer, Some(text), "{what}: the type layer is the active one"); + let before = (past(&app), app.session.active().unwrap().doc.clone()); + drag_patch(&mut app); + assert!(prompt(&app).is_none(), "{what}: no prompt, the patch could never land there"); + assert!(app.ui.status_error, "{what}: the refusal stands: {}", app.ui.status); + assert!(patch_params(&app).is_empty(), "{what}"); + assert_eq!(past(&app), before.0, "{what}"); + assert_eq!(*app.session.active().unwrap().doc, *before.1, "{what}: the document is untouched"); + assert_eq!(active_content(&app), "Type", "{what} stays editable"); + } +} + +/// Review round: OK applies the patch to the layer the question was about. The params name no layer, +/// so a different layer that is active when OK comes must not take the pixels. +#[test] +fn ok_after_the_active_layer_changed_refuses_the_patch() { + let mut app = app("Type"); + let text = app.session.active().unwrap().active_layer.unwrap(); + drag_patch(&mut app); + let id = prompt(&app).expect("the prompt"); + // Another layer becomes the active one while the question is up. + let elsewhere = app.run("layer.new.layer", json!({"name": "Elsewhere"})).unwrap()["layer"].as_u64().unwrap(); + let before = past(&app); + answer(&mut app, id); + assert_eq!(app.session.active().unwrap().active_layer, Some(photocraft_doc::LayerId(elsewhere)), "the new layer is the active one"); + assert!(app.ui.status_error, "OK says why the patch did not run: {}", app.ui.status); + assert!(patch_params(&app).is_empty(), "the patch did not run on the layer that is active now"); + assert_eq!(past(&app), before + 1, "the layer the question was about is still rasterized"); + let st = app.session.active().unwrap(); + assert_eq!(st.doc.layer(text).unwrap().content.kind_name(), "Pixel", "the stored layer took the rasterize"); +} + +/// Review round: a document read from a file keeps the layer ids stored in it, so two open +/// documents can hold the same number. OK must not rasterize or patch in another document than the +/// one the question was opened for. +#[test] +fn ok_after_another_document_became_active_refuses_the_patch() { + let mut app = app("Type"); + let text = app.session.active().unwrap().active_layer.unwrap(); + let mine = app.session.active_index().unwrap(); + drag_patch(&mut app); + let id = prompt(&app).expect("the prompt"); + // A second document made from this one's bytes: its layers keep the ids stored in the file, so + // the same number names a Type layer there too, and that layer is the active one there. + let bytes = photocraft_format::save_to_bytes(&app.session.active().unwrap().doc, &Default::default()).unwrap(); + let twin = photocraft_format::load_from_bytes(&bytes).unwrap(); + assert_eq!(twin.layer(text).map(|l| l.content.kind_name()), Some("Type"), "both documents hold a Type layer {}", text.0); + app.session.add_document(twin, None); + let other = app.session.active_index().unwrap(); + assert_ne!(other, mine, "the second document is the active one now"); + assert_eq!(app.session.documents()[other].active_layer, Some(text), "the other document's active layer carries the same id"); + let (before_other, before_mine) = (app.session.documents()[other].revision, app.session.documents()[mine].revision); + answer(&mut app, id); + assert!(app.ui.status_error, "OK says why nothing ran: {}", app.ui.status); + assert!(patch_params(&app).is_empty(), "no patch ran in the other document"); + // Read both documents live, after the answer: a copy taken before it would show the old document + // however much the answer changed it. + assert_eq!(app.session.documents()[other].revision, before_other, "the other document is untouched"); + assert_eq!(app.session.documents()[other].doc.layer(text).unwrap().content.kind_name(), "Type", "the other document keeps its text"); + assert_eq!(app.session.documents()[mine].revision, before_mine, "the document the question was about is untouched too"); + assert_eq!(app.session.documents()[mine].doc.layer(text).unwrap().content.kind_name(), "Type", "and keeps its text"); +} + +/// Pin, passes before and after the change: a locked Type layer cannot be rasterized either, so the +/// drag keeps today's refusal and no prompt opens. +#[test] +fn a_locked_type_layer_still_refuses() { + let mut app = app("Type"); + app.run("layer.lockLayers", json!({"pixels": true})).unwrap(); + let before = (past(&app), app.session.active().unwrap().doc.clone()); + drag_patch(&mut app); + assert!(prompt(&app).is_none(), "a locked layer gets no prompt"); + assert!(app.ui.status_error, "the refusal stands: {}", app.ui.status); + assert!(patch_params(&app).is_empty()); + assert_eq!(past(&app), before.0); + assert_eq!(*app.session.active().unwrap().doc, *before.1, "the document is untouched"); + assert_eq!(active_content(&app), "Type"); +} + +/// A pixel layer is unaffected: the patch applies with no question asked. +#[test] +fn a_pixel_layer_patch_still_applies_without_asking() { + let mut app = app("Pixel"); + let before = past(&app); + drag_patch(&mut app); + assert!(prompt(&app).is_none()); + assert!(!app.ui.status_error, "{}", app.ui.status); + assert_eq!(patch_params(&app), vec![json!({"offset": [60, 0], "mode": "source", "target": "pixels"})]); + assert_eq!(past(&app), before + 1); +} diff --git a/crates/ui-egui/src/tools/retouch_ui.rs b/crates/ui-egui/src/tools/retouch_ui.rs index ab272483bf..3bf6011d19 100644 --- a/crates/ui-egui/src/tools/retouch_ui.rs +++ b/crates/ui-egui/src/tools/retouch_ui.rs @@ -476,6 +476,18 @@ pub fn finish_patch(app: &mut PhotocraftApp, start: [f64; 2], end: [f64; 2]) { return; } let p = json!({"offset": off, "mode": app.ui.tool_options.patch_mode, "target": crate::canvas::paint_target(app)}); + // #12121: on a Type layer the reference asks its rasterize question instead of refusing, and OK + // applies this very patch on the layer it just rasterized. Only the measured case asks: the + // layer's own pixels in plain mode, and only on a layer that is actually shown: the engine never + // writes to a hidden one, so a prompt there would let OK destroy the text for no patch. Every + // other target, hidden layer and layer kind keeps the engine's refusal. + if let Some((kind, layer)) = crate::rasterize_prompt::needed(app) + && kind == crate::rasterize_prompt::Kind::Type + && photocraft_engine::hidden_target::refusal(&app.session, "paint.patch", &p).is_none() + { + crate::rasterize_prompt::open_patch(app, kind, layer, p); + return; + } match app.run("paint.patch", p) { Ok(_) => crate::patch_preview::committed(app, preview, off), Err(e) => {