From fabec2187fa310bac44f1f40de0d896fad0fac77 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 22 Nov 2026 13:09:47 -0800 Subject: [PATCH] History: an Effect Options step names an effect only when every target has it effect.setParams named its step after the edited effect when the targets that have an effect at the index all shared one, so with Feather on one object and no effect on the other the step still recorded Feather. The step now records the effect only when every target has one at the index and all are the same. The target that has the effect is still edited. The i18n menu-label test helper now reads an Effect menu item's label through menus::effect_item_label, the function the menus draw it with. --- crates/engine/src/cmd/effectcmd.rs | 5 +++-- crates/engine/src/tests/history_mem.rs | 28 +++++++++++++++++++++++++- crates/ui-egui/src/i18n/tests.rs | 21 +++++++++---------- crates/ui-egui/src/menus.rs | 2 ++ crates/ui-egui/src/menus/entries.rs | 2 +- crates/ui-egui/src/menus/labels.rs | 6 +++--- 6 files changed, 45 insertions(+), 19 deletions(-) diff --git a/crates/engine/src/cmd/effectcmd.rs b/crates/engine/src/cmd/effectcmd.rs index 2fd764fe8f..6bde9110ee 100644 --- a/crates/engine/src/cmd/effectcmd.rs +++ b/crates/engine/src/cmd/effectcmd.rs @@ -891,8 +891,9 @@ pub(crate) fn set_params(s: &mut Session, p: &Value) -> Result { } } } - // The effect edited, when every target has the same one: a dialog's preview names its step after it. - let one = edited.first().filter(|e| edited.iter().all(|other| other == *e)).cloned().unwrap_or_default(); + // The effect edited, when every target has one at `index` and all are the same: a dialog's + // preview names its step after it. + let one = edited.first().filter(|e| edited.len() == targets.len() && edited.iter().all(|other| other == *e)).cloned().unwrap_or_default(); let ids = s.edit_effect(&one, |s| { edit_effects(s, p, item, C, "Effect Options", &format!("{C}: no effect at index {index}"), index, |fx, index| { let Some(e) = fx.get_mut(index) else { return false }; diff --git a/crates/engine/src/tests/history_mem.rs b/crates/engine/src/tests/history_mem.rs index b26f970966..56580097f2 100644 --- a/crates/engine/src/tests/history_mem.rs +++ b/crates/engine/src/tests/history_mem.rs @@ -2,7 +2,7 @@ //! shared between steps, and past a memory budget the oldest steps go (never the last few). use serde_json::json; -use vectorcraft_doc::{Appearance, Node}; +use vectorcraft_doc::{Appearance, Node, NodeId}; use vectorcraft_geom::shapes; use crate::*; @@ -501,3 +501,29 @@ fn only_steps_an_effect_records_keep_the_effect() { s.commit_interaction().unwrap(); assert_eq!(last(&s), Some(("Feather".into(), None)), "a batch the preview ran"); } + +/// An `effect.setParams` step keeps the effect it edited only when every target has that effect at +/// the index: with one target lacking it, the step keeps none, and the target that has it is still +/// edited. +#[test] +fn effect_options_keep_the_effect_only_when_every_target_has_it() { + let mut s = session_with_rects(2); + let ids: Vec = s.doc().unwrap().doc.layers[0].children().unwrap().iter().map(|n| n.id.0).collect(); + s.execute("effect.apply", &json!({"effect": "stylize.feather", "ids": [ids[0]], "item": null})).unwrap(); + let last = |s: &Session| s.doc().unwrap().history.undo.last().map(|e| (e.label.clone(), e.effect())); + let radius = |s: &Session| { + let d = &s.doc().unwrap().doc; + d.node(NodeId(ids[0])).unwrap().appearance.effects[0].params["radius"].as_f64() + }; + s.begin_interaction("Feather").unwrap(); + s.preview("effect.setParams", &json!({"ids": ids, "item": null, "index": 0, "params": {"radius": 7}})).unwrap(); + s.commit_interaction().unwrap(); + assert_eq!(last(&s), Some(("Feather".into(), None)), "one target without the effect"); + assert_eq!(radius(&s), Some(7.0), "the target with the effect is edited"); + s.execute("effect.apply", &json!({"effect": "stylize.feather", "ids": [ids[1]], "item": null})).unwrap(); + s.begin_interaction("Feather").unwrap(); + s.preview("effect.setParams", &json!({"ids": ids, "item": null, "index": 0, "params": {"radius": 9}})).unwrap(); + s.commit_interaction().unwrap(); + assert_eq!(last(&s), Some(("Feather".into(), Some("stylize.feather"))), "every target with the effect"); + assert_eq!(radius(&s), Some(9.0)); +} diff --git a/crates/ui-egui/src/i18n/tests.rs b/crates/ui-egui/src/i18n/tests.rs index 8c0e0587dc..5aa474704f 100644 --- a/crates/ui-egui/src/i18n/tests.rs +++ b/crates/ui-egui/src/i18n/tests.rs @@ -2287,17 +2287,14 @@ fn menu_item_effect<'a>(id: &str, p: &'a serde_json::Value) -> Option<&'a str> { p.get("effect").and_then(serde_json::Value::as_str).filter(|_| matches!(id, "effect.apply" | "effect.dialog")) } -/// The label a menu item shows in `lang`, as the menus resolve it: an Effect menu item by its -/// effect's `@id` row when the catalog has one, any other command by its id's row or its English -/// label, and a submenu or an unbuilt item by its English label. -fn shown_menu_label(lang: Lang, item: &crate::menus::Item) -> Option<&'static str> { +/// The label a menu item shows in `lang`, as the menus resolve it: a built-in Effect menu item by +/// its effect's name ([`crate::menus::effect_item_label`]), any other command by its id's row or +/// its English label, and a submenu or an unbuilt item by its English label. +fn shown_menu_label(lang: Lang, item: &crate::menus::Item) -> Option { use crate::menus::Item; match item { - Item::Cmd(l, id, p) => { - let by_effect = menu_item_effect(id, p).map(|e| tr_id(lang, e, "")).filter(|s| !s.is_empty()); - Some(by_effect.unwrap_or_else(|| tr_id(lang, id, l))) - } - Item::Todo(l, _) | Item::Sub(l, _) => Some(tr(lang, l)), + Item::Cmd(l, id, p) => Some(crate::menus::effect_item_label(id, p, lang).unwrap_or_else(|| tr_id(lang, id, l).to_string())), + Item::Todo(l, _) | Item::Sub(l, _) => Some(tr(lang, l).to_string()), Item::Header(_) | Item::Sep => None, } } @@ -2309,7 +2306,7 @@ fn shown_menu_label(lang: Lang, item: &crate::menus::Item) -> Option<&'static st fn menu_items_side_by_side_have_different_labels() { use crate::menus::Item; fn walk(lang: Lang, path: &str, items: &[Item], out: &mut Vec) { - let mut seen: std::collections::BTreeMap<&str, (&str, &str)> = std::collections::BTreeMap::new(); + let mut seen: std::collections::BTreeMap = std::collections::BTreeMap::new(); for item in items { let Some(shown) = shown_menu_label(lang, item) else { continue }; let (english, what) = match item { @@ -2317,7 +2314,7 @@ fn menu_items_side_by_side_have_different_labels() { Item::Todo(l, _) | Item::Sub(l, _) => (*l, "submenu or unbuilt item"), Item::Header(_) | Item::Sep => continue, }; - match seen.get(shown) { + match seen.get(&shown) { Some(&(other, other_what)) if other != english => { out.push(format!("{}: {path}: {other:?} ({other_what}) and {english:?} ({what}) both show {shown:?}", lang.0.code)); } @@ -2369,7 +2366,7 @@ fn menu_items_never_show_an_id() { if let crate::menus::Item::Cmd(l, id, p) = item { let key = menu_item_effect(id, p).unwrap_or(id); let shown = shown_menu_label(lang, item).unwrap_or_default(); - if label_shows_an_id(shown, key) && !label_shows_an_id(l, key) { + if label_shows_an_id(&shown, key) && !label_shows_an_id(l, key) { out.push(format!("{}: {l:?} ({key}) shows {shown:?}", lang.0.code)); } } diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs index ac5ec6825b..13fa7c5a0c 100644 --- a/crates/ui-egui/src/menus.rs +++ b/crates/ui-egui/src/menus.rs @@ -76,6 +76,8 @@ pub use invoke::{click_target, invoke, invoke_from_system_menu}; pub(crate) use keyboard::handle_input as handle_keyboard_input; // Shared numeric fields preserve their caret when a transient menu returns their focus. pub(crate) use keyboard::restoring_field_focus; +#[cfg(test)] +pub(crate) use labels::effect_item_label; pub(crate) use labels::lists_plugin; pub(crate) use labels::shows_a_name; pub use labels::{CONTEXT_LABELS, DYNAMIC_LABELS, display_label, dynamic_label, item_shortcut, menu_strings}; diff --git a/crates/ui-egui/src/menus/entries.rs b/crates/ui-egui/src/menus/entries.rs index 5a3ade8633..5a763271cd 100644 --- a/crates/ui-egui/src/menus/entries.rs +++ b/crates/ui-egui/src/menus/entries.rs @@ -66,7 +66,7 @@ pub fn entry<'a>(app: &VectorcraftApp, it: &'a Item) -> Option> { /// draw it in the UI language: an effect's own name (Pathfinder › Add), an object's name, a /// spelling suggestion, or [`display_label`]. pub fn shown_label(app: &VectorcraftApp, id: &str, p: &Value, label: &str) -> String { - effect_item_label(id, p) + effect_item_label(id, p, crate::i18n::current()) .or_else(|| unlock_item_label(app, id, p, crate::i18n::current())) .or_else(|| spelling_item_label(id, p)) .unwrap_or_else(|| display_label(app, id, label)) diff --git a/crates/ui-egui/src/menus/labels.rs b/crates/ui-egui/src/menus/labels.rs index dd73c842f7..04da5269b0 100644 --- a/crates/ui-egui/src/menus/labels.rs +++ b/crates/ui-egui/src/menus/labels.rs @@ -199,11 +199,11 @@ pub fn display_label(app: &VectorcraftApp, id: &str, label: &str) -> String { } } -/// The label of an Effect menu item in the UI language, by the effect's own id +/// The label of an Effect menu item in `lang`, by the effect's own id /// ([`crate::i18n::effect_menu_label`]). Every effect item runs `effect.apply` or `effect.dialog`, /// so the command id can't tell them apart. `None` for any other item, and for a plug-in effect, /// whose item shows the plug-in's own text. -pub(super) fn effect_item_label(id: &str, p: &Value) -> Option { +pub(crate) fn effect_item_label(id: &str, p: &Value, lang: crate::i18n::Lang) -> Option { if !matches!(id, "effect.apply" | "effect.dialog") { return None; } @@ -211,7 +211,7 @@ pub(super) fn effect_item_label(id: &str, p: &Value) -> Option { if vectorcraft_plugins::effect::plugin_id(effect).is_some() { return None; } - crate::i18n::effect_menu_label(crate::i18n::current(), effect) + crate::i18n::effect_menu_label(lang, effect) } /// The label of an item of the canvas context menu's Unlock submenu (`layer.setProps` with one id