diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 747a5e0..3750c0b 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -378,6 +378,13 @@ impl SheetApp { } let r = self.session.run(id, params); self.after_engine(); + // The new chart is selected, as after inserting any object, so Delete removes it, not its data. + if id == "insert.chart" + && let Ok(r) = &r + && let Some(chart) = r.get("chart").and_then(Json::as_u64).and_then(|id| u32::try_from(id).ok()) + { + self.selected_chart = Some(chart); + } if id == "file.exportPdf" && let Ok(r) = &r { diff --git a/crates/ui-egui/tests/interaction.rs b/crates/ui-egui/tests/interaction.rs index fc9ee52..f5d7d3e 100644 --- a/crates/ui-egui/tests/interaction.rs +++ b/crates/ui-egui/tests/interaction.rs @@ -224,6 +224,25 @@ fn delete_key_removes_the_selected_chart() { assert_eq!(value(&h, "A1"), Value::Number(1.0)); } +#[test] +fn inserted_chart_is_selected_so_delete_removes_it() { + // #668, #256: after Insert › Chart the selection stayed on the source cells, so Delete + // cleared the data and left the chart behind. + let mut session = blank(); + session.execute("range.setValues", json!({"range": "A1:B2", "values": [[1, 2], [3, 4]]})).unwrap(); + session.execute("selection.set", json!({"range": "A1:B2"})).unwrap(); + let mut h = harness(session); + + let id = h.state_mut().run("insert.chart", json!({"type": "column"})).unwrap()["chart"].as_u64().unwrap() as u32; + h.run_steps(2); + assert_eq!(h.state().selected_chart, Some(id), "the new chart is selected"); + + key(&mut h, Key::Delete, Modifiers::NONE); + + assert!(h.state().session.active().unwrap().wb.active().unwrap().charts.is_empty()); + assert_eq!(value(&h, "A1"), Value::Number(1.0)); +} + #[test] fn renders_every_ribbon_tab_without_panicking() { let mut s = Session::new();