diff --git a/crates/ui-egui/src/dialogs/path_ops.rs b/crates/ui-egui/src/dialogs/path_ops.rs index 37ea857dd6..7351a70863 100644 --- a/crates/ui-egui/src/dialogs/path_ops.rs +++ b/crates/ui-egui/src/dialogs/path_ops.rs @@ -3,6 +3,8 @@ //! Every dialog is a held parameter dialog: its OK is the closure given to `open_held`, a refusal keeps it open with //! the reason, and [`pump`] only runs the live preview of the kinds that have one. +use std::sync::{Arc, Weak}; + // PLAN: the framework import paths below are assumed from the contract rows (craft-ui-patterns re-exports contract and // runtime; the form-dialog items live under `patterns::form_dialogs`; `Field` and `NumberField` under craft-ui-widgets). use craft::behavior::ColumnWidth; @@ -19,7 +21,7 @@ use craft::ui::runtime::memory::{SaveKey, saved_value, set_saved_value}; use craft::ui::text::{NumberField, NumberOutput}; use craft::ui::value_drag::SliderScale; use serde_json::{Map, Value, json}; -use vectorcraft_doc::{NodeId, NodeKind}; +use vectorcraft_doc::{NodeId, NodeKind, Selection}; use vectorcraft_geom::PathData; use super::form; @@ -111,7 +113,10 @@ impl PathOp { pub(crate) struct Open { op: PathOp, id: DialogId, + document: u64, preview: LivePreview, + /// A distinct selection snapshot identifies the interaction that ran this preview. + preview_selection: Option>, /// The params the preview last answered for. last: Value, /// Simplify: the paths the preview ran on. @@ -309,6 +314,36 @@ fn take_open(app: &mut VectorcraftApp, op: PathOp) -> Option { Some(app.object_dialogs.path.remove(i)) } +impl Open { + fn owns_preview(&self, app: &VectorcraftApp) -> bool { + let Some(selection) = &self.preview_selection else { return false }; + app.session.active().is_some_and(|state| { + state.uid == self.document + && state.interaction.as_ref().is_some_and(|interaction| selection.as_ptr() == Arc::as_ptr(&interaction.selection)) + }) + } + + fn cancel(self, app: &mut VectorcraftApp) { + if self.owns_preview(app) { + self.preview.cancel(app); + } + } + + fn commit(&mut self, app: &mut VectorcraftApp, cmd: &str, params: Value) -> Result<(), String> { + if app.session.active().is_none_or(|state| state.uid != self.document) { + return Err(tl!("Return to the document where this dialog opened.").to_owned()); + } + if app.session.in_interaction() && !self.owns_preview(app) { + return Err(tl!("Finish the active edit before confirming.").to_owned()); + } + self.preview_selection = None; + self.last = Value::Null; + self.original.clear(); + self.shown = false; + std::mem::take(&mut self.preview).commit(app, cmd, params).map(drop) + } +} + /// Whether Object › Path › Simplify opens its dialog (the user kept its settings) instead of the slider. pub(crate) fn retained_simplify(app: &VectorcraftApp) -> bool { let Some(ctx) = app.ui_ctx.as_ref() else { return false }; @@ -440,6 +475,11 @@ fn open_dialog(app: &mut VectorcraftApp, op: PathOp, fields: DialogFields, remem app.status("There is no window to show the dialog in."); return; }; + let document = app.session.active().map(|state| state.uid); + if op.previews() && document.is_none() { + app.status("Open a document before editing paths."); + return; + } let lengths = crate::canvas::general_lengths(app); let opening = op.opening(); let key = op.key(); @@ -455,17 +495,11 @@ fn open_dialog(app: &mut VectorcraftApp, op: PathOp, fields: DialogFields, remem }); let ok = move |app: &mut VectorcraftApp, result: &craft::ui::patterns::form_dialogs::ParamResult| { let (cmd, params) = command(op, &result.fields); - match take_open(app, op) { - Some(record) => { - let id = record.id; - record.preview.commit(app, cmd, params).map(drop).inspect_err(|_| { - // The dialog stays open on the refusal, so it keeps previewing. - app.object_dialogs.path.push(Open { op, id, preview: LivePreview::default(), last: Value::Null, original: vec![], shown: false }); - })?; - } - None => { - app.run(cmd, params).map(drop)?; - } + if op.previews() { + let Some(mut record) = take_open(app, op) else { return Err(tl!("The path dialog is closed.").to_owned()) }; + record.commit(app, cmd, params).inspect_err(|_| app.object_dialogs.path.push(record))?; + } else { + app.run(cmd, params).map(drop)?; } match op { PathOp::Simplify => { @@ -479,24 +513,40 @@ fn open_dialog(app: &mut VectorcraftApp, op: PathOp, fields: DialogFields, remem Ok(()) }; if let Some(old) = take_open(app, op) { - old.preview.cancel(app); + old.cancel(app); } let id = if op.previews() { let cancel = Box::new(move |app: &mut VectorcraftApp| { + // A cancelled opening can leave a replacement with the same dialog ID. + let id = app.object_dialogs.path.iter().find(|record| record.op == op).map(|record| record.id); + if let Some(id) = id + && app.dialogs().is_some_and(|dialogs| dialogs.with(|host| host.get_by_id::(id).is_some())) + { + return; + } if let Some(record) = take_open(app, op) { - record.preview.cancel(app); + record.cancel(app); } }); open_held_with(app, dialog, ok, HeldOptions { parent: None, cancel: Some(cancel) }) } else { open_held(app, dialog, ok) }; - match id { - Some(id) if op.previews() => { - app.object_dialogs.path.push(Open { op, id, preview: LivePreview::default(), last: Value::Null, original: vec![], shown: false }) + match (id, document) { + (Some(id), Some(document)) if op.previews() => { + app.object_dialogs.path.push(Open { + op, + id, + document, + preview: LivePreview::default(), + preview_selection: None, + last: Value::Null, + original: vec![], + shown: false, + }); } - Some(_) => {} - None => app.status("There is no window to show the dialog in."), + (Some(_), _) => {} + (None, _) => app.status("There is no window to show the dialog in."), } } @@ -568,6 +618,17 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) { fn step(app: &mut VectorcraftApp, r: &mut Open) { let Some((on, fields)) = dialog_state(app, r.id) else { return }; + let Some(state) = app.session.active().filter(|state| state.uid == r.document) else { return }; + if !r.owns_preview(app) { + r.preview = LivePreview::default(); + r.preview_selection = None; + r.last = Value::Null; + r.original.clear(); + r.shown = false; + if state.interaction.is_some() { + return; + } + } let (cmd, params) = command(r.op, &fields); let stale = !on || r.last != params; if stale { @@ -575,13 +636,17 @@ fn step(app: &mut VectorcraftApp, r: &mut Open) { } if !on { r.last = Value::Null; + r.preview_selection = None; } let mut counts = stale.then_some(None); - if let Some(Ok(answer)) = r.preview.run(app, on, r.op.title(), cmd, ¶ms) { - r.last = params; - if r.op == PathOp::Simplify { - r.original = answer["ids"].as_array().into_iter().flatten().filter_map(|id| id.as_u64().map(NodeId)).collect(); - counts = Some(Some(json!({"before": answer["before"], "after": answer["after"]}))); + if let Some(answer) = r.preview.run(app, on, r.op.title(), cmd, ¶ms) { + r.preview_selection = app.session.active().and_then(|state| state.interaction.as_ref()).map(|it| Arc::downgrade(&it.selection)); + if let Ok(answer) = answer { + r.last = params; + if r.op == PathOp::Simplify { + r.original = answer["ids"].as_array().into_iter().flatten().filter_map(|id| id.as_u64().map(NodeId)).collect(); + counts = Some(Some(json!({"before": answer["before"], "after": answer["after"]}))); + } } } r.shown = on && fields.bool("showOriginal").unwrap_or(false); @@ -602,7 +667,7 @@ fn step(app: &mut VectorcraftApp, r: &mut Open) { /// on, with its id, as it was before the preview (from the interaction's snapshot). Empty /// otherwise. pub(crate) fn original_paths(app: &VectorcraftApp) -> Vec<(NodeId, &PathData)> { - let Some(r) = app.object_dialogs.path.iter().find(|r| r.op == PathOp::Simplify && r.shown) else { return vec![] }; + let Some(r) = app.object_dialogs.path.iter().find(|r| r.op == PathOp::Simplify && r.shown && r.owns_preview(app)) else { return vec![] }; let Some(it) = app.session.active().and_then(|st| st.interaction.as_ref()) else { return vec![] }; r.original .iter()