diff --git a/crates/ui-egui/src/panels/text_styles.rs b/crates/ui-egui/src/panels/text_styles.rs index dbd7447b34..bfcffa6e65 100644 --- a/crates/ui-egui/src/panels/text_styles.rs +++ b/crates/ui-egui/src/panels/text_styles.rs @@ -66,6 +66,8 @@ impl StylesPanels { pub(crate) struct StylesPanel { /// The uid of the document the highlight was made in. document: Option, + /// Changes when the panel switches documents, so reopening a document cannot restore its old popup. + scope_generation: u64, /// The highlighted styles, in the order they were picked. highlight: Vec, /// The selection's lead style. @@ -83,7 +85,7 @@ fn highlighted(app: &mut VectorcraftApp, kind: Kind, listed: &[&str]) -> Vec = listed.iter().enumerate().map(|(i, name)| TextStyleRow::new(i as u64, super::label_or_name(name, i == 0))).collect(); @@ -271,7 +274,8 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui, kind: Kind) { if let Some(id) = reveal.as_deref().and_then(id_of) { list = list.reveal(id); } - let out = list.show(ui); + // The framework's row menu and footer drag use list indices that are local to this document. + let out = ui.push_id(("text-styles-document", kind.panel_kind(), scope), |ui| list.show(ui)).inner; let name_of = |id: &u64| listed.get(*id as usize).map(|n| n.to_string()); let panel = app.ui.text_styles.of(kind); panel.highlight = sel.items.iter().filter_map(name_of).collect(); diff --git a/crates/ui-egui/src/panels/text_styles/tests.rs b/crates/ui-egui/src/panels/text_styles/tests.rs index 3e80710437..5144f8ce9a 100644 --- a/crates/ui-egui/src/panels/text_styles/tests.rs +++ b/crates/ui-egui/src/panels/text_styles/tests.rs @@ -4,6 +4,54 @@ use crate::tests::picks::{answer_dialog, cancel_dialog, open_dialogs, poll, scri use crate::tests::synthetic::key_frame; use vectorcraft_doc::{NodeId, NodeKind}; +#[test] +fn switching_documents_dismisses_text_styles_row_menus() { + use crate::tests::panel_harness::Panel; + use vectorcraft_engine::Session; + + for kind in [Kind::Char, Kind::Para] { + let mut app = crate::testing::windowed(VectorcraftApp::new(Session::new(), Default::default())); + let command = format!("{}.new", kind.prefix()); + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + app.run(&command, json!({"name": "First Style"})).unwrap(); + let first = style_id(&mut app, kind, "First Style"); + let source = app.session.active().unwrap().uid; + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + app.run(&command, json!({"name": "Second Style"})).unwrap(); + let second = style_id(&mut app, kind, "Second Style"); + let destination = app.session.active().unwrap().uid; + assert_eq!(first, second, "the two documents carry styles at the same list index"); + app.run("window.previousDocument", json!({})).unwrap(); + + let draw: fn(&mut VectorcraftApp, &mut Ui) = match kind { + Kind::Char => |app, ui| show(app, ui, Kind::Char), + Kind::Para => |app, ui| show(app, ui, Kind::Para), + }; + let mut panel = Panel::new(app, draw); + 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); + }; + let has_copy = |panel: &Panel, name: &str| styles(&mut panel.app(), kind).iter().any(|style| style.0 == name); + panel.secondary_click("First Style"); + assert!(panel.menu_item_enabled("Duplicate Style")); + activate(&mut panel, 1, destination); + assert!(!panel.shows("Duplicate Style"), "the new document does not inherit the row menu"); + assert!(!has_copy(&panel, "Second Style copy")); + + activate(&mut panel, 0, source); + assert!(!panel.shows("Duplicate Style"), "the old row menu does not reappear"); + assert!(!has_copy(&panel, "First Style copy")); + + activate(&mut panel, 1, destination); + panel.secondary_click("Second Style"); + panel.click_menu_item("Duplicate Style"); + assert!(has_copy(&panel, "Second Style copy")); + } +} + // PLAN: assumed type-w19 items reachable through `super::*`: `run_action(app, kind, action)`, // `StylesPanel { highlight: Vec, .. }` and `StylesPanels { char, para, made }` at // `app.ui.text_styles`, `highlighted(app, kind, listed: &[&str]) -> Vec`, `TextStyleAction`, and a