From 418ad5dcff7ee3ff616ef700b8475f6f37d125e1 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 4 Dec 2026 17:50:56 -0800 Subject: [PATCH] Tests: native Undo and Redo from a floating panel or toolbar window A native menu's Cmd+Z and Shift+Cmd+Z, made while the OS window of a floating panel or a floating toolbar has the keys and none of its text fields is focused, undo and redo once each. --- .../ui-egui/src/panel_docking/native_tests.rs | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/crates/ui-egui/src/panel_docking/native_tests.rs b/crates/ui-egui/src/panel_docking/native_tests.rs index 2c03c5b989..ca5fc4d271 100644 --- a/crates/ui-egui/src/panel_docking/native_tests.rs +++ b/crates/ui-egui/src/panel_docking/native_tests.rs @@ -329,6 +329,61 @@ fn keys_typed_in_a_native_window_run_the_main_windows_shortcuts() { assert!(h.app.session.active().unwrap().selection.objects.is_empty()); } +/// A stand-in for the platform menu bar: the key equivalents it reports. +#[derive(Clone, Default)] +struct FakeMenu(Rc>>); + +impl craft::menu::Backend for FakeMenu { + fn sync(&mut self, _bar: &craft::menu::MenuBar) {} + fn drain(&mut self) -> Vec { + std::mem::take(&mut self.0.borrow_mut()) + } + fn attach(&mut self, _dispatch: craft::menu::Dispatch) {} +} + +/// The native menu's Undo and Redo key equivalents, made while a floating panel's or a floating toolbar's OS window +/// has the keys and none of its text fields is focused, change the document once each. +#[test] +fn native_undo_and_redo_from_a_floating_window_change_the_document_once() { + for what in ["panel", "toolbar"] { + let mut h = Harness::new(); + h.ctx.set_os(egui::os::OperatingSystem::Mac); + let menu = FakeMenu::default(); + h.app.services.native_menu = Some(craft::menu::NativeMenu::new(Box::new(menu.clone()))); + let viewport = if what == "panel" { + h.float("swatches") + } else { + h.app.run("window.toolbar.new", json!({"name": "Mine"})).unwrap(); + h.app.run("window.toolbar.float", json!({"name": "Mine", "x": 240, "y": 160})).unwrap(); + h.frames(3); + let panel = crate::custom_toolbars::panel_id(crate::custom_toolbars::find(&h.app.ui, "Mine").unwrap().id); + let viewport = h.viewport(&panel); + h.host.borrow_mut().focus = viewport; + viewport + }; + let [first, second] = [10, 80].map(|x| { + let id = h.app.run("shape.rectangle", json!({"x": x, "y": 10, "width": 50, "height": 40})).unwrap()["id"].as_u64().unwrap(); + vectorcraft_doc::NodeId(id) + }); + h.frames(2); + let host = craft::ui::runtime::windows::WindowHost::of(&h.ctx); + assert!(host.key_window() == viewport && !host.typing(), "{what}: the floating window has the keys, with no field typing"); + let present = |h: &Harness| { + let doc = &h.app.session.active().unwrap().doc; + [first, second].map(|id| doc.node(id).is_some()) + }; + for (chord, shift, after) in [("Cmd+Z", false, [true, false]), ("Cmd+Shift+Z", true, [true, true])] { + menu.0.borrow_mut().push(craft::menu::Event::Key(craft::menu::Chord::parse(chord).unwrap())); + h.frame(vec![]); + // The key-up reaches the window that has the keys. + let modifiers = egui::Modifiers { mac_cmd: true, command: true, shift, ..Default::default() }; + frame_in(&mut h, viewport, vec![Event::Key { key: Key::Z, physical_key: Some(Key::Z), pressed: false, repeat: false, modifiers }]); + h.frames(3); + assert_eq!(present(&h), after, "{what}: {chord} changes the document once"); + } + } +} + /// Enter that commits a field of a floating panel's window, and Escape that cancels one, are the /// field's: with an object selected and the Selection tool, Enter didn't also open the Move dialog /// (#4803).