From cbbb98149e0fd1801e5cc42ff6e7eccf018ed753 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 25 Nov 2026 18:19:41 -0800 Subject: [PATCH] Toolbars: a toolbar's command buttons are looked up together The footer of a toolbar made with New Toolbar looked up each command button's label on its own, which rebuilt the tool homes once per button on every render. `palette::button_texts` looks up all of a toolbar's buttons from one kept build, and `command_looks` uses it. docs/mcp.md now says which errors apply to `toolbar.addCommand` and which to both commands, and that `toolbar.removeCommand` answers with the unchanged list for a command the toolbar has no button for. --- crates/ui-egui/src/custom_toolbars.rs | 9 ++------- crates/ui-egui/src/edit_toolbar/tests.rs | 19 +++++++++++++++++++ crates/ui-egui/src/palette.rs | 12 ++++++++++++ docs/mcp.md | 8 +++++--- 4 files changed, 38 insertions(+), 10 deletions(-) diff --git a/crates/ui-egui/src/custom_toolbars.rs b/crates/ui-egui/src/custom_toolbars.rs index b2f60c88d4..ff7c19fc3e 100644 --- a/crates/ui-egui/src/custom_toolbars.rs +++ b/crates/ui-egui/src/custom_toolbars.rs @@ -616,13 +616,8 @@ impl CommandLook { /// How each command button of the toolbar with id `id` looks now. fn command_looks(app: &mut VectorcraftApp, ui: &egui::Ui, id: u32) -> Vec { let commands = app.ui.custom_toolbars.iter().find(|b| b.id == id).map(|b| b.commands.clone()).unwrap_or_default(); - commands - .into_iter() - .map(|id| { - let text = crate::palette::is_command(&id).then(|| crate::palette::button_text(app, ui, &id)).flatten(); - CommandLook { id, text } - }) - .collect() + let texts = crate::palette::button_texts(app, ui, &commands); + commands.into_iter().zip(texts).map(|(id, text)| CommandLook { id, text }).collect() } /// A command button on the toolbar named `bar`: the command's label, cut short with … when it is diff --git a/crates/ui-egui/src/edit_toolbar/tests.rs b/crates/ui-egui/src/edit_toolbar/tests.rs index e3e8eb5dca..82bf402a18 100644 --- a/crates/ui-egui/src/edit_toolbar/tests.rs +++ b/crates/ui-egui/src/edit_toolbar/tests.rs @@ -607,6 +607,25 @@ fn a_missing_command_stays_dimmed_until_it_is_removed() { assert_eq!(left, json!(["object.group"])); } +/// The footer looks up all its command buttons in one call: each id gets what a lookup of it alone +/// gives, in order, and none for a tool, a command that needs params or a command that is gone. +#[test] +fn command_buttons_are_looked_up_together() { + let mut app = app(); + let ctx = ctx(); + let ids: Vec = + ["object.group", "tool:pen", "plugin:x", "toolbar.addTool", "object.path.cleanUp"].into_iter().map(str::to_string).collect(); + let together = in_pass(&mut app, &ctx, |app, ui| crate::palette::button_texts(app, ui, &ids)); + let alone: Vec<_> = ids + .iter() + .map(|id| crate::palette::is_command(id).then(|| in_pass(&mut app, &ctx, |app, ui| crate::palette::button_text(app, ui, id))).flatten()) + .collect(); + assert_eq!(together, alone); + assert!(together[0].is_some() && together[4].is_some(), "Group and Clean Up are found: {together:?}"); + assert!(together[1..4].iter().all(Option::is_none), "a tool, a missing command and one that needs params: {together:?}"); + assert!(in_pass(&mut app, &ctx, |app, ui| crate::palette::button_texts(app, ui, &[])).is_empty()); +} + /// Add Tool…'s menu lists a toolbar's command buttons under Remove Command, where a click takes one /// off, and its Add Command submenu searches the Command Palette's results other than tools: a click /// on a match adds its button at the end of the toolbar. diff --git a/crates/ui-egui/src/palette.rs b/crates/ui-egui/src/palette.rs index e1dfc3d5ac..0860a3e10c 100644 --- a/crates/ui-egui/src/palette.rs +++ b/crates/ui-egui/src/palette.rs @@ -608,12 +608,24 @@ pub(crate) struct ButtonText { /// The label, name and command of the palette result `id` in the interface language, from the /// entries built once per catalog generation; none when the palette lists no result with that id /// now (an uninstalled plug-in's command). +#[cfg(test)] pub(crate) fn button_text(app: &mut VectorcraftApp, ui: &egui::Ui, id: &str) -> Option { let built = kept_build(app, ui); let e = built.by_id.get(id).and_then(|&i| built.entries.get(i))?; Some(button_text_of(e)) } +/// The [`ButtonText`] of each result id in `ids`, in order, from one kept build: none for an id +/// that [`is_command`] does not allow or that the palette lists no longer (an uninstalled plug-in's +/// command). A toolbar's footer draws all its command buttons from one call. +pub(crate) fn button_texts(app: &mut VectorcraftApp, ui: &egui::Ui, ids: &[String]) -> Vec> { + if ids.is_empty() { + return Vec::new(); + } + let built = kept_build(app, ui); + ids.iter().map(|id| if is_command(id) { built.by_id.get(id).and_then(|&i| built.entries.get(i)).map(button_text_of) } else { None }).collect() +} + /// [`ButtonText`] of the entry `e`. fn button_text_of(e: &HelpEntry) -> ButtonText { let label = e.label().as_str().trim_end(); diff --git a/docs/mcp.md b/docs/mcp.md index d788800b32..aece27dedb 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -3798,9 +3798,11 @@ A toolbar made with New Toolbar holds command buttons after its tools. `toolbar. named `toolbar`, or moves the button when the toolbar has it already: at place `at` among its command buttons, 0 for the first, or at the end without `at`. `command` is a Command Palette result's id: a command's id, or `plugin:`, `effect:` or `panel:`. `toolbar.removeCommand {command, toolbar}` takes a button off. Both -answer with the toolbar's command buttons, and `window.toolbar.list` reports `commands` for each toolbar. An unknown -command, a tool, a command that needs params a click does not give (`toolbar.addTool`), an `at` past the end or a -missing `toolbar` is an error and changes nothing. A click on a button runs what the command's menu item runs, so a +answer with the toolbar's command buttons, and `window.toolbar.list` reports `commands` for each toolbar. A missing +`toolbar` is an error for both and changes nothing. For `toolbar.addCommand`, an unknown command, a tool, a command +that needs params a click does not give (`toolbar.addTool`) or an `at` past the end is also an error and changes +nothing. `toolbar.removeCommand` with a command the toolbar has no button for, a tool's id included, is not an error: +it answers with the toolbar's command buttons unchanged. A click on a button runs what the command's menu item runs, so a command whose item ends in … opens its dialog, and the button is dimmed while that item is. A command that is no longer installed, such as an uninstalled plug-in's, keeps its button, dimmed and labeled with its id, until it is installed again or removed. `toolbar.reset {toolbar}` takes the buttons off with the tools. The buttons are kept with