From 1f6ad294ab42d22f47dd52156dd897be15ef3f3e Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 2 Dec 2026 16:07:22 -0800 Subject: [PATCH 1/2] Menus: a click on the canvas or a key the canvas takes keeps the menu states The menu model rebuilt every row's state on each frame with a pointer press or release or a key press. A plain click on the canvas therefore rebuilt it twice (press and release), and so did every Space, Shift or tool key. Several row checks walk the selection, so one rebuild took 4.5 ms with one object selected and 60 ms with 6,000 (dev profile). A press that lands on the canvas, with no other layer over it, and a key while the canvas or nothing has the focus no longer force a rebuild. Those inputs change the rows only through the document, the tool or a command, or through state the key now reads: the tool, whether the panels show (Tab), the open flyout and the dock layout, the current window, the view's rotation and active artboard, and whether a dialog is open or a shortcut is being recorded. Clicks away from the canvas, and keys that a text field, a dialog or a panel control takes, still rebuild once. While the Type tool edits text, clicks and keys on the canvas also still rebuild, since they move the caret. Tests: - canvas::tests::a_canvas_click_that_changes_nothing_keeps_the_menus - canvas::tests::a_selection_move_rebuilds_the_menus_for_its_release_only - canvas::tests::a_click_or_a_key_in_edited_text_rebuilds_the_menus - menus::tests::a_key_press_a_dialog_takes_rebuilds_the_menus_once - menus::tests::hiding_the_panels_unchecks_the_window_rows - menus::tests::a_view_turned_on_the_canvas_moves_the_rotate_view_check - menus::tests::closing_a_panel_flyout_unchecks_its_window_row - panel_docking::tests::a_key_a_panel_control_takes_rebuilds_the_menus --- crates/ui-egui/src/app.rs | 2 + crates/ui-egui/src/caches.rs | 6 ++ crates/ui-egui/src/canvas/tests.rs | 91 ++++++++++++++---- crates/ui-egui/src/canvas/tool_adapter.rs | 11 +++ crates/ui-egui/src/menus/model.rs | 45 ++++++--- crates/ui-egui/src/menus/native.rs | 5 +- crates/ui-egui/src/menus/tests.rs | 111 ++++++++++++++++++++-- crates/ui-egui/src/panel_docking/tests.rs | 14 +++ 8 files changed, 245 insertions(+), 40 deletions(-) diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 571a355846..efb429f73d 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -167,6 +167,8 @@ impl VectorcraftApp { carry_target: None, last_drag: None, escaped_press: false, + pressed_at: None, + keys_at: None, host_caps_lock: None, committed_preview: false, panes: Default::default(), diff --git a/crates/ui-egui/src/caches.rs b/crates/ui-egui/src/caches.rs index 58b2e6c803..6dcec22bff 100644 --- a/crates/ui-egui/src/caches.rs +++ b/crates/ui-egui/src/caches.rs @@ -129,6 +129,12 @@ pub struct CanvasCache { /// Escape cancelled the tool's press while the button is still down: its release still sends the tool its Up, /// which gives back a selection tool Cmd lent. pub escaped_press: bool, + /// The input time of the last pass whose pointer button events all landed on a canvas, with no other layer over it: + /// the menus' state key leaves such a press out (`menus::model::last_press`). + pub pressed_at: Option, + /// The input time of the last pass in which the canvas or nothing had the focus, so that the canvas or the app's + /// shortcuts took its keys: the menus' state key leaves those keys out (`menus::model::last_press`). + pub keys_at: Option, /// The host's Caps Lock state at the last canvas pass, which `VectorcraftApp::follow_caps_lock` compares with the /// next reading; `None` before the first pass. pub host_caps_lock: Option, diff --git a/crates/ui-egui/src/canvas/tests.rs b/crates/ui-egui/src/canvas/tests.rs index 41d0e93837..df0d8fc387 100644 --- a/crates/ui-egui/src/canvas/tests.rs +++ b/crates/ui-egui/src/canvas/tests.rs @@ -897,13 +897,12 @@ fn the_move_of_a_releasing_frame_counts_for_the_tool_drag() { assert_eq!(draw(true), Rect::new(130.0, 115.0, 160.0, 130.0), "with Space held through the release the last move moves it"); } -/// A Selection tool move builds the menus' states again twice, in the frame of its press and in the frame of its -/// release, which commits the move. The frame the move begins in and the frames of its previews, which change the -/// document's revision, keep them. The menus are also built again when a process-wide catalog changes, so the fonts -/// and plug-ins are held steady against tests running alongside for the whole run, and the move runs again on a new -/// app, at most 20 times, until the other catalogs held still through it. -#[test] -fn a_selection_move_rebuilds_the_menus_for_its_press_and_release_only() { +/// How many times each frame of `steps` builds the menus' states again, with the Selection tool and a 60 × 30 +/// rectangle at (100, 100) selected and the pointer resting over its middle (`steps` gets that point). The menus are +/// also built again when a process-wide catalog changes, so the fonts and plug-ins are held steady against tests +/// running alongside for the whole run, and the steps run again on a new app, at most 20 times, until the other +/// catalogs held still through them. +fn selection_tool_rebuilds(steps: impl Fn(Pos2, &craft::behavior::CanvasView) -> Vec>) -> (VectorcraftApp, Vec) { let fonts = vectorcraft_text::FontDb::global(); let _steady = (vectorcraft_text::test_fonts::steady_fonts(fonts), vectorcraft_plugins::steady::steady_plugins()); let catalogs = || { @@ -920,33 +919,59 @@ fn a_selection_move_rebuilds_the_menus_for_its_press_and_release_only() { let id = app.session.execute("shape.rectangle", &json!({"x": 100, "y": 100, "width": 60, "height": 30})).unwrap()["id"].as_u64().unwrap(); app.select_tool("selection"); app.session.execute("select.set", &json!({"ids": [id]})).unwrap(); - let (from, to) = (xf.to_screen(Point::new(130.0, 115.0)), xf.to_screen(Point::new(170.0, 135.0))); + let from = xf.to_screen(Point::new(130.0, 115.0)); app_step(&mut app, &ctx, vec![egui::Event::PointerMoved(from)]); for _ in 0..2 { app_step(&mut app, &ctx, vec![]); } - let none = egui::Modifiers::NONE; - let mut steps = vec![vec![primary(from, true, none)]]; - steps.extend((1..=8).map(|i| vec![egui::Event::PointerMoved(from.lerp(to, i as f32 / 8.0))])); - steps.push(vec![primary(to, false, none)]); - steps.extend([vec![], vec![], vec![]]); let mut rebuilt = vec![]; - for events in steps { + for events in steps(from, &xf) { let before = app.menus.rebuilds; app_step(&mut app, &ctx, events); rebuilt.push(app.menus.rebuilds - before); } (app, rebuilt) }; - let (app, rebuilt) = (0..20) + (0..20) .find_map(|_| { let before = catalogs(); let out = run(); (catalogs() == before).then_some(out) }) - .unwrap_or_else(|| panic!("tests running alongside kept changing the font and plug-in catalogs")); + .unwrap_or_else(|| panic!("tests running alongside kept changing the font and plug-in catalogs")) +} + +/// A Selection tool move builds the menus' states again once, for the release that commits it. Its press lands on the +/// canvas, and the frame the move begins in and the frames of its previews, which change the document's revision, keep +/// them. +#[test] +fn a_selection_move_rebuilds_the_menus_for_its_release_only() { + let none = egui::Modifiers::NONE; + let (app, rebuilt) = selection_tool_rebuilds(|from, xf| { + let to = xf.to_screen(Point::new(170.0, 135.0)); + let mut steps = vec![vec![primary(from, true, none)]]; + steps.extend((1..=8).map(|i| vec![egui::Event::PointerMoved(from.lerp(to, i as f32 / 8.0))])); + steps.push(vec![primary(to, false, none)]); + steps.extend([vec![], vec![], vec![]]); + steps + }); assert_eq!(drawn_paths(&app), vec![Rect::new(140.0, 120.0, 200.0, 150.0)], "the drag moves the rectangle"); - assert_eq!(rebuilt, [1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0], "rebuilds in each frame: press, 8 moves, release, 3 idle"); + assert_eq!(rebuilt[..9].iter().sum::(), 0, "the press and the 8 moves keep the menus: {rebuilt:?}"); + assert_eq!(rebuilt.iter().sum::(), 1, "the release that commits the move rebuilds them once: {rebuilt:?}"); +} + +/// A click on the selected rectangle that leaves it where it was, and F20 pressed and released with no field or dialog +/// taking it and no command on it, change nothing the menus show: no frame builds the menus' states again. +#[test] +fn a_canvas_click_that_changes_nothing_keeps_the_menus() { + let none = egui::Modifiers::NONE; + let f20 = |pressed| egui::Event::Key { key: egui::Key::F20, physical_key: None, pressed, repeat: false, modifiers: none }; + let (app, rebuilt) = selection_tool_rebuilds(|from, _| { + vec![vec![primary(from, true, none)], vec![primary(from, false, none)], vec![f20(true)], vec![f20(false)], vec![], vec![]] + }); + assert_eq!(drawn_paths(&app), vec![Rect::new(100.0, 100.0, 160.0, 130.0)], "the click leaves the rectangle where it was"); + assert_eq!(app.session.active().map(|d| d.selection.objects.len()), Some(1), "the rectangle stays selected"); + assert_eq!(rebuilt, [0; 6], "rebuilds in each frame: press, release, a key press and release, 2 idle"); } /// #6369: only the selection box remains over the temporary halftone, and the original @@ -4489,6 +4514,38 @@ fn plain(app: &VectorcraftApp, id: u64) -> String { } } +/// While the Type tool edits text, a click in the text and an arrow key, which move the caret without an edit, build +/// the menus' states again: the type rows read the caret's range. +#[test] +fn a_click_or_a_key_in_edited_text_rebuilds_the_menus() { + use egui::Event; + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.session.prefs.placeholder_text = false; + let ctx = crate::testing::ui_context(); + let id = start_typing(&mut app, &ctx); + typing_frame(&mut app, &ctx, vec![Event::Text("abc".into())]); + typing_frame(&mut app, &ctx, vec![]); + let at = app.canvas_view().unwrap().to_screen(Point::new(101.0, 100.0)); + let button = |pressed| Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE }; + let left = |pressed| Event::Key { key: egui::Key::ArrowLeft, physical_key: None, pressed, repeat: false, modifiers: egui::Modifiers::NONE }; + let rebuilds = |app: &mut VectorcraftApp, steps: Vec>| { + let before = app.menus.rebuilds; + for events in steps { + typing_frame(app, &ctx, events); + } + app.menus.rebuilds - before + }; + let click = || vec![vec![Event::PointerMoved(at), button(true)], vec![button(false)]]; + // The first click puts the caret there; the second leaves the document's revision as it is. + rebuilds(&mut app, click()); + let revision = app.session.active().map(|d| d.revision); + assert!(rebuilds(&mut app, click()) > 0, "a click in the text"); + assert_eq!(app.session.active().map(|d| d.revision), revision, "the click changes no revision"); + assert!(rebuilds(&mut app, vec![vec![left(true)], vec![left(false)]]) > 0, "an arrow key in the text"); + assert_eq!(plain(&app, id), "abc", "neither edits the text"); + assert!(app.session.tool_wants_text(), "editing goes on"); +} + /// Starts editing new point type with the Type tool; returns its id. fn start_typing(app: &mut VectorcraftApp, ctx: &egui::Context) -> u64 { app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); diff --git a/crates/ui-egui/src/canvas/tool_adapter.rs b/crates/ui-egui/src/canvas/tool_adapter.rs index 8b2b35db16..70f1080db5 100644 --- a/crates/ui-egui/src/canvas/tool_adapter.rs +++ b/crates/ui-egui/src/canvas/tool_adapter.rs @@ -656,6 +656,17 @@ pub(crate) fn run(app: &mut VectorcraftApp, ui: &mut Ui, pass: &ToolPass) -> Too if ui.ctx().current_pass_index() == 0 && view_press { app.session.tool_press_elsewhere(); } + // A press on the canvas, and a key while the canvas or nothing has the focus, change what the menus show only through + // the document, the tool or a command, which the menus' state key reads, so the key leaves them out + // (`menus::model::last_press`). + let presses: Vec = + ui.input(|i| i.events.iter().filter_map(|e| if let egui::Event::PointerButton { pos, .. } = e { Some(*pos) } else { None }).collect()); + if !presses.is_empty() && presses.iter().all(|&at| over(Some(at)) && ui.ctx().layer_id_at(at) == Some(ui.layer_id())) { + app.canvas.pressed_at = Some(ui.input(|i| i.time)); + } + if ui.ctx().memory(|m| m.focused()).is_none_or(|id| id == pass.id) { + app.canvas.keys_at = Some(ui.input(|i| i.time)); + } let view_info = app.view_info(); let mut picked = ToolHostResponse::default(); let mut response = if crate::dialogs::recolor::edit_tab::with_theme_pick(app, |app, tool| picked = host_run(app, ui, pass, tool)) { diff --git a/crates/ui-egui/src/menus/model.rs b/crates/ui-egui/src/menus/model.rs index 73a4296dd9..b49274358a 100644 --- a/crates/ui-egui/src/menus/model.rs +++ b/crates/ui-egui/src/menus/model.rs @@ -91,9 +91,11 @@ pub(crate) fn translate_label(english: &'static str) -> String { /// What the menus' rows depend on, cheaply (no allocation): commands run, the active document, /// its revision (edits, selection and view changes), history and clipboard, the filter plug-in that ran last, the language, /// shortcuts, plug-ins, the Scripts folder's listing, an open file dialog and the fonts (Type › Font lists the loaded fonts -/// until the installed ones are scanned, then all of them, #5483). `input`, the pass of the last frame with a pointer -/// button pressed or released or a key pressed ([`last_press`]), changes once for each, which covers what the hash -/// doesn't list (panels shown, a tool picked). While an +/// until the installed ones are scanned, then all of them, #5483), the tool, whether the panels show, the open panel +/// flyout, the dock workspace's identity and revision, the current window and whether a dialog is open or a shortcut +/// is being recorded. `input`, the pass of the last frame with a click away from the canvas or a key a field or a dialog +/// took ([`last_press`]), changes once for each, which covers what the hash +/// doesn't list (a panel's own state, a dialog's fields). While an /// interaction is open (for example a move, scale or shape drag, a dialog's or the font menu's /// preview, or a typing session), the hash takes the revision the interaction began from in place of the current one, /// which each of its previews changes, and changes as it ends. The hash stays the same as a tool's drag begins. An @@ -125,6 +127,14 @@ fn rows_key(app: &VectorcraftApp, input: Option) -> u64 { // Apply Last Filter and Last Filter… are named after the filter that ran last. app.last_filters.last_id().hash(&mut h); app.file_dialog_open().hash(&mut h); + // What a click on the canvas or a key no field takes changes without a command: the tool (a tool's key, a held + // Space), the panels shown (Tab), the open panel flyout and the dock workspace's identity and revision (panels + // shown, hidden or moved), the current window, the view's rotation (Rotate View's checks) and active artboard + // (a selection tool's press on one), and a dialog opened or closed (Escape, Enter). + (s.tool_id(), crate::panel_docking::panels_shown(app), crate::doc_windows::current(app)).hash(&mut h); + (&app.ui.open_panel, std::sync::Arc::as_ptr(&app.ui.dock_workspace).addr(), app.ui.dock_workspace.revision()).hash(&mut h); + app.view().map(|v| (v.rotation.to_bits(), v.artboard)).hash(&mut h); + (app.any_dialog_open(), recording_shortcut(app)).hash(&mut h); (crate::i18n::current().code(), plugin_revision(), crate::scripts_menu::revision(), crate::dialogs::proof_setup::saved_revision()).hash(&mut h); hash_listed_names(&app.ui, &mut h); let fonts = vectorcraft_text::FontDb::global(); @@ -215,20 +225,33 @@ fn lay_out(app: &mut VectorcraftApp, target: Target) { } } -/// The pass number of the last frame with a pointer button pressed or released or a key pressed, the frame `ctx` read -/// last included: the `input` of [`state_key`]. It stays the same over the frames after that one, so each of them -/// changes the key once. +/// The pass number of the last frame with a press that [`pressed_pass`] counts, the frame `ctx` read last included: the +/// `input` of [`state_key`]. It stays the same over the frames after that one, so each of them changes the key once. pub(crate) fn last_press(app: &mut VectorcraftApp, ctx: &egui::Context) -> Option { - if let Some(pass) = pressed_pass(ctx) { + if let Some(pass) = pressed_pass(app, ctx) { app.menus.pressed = Some(pass); } app.menus.pressed } -/// The frame's pass number when it has a pointer button pressed or released (a click ends at the release) or a key -/// pressed. -fn pressed_pass(ctx: &egui::Context) -> Option { - let pressed = ctx.input(|i| i.events.iter().any(|e| matches!(e, egui::Event::PointerButton { .. } | egui::Event::Key { pressed: true, .. }))); +/// The frame's pass number when it has a pointer button pressed or released (a click ends at the release) away from +/// the canvas, or a key pressed while a text field, a dialog or a panel's control has the keys. A click on the canvas +/// changes the rows only through the document, the tool or a command, and a key the canvas or the app's shortcuts take +/// only through those, the panels shown (Tab) or a dialog it opens or closes, which [`rows_key`] reads. While the tool +/// edits text, a click or a key on the canvas counts too: it moves the caret or the selected range, which the type rows +/// read. +fn pressed_pass(app: &VectorcraftApp, ctx: &egui::Context) -> Option { + let editing = app.session.tool_wants_text(); + let typing = editing || WindowHost::of(ctx).typing() || app.any_dialog_open(); + let pressed = ctx.input(|i| { + let on_canvas = !editing && app.canvas.pressed_at == Some(i.time); + let canvas_keys = !typing && app.canvas.keys_at == Some(i.time); + i.events.iter().any(|e| match e { + egui::Event::PointerButton { .. } => !on_canvas, + egui::Event::Key { pressed: true, .. } => !canvas_keys, + _ => false, + }) + }); pressed.then(|| ctx.cumulative_pass_nr()) } diff --git a/crates/ui-egui/src/menus/native.rs b/crates/ui-egui/src/menus/native.rs index 2450a9b201..fae289c8d5 100644 --- a/crates/ui-egui/src/menus/native.rs +++ b/crates/ui-egui/src/menus/native.rs @@ -39,8 +39,9 @@ pub(crate) fn run(app: &mut VectorcraftApp, ctx: &egui::Context) { } } -/// Once per frame, after the UI: bring the native menu up to date. A frame with a click or a key -/// press reads the menus again, as input may change state the key doesn't list. +/// Once per frame, after the UI: bring the native menu up to date. A frame with a click away from +/// the canvas or a key a field or a dialog took reads the menus again, as that input may change +/// state the key doesn't list ([`last_press`]). pub(crate) fn sync(app: &mut VectorcraftApp, ctx: &egui::Context) { if app.services.native_menu.is_none() { return; diff --git a/crates/ui-egui/src/menus/tests.rs b/crates/ui-egui/src/menus/tests.rs index 88c2e04540..ace8c15829 100644 --- a/crates/ui-egui/src/menus/tests.rs +++ b/crates/ui-egui/src/menus/tests.rs @@ -183,22 +183,113 @@ fn settled_app(test: &str) -> Option<(VectorcraftApp, egui::Context)> { Some((app, ctx)) } -/// A key press builds the menus' states again once: the frames after it, with no press, keep them. +/// A key press that a dialog takes builds the menus' states again once: the frames after it, with no press, keep them. +/// The same key with no dialog or field to take it keeps them. #[test] -fn a_key_press_rebuilds_the_menus_once() { - let Some((mut app, ctx)) = settled_app("menus::tests::a_key_press_rebuilds_the_menus_once") else { return }; - let before = app.menus.rebuilds; +fn a_key_press_a_dialog_takes_rebuilds_the_menus_once() { + let Some((mut app, ctx)) = settled_app("menus::tests::a_key_press_a_dialog_takes_rebuilds_the_menus_once") else { return }; + // F20 runs no command and changes nothing the menus show. + let press = |pressed| egui::Event::Key { key: egui::Key::F20, physical_key: None, pressed, repeat: false, modifiers: egui::Modifiers::NONE }; + let rebuilds = |app: &mut VectorcraftApp, events: Vec| { + let before = app.menus.rebuilds; + app_frame(app, &ctx, events); + for _ in 0..4 { + app_frame(app, &ctx, vec![]); + } + app.menus.rebuilds - before + }; + assert_eq!(rebuilds(&mut app, vec![]), 0, "idle frames keep the menus"); + assert_eq!(rebuilds(&mut app, vec![press(true), press(false)]), 0, "a key nothing takes keeps the menus"); + app.run("file.documentSetup", json!({})).unwrap(); + rebuilds(&mut app, vec![]); + // No field of the dialog has the keys: the dialog itself takes them. + ctx.memory_mut(|m| { + if let Some(field) = m.focused() { + m.surrender_focus(field); + } + }); + rebuilds(&mut app, vec![]); + assert!(app.any_dialog_open(), "Document Setup is open"); + assert!(!craft::ui::runtime::windows::WindowHost::of(&ctx).typing(), "no text field has the keys"); + assert_eq!(rebuilds(&mut app, vec![press(true), press(false)]), 1, "a key the dialog takes rebuilds the menus once"); +} + +/// Show or Hide Panels, which the framework runs itself (Tab) with no app command, takes the check marks off the Window +/// menu's panel rows. +#[test] +fn hiding_the_panels_unchecks_the_window_rows() { + use craft::ui::runtime::commands::{ChromeCommand, CommandSet as _, CommandState, FireSource}; + let Some((mut app, ctx)) = settled_app("menus::tests::hiding_the_panels_unchecks_the_window_rows") else { return }; + let checked_panels = |app: &VectorcraftApp| { + app.menus + .bar + .items() + .into_iter() + .filter(|it| it.command.id == "window.panel" && app.menus.states.get(&it.command).and_then(CommandState::is_checked) == Some(true)) + .count() + }; + assert!(checked_panels(&app) > 0, "a panel shows, and its Window row is checked"); + let raw = || egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1600.0, 850.0))), ..Default::default() }; + crate::testing::part_frame(&mut app, &ctx, raw(), |app, ui| { + craft::ui::runtime::commands::fire(ui.ctx(), CommandRef::new(ChromeCommand::TogglePanels.id()), FireSource::Menu); + app.tick(ui.ctx()); + app.ui(ui); + }); for _ in 0..3 { app_frame(&mut app, &ctx, vec![]); } - assert_eq!(app.menus.rebuilds, before, "idle frames keep the menus"); - // F20 runs no command and changes nothing the menus show. - let press = |pressed| egui::Event::Key { key: egui::Key::F20, physical_key: None, pressed, repeat: false, modifiers: egui::Modifiers::NONE }; - app_frame(&mut app, &ctx, vec![press(true), press(false)]); - for _ in 0..4 { + assert!(!crate::panel_docking::panels_shown(&app), "the panels are hidden"); + assert_eq!(checked_panels(&app), 0, "no Window row is checked while the panels are hidden"); +} + +/// A panel flyout opened or closed without a command or input updates its Window row's check mark. +#[test] +fn closing_a_panel_flyout_unchecks_its_window_row() { + use craft::ui::runtime::commands::CommandState; + let Some((mut app, ctx)) = settled_app("menus::tests::closing_a_panel_flyout_unchecks_its_window_row") else { return }; + let row = app + .menus + .bar + .items() + .into_iter() + .find(|it| { + it.command.id == "window.panel" + && it.command.params["panel"] + .as_str() + .is_some_and(|panel| crate::panel_docking::layout_of(&app.ui).opens_in_flyout(&panel.to_owned())) + }) + .expect("the default layout has an iconic panel's Window row") + .command + .clone(); + let checked = |app: &VectorcraftApp| app.menus.states.get(&row).and_then(CommandState::is_checked) == Some(true); + app.ui.open_panel = Some(row.params["panel"].as_str().unwrap().to_owned()); + for _ in 0..2 { app_frame(&mut app, &ctx, vec![]); } - assert_eq!(app.menus.rebuilds, before + 1); + assert!(checked(&app), "the Window row is checked while its panel shows in a flyout"); + app.ui.open_panel = None; + for _ in 0..2 { + app_frame(&mut app, &ctx, vec![]); + } + assert!(!checked(&app), "the Window row is unchecked after its panel's flyout closes"); +} + +/// A view turned by the canvas's navigation (a Shift+drag on the canvas), with no command and no document change, moves +/// View › Rotate View's check to the angle it was turned to. +#[test] +fn a_view_turned_on_the_canvas_moves_the_rotate_view_check() { + use craft::ui::runtime::commands::CommandState; + let Some((mut app, ctx)) = settled_app("menus::tests::a_view_turned_on_the_canvas_moves_the_rotate_view_check") else { return }; + let rows: Vec = app.menus.bar.items().into_iter().filter(|it| it.command.id == "view.rotate").map(|it| it.command.clone()).collect(); + let angle = |row: &CommandRef| row.params["angle"].as_f64().unwrap_or(0.0); + let turned = rows.iter().find(|row| angle(row) != 0.0).expect("Rotate View lists an angle other than 0").clone(); + let checked = |app: &VectorcraftApp, row: &CommandRef| app.menus.states.get(row).and_then(CommandState::is_checked) == Some(true); + assert!(!checked(&app, &turned), "the view starts unturned"); + app.view_mut().expect("a view").rotation = angle(&turned); + for _ in 0..2 { + app_frame(&mut app, &ctx, vec![]); + } + assert!(checked(&app, &turned), "the angle the view is turned to is checked"); } /// A font added to the fonts the whole process shares builds the menus again with no input, Type › Font then lists it, diff --git a/crates/ui-egui/src/panel_docking/tests.rs b/crates/ui-egui/src/panel_docking/tests.rs index 6bcdd4853e..58b47d2a14 100644 --- a/crates/ui-egui/src/panel_docking/tests.rs +++ b/crates/ui-egui/src/panel_docking/tests.rs @@ -1257,6 +1257,20 @@ fn the_overflow_menu_of_a_docked_group_is_reached_and_used_from_the_keyboard() { assert!(h.strip_has_focus(), "the focus stays on the strip after the pick"); } +/// A key that a panel's control takes builds the menus' states again: the control can change what a panel menu row +/// reads without running a command. +#[test] +fn a_key_a_panel_control_takes_rebuilds_the_menus() { + let mut h = Harness::new(); + let strip = h.named("Panels", egui::accesskit::Role::TabList).expect("a group's tab strip"); + h.click(pos2(strip.center().x, strip.bottom() + 25.0)); + assert!(h.strip_has_focus(), "a press in the group focuses its strip"); + let before = h.app.menus.rebuilds; + h.press(egui::Key::F20, egui::Modifiers::NONE); + h.frames(2); + assert!(h.app.menus.rebuilds > before, "the key the strip took rebuilds the menus"); +} + /// A tab group dragged out of the icon column by its grip floats as an expanded panel: its tabs show /// in the window, and a click on each tab shows that panel. #[test] From 0962257f9f1583ece0ebf51d9b36c4320c1355df Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 2 Dec 2026 17:10:03 -0800 Subject: [PATCH 2/2] Tests: menu tests check their own process before building the app The gate's test-lint step failed on four menu tests: each returned early on the Option from settled_app, which the lint reads as a skip on a value from the code under test. The child-process check now sits in each test and settled_app returns the app directly, so the assertions are unchanged. --- crates/ui-egui/src/menus/tests.rs | 38 ++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/crates/ui-egui/src/menus/tests.rs b/crates/ui-egui/src/menus/tests.rs index ace8c15829..a68f026cef 100644 --- a/crates/ui-egui/src/menus/tests.rs +++ b/crates/ui-egui/src/menus/tests.rs @@ -165,13 +165,10 @@ fn snapping_menu_clicks_toggle_view_state_and_painted_checkmarks() { } /// An app with a new document whose installed fonts are cataloged, after the frames that build its first menus and the -/// window's keymap. It runs in a process of its own ([`crate::testing::in_own_process`]): the menus' key holds the -/// installed plug-ins' revision and the fonts' generation, which are the whole process's, so a plug-in that another test -/// installs, or a font it adds, rebuilds this app's menus. `None` in the parent process. -fn settled_app(test: &str) -> Option<(VectorcraftApp, egui::Context)> { - if !crate::testing::in_own_process(test) { - return None; - } +/// window's keymap. Each test that uses it runs in a process of its own ([`crate::testing::in_own_process`]): the menus' +/// key holds the installed plug-ins' revision and the fonts' generation, which are the whole process's, so a plug-in +/// that another test installs, or a font it adds, rebuilds this app's menus. +fn settled_app() -> (VectorcraftApp, egui::Context) { let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), crate::Services::default()); // The scan of the installed fonts, which the app starts, changes the fonts' generation when it ends: wait for it. vectorcraft_text::FontDb::global().family_list(); @@ -180,14 +177,17 @@ fn settled_app(test: &str) -> Option<(VectorcraftApp, egui::Context)> { for _ in 0..3 { app_frame(&mut app, &ctx, vec![]); } - Some((app, ctx)) + (app, ctx) } /// A key press that a dialog takes builds the menus' states again once: the frames after it, with no press, keep them. /// The same key with no dialog or field to take it keeps them. #[test] fn a_key_press_a_dialog_takes_rebuilds_the_menus_once() { - let Some((mut app, ctx)) = settled_app("menus::tests::a_key_press_a_dialog_takes_rebuilds_the_menus_once") else { return }; + if !crate::testing::in_own_process("menus::tests::a_key_press_a_dialog_takes_rebuilds_the_menus_once") { + return; + } + let (mut app, ctx) = settled_app(); // F20 runs no command and changes nothing the menus show. let press = |pressed| egui::Event::Key { key: egui::Key::F20, physical_key: None, pressed, repeat: false, modifiers: egui::Modifiers::NONE }; let rebuilds = |app: &mut VectorcraftApp, events: Vec| { @@ -219,7 +219,10 @@ fn a_key_press_a_dialog_takes_rebuilds_the_menus_once() { #[test] fn hiding_the_panels_unchecks_the_window_rows() { use craft::ui::runtime::commands::{ChromeCommand, CommandSet as _, CommandState, FireSource}; - let Some((mut app, ctx)) = settled_app("menus::tests::hiding_the_panels_unchecks_the_window_rows") else { return }; + if !crate::testing::in_own_process("menus::tests::hiding_the_panels_unchecks_the_window_rows") { + return; + } + let (mut app, ctx) = settled_app(); let checked_panels = |app: &VectorcraftApp| { app.menus .bar @@ -246,7 +249,10 @@ fn hiding_the_panels_unchecks_the_window_rows() { #[test] fn closing_a_panel_flyout_unchecks_its_window_row() { use craft::ui::runtime::commands::CommandState; - let Some((mut app, ctx)) = settled_app("menus::tests::closing_a_panel_flyout_unchecks_its_window_row") else { return }; + if !crate::testing::in_own_process("menus::tests::closing_a_panel_flyout_unchecks_its_window_row") { + return; + } + let (mut app, ctx) = settled_app(); let row = app .menus .bar @@ -279,7 +285,10 @@ fn closing_a_panel_flyout_unchecks_its_window_row() { #[test] fn a_view_turned_on_the_canvas_moves_the_rotate_view_check() { use craft::ui::runtime::commands::CommandState; - let Some((mut app, ctx)) = settled_app("menus::tests::a_view_turned_on_the_canvas_moves_the_rotate_view_check") else { return }; + if !crate::testing::in_own_process("menus::tests::a_view_turned_on_the_canvas_moves_the_rotate_view_check") { + return; + } + let (mut app, ctx) = settled_app(); let rows: Vec = app.menus.bar.items().into_iter().filter(|it| it.command.id == "view.rotate").map(|it| it.command.clone()).collect(); let angle = |row: &CommandRef| row.params["angle"].as_f64().unwrap_or(0.0); let turned = rows.iter().find(|row| angle(row) != 0.0).expect("Rotate View lists an angle other than 0").clone(); @@ -297,7 +306,10 @@ fn a_view_turned_on_the_canvas_moves_the_rotate_view_check() { #[test] fn a_new_font_rebuilds_the_menus() { const FAMILY: &str = "Menurebuild Sans"; - let Some((mut app, ctx)) = settled_app("menus::tests::a_new_font_rebuilds_the_menus") else { return }; + if !crate::testing::in_own_process("menus::tests::a_new_font_rebuilds_the_menus") { + return; + } + let (mut app, ctx) = settled_app(); let lists = |app: &VectorcraftApp| app.menus.bar.items().into_iter().any(|it| it.command.id == "text.setStyle" && it.command.params["font"] == FAMILY); assert!(!lists(&app));