From c94629e98d8a97ea6d1f4e388416f871a6484d2b Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 16:07:16 -0800 Subject: [PATCH] Dismiss Simplify and Smooth sliders when leaving their drawing --- crates/ui-egui/src/dialogs/simplify_slider.rs | 40 ++++++++++++++++--- crates/ui-egui/src/dialogs/tests_modal.rs | 28 +++++++++++++ 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/dialogs/simplify_slider.rs b/crates/ui-egui/src/dialogs/simplify_slider.rs index e0040bb228..23ed5cfa85 100644 --- a/crates/ui-egui/src/dialogs/simplify_slider.rs +++ b/crates/ui-egui/src/dialogs/simplify_slider.rs @@ -85,6 +85,8 @@ impl StripKind { pub(crate) struct Strip { /// Which command the strip previews. pub kind: StripKind, + /// The drawing whose selection and preview the strip uses. + document: Option, /// The slider's value, 0 to 100. pub level: f64, /// The selection's document bounds when the strip opened, so the strip stays put while the @@ -99,6 +101,18 @@ pub(crate) struct Strip { } impl Strip { + fn in_source(&self, app: &VectorcraftApp) -> bool { + self.document == app.session.active().map(|doc| doc.uid) + } + + fn cancel(self, app: &mut VectorcraftApp) { + if self.in_source(app) + && app.session.active().and_then(|doc| doc.interaction.as_ref()?.preview.as_ref()).is_some_and(|(cmd, _)| cmd == self.kind.cmd()) + { + self.preview.cancel(app); + } + } + /// The slider's level, a whole number from 0 to 100. fn level(&self) -> f64 { if self.level.is_finite() { self.level.round().clamp(0.0, 100.0) } else { self.kind.default_level() } @@ -124,10 +138,17 @@ pub(crate) fn opening_level(app: &VectorcraftApp, kind: StripKind) -> f64 { /// Opens the strip of `kind`, replacing (and cancelling the preview of) one that is open. pub(crate) fn open(app: &mut VectorcraftApp, kind: StripKind) { if let Some(old) = app.object_dialogs.strip.take() { - old.preview.cancel(app); + old.cancel(app); } - let strip = - Strip { kind, level: opening_level(app, kind), bounds: app.selection_bounds(), open: true, auto: None, preview: LivePreview::default() }; + let strip = Strip { + kind, + document: app.session.active().map(|doc| doc.uid), + level: opening_level(app, kind), + bounds: app.selection_bounds(), + open: true, + auto: None, + preview: LivePreview::default(), + }; app.object_dialogs.strip = Some(strip); } @@ -158,6 +179,9 @@ pub(super) fn auto_of(app: &mut VectorcraftApp, kind: StripKind) -> Result Result { return Err(not_open(StripKind::Simplify)); } let Some(strip) = app.object_dialogs.strip.take() else { return Err(not_open(StripKind::Simplify)) }; + if !strip.in_source(app) { + return Err(not_open(StripKind::Simplify)); + } let tolerance = simplify_level_tolerance(strip.level()); - strip.preview.cancel(app); + strip.cancel(app); // PLAN: assumed `path_ops::open_simplify(app: &mut VectorcraftApp, tolerance: f64)` returning `()`. super::path_ops::open_simplify(app, tolerance); Ok(json!({ "tolerance": tolerance })) @@ -202,6 +229,9 @@ fn anchor_rect(view: &CanvasView, bounds: Option) -> ScreenRect { /// the canvas body. pub(crate) fn show(app: &mut VectorcraftApp, ui: &mut egui::Ui, view: &CanvasView) { let Some(mut strip) = app.object_dialogs.strip.take() else { return }; + if !strip.in_source(app) { + return; + } let kind = strip.kind; let anchor = anchor_rect(view, strip.bounds); let (mut level, mut open) = (strip.level, strip.open); @@ -226,7 +256,7 @@ pub(crate) fn show(app: &mut VectorcraftApp, ui: &mut egui::Ui, view: &CanvasVie strip.open = open; let result = match shown.output.closed { Some(CloseReason::Escape) => { - strip.preview.cancel(app); + strip.cancel(app); return; } Some(_) => { diff --git a/crates/ui-egui/src/dialogs/tests_modal.rs b/crates/ui-egui/src/dialogs/tests_modal.rs index e551e71627..8fd5193074 100644 --- a/crates/ui-egui/src/dialogs/tests_modal.rs +++ b/crates/ui-egui/src/dialogs/tests_modal.rs @@ -179,6 +179,34 @@ fn simplify_slider_escape_puts_the_paths_back() { assert_eq!(anchor_count(&app), 20); } +#[test] +fn path_sliders_close_on_a_drawing_switch() { + for (kind, command) in + [(simplify_slider::StripKind::Simplify, "object.path.simplify"), (simplify_slider::StripKind::Smooth, "object.path.smooth")] + { + let mut app = app_with_dense_path(); + let original = app.session.doc().unwrap().doc.clone(); + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + let anchors: Vec<_> = (0..20).map(|i| json!({"x": 30 + i * 15, "y": 120.0 + (i % 2) as f64 * 0.2})).collect(); + let id = app.run("path.create", json!({"anchors": anchors})).unwrap()["id"].as_u64().unwrap(); + app.run("select.anchors", json!({"id": id, "anchors": [[0, 19]]})).unwrap(); + let second = app.session.doc().unwrap().doc.clone(); + app.run("document.activate", json!({"index": 0})).unwrap(); + crate::menus::invoke(&mut app, command, json!({})); + settle(&mut app); + assert!(simplify_slider::showing_of(&app, kind)); + assert!(app.session.in_interaction()); + + app.run("document.activate", json!({"index": 1})).unwrap(); + assert!(simplify_slider::auto_of(&mut app, kind).is_err(), "Auto must not use the second drawing"); + frame(&mut app, vec![]); + assert!(!simplify_slider::showing_of(&app, kind)); + assert!(!app.session.in_interaction()); + assert_eq!(app.session.doc().unwrap().doc, second); + assert_eq!(app.session.documents()[0].doc, original); + } +} + /// The Simplify slider stays where it opened, under the paths as they were, while the preview changes their bounds, so a /// drag doesn't move the rail under the pointer. #[test]