diff --git a/crates/engine/src/session/edit.rs b/crates/engine/src/session/edit.rs index 8417be7815..48c7a8358e 100644 --- a/crates/engine/src/session/edit.rs +++ b/crates/engine/src/session/edit.rs @@ -266,12 +266,34 @@ impl Session { /// Give document `uid` back a selection it had, new art included, through the selection /// filters as they are now: guides locked since, hidden or locked slices and art on a locked - /// artboard that is not new art are left out. + /// artboard that is not new art are left out. In the active document, outside a command and an + /// interaction, the journal records the selection that comes back, with the current layer kept, + /// in the open undo groups too, and the paints for new art load from it as `select.set` loads + /// them, so a recorded action replays as it ran. pub fn restore_selection(&mut self, uid: u64, selection: Selection) -> Result<()> { + let active = self.active().is_some_and(|st| st.uid == uid && st.interaction.is_none()) && self.depth == 0; let st = self.document_mut(uid).ok_or(EngineError::NoDocument)?; + let (made, layer) = (st.selection.clone(), st.active_layer); st.selection_record = None; st.selection = selection; - self.select_in_document(uid, |_, _| {}) + self.select_in_document(uid, |_, _| {})?; + if !active { + return Ok(()); + } + let Some(st) = self.document_mut(uid) else { return Ok(()) }; + if st.selection == made { + return Ok(()); + } + let steps = selection_steps(&st.selection, &made, &st.doc, layer, &st.highlighted_rows()); + let (from, grouped) = (self.journal.len(), self.undo_group_docs()); + for step in steps { + self.journal.push(step); + } + self.journal_in_undo_groups(Some(uid), &grouped, from); + // As `Session::execute` does after a selection command. + self.inherit_new_art(); + self.load_selection_paints(); + Ok(()) } /// Apply the selection filters and update the editing state of document `uid`. diff --git a/crates/ui-egui/src/placing/tests.rs b/crates/ui-egui/src/placing/tests.rs index eef7b0e037..aaa4919874 100644 --- a/crates/ui-egui/src/placing/tests.rs +++ b/crates/ui-egui/src/placing/tests.rs @@ -2069,6 +2069,89 @@ fn swapped_out(app: &mut VectorcraftApp, other: NodeId) -> Reselect { Reselect { doc: st.uid, swapped: st.selection.clone(), selections: st.selections, art: st.doc.clone(), isolation: st.isolation, user } } +/// Restore `back`, then check that the journal entries the restore added select the same again +/// when they are run after the swapped object is selected. +fn reselect_and_replay(app: &mut VectorcraftApp, back: &Reselect, other: NodeId) { + let from = app.session.journal.len(); + reselect(app, back); + let restored = app.session.doc().unwrap().selection.clone(); + let steps: Vec<(String, serde_json::Value)> = app.session.journal.since(from).to_vec(); + assert!(!steps.is_empty(), "the journal records the restored selection"); + app.run("select.set", json!({"ids": [other.0]})).unwrap(); + for (id, p) in steps { + app.run(&id, p).unwrap(); + } + assert_eq!(app.session.doc().unwrap().selection, restored, "the journal replays the restored selection"); +} + +#[test] +fn a_restored_segment_selection_replays_as_segments() { + let mut app = app(false); + let (mine, other) = two_rectangles(&mut app); + app.run("select.segments", json!({"id": mine.0, "segments": [[0, 1]]})).unwrap(); + assert!(!app.session.doc().unwrap().selection.segments.is_empty()); + let back = swapped_out(&mut app, other); + reselect_and_replay(&mut app, &back, other); + assert!(app.session.doc().unwrap().selection.segments.contains_key(&mine), "the segment is selected again"); +} + +#[test] +fn a_restore_inside_a_canceled_undo_group_leaves_the_journal() { + let mut app = app(false); + let (_, other) = two_rectangles(&mut app); + let before = app.session.journal.len(); + app.run("edit.undoGroup.begin", json!({})).unwrap(); + let made = NodeId(app.run("shape.rectangle", json!({"x": 120, "y": 10, "width": 20, "height": 20})).unwrap()["id"].as_u64().unwrap()); + app.run("select.set", json!({"ids": [made.0]})).unwrap(); + let back = swapped_out(&mut app, other); + reselect(&mut app, &back); + assert_eq!(app.session.doc().unwrap().selection.objects, vec![made]); + let last = app.session.journal.last().cloned().unwrap(); + assert_eq!(last, ("select.set".to_string(), json!({"ids": [made.0]})), "the restore is journaled"); + app.run("edit.undoGroup.end", json!({"cancel": true})).unwrap(); + let left: Vec<&str> = app.session.journal.since(before).iter().map(|(id, _)| id.as_str()).collect(); + assert!(!left.iter().any(|id| *id == "select.set" || *id == "shape.rectangle"), "{left:?}"); +} + +#[test] +fn a_restored_selection_is_journaled_and_brings_its_paints_back() { + let mut app = app(false); + let (mine, other) = two_rectangles(&mut app); + app.run("paint.setFill", json!({"ids": [mine.0], "color": "#ff0000"})).unwrap(); + app.run("paint.setFill", json!({"ids": [other.0], "color": "#0000ff"})).unwrap(); + // Selecting the red rectangle loads its paints for new art. + app.run("select.none", json!({})).unwrap(); + app.run("select.set", json!({"ids": [mine.0]})).unwrap(); + let red = app.session.paint.clone(); + let back = swapped_out(&mut app, other); + assert_ne!(app.session.paint, red, "selecting the swapped object loads its paints"); + reselect(&mut app, &back); + assert_eq!(app.session.doc().unwrap().selection.objects, vec![mine]); + assert_eq!(app.session.paint, red, "the restored selection's paints are the paints for new art"); + let last = app.session.journal.last().cloned().unwrap(); + assert_eq!(last, ("select.set".to_string(), json!({"ids": [mine.0]})), "the journal records the restored selection"); + // Replayed, the journal leaves the same paints for new art. + app.run("select.set", json!({"ids": [other.0]})).unwrap(); + app.run(&last.0, last.1).unwrap(); + assert_eq!(app.session.paint, red, "the replay loads the same paints"); +} + +#[test] +fn a_restored_selection_gives_new_art_its_appearance_with_basic_appearance_off() { + let mut app = app(false); + app.session.prefs.new_art_basic = false; + let (mine, other) = two_rectangles(&mut app); + app.run("paint.setFill", json!({"ids": [mine.0], "color": "#ff0000"})).unwrap(); + app.run("paint.setFill", json!({"ids": [other.0], "color": "#0000ff"})).unwrap(); + app.run("select.none", json!({})).unwrap(); + app.run("select.set", json!({"ids": [mine.0]})).unwrap(); + let red = app.session.paint.clone(); + let back = swapped_out(&mut app, other); + assert_ne!(app.session.paint, red, "new art takes the swapped object's appearance"); + reselect(&mut app, &back); + assert_eq!(app.session.paint, red, "new art takes the restored object's appearance again"); +} + /// The objects selected before the swap, with two rectangles drawn: the user's and the swapped one. fn two_rectangles(app: &mut VectorcraftApp) -> (NodeId, NodeId) { let mut rect = |x| NodeId(app.run("shape.rectangle", json!({"x": x, "y": 10, "width": 20, "height": 20})).unwrap()["id"].as_u64().unwrap());