From f5c5adbfa6e4455ba475e5b502f2430d4b312c9e Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 13:50:47 -0800 Subject: [PATCH] Keep Live Paint Gap Options with its original document --- crates/ui-egui/src/dialogs/live_paint_gaps.rs | 138 +++++++++++++++--- crates/ui-egui/src/dialogs/option_form.rs | 14 +- 2 files changed, 132 insertions(+), 20 deletions(-) diff --git a/crates/ui-egui/src/dialogs/live_paint_gaps.rs b/crates/ui-egui/src/dialogs/live_paint_gaps.rs index bcbb884bc2..f03b2167e5 100644 --- a/crates/ui-egui/src/dialogs/live_paint_gaps.rs +++ b/crates/ui-egui/src/dialogs/live_paint_gaps.rs @@ -26,6 +26,7 @@ use crate::VectorcraftApp; pub const KIND: &str = "livePaintGapOptions"; const CMD: &str = "livePaint.gapOptions"; +const WRONG_DOCUMENT: &str = "Gap Options can only be applied in the document this dialog was opened for."; /// The undo step the preview and OK make. const PREVIEW_LABEL: &str = "Gap Options"; @@ -51,20 +52,31 @@ pub(super) static SPEC: OptionSpec = OptionSpec { pub(crate) struct Open { /// The dialog the preview belongs to. pub id: DialogId, + /// The document that owns the preview interaction. + document: u64, /// The preview the options run as. pub preview: LivePreview, } +impl Open { + fn cancel(self, app: &mut VectorcraftApp) { + if app.session.active().is_some_and(|active| active.uid == self.document) { + self.preview.cancel(app); + } + } +} + /// Open Gap Options on the selected Live Paint groups, filled in from the first one's options. pub fn open(app: &mut VectorcraftApp) -> Result { - let groups: Vec = app - .session - .doc() - .map(|d| { - let doc = &d.doc; - d.selection.objects.iter().filter(|id| doc.node(**id).is_some_and(vectorcraft_tools::builder::is_live_paint)).map(|id| id.0).collect() - }) - .unwrap_or_default(); + let active = app.session.active().ok_or("select a Live Paint group")?; + let document = active.uid; + let groups: Vec = active + .selection + .objects + .iter() + .filter(|id| active.doc.node(**id).is_some_and(vectorcraft_tools::builder::is_live_paint)) + .map(|id| id.0) + .collect(); let Some(&first) = groups.first() else { return Err("select a Live Paint group".into()) }; let info = app.session.execute("livePaint.info", &json!({ "group": first })).map_err(|e| e.to_string())?; let o = &info["gapOptions"]; @@ -75,10 +87,15 @@ pub fn open(app: &mut VectorcraftApp) -> Result { let fields = [("detect", o["detect"].clone()), ("size", json!(size)), ("custom", custom), ("color", o["color"].clone()), ("closeGaps", json!(false))]; let values = Map::from_iter(fields.map(|(key, value)| (key.to_owned(), value))); - option_form::open(app, &SPEC, values, json!({ "ids": groups })).ok_or("no window")?; + option_form::open(app, &SPEC, values, json!({ "ids": groups, "document": document })).ok_or("no window")?; Ok(Value::Null) } +fn original_document(app: &VectorcraftApp, extra: &Value) -> Option { + let document = extra["document"].as_u64()?; + app.session.active().filter(|active| active.uid == document).map(|_| document) +} + /// `livePaint.gapOptions` parameters from the values and the groups in `extra`. fn params(values: &Map, extra: &Value) -> Value { let size = match values.get("size").and_then(Value::as_str) { @@ -111,24 +128,43 @@ fn body(ui: &mut egui::Ui, rows: &mut Rows<'_>) { /// OK: keep the previewed options as one undo step; with no preview running they run as one. fn run(app: &mut VectorcraftApp, values: &Map, extra: &Value) -> Result { - let preview = app.object_dialogs.gaps.take().map_or_else(LivePreview::default, |open| open.preview); + let document = original_document(app, extra).ok_or_else(|| tl!(WRONG_DOCUMENT).to_owned())?; + let preview = app.object_dialogs.gaps.take().filter(|open| open.document == document).map_or_else(LivePreview::default, |open| open.preview); preview.commit(app, CMD, params(values, extra)) } /// The dialog closed without OK: roll the preview back. fn cancelled(app: &mut VectorcraftApp) { if let Some(open) = app.object_dialogs.gaps.take() { - open.preview.cancel(app); + open.cancel(app); } } /// Runs the open Gap Options' preview as its options stand, once per frame after the shell pass: with Preview on the /// options apply to the groups, off they are rolled back; the gaps a new preview finds are kept for [`overlays`]. /// Nothing runs while an OK waits for its answer, which keeps or refuses the preview that is running. -pub(crate) fn pump(app: &mut VectorcraftApp, _: &egui::Context) { +pub(crate) fn pump(app: &mut VectorcraftApp, ctx: &egui::Context) { let Some(dialogs) = app.dialogs() else { return }; let shown = dialogs.with(|host| { - let form = host.get::().filter(|form| form.is(KIND))?; + let form = host.get_mut::().filter(|form| form.is(KIND))?; + let source = form.extra["document"].as_u64()?; + if !app.session.documents().iter().any(|doc| doc.uid == source) { + host.cancel::(ctx); + return None; + } + let document = original_document(app, &form.extra); + let paused = document.is_none(); + if form.preview_paused != paused { + form.preview_paused = paused; + if !paused && form.held.refusal().is_some_and(|(reason, _)| reason.as_str() == tl!(WRONG_DOCUMENT)) { + form.held.clear_refusal(); + } + ctx.request_repaint(); + } + let Some(document) = document else { + form.previewed = Value::Null; + return None; + }; if !form.held.state().is_none_or(|state| matches!(state, HeldOk::Refused(_))) { return None; } @@ -137,17 +173,17 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _: &egui::Context) { if form.values.get("color").and_then(Value::as_str).is_some_and(has_alpha) { params["color"] = Value::Null; } - Some((form.id(), params, form.preview)) + Some((form.id(), document, params, form.preview)) }); - let Some((id, params, on)) = shown else { return }; + let Some((id, document, params, on)) = shown else { return }; // A preview of another dialog is rolled back before this one starts its own. let mut open = match app.object_dialogs.gaps.take() { - Some(open) if open.id == id => open, + Some(open) if open.id == id && open.document == document => open, stale => { if let Some(stale) = stale { - stale.preview.cancel(app); + stale.cancel(app); } - Open { id, preview: LivePreview::default() } + Open { id, document, preview: LivePreview::default() } } }; let ran = open.preview.run(app, on, PREVIEW_LABEL, CMD, ¶ms); @@ -177,6 +213,7 @@ pub(crate) fn overlays(app: &VectorcraftApp) -> Vec { let Some(dialogs) = app.dialogs() else { return vec![] }; let shown = dialogs.with(|host| { let form = host.get::().filter(|form| form.is(KIND) && form.preview)?; + original_document(app, &form.extra)?; Some((form.previewed.clone(), form.values.get("color").and_then(Value::as_str)?.to_owned())) }); let Some((gaps, color)) = shown else { return vec![] }; @@ -236,6 +273,71 @@ mod tests { app.run("livePaint.make", json!({})).unwrap()["id"].as_u64().unwrap() } + fn open_with_matching_groups(app: &mut VectorcraftApp) -> u64 { + let group = gapped_group(app); + app.run("file.new", json!({"width": 400, "height": 400})).unwrap(); + assert_eq!(gapped_group(app), group, "the new document reuses the group ID"); + app.run("document.activate", json!({"index": 0})).unwrap(); + app.run("select.set", json!({"ids": [group]})).unwrap(); + app.run("ui.livePaintGapOptions", json!({})).unwrap(); + set(app, "detect", json!(true)).unwrap(); + group + } + + #[test] + fn gap_confirmation_stays_with_its_document() { + let mut app = app(); + let group = open_with_matching_groups(&mut app); + app.run("document.activate", json!({"index": 1})).unwrap(); + let undo = app.session.doc().unwrap().history.undo.len(); + assert!(!press_ok(&mut app), "the unrelated group cannot take the edit"); + assert_eq!(options(&app, group), GapOptions::default()); + assert_eq!(app.session.doc().unwrap().history.undo.len(), undo); + app.run("document.activate", json!({"index": 0})).unwrap(); + pump_all(&mut app); + assert!(held(&app).is_none(), "the stale refusal clears on the original tab"); + assert!(press_ok(&mut app), "the original group's edit can be retried"); + assert!(options(&app, group).detect); + } + + #[test] + fn closing_the_original_document_dismisses_gap_options() { + let mut app = app(); + let group = open_with_matching_groups(&mut app); + app.run("document.activate", json!({"index": 1})).unwrap(); + assert!(app.session.close_document(0)); + pump_all(&mut app); + assert!(!is_open(&app)); + assert_eq!(options(&app, group), GapOptions::default()); + } + + #[test] + fn gap_preview_disappears_in_another_document_and_resumes_on_return() { + let mut app = app(); + let group = open_with_matching_groups(&mut app); + set(&app, "preview", json!(true)).unwrap(); + pump_all(&mut app); + assert!(options(&app, group).detect); + assert!(!overlays(&app).is_empty()); + app.run("document.activate", json!({"index": 1})).unwrap(); + let unrelated_overlays = overlays(&app); + pump_all(&mut app); + let paused = |app: &VectorcraftApp| app.dialogs().unwrap().with(|host| host.get::().unwrap().preview_paused); + assert!(paused(&app)); + assert_eq!(fields(&app)["preview"], json!(true)); + assert_eq!(options(&app, group), GapOptions::default()); + assert!(!app.session.in_interaction()); + assert!(unrelated_overlays.is_empty()); + assert!(overlays(&app).is_empty()); + app.run("document.activate", json!({"index": 0})).unwrap(); + pump_all(&mut app); + assert!(!paused(&app)); + assert!(options(&app, group).detect); + assert!(!overlays(&app).is_empty()); + cancel(&mut app); + assert_eq!(options(&app, group), GapOptions::default()); + } + #[test] fn the_menu_item_opens_gap_options_which_preview_the_gaps_and_ok_keeps_them() { let mut app = app(); diff --git a/crates/ui-egui/src/dialogs/option_form.rs b/crates/ui-egui/src/dialogs/option_form.rs index 8648332c7e..49ef4a34b4 100644 --- a/crates/ui-egui/src/dialogs/option_form.rs +++ b/crates/ui-egui/src/dialogs/option_form.rs @@ -7,7 +7,7 @@ use std::ops::RangeInclusive; use craft::theme::variant::Tone; use craft::ui::choice::{Checkbox, Radio, RadioGroup}; -use craft::ui::contract::{Bind, Entry, Props as _, Show as _, ShowContainer as _}; +use craft::ui::contract::{Bind, Entry, Politeness, Props as _, Show as _, ShowContainer as _}; use craft::ui::dialog::{DialogId, DialogResult, DialogStack, FieldRefusal, HeldAnswer, HeldOk, StackDialog, dialog_id}; use craft::ui::form::{Field, FieldGroup}; use craft::ui::layout_components::Row; @@ -175,6 +175,8 @@ pub(crate) struct OptionForm { lengths: LengthUnits, /// The Preview check box. pub(super) preview: bool, + /// The current document does not own this dialog's preview. + pub(super) preview_paused: bool, /// Whether the dialog's container is open. open: bool, /// The OK the dialog filed for [`pump`] to answer. @@ -202,6 +204,7 @@ pub(super) fn open(app: &mut VectorcraftApp, spec: &'static OptionSpec, values: extra, lengths, preview: false, + preview_paused: false, open: true, held: HeldAnswer::new(), requests: HostRequests::new(), @@ -249,8 +252,9 @@ impl StackDialog for OptionForm { // The app accepted the held OK: this pass closes the dialog with it. A measuring run closes nothing. let finish = self.held.is_accepted() && !measuring; let answers = if measuring { Vec::new() } else { self.requests.take_answers() }; - let OptionForm { spec, values, lengths, preview, open, held, requests, .. } = self; + let OptionForm { spec, values, lengths, preview, preview_paused, open, held, requests, .. } = self; let (spec, lengths) = (*spec, *lengths); + let show_pause = *preview && *preview_paused; let title = (spec.title)(); let refusal = held.refusal().map(|(reason, refused)| (reason.clone(), refused.clone())); let mut form = FormDialog::new(word(&title), Bind::bound(&mut *values)) @@ -274,6 +278,12 @@ impl StackDialog for OptionForm { if let Some((reason, _)) = refusal.as_ref().filter(|(_, refused)| *refused == *values) { Notice::new(Tone::Bad, reason.clone()).layout(NoticeLayout::Inline).show(ui); } + if show_pause { + Notice::new(Tone::Info, tl!("Preview is paused. Return to the document this dialog was opened for.").to_owned()) + .layout(NoticeLayout::Inline) + .announce(Politeness::Polite) + .show(ui); + } if finish { form.request_close_as(CloseReason::Activated); }