From 40f7f2e0a08a75d01acda014c304b2dfafba3f0e Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 08:12:50 -0800 Subject: [PATCH] Keep Graph Data imports with the sheet that asked --- crates/ui-egui/src/dialogs/graph_data.rs | 55 ++++++++++++++++++++---- 1 file changed, 47 insertions(+), 8 deletions(-) diff --git a/crates/ui-egui/src/dialogs/graph_data.rs b/crates/ui-egui/src/dialogs/graph_data.rs index 2ac4764bf5..572f92f664 100644 --- a/crates/ui-egui/src/dialogs/graph_data.rs +++ b/crates/ui-egui/src/dialogs/graph_data.rs @@ -18,6 +18,7 @@ //! empties it). use std::collections::BTreeMap; +use std::sync::Arc; use craft::behavior::{EditEvent, NumberCodec}; use craft::host::{PickRequest, Picked}; @@ -395,8 +396,12 @@ fn can_import(app: &VectorcraftApp) -> bool { file_asks::can(app, craft::host::PickKind::Open) && app.services.read.is_some() } -/// Import Data's answer: a tab-separated text file's rows into the sheet. -fn import(app: &mut VectorcraftApp, picked: Picked) -> Result { +/// Import Data's answer: a tab-separated text file's rows into the sheet that asked for it. +fn import(app: &mut VectorcraftApp, owner: &Arc<()>, picked: Picked) -> Result { + let Some(dialogs) = app.dialogs() else { return Ok(Value::Null) }; + if !dialogs.with(|host| host.get::().is_some_and(|d| Arc::ptr_eq(&d.import_owner, owner))) { + return Ok(Value::Null); + } let bytes = match picked { Picked::Paths(paths) => { let Some(path) = paths.first().map(|p| p.to_string_lossy().into_owned()) else { return Ok(Value::Null) }; @@ -420,15 +425,14 @@ fn import(app: &mut VectorcraftApp, picked: Picked) -> Result { return Err("the file is larger than 1 MB".into()); } let grid = grid_from_text(&decode(&bytes)); - if let Some(dialogs) = app.dialogs() { - dialogs.with(|host| host.get_mut::().map(|d| d.replace(grid))); - } + dialogs.with(|host| host.get_mut::().filter(|d| Arc::ptr_eq(&d.import_owner, owner)).map(|d| d.replace(grid))); Ok(Value::Null) } /// What the window asked of the app since the last [`pump`]. struct Asks { graph: Option, + import_owner: Arc<()>, apply: Option, actions: Vec, } @@ -444,7 +448,7 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) { d.take_asks() }) }); - let Some(Asks { graph, apply: filed, actions }) = asked else { return }; + let Some(Asks { graph, import_owner, apply: filed, actions }) = asked else { return }; if let Some(sheet) = filed { if let Err(e) = apply(app, &sheet, graph) { app.status(e); @@ -455,7 +459,8 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) { match action { Action::Import => { let request = PickRequest::open("document").filter("Text", &["txt", "tsv", "csv"]); - file_asks::ask(app, request, Owner::Document, import); + let owner = import_owner.clone(); + file_asks::ask(app, request, Owner::Document, move |app, picked| import(app, &owner, picked)); } Action::Revert => match graph_grid(app, graph) { Ok(grid) => { @@ -636,6 +641,8 @@ fn body(ui: &mut egui::Ui, values: &mut Sheet, pane: &mut Pane<'_>) -> bool { /// sheet for [`pump`], which sends it to the graph and keeps the window open. pub(crate) struct GraphDataDialog { id: DialogId, + /// Identifies this opening even when another Graph Data dialog reuses `id`. + import_owner: Arc<()>, sheet: Sheet, state: GridState, entry: Entry, @@ -658,6 +665,7 @@ impl GraphDataDialog { load(&sheet, &state, &mut entry); GraphDataDialog { id: dialog_id(KIND), + import_owner: Arc::new(()), sheet, state, entry, @@ -672,7 +680,7 @@ impl GraphDataDialog { /// Everything the window asked since the last call, once. fn take_asks(&mut self) -> Asks { - Asks { graph: self.graph, apply: self.held.take(), actions: std::mem::take(&mut self.actions) } + Asks { graph: self.graph, import_owner: self.import_owner.clone(), apply: self.held.take(), actions: std::mem::take(&mut self.actions) } } /// The pump sent Apply's sheet to the graph; the window stays open and Apply is ready again. @@ -959,6 +967,37 @@ mod tests { assert_eq!(at(&h, 0, 1), "c"); } + #[test] + fn graph_data_import_keeps_late_answers_out_of_a_reopened_sheet() { + let (mut app, _) = app_with(",first\nQ1,1", "column"); + let files = Arc::new(ScriptedFiles::held()); + app.services.read = Some(Box::new(|_| Err("this picker answers with bytes".into()))); + app.host.with_files(files.clone()).install(app.ui_ctx.as_ref().unwrap()); + app.host = Arc::new(app.host.with_files(files.clone())); + open(&mut app).unwrap(); + let mut h = harness(app); + h.run_steps(4); + h.get_by_label("Import Data…").click(); + h.run_steps(3); + assert_eq!(files.open(), 1); + + let app = h.state_mut(); + let ctx = app.ui_ctx.as_ref().unwrap().clone(); + app.dialogs().unwrap().with(|host| host.cancel::(&ctx)); + app.run("graph.create", json!({"type": "column", "x": 20, "y": 20, "width": 100, "height": 100, "csv": ",second\nQ2,2"})).unwrap(); + open(app).unwrap(); + let series = |h: &Harness| read(h.state(), |d| cell_of(&d.sheet.grid, 0, 1)); + assert!(files.answer(Picked::Contents(vec![("first.txt".into(), b"\tstale\nQ1\t7".to_vec())])).is_some()); + h.run_steps(4); + assert_eq!(series(&h), "second"); + + h.get_by_label("Import Data…").click(); + h.run_steps(3); + assert!(files.answer(Picked::Contents(vec![("second.txt".into(), b"\timported\nQ2\t8".to_vec())])).is_some()); + h.run_steps(4); + assert_eq!(series(&h), "imported"); + } + #[test] fn transpose_and_switch_xy() { assert_eq!(transpose(&g(&[&["", "a", "b"], &["Q1", "1", "2"]])), g(&[&["", "Q1"], &["a", "1"], &["b", "2"]]));