diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 283c1bd6a8..97fca4f235 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -246,7 +246,6 @@ impl VectorcraftApp { three_d_dialog: None, three_d: Default::default(), export_dialogs: Default::default(), - slice_dialogs: Default::default(), package_document: None, web_export: Default::default(), flattener_preview: Default::default(), diff --git a/crates/ui-egui/src/dialogs/host.rs b/crates/ui-egui/src/dialogs/host.rs index 67c6683f73..38c572c1c9 100644 --- a/crates/ui-egui/src/dialogs/host.rs +++ b/crates/ui-egui/src/dialogs/host.rs @@ -207,7 +207,6 @@ pub(crate) fn pump(app: &mut VectorcraftApp, ctx: &egui::Context) { super::graph_design::pump, super::perspective_grid::pump, super::perspective_presets::pump, - super::slices::pump, super::blend_options::pump, super::saved_names::pump, super::object_mosaic::pump, diff --git a/crates/ui-egui/src/dialogs/object_dialogs.rs b/crates/ui-egui/src/dialogs/object_dialogs.rs index df0c1be3ed..465fdcb850 100644 --- a/crates/ui-egui/src/dialogs/object_dialogs.rs +++ b/crates/ui-egui/src/dialogs/object_dialogs.rs @@ -4,8 +4,7 @@ use super::path_ops::PathOp; use super::simplify_slider::StripKind; use super::{ blend_options, dimension, flare_options, graph_data, graph_design, graph_type, indexed_color, live_paint_gaps, object_mosaic, option_form, - path_ops, perspective_grid, perspective_presets, saved_names, shapes, simplify_slider, slices, smooth_slider, transform, transform_each, - width_point, + path_ops, perspective_grid, perspective_presets, saved_names, shapes, simplify_slider, smooth_slider, transform, transform_each, width_point, }; use crate::VectorcraftApp; @@ -58,7 +57,6 @@ pub(crate) fn pump(app: &mut VectorcraftApp, ctx: &egui::Context) { graph_design::pump(app, ctx); perspective_grid::pump(app, ctx); perspective_presets::pump(app, ctx); - slices::pump(app, ctx); blend_options::pump(app, ctx); saved_names::pump(app, ctx); object_mosaic::pump(app, ctx); diff --git a/crates/ui-egui/src/dialogs/slices.rs b/crates/ui-egui/src/dialogs/slices.rs index ae99272163..8695ac0574 100644 --- a/crates/ui-egui/src/dialogs/slices.rs +++ b/crates/ui-egui/src/dialogs/slices.rs @@ -24,12 +24,6 @@ pub const OPTIONS: &str = "sliceOptions"; /// The dialog kind of Divide Slices. pub const DIVIDE: &str = "divideSlices"; -#[derive(Default)] -pub(crate) struct SliceDialogs { - options: Option, - divide: Option, -} - /// The document, its revert count and the effective slice selection used when opening either dialog. #[derive(PartialEq)] struct Target { @@ -134,12 +128,8 @@ pub fn open_options(app: &mut VectorcraftApp) -> Result { kinds.push(SliceKind::HtmlText); } let dialog = SliceOptionsDialog::new(options_of(&map)).kinds(&kinds); - app.dialogs().ok_or("no window")?.with(|host| { - host.open(dialog); - // Replacing a still-open dialog leaves its Cancel behind. - let _ = host.take::(); - }); - app.slice_dialogs.options = Some(source); + crate::dialogs::host::open_held(app, dialog, move |app, o: &SliceOptions| apply(app, &source, "object.slice.options", options_json(o))) + .ok_or("no window")?; Ok(Value::Null) } @@ -152,44 +142,27 @@ pub fn open_divide(app: &mut VectorcraftApp) -> Result { let source = Target::new(st.uid, st.reverts, selected); let (w, h) = first.map_or((50.0, 50.0), |r| (r.width() / 2.0, r.height() / 2.0)); let dialog = DivideSlicesDialog::new(DivideSlices::new(Some(DivideBy::Count(2)), Some(DivideBy::Count(2)))).lengths(lengths).start_sizes(h, w); - app.dialogs().ok_or("no window")?.with(|host| { - host.open(dialog); - let _ = host.take::(); - }); - app.slice_dialogs.divide = Some(source); + crate::dialogs::host::open_held(app, dialog, move |app, d: &DivideSlices| apply(app, &source, "object.slice.divide", divide_json(d))) + .ok_or("no window")?; Ok(Value::Null) } -/// Run the answer of whichever slice dialog closed with OK. The framework dialogs hold no answer, so a -/// refusal goes to the status line and the dialog is already closed. -pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) { - let Some(host) = app.dialogs() else { return }; - let (options, divide) = host.with(|h| (h.take::(), h.take::())); - let answers = [ - options.and_then(|r| { - let source = app.slice_dialogs.options.take(); - r.ok().map(|o| (source, "object.slice.options", options_json(&o))) - }), - divide.and_then(|r| { - let source = app.slice_dialogs.divide.take(); - r.ok().map(|d| (source, "object.slice.divide", divide_json(&d))) - }), - ]; - for (source, cmd, params) in answers.into_iter().flatten() { - if source.as_ref().is_some_and(|source| source.reverted(app)) { - app.status(tl!("The document was reverted. Reopen this dialog to continue.")); - } else if !source.is_some_and(|source| Some(source) == Target::current(app)) { - app.status(tl!("The slice selection changed while the dialog was open.")); - } else if let Err(e) = app.run(cmd, params) { - app.status(e); - } +/// Runs a slice dialog's OK on the document and slices it opened with. A refusal keeps the dialog open with +/// what was typed and the reason under its rows. +fn apply(app: &mut VectorcraftApp, source: &Target, cmd: &str, params: Value) -> Result<(), String> { + if source.reverted(app) { + return Err(tl!("The document was reverted. Reopen this dialog to continue.").to_owned()); } + if Target::current(app).as_ref() != Some(source) { + return Err(tl!("The slice selection changed while the dialog was open.").to_owned()); + } + app.run(cmd, params).map(|_| ()) } #[cfg(test)] mod tests { use super::*; - use craft::ui::dialog::{DialogHost, DialogId}; + use craft::ui::dialog::{DialogHost, DialogId, HeldOk}; use vectorcraft_doc::NodeId; use vectorcraft_engine::Session; @@ -217,12 +190,20 @@ mod tests { host(app, |h| h.set_json_field(&ctx, id, field, value)).unwrap(); } - /// OK on the open dialog, then the frame's pump runs its answer; whether OK was enabled. + /// OK on the open dialog, then the frame's pump runs its held answer, and an accepted answer closes the + /// dialog; whether OK was enabled. fn ok(app: &mut VectorcraftApp) -> bool { let (ctx, (id, _)) = (app.ui_ctx.clone().unwrap(), the_dialog(app)); - let enabled = host(app, |h| h.confirm_by_id(&ctx, id)); + let closed = host(app, |h| h.confirm_by_id(&ctx, id)); + if !closed && host(app, |h| h.held_state(id)) != Some(HeldOk::Waiting) { + return false; + } crate::dialogs::host::pump(app, &ctx); - enabled + if host(app, |h| h.held_state(id)) == Some(HeldOk::Accepted) { + host(app, |h| h.confirm_by_id(&ctx, id)); + crate::dialogs::host::pump(app, &ctx); + } + true } fn add_slice(app: &mut VectorcraftApp, x: f64) -> NodeId { @@ -235,15 +216,33 @@ mod tests { app.run("object.slice.select", json!({"slices": [id.0]})).unwrap(); } + /// The reason the open dialog shows for its refused OK, if it has one. + fn refusal(app: &VectorcraftApp) -> Option { + let (id, _) = the_dialog(app); + match host(app, |h| h.held_state(id)) { + Some(HeldOk::Refused(reason)) => Some(reason.as_str().to_string()), + _ => None, + } + } + + /// Whether either slice dialog is open. + fn slice_dialog_open(app: &VectorcraftApp) -> bool { + host(app, |h| h.is_open::() || h.is_open::()) + } + + /// The field each dialog's test types into, and the value typed. + fn draft(divide: bool) -> (&'static str, Value) { + if divide { ("columns", json!(3)) } else { ("name", json!("Original")) } + } + fn check_changed_selection(dialog: &str, divide: bool) { let mut app = app(); let source = app.session.active().unwrap().doc.slice_ids()[0]; let destination = add_slice(&mut app, 80.0); select_slice(&mut app, source); app.run(dialog, json!({})).unwrap(); - if !divide { - set(&app, "name", json!("Original")); - } + let (field, typed) = draft(divide); + set(&app, field, typed.clone()); select_slice(&mut app, destination); let st = app.session.active().unwrap(); let undo = st.history.undo.len(); @@ -253,7 +252,22 @@ mod tests { let st = app.session.active().unwrap(); assert_eq!(*st.doc, *document, "the changed selection is untouched"); assert_eq!(st.history.undo.len(), undo); - assert_eq!(app.ui.status, "The slice selection changed while the dialog was open."); + assert!(slice_dialog_open(&app), "the refused dialog stays open"); + assert_eq!(refusal(&app).as_deref(), Some("The slice selection changed while the dialog was open.")); + assert_eq!(the_dialog(&app).1[field], typed, "the refused dialog keeps what was typed"); + + // With the opening slice selected again, the same draft applies. + select_slice(&mut app, source); + assert!(ok(&mut app)); + assert!(!slice_dialog_open(&app), "the accepted dialog closes"); + let st = app.session.active().unwrap(); + assert_eq!(st.history.undo.len(), undo + 1, "the accepted OK is one undo step"); + if divide { + assert_eq!(st.doc.slices.len(), document.slices.len() + 6, "the opening slice is divided into six cells"); + } else { + assert_eq!(st.doc.slice_options(source).unwrap().name, "Original"); + assert_eq!(st.doc.slice_options(destination).unwrap().name, "", "the other slice keeps its options"); + } } #[test] @@ -267,7 +281,7 @@ mod tests { } /// Opens `dialog` on the saved document's slice, edits the document, reverts it and reselects the same slice id; - /// OK then leaves the restored document as it is and reports the revert. Returns the app and the slice. + /// OK then leaves the restored document as it is and the dialog open with the reason. Returns the app and the slice. fn check_refused_after_file_revert(dialog: &str, divide: bool, tag: &str) -> (VectorcraftApp, NodeId) { let mut app = app(); let slice = app.session.active().unwrap().doc.slice_ids()[0]; @@ -292,7 +306,8 @@ mod tests { let st = app.session.active().unwrap(); assert_eq!(*st.doc, *restored, "OK leaves the restored document's slices unchanged"); assert_eq!(st.history.undo.len(), undo); - assert_eq!(app.ui.status, "The document was reverted. Reopen this dialog to continue."); + assert!(slice_dialog_open(&app), "the refused dialog stays open"); + assert_eq!(refusal(&app).as_deref(), Some("The document was reverted. Reopen this dialog to continue.")); (app, slice) } @@ -332,6 +347,8 @@ mod tests { let st = app.session.active().unwrap(); assert_eq!(st.doc.slice_options(to).unwrap().name, ""); assert_eq!(st.history.undo.len(), undo); + assert_eq!(refusal(&app).as_deref(), Some("The slice selection changed while the dialog was open."), "the refused dialog stays open"); + assert_eq!(the_dialog(&app).1["name"], "Source", "the refused dialog keeps what was typed"); } #[test] @@ -342,7 +359,7 @@ mod tests { select_slice(&mut app, other); app.run("object.slice.options", json!({})).unwrap(); let ctx = app.ui_ctx.clone().unwrap(); - pump(&mut app, &ctx); + crate::dialogs::host::pump(&mut app, &ctx); set(&app, "name", json!("Replacement")); assert!(ok(&mut app)); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 4062cc4302..3cd948fce0 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -288,8 +288,6 @@ pub struct VectorcraftApp { pub(crate) three_d: panels::three_d::Model, /// The open export option dialogs and what runs after each. pub(crate) export_dialogs: dialogs::export_host::ExportDialogs, - /// The document and selection used by each open slice dialog. - pub(crate) slice_dialogs: dialogs::slices::SliceDialogs, /// The drawing and loaded contents the open Package dialog saves and collects. pub(crate) package_document: Option<(u64, u64)>, /// Save for Web's renders and colour table.