diff --git a/crates/ui-egui/src/panels/links.rs b/crates/ui-egui/src/panels/links.rs index 72c90a8efc..87b574f820 100644 --- a/crates/ui-egui/src/panels/links.rs +++ b/crates/ui-egui/src/panels/links.rs @@ -96,7 +96,8 @@ pub(crate) struct LinksModel { pub(super) show: Option<(u64, u64)>, nested: NestedPicks, selection: Selection, - menu: Option, + /// The document uid and row ID whose context menu is open. + menu: Option<(u64, u64)>, } /// `links.list` and the first selected image's `links.info`, with what they were fetched for. @@ -710,13 +711,16 @@ fn act(app: &mut VectorcraftApp, act: Act, target: Option) { /// The Links panel: the images as a list with a footer of link actions and the Link Info section. pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { drop_stale_picks(app); + let uid = app.session.active().map(|st| st.uid); + if app.ui.links.menu.is_some_and(|(document, _)| Some(document) != uid) { + app.ui.links.menu = None; + } let Some(mut cache) = cached(app, ui.ctx()) else { Panel::new(tl!("Links")).show(ui, |ui, _| { Text::new(tl!("No document")).shade(Shade::Muted).show(ui); }); return; }; - let uid = app.session.active().map(|st| st.uid); let reveal = app.ui.links.show.take().filter(|(doc, _)| Some(*doc) == uid).map(|(_, id)| id); let listed = |cache: &Cache, id: u64| cache.list["links"].as_array().is_some_and(|rows| rows.iter().any(|r| r["id"].as_u64() == Some(id))); // A Show filter that leaves the requested row out goes back to Show All. @@ -730,7 +734,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { let selected = selected_links(app); let mut selection = std::mem::take(&mut app.ui.links.selection); selection.items = selected.clone(); - let open_menu = app.ui.links.menu; + let open_menu = app.ui.links.menu.map(|(_, row)| row); let units = app.session.general_unit(); let view: &VectorcraftApp = app; @@ -808,8 +812,8 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { let items = app.ui.links.selection.items.clone(); apply_selection(app, rows_of(&cache), items); } - if menu_for.is_some() { - app.ui.links.menu = menu_for; + if let Some(row) = menu_for { + app.ui.links.menu = uid.map(|document| (document, row)); } if let Some(id) = activated { go_to(app, id); @@ -828,7 +832,9 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { } if let Some((a, id)) = row_act { app.ui.links.menu = None; - act(app, a, Some(id)); + if app.session.active().map(|st| st.uid) == uid { + act(app, a, Some(id)); + } } } diff --git a/crates/ui-egui/src/panels/tests_links.rs b/crates/ui-egui/src/panels/tests_links.rs index 7c07781d02..6f9a6c5046 100644 --- a/crates/ui-egui/src/panels/tests_links.rs +++ b/crates/ui-egui/src/panels/tests_links.rs @@ -663,6 +663,41 @@ fn unembed_writes_several_images_to_one_picked_folder() { assert_eq!(app.ui.status, format!("Unembedded 2 images to {picked}")); } +#[test] +fn switching_documents_dismisses_a_links_row_menu() { + let dir = Folder::new("context-switched-document"); + let (mut app, first, _) = app(&dir); + let source = app.session.active().unwrap().uid; + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + let path = dir.file("other.png", &png(40, 20)); + let second = app.run("file.place", json!({"path": path, "at": [100, 100]})).unwrap()["ids"][0].as_u64().unwrap(); + let destination = app.session.active().unwrap().uid; + assert_eq!(first, second, "the two documents have different images at the same local ID"); + app.run("window.previousDocument", json!({})).unwrap(); + + let mut panel = Panel::new(app, super::links::show); + let activate = |panel: &mut Panel, index, document| { + panel.app().session.execute("document.activate", &json!({"index": index})).unwrap(); + panel.frame(); + panel.frame(); + assert_eq!(panel.app().session.active().unwrap().uid, document); + }; + panel.secondary_click("linked.png"); + assert!(panel.menu_item_enabled("Embed Image")); + activate(&mut panel, 1, destination); + assert!(!panel.shows("Embed Image"), "the other document must not inherit the row menu"); + assert_eq!(panel.app().run("links.info", json!({"id": second})).unwrap()["linked"], true); + + activate(&mut panel, 0, source); + assert!(!panel.shows("Embed Image"), "switching back must not revive the menu"); + assert_eq!(panel.app().run("links.info", json!({"id": first})).unwrap()["linked"], true); + + activate(&mut panel, 1, destination); + panel.secondary_click("other.png"); + panel.click_menu_item("Embed Image"); + assert_eq!(panel.app().run("links.info", json!({"id": second})).unwrap()["status"], "embedded"); +} + #[test] fn right_click_context_menu_in_links_panel() { let dir = Folder::new("context-menu");