From 08ff70c863a71af08e971d39d2ed60b2561cac20 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 25 Nov 2026 16:50:18 -0800 Subject: [PATCH] Plug-ins: the effect editor and palette search use translated names A plug-in effect's editor in the Appearance panel is named by the plug-in's name in the UI language, as its row is. The command palette also finds a filter or live-effect plug-in by its English translation when the UI shows another language. --- crates/ui-egui/src/dialogs/plugin.rs | 32 +++++++++++++++++++ crates/ui-egui/src/palette.rs | 24 +++++++++----- crates/ui-egui/src/panels/appearance.rs | 4 ++- crates/ui-egui/src/panels/appearance/tests.rs | 28 ++++++++++++++++ crates/ui-egui/src/tests/panel_harness.rs | 6 ++++ 5 files changed, 85 insertions(+), 9 deletions(-) diff --git a/crates/ui-egui/src/dialogs/plugin.rs b/crates/ui-egui/src/dialogs/plugin.rs index e1e5f529ab..8da5477054 100644 --- a/crates/ui-egui/src/dialogs/plugin.rs +++ b/crates/ui-egui/src/dialogs/plugin.rs @@ -94,6 +94,38 @@ pub(crate) fn effect_item_label(e: &vectorcraft_effects::EffectInfo) -> &'static } } +/// The menu label of plug-in item `key` (`plugin:` or `effect:`) in language +/// `code`, as [`object_menu`] and [`effect_menu`] show it in that language; `None` for any other key +/// or a plug-in that is not installed. +pub(crate) fn item_label_in(key: &str, code: &str) -> Option<&'static str> { + let m = item_plugin(key)?; + let m = m.manifest(); + Some(label(&m.shown_name(code), !m.params.is_empty())) +} + +/// Menu path `group` of plug-in item `key` ([`item_label_in`]) with its plug-in's submenu, the +/// segment after Object › Plug-ins or Effect › Plug-ins, named in language `code`; `group` as it +/// is for an item without a submenu. +pub(crate) fn item_group_in(key: &str, group: &str, code: &str) -> String { + let mut parts: Vec = group.split(" › ").map(str::to_string).collect(); + if parts.get(1).is_some_and(|p| p == "Plug-ins") + && let (Some(plugin), Some(submenu)) = (item_plugin(key), parts.get_mut(2)) + { + *submenu = plugin.manifest().shown_name(code); + } + parts.join(" › ") +} + +/// The plug-in of item `key` (`plugin:` or `effect:`), if it is installed. +fn item_plugin(key: &str) -> Option> { + let id = match (key.strip_prefix("plugin:"), key.strip_prefix("effect:")) { + (Some(id), _) => id, + (_, Some(effect)) => vectorcraft_plugins::effect::plugin_id(effect)?, + _ => return None, + }; + registry::get(id) +} + /// A menu label for plug-in `name`: with an ellipsis when it opens a dialog. fn label(name: &str, has_params: bool) -> &'static str { let dots = if has_params && !name.ends_with('…') { "…" } else { "" }; diff --git a/crates/ui-egui/src/palette.rs b/crates/ui-egui/src/palette.rs index f1c105d721..d7e8006dc4 100644 --- a/crates/ui-egui/src/palette.rs +++ b/crates/ui-egui/src/palette.rs @@ -159,13 +159,18 @@ impl Draft { let name = format!("{path} › {label}"); return if self.keyed { format!("{name} ({})", self.id) } else { name }; } - // A built-in effect's label translates by its effect id first, as the Effect menu's does. + // A built-in effect's label translates by its effect id first, as the Effect menu's does. A + // filter or live-effect plug-in's name and submenu are the ones its translations give in `lang`. let label = match self.id.strip_prefix("effect:") { - _ if !self.translates => self.label.as_str(), + _ if !self.translates => crate::dialogs::plugin::item_label_in(&self.id, lang.code()).unwrap_or(self.label.as_str()), Some(effect) => crate::i18n::tr_id(lang, effect, &self.label), None => crate::i18n::tr(lang, &self.label), }; - let name = if self.path.is_empty() { label.to_string() } else { format!("{} › {label}", shown_label(lang, &self.path)) }; + let path = match self.translates { + true => std::borrow::Cow::Borrowed(self.path.as_str()), + false => std::borrow::Cow::Owned(crate::dialogs::plugin::item_group_in(&self.id, &self.path, lang.code())), + }; + let name = if path.is_empty() { label.to_string() } else { format!("{} › {label}", shown_label(lang, &path)) }; if self.keyed { format!("{name} ({})", self.id) } else { name } } @@ -1968,7 +1973,8 @@ mod tests { } /// A filter whose name the UI language translates shows the translation, English included, and - /// a search for the manifest's own name still finds it. + /// a search for the manifest's own name still finds it; in German, so does a search for its + /// English translation. #[test] fn a_translated_filter_is_found_by_its_manifest_name() { use vectorcraft_plugins::wat as tpl; @@ -1979,19 +1985,21 @@ mod tests { ); vectorcraft_plugins::registry::install_bytes(&wat::parse_str(tpl::returning(&manifest, r#"{"objects":[]}"#)).unwrap()).unwrap(); let app = app_on(Session::new()); + let key = format!("plugin:{id}"); let found = |lang: Lang| { crate::i18n::set_current(lang); - let found = - entries(&app, lang).iter().find(|e| id_of(e) == format!("plugin:{id}")).map(|e| (e.label().as_str().to_string(), haystack(e))); + let found = entries(&app, lang).iter().find(|e| id_of(e) == key).map(|e| (e.label().as_str().to_string(), haystack(e))); + let by_english = ranked(&built_for(&app), "smooth edges").contains(&key); crate::i18n::set_current(Lang::EN); - found + (found, by_english) }; let (de, en) = (found(Lang::from_code("de").unwrap()), found(Lang::EN)); vectorcraft_plugins::registry::remove(id); - for (found, name) in [(de, "Kanten polieren"), (en, "Smooth Edges")] { + for ((found, by_english), name) in [(de, "Kanten polieren"), (en, "Smooth Edges")] { let (shown, haystack) = found.expect("the filter is a result"); assert!(shown.ends_with(name), "{shown}"); assert!(haystack.contains("polish edges"), "{haystack}"); + assert!(by_english, "a search for the English translation finds {name}"); } } diff --git a/crates/ui-egui/src/panels/appearance.rs b/crates/ui-egui/src/panels/appearance.rs index 5246f1351a..cf106af991 100644 --- a/crates/ui-egui/src/panels/appearance.rs +++ b/crates/ui-egui/src/panels/appearance.rs @@ -949,7 +949,9 @@ fn effect_editor<'r>(app: &VectorcraftApp, pass: &'r RefCell, item: Option if rows.is_empty() { stored_params(&effect, &values) } else { rows } }; let fields = fields_of(¶ms, &values); - let (name, id) = (effect_label(&e.id), e.id.clone()); + // A plug-in effect's editor is named as its row is, by the plug-in's name in the UI language. + let name = crate::dialogs::plugin::effect_name(&e.id).unwrap_or_else(|| effect_label(&e.id)); + let id = e.id.clone(); move |ui| { if free_distort { // The button keeps its own name: the label says what it edits. diff --git a/crates/ui-egui/src/panels/appearance/tests.rs b/crates/ui-egui/src/panels/appearance/tests.rs index 028d89306d..665ba3c3ce 100644 --- a/crates/ui-egui/src/panels/appearance/tests.rs +++ b/crates/ui-egui/src/panels/appearance/tests.rs @@ -214,6 +214,34 @@ fn a_plug_in_effect_s_editor_follows_its_manifest() { panel.app().run("plugin.remove", json!({"id": "test.ui.inlinekeys"})).unwrap(); } +/// A plug-in effect's editor is named by the plug-in's name in the UI language, as its row is. +#[test] +fn a_plug_in_effect_s_editor_is_named_in_the_ui_language() { + use vectorcraft_plugins::wat as tpl; + let id = "test.ui.inlinename"; + let manifest = format!( + r#"{{"id":"{id}","name":"Hatch","version":"1","kind":"effect","params":{{"width":{{"type":"number","min":0,"max":10,"default":1,"label":"Width"}}}}, + "translations":{{"de":{{"name":"Schraffur"}}}}}}"# + ); + let src = tpl::module(&manifest, tpl::ECHO, "", 1); + let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); + app.run("plugin.install", json!({"dataBase64": vectorcraft_format::base64_encode(&wat::parse_str(src).unwrap())})).unwrap(); + app.run("file.new", json!({"width": 100, "height": 100})).unwrap(); + app.run("shape.rectangle", json!({"x": 0, "y": 0, "width": 20, "height": 20})).unwrap(); + app.run("effect.apply", json!({"effect": format!("plugin.{id}")})).unwrap(); + app.ui.appearance_panel.open.insert(RowKey::Effect(0)); + let mut panel = Panel::new(app, show); + let group = craft::ui::contract::Role::GROUP; + let en = panel.shows_role("Hatch", group); + crate::i18n::set_current(crate::i18n::Lang::from_code("de").unwrap()); + panel.frame(); + let de = (panel.shows_role("Schraffur", group), panel.shows_role("Hatch", group)); + crate::i18n::set_current(crate::i18n::Lang::EN); + panel.app().run("plugin.remove", json!({"id": id})).unwrap(); + assert!(en, "the editor in English"); + assert_eq!(de, (true, false), "the editor in German"); +} + /// Glass with a loaded texture lists Loaded Texture in its Texture menu, as its dialog does. #[test] fn the_effect_editor_lists_a_loaded_glass_texture() { diff --git a/crates/ui-egui/src/tests/panel_harness.rs b/crates/ui-egui/src/tests/panel_harness.rs index 0b36cec615..81c54b1e68 100644 --- a/crates/ui-egui/src/tests/panel_harness.rs +++ b/crates/ui-egui/src/tests/panel_harness.rs @@ -276,6 +276,12 @@ impl Panel { self.fixture.by_label(name).is_some() } + /// Whether the latest frame shows a node of `role` named `name`, where nodes of other roles share the name. + pub(crate) fn shows_role(&self, name: &str, role: craft::ui::contract::Role) -> bool { + let tree = self.fixture.tree(); + self.fixture.by_role(role).into_iter().any(|t| tree.name_of(t.node) == Some(name)) + } + /// Whether some of the control named `name` can be seen in the latest frame. pub(crate) fn in_view(&self, name: &str) -> bool { self.fixture.visible(&self.look(name)).is_some()