diff --git a/crates/engine/src/cmd/actions_cmds.rs b/crates/engine/src/cmd/actions_cmds.rs index ea68d86b50..d436b0e48c 100644 --- a/crates/engine/src/cmd/actions_cmds.rs +++ b/crates/engine/src/cmd/actions_cmds.rs @@ -390,10 +390,19 @@ fn record(s: &mut Session, p: &Value) -> Result { Ok(json!({"action": name, "index": idx})) } +/// Whether a journal entry is recorded as an action step: a replayable command, except Check +/// Spelling's corrections and lookups. +fn recorded(id: &str, p: &Value) -> bool { + replayable(id) && (id != "edit.checkSpelling" || crate::type_spell_cmds::records(p)) +} + +/// Commands recorded with their step's dialog on: Check Spelling. +const RECORDED_WITH_DIALOG: &[&str] = &["edit.checkSpelling"]; + /// Replayable journal entries not yet committed to this action while recording. pub fn pending_steps(s: &Session, action: usize) -> impl Iterator { let from = s.actions.recording.filter(|(_, i)| *i == action).map_or(s.journal.len(), |(from, _)| from); - s.journal.iter().skip(from).filter(|(id, _)| replayable(id)) + s.journal.iter().skip(from).filter(|(id, p)| recorded(id, p)) } pub(crate) fn flush_recording(s: &mut Session) -> Result<()> { @@ -404,7 +413,10 @@ pub(crate) fn flush_recording(s: &mut Session) -> Result<()> { let steps: Vec<_> = pending_steps(s, idx).cloned().collect(); s.actions.recording = Some((s.journal.len(), idx)); if !steps.is_empty() { - s.actions.list[idx].steps.extend(steps); + let action = &mut s.actions.list[idx]; + let first = action.steps.len(); + action.dialogs.extend(steps.iter().enumerate().filter(|(_, (id, _))| RECORDED_WITH_DIALOG.contains(&id.as_str())).map(|(i, _)| first + i)); + action.steps.extend(steps); s.actions.touch(); } Ok(()) diff --git a/crates/engine/src/cmd/atn_cmds/events.rs b/crates/engine/src/cmd/atn_cmds/events.rs index e84e60b351..47c68a0a68 100644 --- a/crates/engine/src/cmd/atn_cmds/events.rs +++ b/crates/engine/src/cmd/atn_cmds/events.rs @@ -1,6 +1,6 @@ const DIFFUSE_MODE: &[(&str, &str)] = &[("Nrml", "normal"), ("DrkO", "darkenOnly"), ("LghO", "lightenOnly"), ("Anst", "anisotropic")]; use super::adjustments::{adjustment_by_class, adjustment_steps, levels_from, levels_to, shadows_highlights_from, shadows_highlights_to}; -use super::descriptors::{desc, get_bool, get_ref, jbool, layer_ref, layer_target, ref_class}; +use super::descriptors::{class, code, desc, get_bool, get_obj, get_ref, jbool, layer_ref, layer_target, ref_class, trimmed}; use super::fields::{F, Kind, Mapped, fields_from, fields_to, none, step}; use super::steps_doc::{ align_from, align_to, canvas_size_from, canvas_size_to, close_from, close_to, conditional_from, conditional_to, convert_mode_from, convert_mode_to, @@ -17,7 +17,7 @@ use super::steps_layer::{ }; use super::steps_make::{locking_from, locking_to, make_from, make_to, set_from, set_to}; use super::tables::{NOISE, OFFSET_FILL, SMART_BLUR_MODE, SMART_BLUR_QUALITY, TRIM}; -use super::{AtnEvent, D, Descriptor, STOP_MESSAGE, Value, json}; +use super::{AtnEvent, D, Descriptor, Id, ReferenceItem, STOP_MESSAGE, Value, json}; /// An event: code (empty when Photoshop only has a string name), string name, the panel /// label, the command ids it exports (the first is the one simple events import to), and the @@ -175,6 +175,44 @@ fn clipping_to(_: &str, p: &Value) -> Option { Some(desc("null").with("null", layer_ref(p)?)) } +/// Check Spelling's `null`: the `spellCheck` property of all text layers. +fn spell_check_ref() -> D { + D::Reference(vec![ + ReferenceItem::Property { class: class("Prpr"), key: Id::new("spellCheck") }, + ReferenceItem::Enumerated { class: class("TxLr"), type_id: code("Ordn"), value: code("Al") }, + ]) +} + +/// Edit › Check Spelling (`spellCheck`): `null` is [`spell_check_ref`] and `Usng` holds only +/// `checkAll`. Any other form stays a kept Photoshop step. +fn spell_check_from(d: &Descriptor) -> Mapped { + let same_ref = |r: &[ReferenceItem]| match r { + [ReferenceItem::Property { class: p, key }, ReferenceItem::Enumerated { class: t, type_id, value }] => { + trimmed(&p.class_id) == "Prpr" && key.is("spellCheck") && trimmed(&t.class_id) == "TxLr" && trimmed(type_id) == "Ordn" && trimmed(value) == "Al" + } + _ => false, + }; + let keys: Vec = d.items.iter().map(|(k, _)| trimmed(k)).collect(); + let using = get_obj(d, "Usng").filter(|u| u.class_id.is("spellCheck") && u.items.len() == 1); + match (keys.as_slice(), get_ref(d, "null"), using.and_then(|u| get_bool(u, "checkAll"))) { + ([null, usng], Some(r), Some(all)) if null == "null" && usng == "Usng" && same_ref(r) => step("edit.checkSpelling", json!({"allLayers": all})), + _ => Mapped::Unsupported { harmless: false }, + } +} + +/// A Check Spelling step that holds nothing but `allLayers` (Check All Layers, on by default). +fn spell_check_to(_: &str, p: &Value) -> Option { + let params = p.as_object()?; + if params.keys().any(|k| k != "allLayers") { + return None; + } + let all = match params.get("allLayers") { + None => true, + Some(v) => v.as_bool()?, + }; + Some(desc("null").with("null", spell_check_ref()).with("Usng", D::Descriptor(desc("spellCheck").with("checkAll", D::Boolean(all))))) +} + /// A clipping mask event: `clipping_from` / `clipping_to` with one command id. macro_rules! clipping_event { ($code:literal, $name:literal, $label:literal, $id:literal) => { @@ -581,6 +619,7 @@ pub(super) const EVENTS: &[Event] = &[ }, Event { code: "Rset", name: "reset", label: "Reset", ids: &["tools.defaultColors"], from: reset_from, to: reset_to }, ev!("Exch", "exchange", "Exchange", "tools.swapColors", &[]), + Event { code: "", name: "spellCheck", label: "Check Spelling", ids: &["edit.checkSpelling"], from: spell_check_from, to: spell_check_to }, Event { code: "", name: "assignProfile", diff --git a/crates/engine/src/cmd/atn_cmds/tests.rs b/crates/engine/src/cmd/atn_cmds/tests.rs index 5de5928052..24f09e2e5b 100644 --- a/crates/engine/src/cmd/atn_cmds/tests.rs +++ b/crates/engine/src/cmd/atn_cmds/tests.rs @@ -597,6 +597,8 @@ fn every_mapped_command_exports_and_imports_back() { ("edit.pasteSpecial.pasteInPlace", json!({})), ("edit.paste", json!({})), ("edit.purge.histories", json!({})), + ("edit.checkSpelling", json!({"allLayers": true})), + ("edit.checkSpelling", json!({"allLayers": false})), ("tools.defaultColors", json!({})), ("edit.convertToProfile", json!({"profile": "display-p3", "intent": "perceptual", "bpc": false})), ("edit.assignProfile", json!({"profile": "srgb"})), @@ -3805,3 +3807,66 @@ fn load_rejects_bad_encodings() { } assert!(s.actions.list.is_empty(), "nothing is loaded when the encoding is bad"); } + +/// Edit › Check Spelling in its recorded form: `spellCheck`, `null` = the `spellCheck` property of +/// all text layers, `Usng` = `checkAll`, with the step's dialog on. +fn spell_check(all: bool) -> Descriptor { + desc("null") + .with( + "null", + D::Reference(vec![ + ReferenceItem::Property { class: class("Prpr"), key: Id::new("spellCheck") }, + ReferenceItem::Enumerated { class: class("TxLr"), type_id: code("Ordn"), value: code("Al") }, + ]), + ) + .with("Usng", D::Descriptor(desc("spellCheck").with("checkAll", D::Boolean(all)))) +} + +#[test] +fn check_spelling_loads_and_saves_as_the_spell_check_event() { + for all in [true, false] { + let mut step = item("spellCheck", "Check Spelling", Some(spell_check(all))); + step.with_dialog = true; + let file = AtnFile { + name: UnicodeString::new_nul("S"), + actions: vec![AtnAction { name: UnicodeString::new_nul("A"), items: vec![step], ..Default::default() }], + ..Default::default() + }; + let imported = import(&atn::write(&file).unwrap()).unwrap(); + assert!(imported.unsupported.is_empty(), "{:?}", imported.unsupported); + let action = &imported.actions[0]; + assert_eq!(action.steps, [("edit.checkSpelling".to_string(), json!({"allLayers": all}))]); + assert_eq!(action.dialogs.iter().copied().collect::>(), [0]); + let (bytes, skipped) = export("S", &[action], &[], false).unwrap(); + assert!(skipped.is_empty()); + let saved = atn::parse(&bytes).unwrap(); + let saved = &saved.actions[0].items[0]; + assert_eq!(saved.event.id(), "spellCheck"); + assert!(saved.with_dialog); + assert_eq!(saved.descriptor.as_ref().unwrap().to_bytes(), spell_check(all).to_bytes(), "checkAll {all}"); + } +} + +#[test] +fn other_check_spelling_forms_stay_kept_steps_and_corrections_are_not_saved() { + let event = event_for(&AtnEvent::name("spellCheck")).unwrap(); + let mut extra = spell_check(true); + extra.items.push((Id::new("Fwd "), D::Boolean(true))); + let mut other_target = spell_check(true); + other_target.items[0].1 = D::Reference(vec![ReferenceItem::Property { class: class("Prpr"), key: Id::new("spellCheck") }]); + let mut other_class = spell_check(true); + other_class.items[1].1 = D::Descriptor(desc("findReplace").with("checkAll", D::Boolean(true))); + let mut more_using = spell_check(true); + more_using.items[1].1 = D::Descriptor(desc("spellCheck").with("checkAll", D::Boolean(true)).with("Fwd ", D::Boolean(true))); + for d in [extra, other_target, other_class, more_using, desc("null")] { + assert!(matches!((event.from)(&d), Mapped::Unsupported { harmless: false }), "{d:?}"); + } + for params in [ + json!({"action": "change", "start": 0, "end": 3, "replace": "The"}), + json!({"action": "changeAll", "word": "teh", "replace": "the"}), + json!({"allLayers": true, "layer": "Title"}), + json!({"allLayers": "yes"}), + ] { + assert!(export_item("edit.checkSpelling", ¶ms, "S").is_none(), "{params}"); + } +} diff --git a/crates/engine/src/cmd/layer_multi_cmds/selection.rs b/crates/engine/src/cmd/layer_multi_cmds/selection.rs index 8efcbe3783..a0b5f76a77 100644 --- a/crates/engine/src/cmd/layer_multi_cmds/selection.rs +++ b/crates/engine/src/cmd/layer_multi_cmds/selection.rs @@ -131,8 +131,7 @@ pub(super) fn same_named_targets(doc: &photocraft_doc::Document, name: &str, mov /// For `layer.select`, captures the layer by name. For other commands, if a numeric `layer` /// parameter references the document's sole selected layer, normalises it to the target form /// (omitting `layer`) so that the recorded step applies to whatever layer is targeted when -/// replayed or exported to `.atn` (#7248). When it drops `layer` from an `edit.checkSpelling` step, -/// it also records `"allLayers": false`. +/// replayed or exported to `.atn` (#7248). pub(crate) fn recorded_step(s: &Session, id: &str, p: Value, pre_selected: Option<&[LayerId]>) -> Value { if id == "layer.select" { return recorded_selection(s, p); @@ -157,9 +156,6 @@ pub(crate) fn recorded_step(s: &Session, id: &str, p: Value, pre_selected: Optio let mut p = p; if let Some(fields) = p.as_object_mut() { fields.remove("layer"); - if id == "edit.checkSpelling" { - fields.insert("allLayers".into(), json!(false)); - } } return p; } diff --git a/crates/engine/src/cmd/type_spell_cmds.rs b/crates/engine/src/cmd/type_spell_cmds.rs index 6ae2182e47..de067832ca 100644 --- a/crates/engine/src/cmd/type_spell_cmds.rs +++ b/crates/engine/src/cmd/type_spell_cmds.rs @@ -4,7 +4,8 @@ //! (`photocraft_text::spell`, SCOWL size 50) plus the user dictionary in the preferences //! (`userDictionary`). Headless, it lists misspellings with suggestions and changes, changes all, //! or adds words to the dictionary; the UI's dialog drives the same actions. Offsets are -//! character indices. +//! character indices. An action records Check Spelling as one `{"allLayers"}` step with its +//! dialog on, and records none of the corrections. //! //! `type.insertText` inserts a string (a glyph) into a type layer at a character position or //! over a range. @@ -203,6 +204,18 @@ fn remove_word(s: &mut Session, p: &Value) -> Result { Ok(json!({ "removed": removed, "userDictionary": s.prefs().user_dictionary })) } +/// Whether an action records this call: only Check Spelling itself (no `action`), which the +/// dialog runs when it closes. Its corrections, dictionary edits and lookups are not recorded. +pub(crate) fn records(p: &Value) -> bool { + p.get("action").is_none() +} + +/// The step a recorded Check Spelling becomes: `{"allLayers"}`, `false` when it checked one layer. +pub(crate) fn recorded_step(p: &Value) -> Value { + let one_layer = p.get("allLayers").and_then(Value::as_bool) == Some(false) || p.get("layer").is_some(); + json!({ "allLayers": !one_layer }) +} + fn check_spelling(s: &mut Session, p: &Value) -> Result { match p.get("action").and_then(Value::as_str).unwrap_or("list") { "list" | "check" => list(s, p), @@ -270,7 +283,7 @@ pub fn specs() -> Vec { label: "Check Spelling…", menu: &["Edit"], shortcut: None, - params: r#"{"action":"list|change|changeAll|addToDictionary|removeFromDictionary|suggest"="list","allLayers":bool=true,"layer":id? (check one layer),"ignore":[word]? (Ignore All for this check),"suggestions":n=5; change: "layer"? (default: the active layer),"start","end" (chars),"word"? (verified),"replace"; changeAll: "word","replace"; addToDictionary/removeFromDictionary/suggest: "word"} → list: {"misspellings":[{"layer","start","end","word","suggestions"}],"count"}"#, + params: r#"{"action":"list|change|changeAll|addToDictionary|removeFromDictionary|suggest"="list","allLayers":bool=true,"layer":id? (check one layer),"ignore":[word]? (Ignore All for this check),"suggestions":n=5; change: "layer"? (default: the active layer),"start","end" (chars),"word"? (verified),"replace"; changeAll: "word","replace"; addToDictionary/removeFromDictionary/suggest: "word"} → list: {"misspellings":[{"layer","start","end","word","suggestions"}],"count"} (an action records only a call without "action", as {"allLayers"} with its dialog on)"#, enabled: has_type, journal: true, run: check_spelling, @@ -378,36 +391,48 @@ mod tests { } #[test] - fn recorded_change_on_the_selected_layer_plays_on_another_document() { - let (mut s, id) = session("Teh end"); + fn recording_keeps_check_spelling_with_its_dialog_and_not_its_corrections() { + let (mut s, id) = session("Teh brwon end"); s.execute("actions.record", json!({"name": "Spell"})).unwrap(); + s.execute("edit.checkSpelling", json!({"action": "list", "allLayers": true})).unwrap(); s.execute("edit.checkSpelling", json!({"action": "change", "layer": id, "start": 0, "end": 3, "word": "Teh", "replace": "The"})).unwrap(); + s.execute("edit.checkSpelling", json!({"action": "changeAll", "word": "brwon", "replace": "brown"})).unwrap(); + s.execute("edit.checkSpelling", json!({"action": "suggest", "word": "recieve"})).unwrap(); + s.execute("edit.checkSpelling", json!({"action": "addToDictionary", "word": "Photocraft"})).unwrap(); + s.execute("edit.checkSpelling", json!({"action": "removeFromDictionary", "word": "Photocraft"})).unwrap(); + // The dialog closing. + s.execute("edit.checkSpelling", json!({"allLayers": true, "suggestions": 8})).unwrap(); s.execute("actions.stop", json!({})).unwrap(); - assert_eq!(text_of(&s, id), "The end"); - // The recorded step names no layer, so it acts on the layer targeted when it plays. - assert!(s.actions.list[0].steps[0].1.get("layer").is_none(), "{:?}", s.actions.list[0].steps); - s.execute("file.new", json!({"width": 300, "height": 100, "background": "white"})).unwrap(); - let other = s.execute("type.create", json!({"x": 10, "y": 50, "text": "Teh end", "size": 12})).unwrap()["layer"].as_u64().unwrap(); - let r = s.execute("actions.play", json!({"action": "Spell"})).unwrap(); - assert!(r.get("failed").is_none(), "{r}"); - assert_eq!(r["ran"], 1); - assert_eq!(text_of(&s, other), "The end"); + assert_eq!(text_of(&s, id), "The brown end"); + let action = &s.actions.list[0]; + assert_eq!(action.steps, vec![("edit.checkSpelling".to_string(), json!({"allLayers": true}))]); + assert_eq!(action.dialogs.iter().copied().collect::>(), [0]); } #[test] - fn recorded_change_all_on_the_selected_layer_plays_on_that_layer_only() { - let (mut s, a) = session("teh one"); - let b = s.execute("type.create", json!({"x": 10, "y": 90, "text": "teh two", "size": 12})).unwrap()["layer"].as_u64().unwrap(); - s.execute("layer.select", json!({"layer": a})).unwrap(); - s.execute("actions.record", json!({"name": "All"})).unwrap(); - s.execute("edit.checkSpelling", json!({"action": "changeAll", "layer": a, "word": "teh", "replace": "the"})).unwrap(); + fn recorded_check_of_one_layer_records_all_layers_off() { + let (mut s, id) = session("Teh end"); + s.execute("actions.record", json!({"name": "One"})).unwrap(); + s.execute("edit.checkSpelling", json!({"allLayers": false})).unwrap(); + s.execute("edit.checkSpelling", json!({"layer": id})).unwrap(); s.execute("actions.stop", json!({})).unwrap(); - assert_eq!((text_of(&s, a).as_str(), text_of(&s, b).as_str()), ("the one", "teh two")); - s.execute("edit.undo", json!({})).unwrap(); - s.execute("layer.select", json!({"layer": a})).unwrap(); - let r = s.execute("actions.play", json!({"action": "All"})).unwrap(); + let steps: Vec<&Value> = s.actions.list[0].steps.iter().map(|(_, p)| p).collect(); + assert_eq!(steps, [&json!({"allLayers": false}), &json!({"allLayers": false})]); + assert_eq!(s.actions.list[0].dialogs.len(), 2); + } + + #[test] + fn played_check_spelling_without_its_dialog_changes_nothing() { + let (mut s, id) = session("Teh end"); + s.execute("actions.record", json!({"name": "Spell"})).unwrap(); + s.execute("edit.checkSpelling", json!({"allLayers": true})).unwrap(); + s.execute("actions.stop", json!({})).unwrap(); + let history = s.active().unwrap().history.past_len(); + let r = s.execute("actions.play", json!({"action": "Spell"})).unwrap(); assert!(r.get("failed").is_none(), "{r}"); - assert_eq!((text_of(&s, a).as_str(), text_of(&s, b).as_str()), ("the one", "teh two")); + assert_eq!(r["ran"], 1); + assert_eq!(text_of(&s, id), "Teh end"); + assert_eq!(s.active().unwrap().history.past_len(), history); } #[test] diff --git a/crates/engine/src/jobs.rs b/crates/engine/src/jobs.rs index ac9e38a723..9f237605ef 100644 --- a/crates/engine/src/jobs.rs +++ b/crates/engine/src/jobs.rs @@ -964,6 +964,7 @@ impl Session { _ if self.actions.recording.is_none() => params, "layer.duplicate" => crate::artboard_cmds::recorded_duplicate(self, crate::layer_multi_cmds::recorded_step(self, id, params, pre_selected)), "layerComp.apply" => crate::comps_cmds::recorded_apply(self, params), + "edit.checkSpelling" if crate::type_spell_cmds::records(¶ms) => crate::type_spell_cmds::recorded_step(¶ms), _ => crate::layer_multi_cmds::recorded_step(self, id, params, pre_selected), }; let params = crate::actions_cmds::recorded_params(self, id, params); diff --git a/crates/ui-egui/src/panels/actions.rs b/crates/ui-egui/src/panels/actions.rs index 88034ab2c8..96a806975c 100644 --- a/crates/ui-egui/src/panels/actions.rs +++ b/crates/ui-egui/src/panels/actions.rs @@ -109,7 +109,7 @@ fn play_targets(app: &mut PhotocraftApp, targets: &[usize], before: u64) -> Resu let mut v = app.run("actions.play", json!({"action": i}))?; ran = ran.saturating_add(v.get("ran").and_then(Value::as_u64).unwrap_or(0)); let failed = v.get("failed").is_some_and(Value::is_object); - let paused = v.get("paused").filter(|p| p.is_object()).and_then(|p| p.get("dialog")).and_then(Value::as_u64); + let paused = v.get("paused").filter(|p| p.is_object()).and_then(waiting_dialog).cloned(); if let Some(o) = v.as_object_mut() { o.insert("ran".into(), json!(ran)); } @@ -119,7 +119,7 @@ fn play_targets(app: &mut PhotocraftApp, targets: &[usize], before: u64) -> Resu // (`actions.move`), and one deleted and made again under its name isn't the same action. let list = &app.session.actions.list; let rest: Vec = targets.get(n + 1..).unwrap_or_default().iter().filter_map(|i| list.get(*i).cloned()).collect(); - let token = app.ui.dialog_mut(dialog).and_then(|d| d.fields.get(RESUME)).and_then(Value::as_u64); + let token = paused_token(app, &dialog); if let Some(entry) = token.and_then(|t| app.paused_playbacks.paused.get_mut(&t)) && name.as_ref() == Some(&entry.action) { @@ -135,6 +135,19 @@ fn play_targets(app: &mut PhotocraftApp, targets: &[usize], before: u64) -> Resu Ok(last) } +/// The dialog a paused playback waits at: a step dialog by id, or Check Spelling. +fn waiting_dialog(paused: &Value) -> Option<&Value> { + paused.get("dialog").filter(|d| d.is_u64() || d.as_str() == Some(crate::type_panels_ui::SPELLING)) +} + +/// The token of the paused playback waiting at `dialog` (see [`waiting_dialog`]). +fn paused_token(app: &mut PhotocraftApp, dialog: &Value) -> Option { + match dialog.as_u64() { + Some(id) => app.ui.dialog_mut(id).and_then(|d| d.fields.get(RESUME)).and_then(Value::as_u64), + None => app.ui.type_panels.spell.as_ref().and_then(|d| d.resume), + } +} + /// Index of the playback target. Photoshop's Play button uses the first action when /// the panel has actions but none of its rows is selected. fn playback_selection(app: &PhotocraftApp) -> Option { @@ -239,6 +252,19 @@ fn play_owned(app: &mut PhotocraftApp, params: &Value, suspended: Option) paused = Some(json!({"step": i, "id": id, "dialog": dialog})); break; } + // Check Spelling's dialog opens with Check All Layers as recorded; Done continues the action. + if action.dialogs.contains(&i) && !app.automation_input && id == "edit.checkSpelling" { + if let Err(error) = crate::type_panels_ui::open_spelling(app, p.get("allLayers").and_then(Value::as_bool).unwrap_or(true)) { + failed = Some(json!({"step": i, "id": id, "error": error})); + break; + } + let token = app.paused_playbacks.pause(action.name.clone(), i + 1, app.session.authorize); + if let Some(d) = app.ui.type_panels.spell.as_mut() { + d.resume = Some(token); + } + paused = Some(json!({"step": i, "id": id, "dialog": crate::type_panels_ui::SPELLING})); + break; + } let target = app.session.active().map(|d| d.doc.id); let result = if id == "actions.play" { play(app, p) @@ -277,10 +303,11 @@ fn play_owned(app: &mut PhotocraftApp, params: &Value, suspended: Option) } } actions_cmds::finish_playback(&mut app.session, None, &action, from); - match paused.as_ref().and_then(|p| p.get("dialog")).and_then(Value::as_u64).and_then(|d| app.ui.dialog_mut(d)) { + match paused.as_ref().and_then(waiting_dialog).cloned() { // Paused for a dialog: the call goes on after it, so recording stays suspended. - Some(d) if failed.is_none() => { - let entry = d.fields.get(RESUME).and_then(Value::as_u64).and_then(|t| app.paused_playbacks.paused.get_mut(&t)); + Some(dialog) if failed.is_none() => { + let token = paused_token(app, &dialog); + let entry = token.and_then(|t| app.paused_playbacks.paused.get_mut(&t)); match (owner, entry) { (Some(owner), Some(p)) => p.record = Some(owner), // No continuation to carry the recording: the call ends here. @@ -758,8 +785,8 @@ pub fn resume(app: &mut PhotocraftApp, fields: &serde_json::Map) // Paused again at a later dialog of the same action (not of one it calls, which resumes // alone): that dialog carries the rest of the set. if let Ok(v) = &played - && let Some(dialog) = v.get("paused").and_then(|p| p.get("dialog")).and_then(Value::as_u64) - && let Some(token) = app.ui.dialog_mut(dialog).and_then(|d| d.fields.get(RESUME)).and_then(Value::as_u64) + && let Some(dialog) = v.get("paused").and_then(waiting_dialog) + && let Some(token) = paused_token(app, dialog) && let Some(entry) = app.paused_playbacks.paused.get_mut(&token) && entry.action == paused.action { diff --git a/crates/ui-egui/src/panels/actions/tests.rs b/crates/ui-egui/src/panels/actions/tests.rs index 06a5cd5fb3..6049cdb5ad 100644 --- a/crates/ui-egui/src/panels/actions/tests.rs +++ b/crates/ui-egui/src/panels/actions/tests.rs @@ -1377,6 +1377,42 @@ fn dialog_worker_failure_stops_action_before_following_steps() { } } +#[test] +fn a_check_spelling_step_with_its_dialog_opens_it_and_done_continues() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 300, "height": 100})).unwrap(); + app.run("type.create", json!({"x": 10, "y": 50, "text": "Teh cat"})).unwrap(); + let mut a = Action::new("Spell", vec![("edit.checkSpelling".into(), json!({"allLayers": false})), ("layer.new.layer".into(), json!({"name": "after"}))]); + a.dialogs.insert(0); + app.session.actions.list.push(a); + let v = play(&mut app, &json!({"action": "Spell"})).unwrap(); + assert_eq!((v["ran"].as_u64(), v["paused"]["step"].as_u64()), (Some(0), Some(0)), "{v}"); + assert_eq!(v["paused"]["dialog"], crate::type_panels_ui::SPELLING); + let d = app.ui.type_panels.spell.clone().expect("Check Spelling is open"); + assert!(!d.all_layers, "the dialog opens with Check All Layers as recorded"); + assert_eq!(d.items.len(), 1); + assert!(!layer_names(&app).contains(&"after".to_string()), "nothing after the dialog ran yet"); + crate::type_panels_ui::close_spelling(&mut app); + assert!(layer_names(&app).contains(&"after".to_string()), "Done played the rest"); + assert!(app.ui.status.starts_with("Played"), "{}", app.ui.status); + // With its dialog off, the step checks without opening anything and changes nothing. + app.session.actions.list[0].dialogs.clear(); + let v = play(&mut app, &json!({"action": "Spell"})).unwrap(); + assert_eq!(v["ran"], 2, "{v}"); + assert!(app.ui.type_panels.spell.is_none()); + let st = app.session.active().unwrap(); + let texts: Vec = st + .doc + .walk() + .into_iter() + .filter_map(|(_, _, l)| match &l.content { + photocraft_doc::LayerContent::Text(t) => Some(t.text.clone()), + _ => None, + }) + .collect(); + assert_eq!(texts, ["Teh cat"]); +} + #[test] fn a_dialog_toggled_step_pauses_playback_and_resumes_on_ok() { let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); diff --git a/crates/ui-egui/src/panels/type_panels_ui.rs b/crates/ui-egui/src/panels/type_panels_ui.rs index d95c22addb..dbcb140788 100644 --- a/crates/ui-egui/src/panels/type_panels_ui.rs +++ b/crates/ui-egui/src/panels/type_panels_ui.rs @@ -48,8 +48,15 @@ pub struct SpellDialog { pub change_to: String, pub all_layers: bool, pub message: String, + /// The token of the action playback that opened the dialog at a Check Spelling step: Done + /// continues it ([`crate::actions::resume`]). + #[serde(skip)] + pub resume: Option, } +/// The `dialog` that Check Spelling's results and a playback paused at its step name. +pub const SPELLING: &str = "checkSpelling"; + const IDS: [&str; 6] = [ "type.panels.characterStyles", "window.panel.characterStyles", @@ -772,7 +779,20 @@ pub fn open_spelling(app: &mut PhotocraftApp, all_layers: bool) -> Result app.ui.type_panels.spell = None, + "done" => close_spelling(app), "relist" => { if let Some(s) = app.ui.type_panels.spell.as_mut() { s.index = 0; @@ -964,6 +984,25 @@ mod tests { assert!(menu(&mut app, "edit.checkSpelling", &json!({"action": "list"})).is_none()); } + #[test] + fn recording_the_dialog_records_one_step_with_its_dialog_on() { + let mut app = app_with_text("Teh cat"); + app.run("actions.record", json!({"name": "Spell"})).unwrap(); + menu(&mut app, "edit.checkSpelling", &json!({})).unwrap().unwrap(); + let item = app.ui.type_panels.spell.as_ref().unwrap().items[0].clone(); + // Change, as the dialog's Change button runs it, then untick Check All Layers and press Done. + let change = json!({"action": "change", "layer": item["layer"], "start": item["start"], "end": item["end"], "word": "Teh", "replace": "The"}); + run(&mut app, "edit.checkSpelling", change).unwrap(); + refresh_spelling(&mut app); + app.ui.type_panels.spell.as_mut().unwrap().all_layers = false; + close_spelling(&mut app); + app.run("actions.stop", json!({})).unwrap(); + assert!(app.ui.type_panels.spell.is_none()); + let action = &app.session.actions.list[0]; + assert_eq!(action.steps, vec![("edit.checkSpelling".to_string(), json!({"allLayers": false}))]); + assert_eq!(action.dialogs.iter().copied().collect::>(), [0]); + } + /// #5496: Check Spelling's close box is Done, as in Photoshop on Windows: the window closes /// and the text keeps what it had. None on macOS. #[test]