Merge pull request #10029 from dotCooCoo/fix/speed-i13929-dimmed-rows

Menus: a dimmed row runs its command's check once per rebuild
This commit is contained in:
.CooCoo authored and GitHub committed 2026-11-28 21:13:40 -08:00
commit 7da0740ae9
3 files changed
+54 -10

No files matched your search

+1 -1
View File
@@ -93,7 +93,7 @@ pub(crate) use run::{normalize_panel, opt_bool, opt_index, opt_number};
#[cfg(test)]
use slots::RECENT_FONT_IDS;
pub use state::{checked, enabled, plugin_revision, shortcut_of, shown_state};
pub(crate) use state::{command_shortcuts, why_off};
pub(crate) use state::{command_shortcuts, shown_state_with, why_off};
pub(crate) use submenus::PATH_EFFECTS;
pub use tree::{Listed, menu_tree, menu_tree_named};
pub use ui_commands::UI_COMMANDS;
+38 -8
View File
@@ -18,7 +18,7 @@ use craft::ui::runtime::windows::WindowHost;
use serde_json::{Value, json};
use super::labels::holds_plugin_commands;
use super::{Listed, PLACEHOLDER, TODO, display_label, menu_tree_named, plugin_revision, shown_label, shown_state, why_off};
use super::{Listed, PLACEHOLDER, TODO, display_label, menu_tree_named, plugin_revision, shown_label, shown_state_with, why_off};
use crate::i18n::LangExt as _;
use crate::{UiState, VectorcraftApp};
@@ -424,7 +424,9 @@ fn linked_defaults(command: &CommandRef) -> Option<Vec<Binding>> {
/// The state of the row `command`, named `label` in the catalog: on or dimmed with its reason, checked, and its title
/// where the app shows another.
fn row_state(app: &VectorcraftApp, command: &CommandRef, label: &str) -> CommandState {
let (enabled, checked) = shown_state(app, crate::keymap::state_id(command), &command.params).unwrap_or((false, None));
let state_id = crate::keymap::state_id(command);
let why = why_off(app, state_id);
let (enabled, checked) = shown_state_with(app, state_id, &command.params, &why).unwrap_or((false, None));
let mut state = CommandState::new().enabled(enabled);
if let Some(on) = checked {
state = state.checked(on);
@@ -434,7 +436,7 @@ fn row_state(app: &VectorcraftApp, command: &CommandRef, label: &str) -> Command
state = state.title(shown.clone());
}
if !enabled {
state = state.hint(off_reason(app, command, &shown));
state = state.hint(off_reason_with(app, command, &shown, why));
}
state
}
@@ -442,9 +444,13 @@ fn row_state(app: &VectorcraftApp, command: &CommandRef, label: &str) -> Command
/// What the dimmed row `command`, named `label`, says in the interface language: the engine's reason, the panels' in
/// Presentation Mode, else that the command is not available right now.
pub(crate) fn off_reason(app: &VectorcraftApp, command: &CommandRef, label: &str) -> String {
why_off(app, crate::keymap::state_id(command))
.flatten()
.or_else(|| crate::palette::refusal(app, command).filter(|_| command.id == "window.panel"))
off_reason_with(app, command, label, why_off(app, crate::keymap::state_id(command)))
}
/// [`off_reason`] from `why`, what [`why_off`] answered for the command's state id.
fn off_reason_with(app: &VectorcraftApp, command: &CommandRef, label: &str, why: Option<Option<String>>) -> String {
why.flatten()
.or_else(|| if command.id == "window.panel" { crate::palette::refusal(app, command) } else { None })
.map_or_else(|| crate::i18n::msg(&format!("command `{label}` is not available right now")), |why| crate::i18n::msg(&why))
}
@@ -520,7 +526,8 @@ impl Build<'_> {
return Some(item);
}
let (id, params) = (command.id.as_str(), &command.params);
let (enabled, checked) = shown_state(self.app, id, params)?;
let why = why_off(self.app, id);
let (enabled, checked) = shown_state_with(self.app, id, params, &why)?;
let title = self.item_title(id, params, &item.label);
let mut state = CommandState::new().enabled(enabled).title(title.clone());
if let Some(on) = checked {
@@ -532,7 +539,10 @@ impl Build<'_> {
} else if enabled {
None
} else {
Some(off_reason(self.app, &command, &title))
// The reason reads the check of the command's state id, which is `id` but for the framework's own rows.
let state_id = crate::keymap::state_id(&command);
let why = if state_id == id { why } else { why_off(self.app, state_id) };
Some(off_reason_with(self.app, &command, &title, why))
};
if let Some(tip) = tip {
state = state.hint(tip.clone());
@@ -625,4 +635,24 @@ mod tests {
assert!(app.menus.states.get(&CommandRef::new("edit.undo")).is_some_and(CommandState::is_enabled), "the states follow the edits");
assert!(app.menus.states.get(&CommandRef::new("object.group")).is_some_and(CommandState::is_enabled));
}
/// A rebuild asks each dimmed row's command once whether it can run, for its state and its reason together.
#[test]
fn a_rebuild_checks_each_dimmed_row_once() {
let (mut app, ctx) = app_with_document();
// `object.group` is a row of the Object menu; `appearance.addFill` is a catalog row only, dimmed with no subject.
let ids = ["object.group", "appearance.addFill"];
for id in ids {
assert!(!app.menus.states.get(&CommandRef::new(id)).is_some_and(CommandState::is_enabled), "{id} is dimmed");
}
app.run("shape.rectangle", json!({"x": 0, "y": 0, "width": 10, "height": 10})).unwrap();
app.run("select.none", json!({})).unwrap();
super::super::state::WHY_OFF_ASKED.with_borrow_mut(Vec::clear);
refresh(&mut app, &ctx);
let asked = super::super::state::WHY_OFF_ASKED.take();
for id in ids {
assert_eq!(asked.iter().filter(|a| *a == id).count(), 1, "{id}");
assert!(app.menus.states.get(&CommandRef::new(id)).is_some_and(|s| !s.is_enabled()), "{id} is still dimmed");
}
}
}
+15 -1
View File
@@ -173,7 +173,13 @@ fn east_asian_hidden(app: &VectorcraftApp, id: &str) -> bool {
/// another stands in for ([`swapped_out`]) or an East Asian item with those options hidden
/// ([`east_asian_hidden`]).
pub fn shown_state(app: &VectorcraftApp, id: &str, p: &Value) -> Option<(bool, Option<bool>)> {
let en = enabled(app, id)
shown_state_with(app, id, p, &why_off(app, id))
}
/// [`shown_state`] from `why`, what [`why_off`] answered for `id`: a caller that also writes the reason a dimmed row
/// gives runs the command's check once.
pub(crate) fn shown_state_with(app: &VectorcraftApp, id: &str, p: &Value, why: &Option<Option<String>>) -> Option<(bool, Option<bool>)> {
let en = why.is_none()
&& (id != crate::addons::select::link_status::COMMAND || crate::addons::select::link_status::enabled(app, p))
&& (!vectorcraft_engine::cmd::plugin::is_command_id(id) || crate::dialogs::plugin::canvas_item_enabled(app, id, p));
if (!en && hidden_when_disabled(id)) || swapped_out(app, id) || east_asian_hidden(app, id) {
@@ -252,11 +258,19 @@ pub fn enabled(app: &VectorcraftApp, id: &str) -> bool {
why_off(app, id).is_none()
}
#[cfg(test)]
thread_local! {
/// The ids [`why_off`] was asked about on this thread, in order.
pub(crate) static WHY_OFF_ASKED: std::cell::RefCell<Vec<String>> = const { std::cell::RefCell::new(Vec::new()) };
}
/// Why command `id` can't run now ([`enabled`]): `None` while it can, else `Some` with the
/// engine's reason when the engine's check of an engine command fails, and with the reason of
/// [`crate::panels::align::why_off`] for an Align panel button. The engine's check runs at most
/// once.
pub(crate) fn why_off(app: &VectorcraftApp, id: &str) -> Option<Option<String>> {
#[cfg(test)]
WHY_OFF_ASKED.with_borrow_mut(|asked| asked.push(id.to_string()));
if waits_for_ime(app, id) {
return Some(None);
}