mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-30 02:07:10 +01:00
Merge pull request #10028 from dotCooCoo/fix/speed-i13929-menu-model
Menus: edits and selection changes keep the command catalog's rows
This commit is contained in:
1 file changed
+142
-29
@@ -44,6 +44,8 @@ pub(crate) struct Menus {
|
||||
/// The states of the catalogued commands and of every menu item's reference.
|
||||
pub(crate) states: Tracked<CommandStates>,
|
||||
catalog: Option<AppCatalog>,
|
||||
/// The rows `catalog` was made from, kept while what they are made from stays the same.
|
||||
rows: Option<CatalogRows>,
|
||||
}
|
||||
|
||||
impl Menus {
|
||||
@@ -245,26 +247,35 @@ fn rebuild(app: &mut VectorcraftApp, target: Target) {
|
||||
.collect();
|
||||
let Build { mut states, refs, .. } = build;
|
||||
|
||||
let defs = catalog_defs(app, &refs, &mut states);
|
||||
// The catalog is built again only when what its rows are made from changed; edits and selection changes change
|
||||
// the rows' states alone.
|
||||
let inputs = CatalogInputs {
|
||||
snapshot: crate::shortcut_editor::snapshot(),
|
||||
platform: crate::keymap::platform(),
|
||||
plays: crate::panels::actions::function_keys(&app.ui.actions),
|
||||
refs,
|
||||
overrides: app.shortcut_overrides.clone(),
|
||||
generation: generation(&listed, &[], &[]),
|
||||
};
|
||||
let rows = match app.menus.rows.take().filter(|rows| rows.accepted && rows.inputs == inputs) {
|
||||
Some(rows) => rows,
|
||||
None => {
|
||||
let (defs, known) = catalog_defs(&inputs);
|
||||
let catalog = build_catalog(app, &listed, &inputs.plays, defs.clone());
|
||||
let accepted = catalog.is_some();
|
||||
if catalog.is_some() {
|
||||
app.menus.catalog = catalog;
|
||||
}
|
||||
CatalogRows { inputs, defs, known, accepted }
|
||||
}
|
||||
};
|
||||
row_states(app, &rows, &mut states);
|
||||
crate::panels::panel_menu_states(app, &mut states);
|
||||
|
||||
let bar = MenuBar::new(menus);
|
||||
let options = LayoutOptions::new(APP_NAME).label(&translate_label);
|
||||
let laid = layout(&bar, target, &options).bar;
|
||||
let type_keys = crate::type_editing::app_keys(&defs, &app.shortcut_overrides);
|
||||
let (defs, linked_keys) = without_linked(defs);
|
||||
match AppCatalog::new(defs, generation(&listed, &crate::panels::actions::function_keys(&app.ui.actions), &type_keys)) {
|
||||
Ok(catalog) => {
|
||||
// While a text field has the keys no shortcut runs, as in production; the text-editing chords still edit it,
|
||||
// and the Type tool's commands run while it types.
|
||||
let catalog = catalog.typing_rule(TypingRule::AllWait);
|
||||
let catalog = linked_keys.into_iter().fold(catalog, |catalog, (id, keys)| catalog.replace_defaults(id, keys));
|
||||
let catalog = UNROUTED.iter().fold(catalog, |catalog, id| catalog.replace_defaults(id.as_str(), Vec::new()));
|
||||
app.menus.catalog = Some(type_keys.into_iter().fold(catalog, |catalog, (id, keys)| catalog.replace_defaults(id, keys)));
|
||||
}
|
||||
// A refused catalog is a bug in the rows; the last good one keeps the commands working.
|
||||
Err(e) => eprintln!("menu catalog refused: {e:?}"),
|
||||
}
|
||||
app.menus.rows = Some(rows);
|
||||
app.menus.bar = bar;
|
||||
if *app.menus.laid != laid {
|
||||
app.menus.laid.edit(|l| *l = laid);
|
||||
@@ -272,28 +283,63 @@ fn rebuild(app: &mut VectorcraftApp, target: Target) {
|
||||
app.menus.states.edit(|s| *s = states);
|
||||
}
|
||||
|
||||
/// What the catalog's rows are made from: the editor's entries, the platform, the actions' function keys, the menu
|
||||
/// references with their labels, the user's shortcuts, and the language, plug-ins, scripts, fonts and listed names.
|
||||
struct CatalogInputs {
|
||||
snapshot: std::sync::Arc<crate::shortcut_editor::Snapshot>,
|
||||
platform: craft::keymap::Platform,
|
||||
plays: Vec<(String, craft::keymap::Chord)>,
|
||||
refs: Vec<(CommandRef, String, bool)>,
|
||||
overrides: std::sync::Arc<craft::keymap::Overrides>,
|
||||
generation: u64,
|
||||
}
|
||||
|
||||
impl PartialEq for CatalogInputs {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
std::sync::Arc::ptr_eq(&self.snapshot, &other.snapshot)
|
||||
&& std::sync::Arc::ptr_eq(&self.overrides, &other.overrides)
|
||||
&& (self.platform, self.generation) == (other.platform, other.generation)
|
||||
&& self.plays == other.plays
|
||||
&& self.refs == other.refs
|
||||
}
|
||||
}
|
||||
|
||||
/// The catalog's rows as the last rebuild made them from `inputs`, the keys of those rows, and whether the catalog
|
||||
/// took them (a refused set is made again on the next rebuild).
|
||||
struct CatalogRows {
|
||||
inputs: CatalogInputs,
|
||||
defs: Vec<CommandDef>,
|
||||
known: HashSet<String>,
|
||||
accepted: bool,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
/// How many times this thread made the catalog's rows.
|
||||
pub(crate) static CATALOG_BUILDS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
|
||||
}
|
||||
|
||||
/// The catalog's rows: the keymap's, with icons, then a row for every menu reference the keymap
|
||||
/// does not name (by its key or its bare id), and the state of every row not set by the tree.
|
||||
/// does not name (by its key or its bare id), with the keys of every row.
|
||||
// PLAN: the Shift+Space row for MomentaryCommands::RotateView (S-CV8) is the canvas area's: that
|
||||
// command is not in the app scope, which `AppCatalog` accepts.
|
||||
// PLAN: ActionsPanelCommands' defs (area plan X6) are menus-w18's; their signature is not visible here.
|
||||
fn catalog_defs(app: &VectorcraftApp, refs: &[(CommandRef, String, bool)], states: &mut CommandStates) -> Vec<CommandDef> {
|
||||
let snapshot = crate::shortcut_editor::snapshot();
|
||||
fn catalog_defs(inputs: &CatalogInputs) -> (Vec<CommandDef>, HashSet<String>) {
|
||||
#[cfg(test)]
|
||||
CATALOG_BUILDS.set(CATALOG_BUILDS.get() + 1);
|
||||
// An action's function key comes first: it takes priority over the command shortcuts.
|
||||
let plays = crate::panels::actions::function_keys(&app.ui.actions).into_iter().map(|(name, chord)| {
|
||||
let plays = inputs.plays.iter().map(|(name, chord)| {
|
||||
let play = CommandRef::with_params(crate::panels::actions::PLAY_ACTION, json!({ "action": name }));
|
||||
CommandDef::new(play, name, Scope::app()).with_default(Binding::new(chord))
|
||||
CommandDef::new(play, name.clone(), Scope::app()).with_default(Binding::new(*chord))
|
||||
});
|
||||
let mut defs: Vec<CommandDef> = plays
|
||||
.chain(crate::keymap::catalog_defs(crate::keymap::platform(), &snapshot).into_iter().map(|d| {
|
||||
match crate::panels::command_icon(&d.command.id) {
|
||||
Some(icon) => d.with_icon(icon),
|
||||
None => d,
|
||||
}
|
||||
.chain(crate::keymap::catalog_defs(inputs.platform, &inputs.snapshot).into_iter().map(|d| match crate::panels::command_icon(&d.command.id) {
|
||||
Some(icon) => d.with_icon(icon),
|
||||
None => d,
|
||||
}))
|
||||
.collect();
|
||||
let mut known: HashSet<String> = defs.iter().map(|d| d.command.key()).collect();
|
||||
for (command, label, checkable) in refs {
|
||||
for (command, label, checkable) in &inputs.refs {
|
||||
if known.contains(&command.key()) || known.contains(&command.id) {
|
||||
continue;
|
||||
}
|
||||
@@ -301,20 +347,46 @@ fn catalog_defs(app: &VectorcraftApp, refs: &[(CommandRef, String, bool)], state
|
||||
let def = CommandDef::new(command.clone(), label.clone(), Scope::app());
|
||||
defs.push(if *checkable { def.toggle() } else { def });
|
||||
}
|
||||
for d in &defs {
|
||||
(defs, known)
|
||||
}
|
||||
|
||||
/// The catalog made from `defs`, or `None` when it refuses them.
|
||||
fn build_catalog(app: &VectorcraftApp, listed: &Listed, plays: &[(String, craft::keymap::Chord)], defs: Vec<CommandDef>) -> Option<AppCatalog> {
|
||||
let type_keys = crate::type_editing::app_keys(&defs, &app.shortcut_overrides);
|
||||
let (defs, linked_keys) = without_linked(defs);
|
||||
match AppCatalog::new(defs, generation(listed, plays, &type_keys)) {
|
||||
Ok(catalog) => {
|
||||
// While a text field has the keys no shortcut runs, as in production; the text-editing chords still edit it,
|
||||
// and the Type tool's commands run while it types.
|
||||
let catalog = catalog.typing_rule(TypingRule::AllWait);
|
||||
let catalog = linked_keys.into_iter().fold(catalog, |catalog, (id, keys)| catalog.replace_defaults(id, keys));
|
||||
let catalog = UNROUTED.iter().fold(catalog, |catalog, id| catalog.replace_defaults(id.as_str(), Vec::new()));
|
||||
Some(type_keys.into_iter().fold(catalog, |catalog, (id, keys)| catalog.replace_defaults(id, keys)))
|
||||
}
|
||||
// A refused catalog is a bug in the rows; the last good one keeps the commands working.
|
||||
Err(e) => {
|
||||
eprintln!("menu catalog refused: {e:?}");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The state of every catalog row the tree did not set, and of the engine commands the palette lists that no row
|
||||
/// names.
|
||||
fn row_states(app: &VectorcraftApp, rows: &CatalogRows, states: &mut CommandStates) {
|
||||
for d in &rows.defs {
|
||||
if states.get(&d.command).is_none() {
|
||||
states.set(d.command.clone(), row_state(app, &d.command, &d.label));
|
||||
}
|
||||
}
|
||||
// The engine commands the palette lists and no row names (Asset Export's Remove and Rename) are dimmed there and say
|
||||
// why, as the rows are.
|
||||
for c in crate::palette::listed_engine_commands().filter(|c| !known.contains(c.id)) {
|
||||
for c in crate::palette::listed_engine_commands().filter(|c| !rows.known.contains(c.id)) {
|
||||
let command = CommandRef::new(c.id);
|
||||
if states.get(&command).is_none() {
|
||||
states.set(command.clone(), row_state(app, &command, c.label));
|
||||
}
|
||||
}
|
||||
defs
|
||||
}
|
||||
|
||||
/// `defs` without the commands a linked framework set defines (Undo, Redo, Cut, Copy, Paste and Tab's panels toggles):
|
||||
@@ -506,3 +578,44 @@ impl Build<'_> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn app_with_document() -> (VectorcraftApp, egui::Context) {
|
||||
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);
|
||||
(app, ctx)
|
||||
}
|
||||
|
||||
/// Edits and selection changes give the rows new states and leave the catalog's rows as they were made.
|
||||
#[test]
|
||||
fn edits_and_selection_changes_keep_the_catalog_rows() {
|
||||
let (mut app, ctx) = app_with_document();
|
||||
// The plug-ins, fonts and scripts are the process's, which tests on other threads change, and a row the menus
|
||||
// leave out while it is off comes back with the art: a rebuild that made the rows while those moved is not counted.
|
||||
let shared = |app: &VectorcraftApp| {
|
||||
app.menus.rows.as_ref().map(|r| (std::sync::Arc::as_ptr(&r.inputs.snapshot).addr(), r.inputs.generation, r.inputs.refs.clone()))
|
||||
};
|
||||
let mut remade = 0;
|
||||
let mut step = |app: &mut VectorcraftApp, id: &str, params: Value| {
|
||||
app.run(id, params).unwrap();
|
||||
let (builds, before) = (CATALOG_BUILDS.get(), shared(app));
|
||||
refresh(app, &ctx);
|
||||
if CATALOG_BUILDS.get() > builds && shared(app) == before {
|
||||
remade += 1;
|
||||
}
|
||||
};
|
||||
for k in 0..5 {
|
||||
step(&mut app, "shape.rectangle", json!({"x": k * 20, "y": 0, "width": 10, "height": 10}));
|
||||
step(&mut app, "select.none", json!({}));
|
||||
}
|
||||
step(&mut app, "select.all", json!({}));
|
||||
assert_eq!(remade, 0, "11 rebuilds after edits and selection changes made the catalog's rows again");
|
||||
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));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user