mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-30 02:07:10 +01:00
Menus: a dimmed row runs its command's check once per rebuild
The menu model is rebuilt on every edit and selection change. For each dimmed row, shown_state asked why_off whether the command could run, and off_reason asked it again for the row's reason. For a row whose check gives no reason, off_reason also called palette::refusal, which asked why_off a third time and ran the engine check, then dropped the answer for every row but window.panel. With 10,000 objects selected, many of these checks walk the selection and take 1.5 to 7 ms each. shown_state_with takes the why_off answer, and the tree's rows and the catalog's rows pass it on to the reason. palette::refusal runs only for window.panel rows.
This commit is contained in:
1 parent
d4b6d7f10d
commit
78472e947f
3 files changed
+64
-11
No files matched your search
@@ -92,7 +92,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;
|
||||
|
||||
@@ -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};
|
||||
|
||||
@@ -352,7 +352,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);
|
||||
@@ -362,7 +364,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
|
||||
}
|
||||
@@ -370,9 +372,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))
|
||||
}
|
||||
|
||||
@@ -448,8 +454,10 @@ impl Build<'_> {
|
||||
return Some(item);
|
||||
}
|
||||
let (id, params) = (command.id.as_str(), &command.params);
|
||||
let (enabled, checked) = shown_state(self.app, id, params)?;
|
||||
let mut state = CommandState::new().enabled(enabled).title(self.item_title(id, params, &item.label));
|
||||
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 {
|
||||
state = state.checked(on);
|
||||
}
|
||||
@@ -459,7 +467,10 @@ impl Build<'_> {
|
||||
} else if enabled {
|
||||
None
|
||||
} else {
|
||||
Some(off_reason(self.app, &command, &self.item_title(id, params, &item.label)))
|
||||
// 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());
|
||||
@@ -506,3 +517,31 @@ impl Build<'_> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// 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 = VectorcraftApp::new(vectorcraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width": 200, "height": 200})).unwrap();
|
||||
let ctx = crate::testing::ui_context();
|
||||
refresh(&mut app, &ctx);
|
||||
// `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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user