diff --git a/crates/engine/src/cmd.rs b/crates/engine/src/cmd.rs index 8622497c6a..c75e06c776 100644 --- a/crates/engine/src/cmd.rs +++ b/crates/engine/src/cmd.rs @@ -844,7 +844,7 @@ pub(crate) const MAX_IMPORTED_PRESETS: usize = 1000; pub(crate) use addons::{DecodedImages, refresh_color_masks}; /// Packaging Tools' blanks and the objects their print leaves out, for the Fold Preview window. -pub use addons::{FoldAngles, fold_source, print_hidden, selection_fold_angles}; +pub use addons::{FoldAngles, fold_source, print_hidden, selection_fold_angles, selection_fold_lines}; /// Document Info's category titles and the Objects category's count labels. pub use docinfo::{CATEGORIES, OBJECT_LABELS}; pub use docmenu::guide_indexes; diff --git a/crates/engine/src/cmd/addons.rs b/crates/engine/src/cmd/addons.rs index 32f8cfa645..9fa5806c52 100644 --- a/crates/engine/src/cmd/addons.rs +++ b/crates/engine/src/cmd/addons.rs @@ -17,7 +17,7 @@ mod typetools; mod units; pub(crate) use image::{DecodedImages, refresh_color_masks}; -pub use packaging::{FoldAngles, fold_source, print_hidden, selection_fold_angles}; +pub use packaging::{FoldAngles, fold_source, print_hidden, selection_fold_angles, selection_fold_lines}; pub(crate) use packaging::{role_swatch_edited, role_swatches_merged}; use super::CommandSpec; diff --git a/crates/engine/src/cmd/addons/packaging.rs b/crates/engine/src/cmd/addons/packaging.rs index 779b044358..a0efc6f265 100644 --- a/crates/engine/src/cmd/addons/packaging.rs +++ b/crates/engine/src/cmd/addons/packaging.rs @@ -11,7 +11,7 @@ mod check; mod fold; mod roles; -pub use angle::{FoldAngles, selection_fold_angles}; +pub use angle::{FoldAngles, selection_fold_angles, selection_fold_lines}; pub use fold::{fold_source, print_hidden}; pub(crate) use roles::{role_swatch_edited, role_swatches_merged}; diff --git a/crates/engine/src/cmd/addons/packaging/angle.rs b/crates/engine/src/cmd/addons/packaging/angle.rs index b458a3e410..f38645774f 100644 --- a/crates/engine/src/cmd/addons/packaging/angle.rs +++ b/crates/engine/src/cmd/addons/packaging/angle.rs @@ -216,23 +216,39 @@ pub fn selection_fold_angles(s: &Session) -> FoldAngles { } } +/// The lines Set Fold Angle changes among the selected objects of the active document, as it takes +/// them with no `ids` (none, while it would refuse the selection). Given back as `ids`, they are the +/// lines it changes. +pub fn selection_fold_lines(s: &Session) -> Vec { + match (taken_roots(s, &Value::Null, SET_FOLD_ANGLE), s.doc()) { + (Ok((roots, _)), Ok(st)) => changeable(&st.doc, &roots).into_iter().map(|(n, _)| n.id).collect(), + _ => Vec::new(), + } +} + /// The [`FoldAngles`] of the outermost objects `roots` of `d`. pub(super) fn fold_angles(d: &Document, roots: &[NodeId]) -> FoldAngles { - let table = RoleTable::of(d); - let editable = d.editable(); let mut f = FoldAngles::default(); - for n in role_targets(d, &editable_members(d, roots.to_vec()).0, false).0.iter().filter_map(|id| d.node(*id)) { - // The role first: whether an object can be edited can take its bounds to find out. - if let Some(role) = table.role_of(n) - && folds(role) - && editable(n.id) - { - f.add(n, role); - } + for (n, role) in changeable(d, roots) { + f.add(n, role); } f } +/// The lines that fold among the outermost objects `roots` of `d` that Set Fold Angle can change, +/// with their roles, in order. +fn changeable<'a>(d: &'a Document, roots: &[NodeId]) -> Vec<(&'a Node, Role)> { + let table = RoleTable::of(d); + let editable = d.editable(); + let targets = role_targets(d, &editable_members(d, roots.to_vec()).0, false).0; + // The role first: whether an object can be edited can take its bounds to find out. + targets + .iter() + .filter_map(|id| d.node(*id)) + .filter_map(|n| table.role_of(n).filter(|r| folds(*r)).filter(|_| editable(n.id)).map(|r| (n, r))) + .collect() +} + impl FoldAngles { /// Counts line `n`, which has `role` and can be changed; a line that does not fold is left out. pub(super) fn add(&mut self, n: &Node, role: Role) { diff --git a/crates/three-d/src/fold/raster/tests.rs b/crates/three-d/src/fold/raster/tests.rs index b6251985ca..014cf010d7 100644 --- a/crates/three-d/src/fold/raster/tests.rs +++ b/crates/three-d/src/fold/raster/tests.rs @@ -616,9 +616,16 @@ fn a_flap_split_by_a_0_degree_crease_stacks_as_one_against_another_flap() { let camera = Camera { width: 140, center: [70.0, -50.0, 0.0], rotate_y: 180.0, ..front() }; let img = render(&Scene { blank: b, folded: &f, print: None, inside: [0, 0, 0, 255], outside: WHITE, camera }).expect("drawn"); // From the back the image is mirrored: x on the blank is 140 - x in the image. - let right_flap = b.panels.iter().position(|p| p.outline.bounds().is_some_and(|r| (r.x0 - 90.0).abs() < 1e-6)).expect("the right flap"); + let starting_at = |x0: f64| b.panels.iter().position(|p| p.outline.bounds().is_some_and(|r| (r.x0 - x0).abs() < 1e-6)); + let right_flap = starting_at(90.0).expect("the right flap"); + // Folded back about x 30, the left flap's part from 15 to 30 lies at 30 to 45 and its part from + // 0 to 15 at 45 to 60. + let (inner, outer) = (starting_at(15.0).expect("the left flap's inner part"), starting_at(0.0).expect("its outer part")); let (near, far) = (img.panel_at(140 - 42, 30), img.panel_at(140 - 52, 30)); - assert_eq!(near == Some(right_flap), far == Some(right_flap), "{near:?} {far:?}"); + // Either flap may be in front, but the same one at both samples. + let right = (near, far) == (Some(right_flap), Some(right_flap)); + let left = (near, far) == (Some(inner), Some(outer)); + assert!(right || left, "{near:?} {far:?}: right flap {right_flap}, left flap {inner} and {outer}"); } #[test] diff --git a/crates/ui-egui/src/addons/packaging.rs b/crates/ui-egui/src/addons/packaging.rs index e43aed7ec8..01d038bd72 100644 --- a/crates/ui-egui/src/addons/packaging.rs +++ b/crates/ui-egui/src/addons/packaging.rs @@ -269,13 +269,17 @@ fn rows(app: &mut VectorcraftApp, ctx: &egui::Context) -> Vec { } /// The fields Set Fold Angle… opens with: `addon.packaging.setFoldAngle`'s options on the angle -/// the selected lines that it can change all fold at, else 90°. The command's reason while it -/// can't change any. +/// the selected lines that it can change all fold at, else 90°, and those lines as the `ids` OK +/// sends, so OK changes the lines the dialog opened on even when the selection changes while it is +/// open, with the uid of their document as `__doc`, so OK changes nothing once another document is +/// the active one. The command's reason while it can't change any. pub(crate) fn dialog_fields(app: &VectorcraftApp) -> Result { let command = vectorcraft_engine::find_command(SET_FOLD_ANGLE).ok_or_else(|| format!("unknown command {SET_FOLD_ANGLE}"))?; (command.enabled)(&app.session)?; let angle = vectorcraft_engine::cmd::selection_fold_angles(&app.session).angle.unwrap_or(90.0); - Ok(json!({ "__command": SET_FOLD_ANGLE, "__label": "Set Fold Angle", "angle": angle })) + let ids: Vec = vectorcraft_engine::cmd::selection_fold_lines(&app.session).iter().map(|id| id.0).collect(); + let doc = app.session.active().map(|st| st.uid); + Ok(json!({ "__command": SET_FOLD_ANGLE, "__label": "Set Fold Angle", "angle": angle, "__params": { "ids": ids }, "__doc": doc })) } /// The Fold Angle row, while the selection holds lines that fold and can be changed: the angle diff --git a/crates/ui-egui/src/addons/packaging/tests.rs b/crates/ui-egui/src/addons/packaging/tests.rs index 4ee1334759..81d7338be3 100644 --- a/crates/ui-egui/src/addons/packaging/tests.rs +++ b/crates/ui-egui/src/addons/packaging/tests.rs @@ -324,6 +324,52 @@ fn set_fold_angle_opens_its_dialog_on_the_selected_lines_angle() { assert_eq!(app.ui.dialog.take().expect("a dialog").fields["angle"].as_f64(), Some(30.0)); } +/// Set Fold Angle…'s OK changes the lines the dialog opened on, even when the selection changes +/// while it is open (a remote command can change it): the other crease keeps its angle. +#[test] +fn set_fold_angle_s_ok_changes_the_lines_the_dialog_opened_on() { + let (mut app, a, b) = app(); + app.run(SET_ROLE, json!({"role": "crease", "ids": [a, b]})).unwrap(); + app.run("select.set", json!({"ids": [a]})).unwrap(); + menus::invoke(&mut app, SET_FOLD_ANGLE, json!({})); + assert!(app.ui.dialog.is_some(), "the dialog opens"); + app.run("select.set", json!({"ids": [b]})).unwrap(); + app.ui.dialog.as_mut().unwrap().fields.insert("angle".into(), json!(30.0)); + crate::dialogs::confirm(&mut app).unwrap(); + assert!(app.ui.dialog.is_none(), "OK closes the dialog"); + let angle = |app: &mut VectorcraftApp, id: u64| { + app.run("select.set", json!({"ids": [id]})).unwrap(); + vectorcraft_engine::cmd::selection_fold_angles(&app.session).angle + }; + assert_eq!(angle(&mut app, a), Some(30.0), "the line the dialog opened on"); + assert_eq!(angle(&mut app, b), Some(90.0), "the line selected later keeps its role's angle"); +} + +/// Set Fold Angle…'s OK changes nothing once another document is the active one, even a line there +/// with the same id as the line the dialog opened on. +#[test] +fn set_fold_angle_s_ok_changes_nothing_in_another_document() { + let (mut app, a, _) = app(); + app.run(SET_ROLE, json!({"role": "crease", "ids": [a]})).unwrap(); + app.run("select.set", json!({"ids": [a]})).unwrap(); + menus::invoke(&mut app, SET_FOLD_ANGLE, json!({})); + assert!(app.ui.dialog.is_some(), "the dialog opens"); + app.run("file.new", json!({"width": 300, "height": 200})).unwrap(); + let other = app.run("shape.rectangle", json!({"x": 20, "y": 20, "width": 40, "height": 40})).unwrap()["id"].as_u64().unwrap(); + assert_eq!(other, a, "the same id in the other document"); + app.run(SET_ROLE, json!({"role": "crease", "ids": [other]})).unwrap(); + app.ui.dialog.as_mut().unwrap().fields.insert("angle".into(), json!(30.0)); + crate::dialogs::confirm(&mut app).unwrap(); + assert!(app.ui.dialog.is_none(), "OK closes the dialog"); + let angle = |app: &mut VectorcraftApp| { + app.run("select.set", json!({"ids": [a]})).unwrap(); + vectorcraft_engine::cmd::selection_fold_angles(&app.session).angle + }; + assert_eq!(angle(&mut app), Some(90.0), "the other document's line keeps its role's angle"); + app.run("document.activate", json!({"index": 0})).unwrap(); + assert_eq!(angle(&mut app), Some(90.0), "the first document's line is left as it was"); +} + /// Set Fold Angle… keeps the angle typed into its dialog within -180° to 180°, so OK applies the /// nearest end instead of closing on an angle the command refuses. #[test] diff --git a/crates/ui-egui/src/dialogs/command.rs b/crates/ui-egui/src/dialogs/command.rs index 16befd8f4b..cca99c9d6c 100644 --- a/crates/ui-egui/src/dialogs/command.rs +++ b/crates/ui-egui/src/dialogs/command.rs @@ -282,8 +282,16 @@ const FIRST_BASELINE: form::Choices = &[ const VERTICAL_ALIGN: form::Choices = &[("Top", "top"), ("Center", "center"), ("Bottom", "bottom"), ("Justify", "justify")]; /// Closes before running, so a dialog the command opens stays open. A previewed one keeps its -/// preview as the one undo step. +/// preview as the one undo step. A dialog opened on one document's objects (`__doc`, its uid) +/// closes as Cancel does and runs nothing once another document is the active one, as Delete +/// Brush's question does (`brush_delete.rs`, `confirm`). fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result { + if let Some(doc) = d.fields.get("__doc") + && doc.as_u64() != app.session.active().map(|st| st.uid) + { + app.ui.dialog = None; + return Ok(json!({})); + } let (cmd, params) = call(d); if previews(d) { return form::commit_preview(app, &cmd, params);