diff --git a/crates/ui-egui/src/panels/layers.rs b/crates/ui-egui/src/panels/layers.rs index 67d534c178..db2b8dd63b 100644 --- a/crates/ui-egui/src/panels/layers.rs +++ b/crates/ui-egui/src/panels/layers.rs @@ -50,7 +50,7 @@ //! document change, and only those scrolled into view are laid out, so a document of many thousand //! objects costs no more per frame than a small one. -use std::collections::HashSet; +use std::collections::{HashMap, HashSet}; use std::sync::Arc; use craft::layers::ui::filter::{FilterBy, LayerFilter}; @@ -179,8 +179,9 @@ pub struct LayersUi { pub focused: bool, /// The target circle clicked last, with its document's `uid`: where a Shift-click on another circle starts. pub target_anchor: Option<(u64, NodeId)>, - /// The selection square clicked last, with its document's `uid`: where a Shift-click on another square starts. - pub art_anchor: Option<(u64, NodeId)>, + /// The selection square clicked last in each open document, by the document's `uid`: where a Shift-click on another + /// square starts. + pub art_anchors: HashMap, /// The active document's rows with the [`LayerRows::key`] they were listed at. rows: Option<(u64, Tracked)>, } @@ -190,7 +191,7 @@ impl Default for LayersUi { fn default() -> Self { let mut filter = LayerFilter::default(); filter.by = FilterBy::Objects; - Self { filter, rename: None, focused: false, target_anchor: None, art_anchor: None, rows: None } + Self { filter, rename: None, focused: false, target_anchor: None, art_anchors: HashMap::new(), rows: None } } } @@ -250,14 +251,18 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { let anchor = app.ui.layers_ui.target_anchor.filter(|(doc, _)| *doc == uid).map(|(_, id)| id); let target = target_click.map(|(id, mods)| (id, rows::target_click(&tree, &filter, anchor, id, mods))); // A Shift-click on a selection square starts at the last square clicked in this document. - let art_anchor = app.ui.layers_ui.art_anchor.filter(|(doc, _)| *doc == uid).map(|(_, id)| id); + let art_anchor = app.ui.layers_ui.art_anchors.get(&uid).copied(); let art = art_click.map(|(id, mods)| (id, rows::art_click(&tree, &filter, art_anchor, id, mods))); app.ui.layers_ui.filter = filter; if let Some((id, (_, true))) = &target { app.ui.layers_ui.target_anchor = Some((uid, *id)); } if let Some((id, (_, true))) = &art { - app.ui.layers_ui.art_anchor = Some((uid, *id)); + // Closed documents' starts are dropped, so the map holds one entry per open document at most. + let open: HashSet = app.session.documents().iter().map(|st| st.uid).collect(); + let anchors = &mut app.ui.layers_ui.art_anchors; + anchors.retain(|doc, _| open.contains(doc)); + anchors.insert(uid, *id); } app.ui.layers_ui.focused = output.focused; rows::apply(app, output.events); diff --git a/crates/ui-egui/src/panels/layers/tests.rs b/crates/ui-egui/src/panels/layers/tests.rs index 32c8add30f..4aa665fd48 100644 --- a/crates/ui-egui/src/panels/layers/tests.rs +++ b/crates/ui-egui/src/panels/layers/tests.rs @@ -621,6 +621,38 @@ fn shift_clicking_the_first_selection_square_adds_or_removes_one_row() { assert!(sel.contains(&a) && sel.contains(&c) && !sel.contains(&g), "the range from A to C: {sel:?}"); } +/// Each document keeps the selection square clicked last in it: after a click in a second +/// document, a Shift-click back in the first selects the range from the first document's square. +#[test] +fn each_document_keeps_its_selection_square_anchor() { + use crate::tests::panel_harness::Panel; + use egui::Modifiers; + let (mut app, _, [a, b]) = two_rects(); + let run = |app: &mut VectorcraftApp, id: &str, p: serde_json::Value| app.session.execute(id, &p).unwrap(); + let c = NodeId(run(&mut app, "shape.rectangle", json!({"x": 120, "y": 0, "width": 50, "height": 50}))["id"].as_u64().unwrap()); + for (id, name) in [(a, "A"), (b, "B"), (c, "C")] { + name_row(&mut app, id, name); + } + run(&mut app, "select.none", json!({})); + let mut panel = Panel::new(app, show); + panel.click_with("Select art of A", Modifiers::NONE); + { + let mut app = panel.app(); + run(&mut app, "file.new", json!({"width": 200, "height": 200})); + let other = NodeId(run(&mut app, "shape.rectangle", json!({"x": 0, "y": 0, "width": 50, "height": 50}))["id"].as_u64().unwrap()); + name_row(&mut app, other, "D"); + run(&mut app, "select.none", json!({})); + } + panel.click_with("Select art of D", Modifiers::NONE); + { + let mut app = panel.app(); + run(&mut app, "document.activate", json!({"index": 0})); + run(&mut app, "select.none", json!({})); + } + panel.click_with("Select art of C", Modifiers::SHIFT); + assert_eq!(targeted(&panel.app()), (None, vec![a, b, c]), "the range from A, the first document's square, to C"); +} + /// The rows among `rows` whose selection square is full-sized, then those whose square is the /// small one, in the order given. fn squares(app: &VectorcraftApp, rows: &[NodeId]) -> (Vec, Vec) {