diff --git a/crates/engine/src/cmd/prefscmds.rs b/crates/engine/src/cmd/prefscmds.rs index f66322498e..49b6680bde 100644 --- a/crates/engine/src/cmd/prefscmds.rs +++ b/crates/engine/src/cmd/prefscmds.rs @@ -752,6 +752,13 @@ impl Session { t.set_option("cornerRadius", &r); } } + // A press of the tool that is dropped keeps the new radius, also when the tool lent a + // selection tool for the press. + if self.press_taken_by("roundedRectangle") + && let Some((_, saved)) = self.press_options.as_mut() + { + saved.insert("cornerRadius".into(), r); + } } if fonts_folder_changed { let folder = self.prefs.fonts_folder.trim(); diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 1e9ef6d699..f35a64c5ee 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -329,6 +329,9 @@ pub struct Session { /// The id and persistent options of the tool when the press under way began, which a dropped /// press puts back ([`Session::drop_tool_press`]). pub(crate) press_options: Option<(String, serde_json::Map)>, + /// An option of the active tool was set during the press under way, which stored all of its + /// options, so a dropped press stores the options it puts back. + pub(crate) press_options_stored: bool, /// The engine saves the selection and active layer before a Type press and restores them if it drops. pub(crate) press_type_selection: Option<(Selection, Option)>, /// The freeform gradient point selected once the tool took the press under way, which a diff --git a/crates/engine/src/session/documents.rs b/crates/engine/src/session/documents.rs index c93239442d..cb85e3e47c 100644 --- a/crates/engine/src/session/documents.rs +++ b/crates/engine/src/session/documents.rs @@ -137,6 +137,7 @@ impl Session { plane_widget_press: false, pointer_held: false, press_options: None, + press_options_stored: false, press_type_selection: None, press_point: None, ruler_guide: None, diff --git a/crates/engine/src/tests/toolsettings.rs b/crates/engine/src/tests/toolsettings.rs index fbde9edca7..69d382f2dc 100644 --- a/crates/engine/src/tests/toolsettings.rs +++ b/crates/engine/src/tests/toolsettings.rs @@ -138,6 +138,86 @@ fn a_dropped_press_does_not_keep_what_its_drag_changed() { assert_eq!(s.prefs.tool_settings["polygon"]["sides"], json!(7), "and stores its sides"); } +/// An option set with `tool.setOption` while a press is held survives the dropped press, in the +/// tool and in the stored options, while what the drag itself changed is put back in both. +#[test] +fn an_option_set_during_a_dropped_press_is_kept() { + let mut s = session(); + select(&mut s, "rectangularGrid"); + let v = ViewInfo { smart_guides: false, ..Default::default() }; + s.pointer(&PointerEvent::new(PointerKind::Down, 100.0, 100.0), v).unwrap(); + s.pointer(&PointerEvent::new(PointerKind::Drag, 200.0, 180.0), v).unwrap(); + s.tool_key(ToolKey::Up, Mods::default(), v).unwrap(); + s.tool_key(ToolKey::Right, Mods::default(), v).unwrap(); + assert_eq!((s.tool_options()["rows"].clone(), s.tool_options()["columns"].clone()), (json!(6), json!(6))); + s.set_tool_option_cmd(&json!({"key": "columns", "value": 9})).unwrap(); + s.drop_tool_press(v).unwrap(); + let o = s.tool_options(); + assert_eq!((o["rows"].clone(), o["columns"].clone()), (json!(5), json!(9)), "the drag's rows go back; the set columns stay"); + let saved = &s.prefs.tool_settings["rectangularGrid"]; + assert_eq!((saved["rows"].clone(), saved["columns"].clone()), (json!(5), json!(9)), "the drag's rows are not stored; the set columns are"); + select(&mut s, "selection"); + select(&mut s, "rectangularGrid"); + let o = s.tool_options(); + assert_eq!((o["rows"].clone(), o["columns"].clone()), (json!(5), json!(9)), "a tool switch keeps the set columns"); + // The same through the desktop app's setter. + s.pointer(&PointerEvent::new(PointerKind::Down, 100.0, 100.0), v).unwrap(); + s.pointer(&PointerEvent::new(PointerKind::Drag, 200.0, 180.0), v).unwrap(); + s.tool_key(ToolKey::Up, Mods::default(), v).unwrap(); + s.set_tool_option("columns", &json!(3)); + s.drop_tool_press(v).unwrap(); + let o = s.tool_options(); + assert_eq!((o["rows"].clone(), o["columns"].clone()), (json!(5), json!(3))); + let saved = &s.prefs.tool_settings["rectangularGrid"]; + assert_eq!((saved["rows"].clone(), saved["columns"].clone()), (json!(5), json!(3))); +} + +/// A Corner Radius preference set while a Rounded Rectangle press is held survives the dropped +/// press, in the tool and in the preference. +#[test] +fn a_corner_radius_set_during_a_dropped_press_is_kept() { + let mut s = session(); + select(&mut s, "roundedRectangle"); + let v = ViewInfo { smart_guides: false, ..Default::default() }; + s.pointer(&PointerEvent::new(PointerKind::Down, 100.0, 100.0), v).unwrap(); + s.pointer(&PointerEvent::new(PointerKind::Drag, 200.0, 180.0), v).unwrap(); + let mut p = s.prefs.clone(); + p.corner_radius = 30.0; + s.apply_prefs(p); + s.drop_tool_press(v).unwrap(); + assert_eq!(s.tool_options()["cornerRadius"].as_f64(), Some(30.0), "the tool keeps the new radius"); + assert_eq!(s.prefs.corner_radius, 30.0, "the preference keeps it"); +} + +/// An option set with `tool.setOption` on a tool that lent a selection tool for a Cmd-drag +/// survives the dropped press, in the tool and in the stored options. So does a Corner Radius +/// preference set while the Rounded Rectangle lends one. +#[test] +fn an_option_set_on_a_lending_tool_during_a_dropped_press_is_kept() { + let mut s = session(); + select(&mut s, "rectangularGrid"); + let v = ViewInfo { smart_guides: false, ..Default::default() }; + let cmd = Mods { cmd: true, ..Default::default() }; + s.pointer(&PointerEvent::new(PointerKind::Down, 100.0, 100.0).with_mods(cmd), v).unwrap(); + s.pointer(&PointerEvent::new(PointerKind::Drag, 200.0, 180.0).with_mods(cmd), v).unwrap(); + assert_eq!(s.lender.as_ref().map(|t| t.id()), Some("rectangularGrid"), "the press lent a selection tool"); + s.set_tool_option_cmd(&json!({"tool": "rectangularGrid", "key": "columns", "value": 9})).unwrap(); + s.drop_tool_press(v).unwrap(); + assert_eq!(s.tool_id(), "rectangularGrid", "the lending tool is back"); + assert_eq!(s.tool_options()["columns"], json!(9), "the tool keeps the set columns"); + assert_eq!(s.prefs.tool_settings["rectangularGrid"]["columns"], json!(9), "and stores them"); + select(&mut s, "roundedRectangle"); + s.pointer(&PointerEvent::new(PointerKind::Down, 100.0, 100.0).with_mods(cmd), v).unwrap(); + s.pointer(&PointerEvent::new(PointerKind::Drag, 200.0, 180.0).with_mods(cmd), v).unwrap(); + assert_eq!(s.lender.as_ref().map(|t| t.id()), Some("roundedRectangle"), "the press lent a selection tool"); + let mut p = s.prefs.clone(); + p.corner_radius = 30.0; + s.apply_prefs(p); + s.drop_tool_press(v).unwrap(); + assert_eq!(s.tool_options()["cornerRadius"].as_f64(), Some(30.0), "the tool keeps the new radius"); + assert_eq!(s.prefs.corner_radius, 30.0, "the preference keeps it"); +} + /// A dropped Pen press drops only the anchor it was placing: the path being drawn goes on with /// the next click. #[test] diff --git a/crates/engine/src/tooling.rs b/crates/engine/src/tooling.rs index 7d86dcc67f..9923a3a7a2 100644 --- a/crates/engine/src/tooling.rs +++ b/crates/engine/src/tooling.rs @@ -615,13 +615,18 @@ impl Session { /// Escape, as during a drag, for most tools), and a release returns the tool to rest. Of what /// the tool asks for along the way, only a cancel and a notify run, so a marquee selects /// nothing and a click opens no size dialog. The tool gets back the persistent options it had - /// when the press began, and none of the press's changes to them are stored. A dropped Type - /// press restores the prior selection. The freeform gradient point selected once the tool - /// took the press is selected again. A press held while the tool computes an edit on another - /// thread has not reached the tool: it is removed from the held input with the pointer events - /// and tool keys held after it, and the tool is left as it is. A perspective plane key held - /// after it stays held, since the plane it picks outlasts a dropped press. Pointer events held - /// after the last held release belong to the press the tool has, and are removed with it. + /// when the press began, and none of the press's changes to them are stored. An option set + /// during the press with [`Self::set_tool_option`] or [`Self::set_tool_options`], or a General › + /// Corner Radius change during a Rounded Rectangle press, keeps its new value, also when the + /// tool lent a selection tool for the press (a Cmd-drag). Setting an option stores all of the + /// tool's options, the press's changes included, so the dropped press then stores the options + /// it puts back. A dropped Type press restores the prior selection. The freeform gradient + /// point selected once the tool took the press is selected again. A press held while the tool + /// computes an edit on another thread has not reached the tool: it is removed from the held + /// input with the pointer events and tool keys held after it, and the tool is left as it is. + /// A perspective plane key held after it stays held, since the plane it picks outlasts a + /// dropped press. Pointer events held after the last held release belong to the press the tool + /// has, and are removed with it. pub fn drop_tool_press(&mut self, view: ViewInfo) -> Result<()> { let (from, held) = self.held_press(); let keep: Vec = (self.held_input.iter().enumerate()) @@ -639,6 +644,7 @@ impl Session { // Nothing to roll back without a document; the tool still drops the press and gets the release. let _ = self.cancel_interaction(); let before = self.press_options.take(); + let stored = std::mem::take(&mut self.press_options_stored); let type_selection = self.press_type_selection.take(); let point = self.press_point.take(); let mut acts = self.with_tool_cx(view, |t, cx| t.drop_press(cx)); @@ -673,6 +679,11 @@ impl Session { self.tool.set_option(k, v); } } + // An option set during the press stored every option the tool had then, the press's + // changes included: those stored values are put back too. + if stored { + self.keep_tool_settings(); + } } r.map(drop) } @@ -710,6 +721,7 @@ impl Session { match ev.kind { PointerKind::Down => { self.press_options = Some((self.tool.id().to_string(), Self::persistent_options(self.tool.as_ref()))); + self.press_options_stored = false; self.press_type_selection = if matches!(self.tool.id(), "type" | "areaType" | "typeOnPath" | "verticalType" | "verticalAreaType" | "verticalTypeOnPath") { self.active().map(|st| (st.selection.clone(), st.active_layer)) @@ -719,6 +731,7 @@ impl Session { } PointerKind::Up => { self.press_options = None; + self.press_options_stored = false; self.press_type_selection = None; } _ => {} @@ -1313,9 +1326,12 @@ impl Session { if key == "queue" && self.tool.id() == "place" { self.note_place_queue_roots(); } + let before_press = self.options_before_press_set(); + let sets_pressed = self.press_taken_by(self.tool.id()); self.tool.set_option(key, v); self.tool_set_since_reset = true; self.keep_tool_settings(); + self.keep_press_set(before_press, |k| sets_pressed && k == key, sets_pressed); } /// Keep the automation roots in force now as those the place cursor's queue, being loaded, @@ -1360,6 +1376,8 @@ impl Session { self.note_place_queue_roots(); } let active = id.is_none_or(|id| id == self.tool.id()); + let before_press = self.options_before_press_set(); + let sets_pressed = self.press_taken_by(id.unwrap_or(self.tool.id())); // The tool that lent a selection tool for a Cmd-drag is the tool chosen, and active again // when the drag ends. let lender = id.is_some_and(|id| self.lender.as_deref().is_some_and(|l| l.id() == id)); @@ -1397,9 +1415,46 @@ impl Session { let set = other.id(); Self::restore_stores(&self.prefs, self.tool.as_mut(), |store| settings::stores(set).any(|(s, _)| s == store)); } + self.keep_press_set(before_press, |k| sets_pressed && values.contains_key(k), sets_pressed); out } + /// The tool that took the press under way: the active tool, or the tool that lent a selection + /// tool for it (a Cmd-drag), which the dropped press gives its options back to. + fn pressed_tool(&self) -> Option<&dyn Tool> { + let (pressed, _) = self.press_options.as_ref()?; + std::iter::once(&self.tool).chain(self.lender.as_ref()).map(|t| t.as_ref()).find(|t| t.id() == pressed) + } + + /// Tool `id` took the press under way ([`Self::pressed_tool`]). + pub(crate) fn press_taken_by(&self, id: &str) -> bool { + self.pressed_tool().is_some_and(|t| t.id() == id) + } + + /// The persistent options of the tool that took the press under way ([`Self::pressed_tool`]) + /// before an option is set: a dropped press puts back the options it began with, except those + /// the set changes ([`Self::keep_press_set`]). + fn options_before_press_set(&self) -> Option> { + self.pressed_tool().map(Self::persistent_options) + } + + /// After an option is set during a held press, the press's saved options take the options of + /// the tool that took the press that the set changed from `before` or named (`named`), so a + /// dropped press keeps them. `stored`: the set stored that tool's options, the press's changes + /// included, so a dropped press stores the options it puts back. + fn keep_press_set(&mut self, before: Option>, named: impl Fn(&str) -> bool, stored: bool) { + let Some(before) = before else { return }; + let Some(now) = self.pressed_tool().map(Self::persistent_options) else { return }; + self.press_options_stored |= stored; + if let Some((_, saved)) = self.press_options.as_mut() { + for (k, v) in now { + if before.get(&k) != Some(&v) || named(&k) { + saved.insert(k, v); + } + } + } + } + /// `tool.setOption {tool?, key?, value?, values?}` (a command of the desktop app and of the /// headless host): set one option (`key`, `value`) or several (`values`) of `tool` (default: /// the tool active when the call arrives, named before the edit in progress is finished) → its