From 265003fb6da79bdc5d01f96b7788bf42a820486a Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 15:21:45 -0800 Subject: [PATCH] Keep Flare Tool Options attached to their original drawing --- crates/ui-egui/src/dialogs/flare_options.rs | 87 ++++++++++++++++++--- 1 file changed, 76 insertions(+), 11 deletions(-) diff --git a/crates/ui-egui/src/dialogs/flare_options.rs b/crates/ui-egui/src/dialogs/flare_options.rs index 6b1d6770cd..d479b57368 100644 --- a/crates/ui-egui/src/dialogs/flare_options.rs +++ b/crates/ui-egui/src/dialogs/flare_options.rs @@ -16,6 +16,8 @@ //! (`ObjectDialogs::flare`), not fields. // PLAN: the `craft_ui` paths and `LivePreview`'s home (`form`) are assumed; `general_lengths` is cv.B5b's. +use std::sync::Arc; + use craft::ui::contract::Bind; use craft::ui::patterns::form_dialogs::{HostedParamDialog, Param, ParamDialog, ParamResult}; use craft::ui::runtime::env::{LengthUnits, UnitKind}; @@ -39,6 +41,8 @@ pub const KIND: &str = "flareOptions"; pub(crate) struct Open { /// The hosted dialog. id: DialogId, + opening: Arc<()>, + document: Option, /// Where a click opened it, to draw a flare there on OK. click: Option, /// The selected flares (ids) and the settings the dialog opened with, to change them on OK. @@ -47,6 +51,27 @@ pub(crate) struct Open { preview: Option, } +const PREVIEW_LABEL: &str = "Flare"; + +impl Open { + fn on_original_drawing(&self, app: &VectorcraftApp) -> bool { + self.preview.is_none() || app.session.active().map(|st| st.uid) == self.document + } + + fn cancel_preview(self, app: &mut VectorcraftApp) { + if self.on_original_drawing(app) + && !other_preview(app) + && let Some(preview) = self.preview + { + preview.cancel(app); + } + } +} + +fn other_preview(app: &VectorcraftApp) -> bool { + app.session.active().and_then(|st| st.interaction.as_ref()).is_some_and(|p| p.label != PREVIEW_LABEL) +} + /// Open the Flare Tool Options with the tool's values; `at`: where a click opened them, to draw a /// flare there on OK. Opened without a click while flares are selected, they show the first one's /// settings, to change the selected flares on OK. @@ -91,24 +116,25 @@ pub fn open(app: &mut VectorcraftApp, at: Option) -> Value { return Value::Null; }; // An open the host replaces leaves its preview behind unless it is rolled back here. - if let Some(old) = app.object_dialogs.flare.take() - && let Some(preview) = old.preview - { - preview.cancel(app); + if let Some(old) = app.object_dialogs.flare.take() { + old.cancel_preview(app); } - let cancel: Box = Box::new(|app| { - if let Some(open) = app.object_dialogs.flare.take() - && let Some(preview) = open.preview + let opening = Arc::new(()); + let cancelled = Arc::clone(&opening); + let cancel: Box = Box::new(move |app| { + if app.object_dialogs.flare.as_ref().is_some_and(|open| Arc::ptr_eq(&open.opening, &cancelled)) + && let Some(open) = app.object_dialogs.flare.take() { - preview.cancel(app); + open.cancel_preview(app); } }); let held = HeldOptions { parent: None, cancel: Some(cancel) }; let dialog = HostedParamDialog::new(&ctx, KIND, fields, move |fields| { ParamDialog::new(KIND, tl!("Flare Tool Options"), params(lengths)).values(Bind::bound(fields)).previews(previews).reset_to(defaults.clone()) }); + let document = app.session.active().map(|st| st.uid); match open_held_with(app, dialog, ok, held) { - Some(id) => app.object_dialogs.flare = Some(Open { id, click: at, edit, preview: previews.then(LivePreview::default) }), + Some(id) => app.object_dialogs.flare = Some(Open { id, opening, document, click: at, edit, preview: previews.then(LivePreview::default) }), None => app.status("no window"), } json!({ "dialog": KIND }) @@ -242,15 +268,27 @@ fn option_values(fields: &Map) -> Map { pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) { // PLAN: registered in hub-w2's pump file (wg.A18), one line. let Some(mut open) = app.object_dialogs.flare.take() else { return }; + if !open.on_original_drawing(app) { + app.object_dialogs.flare = Some(open); + return; + } if let Some(mut preview) = open.preview.take() { let state = app .dialogs() .and_then(|host| host.with(|host| host.get_by_id::(open.id).map(|d| (d.is_previewing(), d.fields().clone())))); if let Some((previewing, fields)) = state { let values = form::json_of(¶ms(general_lengths(app)), &fields); - if let Some((cmd, params)) = command(app, &open, &values) { + if previewing + && other_preview(app) + && let Err(e) = app.keep_typing() + { + app.status(e); + } + if !other_preview(app) + && let Some((cmd, params)) = command(app, &open, &values) + { // The preview reports its own failures on the status line. - let _ = preview.run(app, previewing, "Flare", &cmd, ¶ms); + let _ = preview.run(app, previewing, PREVIEW_LABEL, &cmd, ¶ms); } } open.preview = Some(preview); @@ -261,6 +299,16 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) { /// OK: set the tool's options; opened by a click, draw a flare there with them. Opened for the /// selected flares, draw them again with the settings changed, leaving the tool's options. fn ok(app: &mut VectorcraftApp, result: &ParamResult) -> Result<(), String> { + let open = app.object_dialogs.flare.as_ref().ok_or("the Flare Tool Options dialog is gone")?; + if !open.on_original_drawing(app) { + return Err(tl!("Return to the original drawing to place or edit the flare").into()); + } + if open.preview.is_some() && other_preview(app) { + app.keep_typing()?; + if other_preview(app) { + return Err(tl!("Finish the current edit before drawing the flare").into()); + } + } let fields = form::json_of(¶ms(general_lengths(app)), &result.fields); let accepted = accept(app, &fields); if accepted.is_ok() { @@ -430,6 +478,23 @@ mod tests { assert_eq!(parts.iter().filter(|p| *p == "Ring").count(), 3, "{parts:?}"); } + #[test] + fn a_pending_flare_click_waits_for_its_original_drawing() { + let mut app = app(); + open(&mut app, Some(Point::new(100.0, 110.0))); + app.run("file.new", json!({"width": 300, "height": 300})).unwrap(); + let count = |app: &VectorcraftApp| app.session.active().unwrap().doc.layers[0].children().unwrap().len(); + pump(&mut app); + assert_eq!(count(&app), 0, "the other drawing never shows the flare preview"); + press_ok(&mut app); + assert_eq!(count(&app), 0, "the other drawing never keeps the flare"); + assert!(app.object_dialogs.flare.is_some()); + app.run("document.activate", json!({"index": 0})).unwrap(); + press_ok(&mut app); + assert_eq!(count(&app), 1); + assert!(!flare_parts(&app).is_empty()); + } + /// With a flare selected, the toolbar's Flare Tool Options show its settings, and OK draws it /// again with the settings changed, as one undo step, leaving the tool's options. #[test]