diff --git a/crates/ui-egui/src/dialogs/revolve/tests.rs b/crates/ui-egui/src/dialogs/revolve/tests.rs index 16b99491a6..c3fae6fd44 100644 --- a/crates/ui-egui/src/dialogs/revolve/tests.rs +++ b/crates/ui-egui/src/dialogs/revolve/tests.rs @@ -114,22 +114,32 @@ fn rows() -> Vec<(&'static str, &'static str, &'static str, Value)> { #[test] fn a_3d_row_sets_its_param_previews_and_applies_in_one_step() { + // One window serves every row: Cancel, and OK followed by Undo, leave the drawing as the dialog found it, and each + // row opens the dialog again over it. + let mut shown: Option = None; for (effect, page, key, value) in rows() { let name = format!("{effect}.{key}"); for confirm in [false, true] { - let mut h = drawn(SQUARE, effect); - let before = source(&h); + let p = json!({"effect": effect, "item": null}); + let h = match shown.as_mut() { + Some(h) => { + reopen(h, p); + h + } + None => shown.insert(opened(app_with(SQUARE), p)), + }; + let before = source(h); h.set("__page", json!(page)); h.set(key, value.clone()); - assert!(same(&stored(&h, effect)[key], &value), "{name}: the preview follows the row"); + assert!(same(&stored(h, effect)[key], &value), "{name}: the preview follows the row"); if confirm { h.ok(); - assert!(same(&stored(&h, effect)[key], &value), "{name}: OK keeps it"); - undo(&h); + assert!(same(&stored(h, effect)[key], &value), "{name}: OK keeps it"); + undo(h); } else { h.cancel(); } - assert_eq!(doc(&h), before, "{name}: {}", if confirm { "one undo step" } else { "Cancel restores" }); + assert_eq!(doc(h), before, "{name}: {}", if confirm { "one undo step" } else { "Cancel restores" }); } } } diff --git a/crates/ui-egui/src/menus/entries.rs b/crates/ui-egui/src/menus/entries.rs index 69722bd1ae..66353c55be 100644 --- a/crates/ui-egui/src/menus/entries.rs +++ b/crates/ui-egui/src/menus/entries.rs @@ -1,7 +1,9 @@ +use craft::keymap::{Chord, CommandRef}; + use super::context::context_items; -use super::labels::{display_label, dynamic_label, effect_item_label, item_shortcut, spelling_item_label, unlock_item_label}; +use super::labels::{display_label, dynamic_label, effect_item_label, item_shortcut_in, spelling_item_label, unlock_item_label}; use super::slots::{recent_readable, recent_slot}; -use super::state::{shortcut_of, shown_state}; +use super::state::{command_shortcuts, shown_state}; use super::tree::{Listed, menu_tree_named}; use super::{Node, Serialize, Value, VectorcraftApp, unbound}; @@ -32,8 +34,9 @@ pub struct MenuEntry { /// Flattened menu for `ui.menu.list`. pub fn menu_entries(app: &VectorcraftApp) -> Vec { let mut out = vec![]; + let chord = command_shortcuts(app); for (title, items) in menu_tree_named(app.session.prefs.font_names_in_english, &Listed::of(app)) { - flatten(app, vec![title.to_string()], vec![crate::i18n::t(title).to_string()], &items, &mut out); + flatten(app, &chord, vec![title.to_string()], vec![crate::i18n::t(title).to_string()], &items, &mut out); } out } @@ -41,7 +44,7 @@ pub fn menu_entries(app: &VectorcraftApp) -> Vec { /// Flattened canvas context menu for `ui.contextMenu.list` (paths are its submenus). pub fn context_entries(app: &VectorcraftApp) -> Vec { let mut out = vec![]; - flatten(app, vec![], vec![], &context_items(app), &mut out); + flatten(app, &command_shortcuts(app), vec![], vec![], &context_items(app), &mut out); out } @@ -166,7 +169,15 @@ pub fn insertable_items(english_names: bool, listed: &Listed) -> Vec, shown: Vec, nodes: &[Node], out: &mut Vec) { +/// `nodes` flattened into `out`, each item's shortcut looked up in `chord` ([`command_shortcuts`]). +pub(super) fn flatten( + app: &VectorcraftApp, + chord: &dyn Fn(&CommandRef) -> Option, + path: Vec, + shown: Vec, + nodes: &[Node], + out: &mut Vec, +) { for node in nodes { match node { Node::Item(it) if unbound(&it.command.id) => out.push(MenuEntry { @@ -197,7 +208,10 @@ pub(super) fn flatten(app: &VectorcraftApp, path: Vec, shown: Vec, shown: Vec {} } diff --git a/crates/ui-egui/src/menus/tests.rs b/crates/ui-egui/src/menus/tests.rs index b21b2e8ccf..8930142be6 100644 --- a/crates/ui-egui/src/menus/tests.rs +++ b/crates/ui-egui/src/menus/tests.rs @@ -401,7 +401,7 @@ fn menu_entry_shown_paths_keep_script_folder_names() { let items = vec![sub("Scripts", vec![sub("File", folder), cp("Other Script…", "ui.runScript", Value::Null)])]; let mut out = vec![]; crate::i18n::set_current(de); - flatten(&app, vec!["File".into()], vec![crate::i18n::t("File").into()], &items, &mut out); + flatten(&app, &super::command_shortcuts(&app), vec!["File".into()], vec![crate::i18n::t("File").into()], &items, &mut out); crate::i18n::set_current(crate::i18n::Lang::EN); let script = out.iter().find(|e| e.command.as_deref() == Some(crate::scripts_menu::COMMAND)).unwrap(); assert_eq!(script.path, ["File", "Scripts", "File"]); diff --git a/crates/ui-egui/src/palette.rs b/crates/ui-egui/src/palette.rs index 6deacc0c01..017486e7ad 100644 --- a/crates/ui-egui/src/palette.rs +++ b/crates/ui-egui/src/palette.rs @@ -1753,8 +1753,8 @@ mod tests { /// the menu bar, and commands that need a value no menu item gives, with a menu path of their own /// or without one. A pick of each one it leaves /// out (with no document, one object selected or several) changes neither the document, its - /// selection, history and layer state, nor the interface. Every [`PICK_RUNS`] query is still - /// listed. + /// selection, history and layer state, the tool's state, nor the interface. Every [`PICK_RUNS`] + /// query is still listed. #[test] fn the_palette_leaves_out_the_commands_a_pick_cannot_run() { let ui_ids: std::collections::HashSet<&str> = menus::UI_COMMANDS.iter().map(|c| c.0).collect(); @@ -1785,16 +1785,33 @@ mod tests { (st.doc.clone(), format!("{:?}", st.selection), st.history.undo.len(), st.revision, st.active_layer, st.layer_rows.clone()) }); // A modeless dialog (Find and Replace) blocks nothing, so `any_dialog_open` leaves it out: the host lists it. - (doc, app.session.script_console().lines().len(), (app.any_dialog_open(), crate::tests::picks::open_dialogs(app).len()), ui) + // The tool's own state: the tool, a gesture or an input method's text in progress, and an interaction the + // session holds open. + let tool = (app.session.tool_id().to_owned(), app.session.tool_composing(), app.session.pointer_held(), app.session.in_interaction()); + (doc, app.session.script_console().lines().len(), (app.any_dialog_open(), crate::tests::picks::open_dialogs(app).len()), ui, tool) }; - let acts = |id: &str| { - [Session::new(), Fixture::Single.session(), Fixture::Multi.session()].into_iter().any(|session| { - let mut app = app_on(session); - let before = look(&app); - pick(&mut app, id); - look(&app) != before + // One app for each of the three cases, picked in one after another while a pick changes nothing that `look` + // sees; a pick that changes something is recorded, and its case starts again from a new app. + let fresh = |case: usize| { + app_on(match case { + 0 => Session::new(), + 1 => Fixture::Single.session(), + _ => Fixture::Multi.session(), }) }; + let mut apps: Vec = (0..3).map(fresh).collect(); + let mut acts = |id: &str| { + let mut changed = false; + for (case, app) in apps.iter_mut().enumerate() { + let before = look(app); + pick(app, id); + if look(app) != before { + changed = true; + *app = fresh(case); + } + } + changed + }; let changed: Vec<&str> = ids.iter().copied().filter(|id| acts(id)).collect(); assert!(changed.is_empty(), "a pick of these does something: {changed:?}"); for c in &dropped { diff --git a/crates/ui-egui/src/panels/actions/tests_edits.rs b/crates/ui-egui/src/panels/actions/tests_edits.rs index 1fe2f13711..208c293b70 100644 --- a/crates/ui-egui/src/panels/actions/tests_edits.rs +++ b/crates/ui-egui/src/panels/actions/tests_edits.rs @@ -927,8 +927,10 @@ fn insert_menu_item_refuses_what_no_menu_item_runs_and_changes_nothing() { fn every_listed_menu_item_runs_from_the_menu_what_its_step_holds() { // The View menu's proof items change the whole process's proof view. let _view = crate::testing::DefaultProofView::new(); + // A new app for each item, all in one window. + let window = crate::testing::window_context(); for item in crate::menus::insertable_items(false, &crate::menus::Listed::default()) { - let (mut app, _) = app_with_three_moves(); + let (mut app, _) = three_moves_in(new_app_in(&window)); app.run(INSERT_MENU_ITEM, json!({"set": 0, "action": 0, "command": item.command, "params": item.params})).unwrap(); let held = steps(&app, 0, 0)[3].1.clone(); let start = app.session.journal.len(); diff --git a/crates/ui-egui/src/panels/actions/tests_support.rs b/crates/ui-egui/src/panels/actions/tests_support.rs index 610e180bf0..3f763ad877 100644 --- a/crates/ui-egui/src/panels/actions/tests_support.rs +++ b/crates/ui-egui/src/panels/actions/tests_support.rs @@ -20,9 +20,14 @@ use crate::VectorcraftApp; /// An app with the built-in set, in a window whose dialog host the tests drive and which has drawn its first frame, so /// the window's keymap holds the app's commands. pub(crate) fn new_app() -> VectorcraftApp { - let mut app = crate::testing::windowed(VectorcraftApp::new(Session::new(), Default::default())); - let ctx = app.ui_ctx.clone().expect("a window"); - crate::testing::app_frame(&mut app, &ctx, Default::default()); + new_app_in(&crate::testing::window_context()) +} + +/// [`new_app`] in the window of `ctx` ([`crate::testing::window_context`]), which a test can reuse for a new app at +/// each step. +pub(crate) fn new_app_in(ctx: &egui::Context) -> VectorcraftApp { + let mut app = crate::testing::windowed_in(VectorcraftApp::new(Session::new(), Default::default()), ctx); + crate::testing::app_frame(&mut app, ctx, Default::default()); app } @@ -251,7 +256,11 @@ pub(crate) fn cancel_dialog(app: &mut VectorcraftApp) { /// A document with one rectangle selected and the set "Moves" with one action that moves it /// right three times, by 1, 2 and 4 pt → (the app, the rectangle's left edge as it moves). pub(crate) fn app_with_three_moves() -> (VectorcraftApp, impl Fn(&VectorcraftApp) -> f64) { - let mut app = new_app(); + three_moves_in(new_app()) +} + +/// [`app_with_three_moves`] made from `app`, a new app such as [`new_app_in`] gives. +pub(crate) fn three_moves_in(mut app: VectorcraftApp) -> (VectorcraftApp, impl Fn(&VectorcraftApp) -> f64) { app.run("file.new", json!({"width": 200, "height": 100})).unwrap(); let id = vectorcraft_doc::NodeId(app.run("shape.rectangle", json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap()["id"].as_u64().unwrap()); diff --git a/crates/ui-egui/src/panels/discover.rs b/crates/ui-egui/src/panels/discover.rs index 649727d609..58c9bb5c12 100644 --- a/crates/ui-egui/src/panels/discover.rs +++ b/crates/ui-egui/src/panels/discover.rs @@ -52,7 +52,12 @@ pub struct View { /// The text of a tool's shortcut in the window's keymap, empty when it has none. fn tool_shortcut(app: &VectorcraftApp, id: &str) -> String { - menus::shortcut_of(app, &format!("tool:{id}")).map_or_else(String::new, |chord| chord.display(crate::keymap::platform())) + tool_shortcut_in(&menus::command_shortcuts(app), id) +} + +/// [`tool_shortcut`] looked up in `chord` ([`menus::command_shortcuts`]). +fn tool_shortcut_in(chord: &dyn Fn(&craft::keymap::CommandRef) -> Option, id: &str) -> String { + chord(&crate::keymap::command_of(&format!("tool:{id}"))).map_or_else(String::new, |chord| chord.display(crate::keymap::platform())) } /// Updates the retained results' labels, menu paths and shortcuts after the menu state changes (a @@ -109,6 +114,7 @@ fn refresh(app: &mut VectorcraftApp) { /// The tools, each with the entry the search ranks it by. fn tool_hits(app: &VectorcraftApp) -> Vec<(HelpEntry, Hit)> { let lang = crate::i18n::current(); + let chord = menus::command_shortcuts(app); vectorcraft_tools::catalog::all_tools() .map(|tool| { let label: String = crate::i18n::tr(lang, tool.label).into(); @@ -120,7 +126,7 @@ fn tool_hits(app: &VectorcraftApp) -> Vec<(HelpEntry, Hit)> { id: tool.id.into(), label, path: vec![], - shortcut: tool_shortcut(app, tool.id), + shortcut: tool_shortcut_in(&chord, tool.id), english_label: tool.label.into(), english_path: vec![], params: Value::Null, diff --git a/crates/ui-egui/src/shortcuts/tests.rs b/crates/ui-egui/src/shortcuts/tests.rs index f5cd51ebb5..f33216e978 100644 --- a/crates/ui-egui/src/shortcuts/tests.rs +++ b/crates/ui-egui/src/shortcuts/tests.rs @@ -210,6 +210,18 @@ fn editing_hi() -> (VectorcraftApp, u64) { (app, id) } +/// [`editing_hi`] in the window of `ctx`, which a test can reuse for a new app at each step, after the frames +/// [`typing_ready`] runs. +fn editing_hi_in(ctx: &egui::Context) -> (VectorcraftApp, u64) { + let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); + app.session.execute("file.new", &json!({"width": 200, "height": 200})).unwrap(); + key_frame(&mut app, ctx, vec![]); + let id = app.session.execute("text.create", &json!({"x": 20, "y": 50, "text": "Hi"})).unwrap()["id"].as_u64().unwrap(); + edit(&mut app, 2, 2); + idle(&mut app, ctx, 2); + (app, id) +} + /// Starts editing the selected type object with the Type tool and selects `start..end`. fn edit(app: &mut VectorcraftApp, start: u64, end: u64) { app.select_tool("type"); @@ -901,22 +913,33 @@ fn alt_chords_type_special_characters_while_editing_text() { return; } let mut wrong = vec![]; + // A new app for each chord, all in one window. + let ctx = crate::testing::ui_context(); for (key, shift, own, character) in ALT_CHARACTER_CHORDS { for typed in [None, Some(own), Some(own.to_uppercase().as_str())] { - let (mut app, id) = editing_hi(); - let ctx = typing_ready(&mut app); + let (mut app, id) = editing_hi_in(&ctx); key_frame(&mut app, &ctx, alt_press(key, shift, typed)); let case = format!("{key:?} shift={shift} text={typed:?}"); let text = text_and_layers(&mut app, id).0; if text != format!("Hi{character}") { wrong.push(format!("{case}: text = {text:?}")); - continue; - } - app.session.execute("edit.undo", &json!({})).unwrap(); - let undone = text_and_layers(&mut app, id).0; - if undone != "Hi" { - wrong.push(format!("{case}: after Undo = {undone:?}")); + } else { + app.session.execute("edit.undo", &json!({})).unwrap(); + let undone = text_and_layers(&mut app, id).0; + if undone != "Hi" { + wrong.push(format!("{case}: after Undo = {undone:?}")); + } } + // The window keeps a key held from one app to the next: the release makes the next chord a new press. + let m = if shift { Modifiers::ALT | Modifiers::SHIFT } else { Modifiers::ALT }; + key_frame( + &mut app, + &ctx, + vec![ + egui::Event::Key { key, physical_key: Some(key), pressed: false, repeat: false, modifiers: m }, + egui::Event::ModifiersChanged(Modifiers::NONE), + ], + ); } } assert!(wrong.is_empty(), "{wrong:#?}"); diff --git a/crates/ui-egui/src/testing.rs b/crates/ui-egui/src/testing.rs index 6098a8467a..89a74801bc 100644 --- a/crates/ui-egui/src/testing.rs +++ b/crates/ui-egui/src/testing.rs @@ -22,10 +22,21 @@ pub(crate) fn install(ctx: &egui::Context) { /// `app` shown in a window of its own: its context is the one every frame of the test runs on, so its dialogs open on /// the window's host, focus and drags last from frame to frame, and the access tree is on for the tests that find a /// control by its role. -pub(crate) fn windowed(mut app: crate::VectorcraftApp) -> crate::VectorcraftApp { +pub(crate) fn windowed(app: crate::VectorcraftApp) -> crate::VectorcraftApp { + windowed_in(app, &window_context()) +} + +/// The context of a window [`windowed`] shows an app in, with the access tree on. A test that makes a new app for each +/// item can show each one in the same window with [`windowed_in`]. +pub(crate) fn window_context() -> egui::Context { let ctx = ui_context(); ctx.enable_accesskit(); - app.ui_ctx = Some(ctx); + ctx +} + +/// `app` shown in the window of `ctx`, as [`windowed`] shows it in a window of its own. +pub(crate) fn windowed_in(mut app: crate::VectorcraftApp, ctx: &egui::Context) -> crate::VectorcraftApp { + app.ui_ctx = Some(ctx.clone()); app }