diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 5c6dd3510b..fc33cf112d 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -180,6 +180,8 @@ impl VectorcraftApp { multi_color: None, global_edit_count: None, shape_angle: None, + radius_corners: None, + selected_rectangles: None, trace_presets: None, trace_target: None, attributes_info: None, diff --git a/crates/ui-egui/src/caches.rs b/crates/ui-egui/src/caches.rs index 77b94c9ec0..d7d333402a 100644 --- a/crates/ui-egui/src/caches.rs +++ b/crates/ui-egui/src/caches.rs @@ -160,6 +160,12 @@ pub struct CanvasCache { pub(crate) global_edit_count: Option<(crate::panels::properties::GlobalEditKey, usize)>, /// The selected live shape's angle, for (document uid, revision, kind of shape). pub shape_angle: Option, + /// The corners a live shape's Corner Radius field sets, as one hash, for (document uid, + /// File › Revert count, revision). + pub(crate) radius_corners: Option<((u64, u64, u64), u64)>, + /// The selected live rectangles the per-corner fields set, as one hash, for (document uid, + /// File › Revert count, revision). + pub(crate) selected_rectangles: Option<((u64, u64, u64), u64)>, /// The Image Trace presets, for the session's preset revision. pub(crate) trace_presets: Option<(u64, Arc)>, /// What the Image Trace panel traces, for (document uid, revision). diff --git a/crates/ui-egui/src/chrome/control_bar.rs b/crates/ui-egui/src/chrome/control_bar.rs index 985646a065..0c02c32828 100644 --- a/crates/ui-egui/src/chrome/control_bar.rs +++ b/crates/ui-egui/src/chrome/control_bar.rs @@ -127,10 +127,11 @@ fn corner_radius(app: &mut VectorcraftApp, ui: &mut Ui) { let Some(picked) = crate::panels::picked_corners(app) else { return }; let label = tl!("Corner Radius:"); let lengths = crate::canvas::general_lengths(app); + let target = crate::panels::corner_target(app, &picked.corners); let mut typed = None; group(ui, rank::GROUP, |ui| { crate::panels::corners_link(app, ui); - let number = NumberField::length(label, Bind::given(picked.radius), lengths).show(ui); + let number = NumberField::length(label, Bind::given(picked.radius), lengths).edit_target(target).show(ui); typed = number.edit.events.committed().copied().flatten().or(typed); }); if let Some(radius) = typed { diff --git a/crates/ui-egui/src/chrome/tests.rs b/crates/ui-egui/src/chrome/tests.rs index aeafbc9936..b7d7f0b4dd 100644 --- a/crates/ui-egui/src/chrome/tests.rs +++ b/crates/ui-egui/src/chrome/tests.rs @@ -3224,6 +3224,34 @@ fn the_control_bar_opacity_drops_an_unfinished_value_on_file_revert() { assert_eq!(opacity, 0.01, "Revert drops the unfinished value: 1%, not 20+1"); } +/// A radius typed into the Control bar's Corner Radius field and not yet entered is dropped when +/// another corner with the same radius is Direct-Selected: Enter sets only what was typed after. +#[test] +fn the_control_bar_corner_radius_drops_an_unfinished_value_when_the_corners_change() { + use egui::{Key, Modifiers}; + crate::i18n::set_current(crate::i18n::Lang::EN); + let mut app = crate::VectorcraftApp::new(Session::new(), Default::default()); + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + let id = app.run("shape.rectangle", json!({"x": 50, "y": 50, "width": 100, "height": 100})).unwrap()["id"].as_u64().unwrap(); + app.run("select.set", json!({"ids": [id]})).unwrap(); + app.run("object.expandShape", json!({})).unwrap(); + app.select_tool("directSelection"); + app.run("select.anchors", json!({"id": id, "anchors": [[0, 0]], "mode": "set"})).unwrap(); + let mut bar = crate::tests::panel_harness::Panel::new(app, crate::chrome::control_bar); + bar.room(egui::vec2(2400.0, 400.0)).frame(); + bar.click_role("Corner Radius:", Role::SPIN_BUTTON); + bar.key(Key::A, Modifiers::COMMAND); + bar.type_text("3+"); + bar.app().run("select.anchors", json!({"id": id, "anchors": [[0, 1]], "mode": "set"})).unwrap(); + bar.frame(); + assert_eq!(bar.value_role("Corner Radius:", Role::SPIN_BUTTON).as_deref(), Some("0 pt"), "the field shows the new corner's radius"); + bar.type_text("1"); + bar.key(Key::Enter, Modifiers::NONE); + let n = bar.app().session.active().unwrap().doc.node(vectorcraft_doc::NodeId(id)).unwrap().clone(); + let lc = vectorcraft_doc::LiveCorners::of(&n).unwrap(); + assert_eq!((0..4).map(|k| lc.radius(k)).collect::>(), [0.0, 1.0, 0.0, 0.0], "the second corner takes 1 pt, not 3+1"); +} + /// With the Artboard tool in use, the Control bar shows the Align buttons after the artboard /// controls while it has room, and the Distribute buttons for two or more selected artboards with /// Align to Artboard and three or more with Align to Selection. A diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs index 437fbf8524..6ca221d315 100644 --- a/crates/ui-egui/src/panels.rs +++ b/crates/ui-egui/src/panels.rs @@ -54,6 +54,8 @@ pub mod transform; pub mod transparency; pub mod variables; +use std::collections::BTreeSet; +use std::hash::{DefaultHasher, Hash, Hasher}; use std::sync::Arc; use craft::behavior::{IconId, Mixed}; @@ -83,7 +85,7 @@ use craft::ui::text::{NumberField, link_toggle}; use egui::Ui; use serde_json::{Value, json}; use vectorcraft_color::{BlendMode, Color, Paint}; -use vectorcraft_doc::{LiveCorners, Node, NodeId, NodeKind, StrokeLayer}; +use vectorcraft_doc::{LiveCorners, LiveShape, Node, NodeId, NodeKind, StrokeLayer}; use vectorcraft_engine::inspect::StrokeMixed; use vectorcraft_geom::shapes::CornerKind; @@ -124,6 +126,28 @@ pub(crate) fn corner_length(c: &LiveCorners, k: usize) -> f64 { if c.radii_relative() { c.radius(k) * c.corner(k).map_or(0.0, |c| c.max_radius()) } else { c.radius(k) } } +/// What a corner field edits, as its edit target: the active document's uid, how many times +/// File › Revert and Undo or Redo replaced its content (after which a node id can name another +/// object), the selected objects in id order, and `corners`, the corners the field sets (a picked +/// corner as its [`CornerPlace`]). A field drops a value typed and not yet committed when this +/// changes, even where the new corners have the same radius, and keeps it when the same objects +/// are selected in another order. Rounding a corner replaces its anchors without changing the +/// corner, so a scrub that rounds a Direct-Selected corner keeps its target. The Layers panel's +/// target is not part of it. +pub(crate) fn corner_target(app: &VectorcraftApp, corners: impl Hash) -> u64 { + let mut hasher = DefaultHasher::new(); + app.session + .active() + .map(|st| { + let mut objects = st.selection.objects.to_vec(); + objects.sort_unstable(); + (st.uid, st.reverts, st.history_restores, objects) + }) + .hash(&mut hasher); + corners.hash(&mut hasher); + hasher.finish() +} + /// One entry per corner kind, labelled as the Corners dialog labels them. pub(crate) fn corner_entries() -> Vec> { CornerKind::ALL.iter().map(|k| Entry::new(*k, crate::dialogs::corners::kind_label(*k))).collect() @@ -227,6 +251,8 @@ fn rectangle_corners() -> [(usize, &'static str, &'static str); 4] { fn corner_fields(app: &mut VectorcraftApp, ui: &mut Ui, n: &Node) { let Some(lc) = LiveCorners::of(n) else { return }; let lengths = general_lengths(app); + let rectangles = selected_rectangles(app); + let target = corner_target(app, rectangles); let entries = corner_entries(); let mut radii = Vec::new(); let mut kinds = Vec::new(); @@ -242,7 +268,8 @@ fn corner_fields(app: &mut VectorcraftApp, ui: &mut Ui, n: &Node) { for (k, label, corner) in rectangle_corners() { Field::new(label).show(ui, |ui, _| { Row::new().wrap(true).show(ui, |ui, _| { - radii.push((k, NumberField::length(label, Bind::given(Some(corner_length(&lc, k))), lengths).show(ui).edit)); + let field = NumberField::length(label, Bind::given(Some(corner_length(&lc, k))), lengths).edit_target(target); + radii.push((k, field.show(ui).edit)); if let Some(kind) = corner_select(ui, corner, Some(lc.kind(k)), &entries) { kinds.push((k, kind)); } @@ -267,7 +294,9 @@ fn corner_fields(app: &mut VectorcraftApp, ui: &mut Ui, n: &Node) { /// [`corner_radius_row`]'s field: [`corner_radius`], which a new value sets on those corners. pub(crate) fn corner_radius_field(app: &mut VectorcraftApp, ui: &mut Ui, n: &Node) { - let edit = NumberField::length(tl!("Corner Radius"), Bind::given(corner_radius(app, n)), general_lengths(app)).show(ui).edit; + let corners = radius_corners(app); + let target = corner_target(app, corners); + let edit = NumberField::length(tl!("Corner Radius"), Bind::given(corner_radius(app, n)), general_lengths(app)).edit_target(target).show(ui).edit; apply_edits(app, &edit, EditPath::Scope, |app, v: &Option, _| { if let Some(r) = v { app.run("object.setLiveShape", json!({"radius": r.max(0.0)})).ok(); @@ -309,6 +338,8 @@ pub(crate) fn command_icon(id: &str, tools: crate::icons::ToolIcons) -> Option<& pub(crate) struct PickedCorners { /// `object.setLiveShape`'s `items`: `{id, corners}` for each path. pub items: Vec, + /// The same corners as `items`: each path's id and its [`CornerPlace`]s. + pub corners: Vec<(NodeId, Vec)>, /// The radius the corners share, as a document length; None when they differ. pub radius: Option, /// The kind the corners share; None when they differ. @@ -317,6 +348,100 @@ pub(crate) struct PickedCorners { pub relative: Option, } +/// A corner: its anchor index, counted across subpaths, and a hash of what the index is counted +/// in. A live rectangle's, polygon's or star's corners keep their order, so the hash covers only +/// their number, and moving, rotating or scaling the shape keeps it. On any other path the hash +/// covers the corner's subpath of the unrounded path: its first anchor index, whether it is closed, +/// and each anchor's position and handles relative to the subpath's first anchor, in millionths of +/// a point. Moving the path, or changing another subpath without changing the subpath's first +/// anchor index, keeps it. Reversing, rotating or scaling the subpath, or changing its anchors, +/// changes it. Rounding a corner leaves the unrounded path as it is. +pub(crate) type CornerPlace = (usize, u64); + +/// Each corner of node `n`'s Live Corners `lc` as its [`CornerPlace`], hashing each subpath once. +fn corner_places(n: &Node, lc: &LiveCorners, corners: &BTreeSet) -> Vec { + if matches!(n.live_shape(), Some(LiveShape::Rectangle { .. } | LiveShape::Polygon { .. } | LiveShape::Star { .. })) { + let mut hasher = DefaultHasher::new(); + lc.corners.len().hash(&mut hasher); + let shape = hasher.finish(); + return corners.iter().map(|&k| (k, shape)).collect(); + } + let mut places = Vec::with_capacity(corners.len()); + let mut first = 0; + for subpath in &lc.base.subpaths { + let end = first + subpath.anchors.len(); + let mut picked = corners.range(first..end).peekable(); + if picked.peek().is_some() + && let Some(origin) = subpath.anchors.first().map(|a| a.p) + { + let mut hasher = DefaultHasher::new(); + first.hash(&mut hasher); + subpath.closed.hash(&mut hasher); + subpath.anchors.len().hash(&mut hasher); + for anchor in &subpath.anchors { + for p in [anchor.p, anchor.h_in, anchor.h_out] { + [((p.x - origin.x) * 1e6).round() as i64, ((p.y - origin.y) * 1e6).round() as i64].hash(&mut hasher); + } + } + let shape = hasher.finish(); + places.extend(picked.map(|&k| (k, shape))); + } + first = end; + } + places +} + +/// The selected live rectangles, as one hash, for (document uid, File › Revert count, revision). +fn selected_rectangles(app: &mut VectorcraftApp) -> u64 { + let Some(st) = app.session.active() else { return 0 }; + let key = (st.uid, st.reverts, st.revision); + if let Some((k, id)) = app.canvas.selected_rectangles + && k == key + { + return id; + } + // The live rectangles among the selected objects, the ones `set_rectangle_corners` sets. + let mut hasher = DefaultHasher::new(); + let mut objects = st.selection.objects.to_vec(); + objects.sort_unstable(); + for id in objects { + if st.doc.node(id).is_some_and(|n| matches!(n.live_shape(), Some(LiveShape::Rectangle { .. }))) { + id.hash(&mut hasher); + } + } + let rectangles = hasher.finish(); + app.canvas.selected_rectangles = Some((key, rectangles)); + rectangles +} + +/// The corners that `object.setLiveShape` with a `radius` and no `items` sets, as one hash: each +/// selected object with Live Corners and its [`LiveCorners::picked`] corners. Measured once per +/// (document uid, File › Revert count, revision): the panels read it every frame, and Select All +/// can select hundreds of thousands of shapes. +fn radius_corners(app: &mut VectorcraftApp) -> u64 { + let Some(st) = app.session.active() else { return 0 }; + let key = (st.uid, st.reverts, st.revision); + if let Some((k, id)) = app.canvas.radius_corners + && k == key + { + return id; + } + let mut hasher = DefaultHasher::new(); + let mut objects = st.selection.objects.to_vec(); + objects.sort_unstable(); + for id in objects { + let Some(n) = st.doc.node(id) else { continue }; + let Some(lc) = LiveCorners::of(n) else { continue }; + let corners = lc.picked(st.selection.partial(id)); + if !corners.is_empty() { + (id, corner_places(n, &lc, &corners)).hash(&mut hasher); + } + } + let corners = hasher.finish(); + app.canvas.radius_corners = Some((key, corners)); + corners +} + /// The picked corners of the selection: for each selected path with Live Corners, the corners /// holding a Direct-Selected anchor, or every corner of a path selected whole. A path whose /// selected anchors hold no corner adds none. None when no path adds a corner. @@ -326,9 +451,10 @@ pub(crate) fn picked_corners(app: &VectorcraftApp) -> Option { rest.iter().all(|v| same(*v, first)).then_some(first) } let st = app.session.active()?; - let (mut items, mut radii, mut kinds, mut relative) = (Vec::new(), Vec::new(), Vec::new(), Vec::new()); + let (mut items, mut picked, mut radii, mut kinds, mut relative) = (Vec::new(), Vec::new(), Vec::new(), Vec::new(), Vec::new()); for &id in &st.selection.objects { - let Some(lc) = st.doc.node(id).and_then(LiveCorners::of) else { continue }; + let Some(n) = st.doc.node(id) else { continue }; + let Some(lc) = LiveCorners::of(n) else { continue }; let corners = st.selection.partial(id).map_or_else(|| lc.all(), |anchors| lc.corners_of(anchors)); if corners.is_empty() { continue; @@ -339,12 +465,15 @@ pub(crate) fn picked_corners(app: &VectorcraftApp) -> Option { } relative.push(lc.radii_relative()); items.push(json!({"id": id, "corners": corners})); + picked.push((id, corner_places(n, &lc, &corners))); } if items.is_empty() { return None; } + picked.sort_unstable_by_key(|(id, _)| *id); Some(PickedCorners { items, + corners: picked, radius: shared(&radii, |a, b| (a - b).abs() < 1e-9), kind: shared(&kinds, |a, b| a == b), relative: shared(&relative, |a, b| a == b), @@ -374,6 +503,7 @@ pub(crate) fn corners_link(app: &mut VectorcraftApp, ui: &mut Ui) { fn corners_popup(app: &mut VectorcraftApp, ui: &mut Ui) { let Some(picked) = picked_corners(app) else { return }; let lengths = general_lengths(app); + let target = corner_target(app, &picked.corners); let (mut set, mut radius) = (None, None); Stack::new().show(ui, |ui, _| { Field::new(tl!("Corner")).show(ui, |ui, _| { @@ -389,7 +519,7 @@ fn corners_popup(app: &mut VectorcraftApp, ui: &mut Ui) { } }); Field::new(tl!("Radius")).show(ui, |ui, _| { - radius = Some(NumberField::length(tl!("Radius"), Bind::given(picked.radius), lengths).show(ui).edit); + radius = Some(NumberField::length(tl!("Radius"), Bind::given(picked.radius), lengths).edit_target(target).show(ui).edit); }); let rounding = RadioGroup::new(tl!("Rounding"), Bind::given(picked.relative)).show(ui, |ui, _| { Row::new().show(ui, |ui, _| { diff --git a/crates/ui-egui/src/panels/properties/tests.rs b/crates/ui-egui/src/panels/properties/tests.rs index 1fcb9a6f52..3076231e18 100644 --- a/crates/ui-egui/src/panels/properties/tests.rs +++ b/crates/ui-egui/src/panels/properties/tests.rs @@ -1588,6 +1588,443 @@ fn the_corners_pop_up_takes_every_corner_of_a_whole_path_and_mixed_rounding() { assert_eq!(path_corners(&panel.app(), b), vec![(7.0, round); 4], "every corner of the path selected whole"); } +/// Types `3+` into number field `name`, runs `change`, checks that the field then shows `shown`, +/// and types `1` and presses Enter. `case` names the step in a failure. +fn finish_after_change(panel: &mut Panel, case: &str, name: &str, shown: &str, change: impl FnOnce(&mut VectorcraftApp)) { + use craft::ui::contract::Role; + panel.click_role(name, Role::SPIN_BUTTON); + panel.key(Key::A, Modifiers::COMMAND); + panel.type_text("3+"); + change(&mut panel.app()); + panel.frame(); + assert_eq!(panel.value_role(name, Role::SPIN_BUTTON).as_deref(), Some(shown), "{case}: the field shows the new target's radius"); + panel.type_text("1"); + panel.key(Key::Enter, Modifiers::NONE); +} + +/// The one corner the Direct-Selected anchor picks ([`crate::panels::picked_corners`]). +fn picked_corner(app: &VectorcraftApp) -> usize { + let picked = crate::panels::picked_corners(app).unwrap(); + let corners = picked.items[0]["corners"].as_array().unwrap(); + assert_eq!((picked.items.len(), corners.len()), (1, 1), "one corner is picked"); + corners[0].as_u64().unwrap() as usize +} + +/// The radii of live-cornered paths `a` and `b` ([`corner_radii`]). +fn two_corner_radii(panel: &Panel, a: u64, b: u64) -> (Vec, Vec) { + let app = panel.app(); + (corner_radii(&app, a), corner_radii(&app, b)) +} + +/// The radius of each corner of live-cornered path `id`, in document lengths. +fn corner_radii(app: &VectorcraftApp, id: u64) -> Vec { + let n = app.session.active().unwrap().doc.node(vectorcraft_doc::NodeId(id)).unwrap().clone(); + let lc = vectorcraft_doc::LiveCorners::of(&n).unwrap(); + (0..lc.all().len()).map(|k| crate::panels::corner_length(&lc, k)).collect() +} + +/// A corner radius typed and not yet entered is dropped when the selection moves to other corners +/// with the same radius: Enter then sets only what was typed after the change, on the corners now +/// shown. Covers the Corner Radius field of live rectangles and live polygons, each corner's field +/// of a live rectangle, the field of Direct-Selected corners and the Corners pop-up's Radius. +#[test] +fn an_unfinished_corner_radius_does_not_reach_other_corners() { + let polygon = |app: &mut VectorcraftApp, cx: f64| { + app.run("shape.polygon", json!({"cx": cx, "cy": 150, "radius": 40, "sides": 5})).unwrap()["id"].as_u64().unwrap() + }; + let mut app = new_app(); + let (a, b) = (rect(&mut app, 20.0, 20.0, 60.0, 40.0).as_u64().unwrap(), rect(&mut app, 120.0, 20.0, 60.0, 40.0).as_u64().unwrap()); + let (p, q) = (polygon(&mut app, 80.0), polygon(&mut app, 240.0)); + let star = app.run("shape.star", json!({"cx": 200, "cy": 200, "radius1": 80, "radius2": 40})).unwrap()["id"].as_u64().unwrap(); + app.run("select.set", json!({"ids": [a]})).unwrap(); + let mut panel = Panel::new(app, show); + panel.room(egui::vec2(420.0, 920.0)); + + // With the selection unchanged, the value typed so far is kept: 3+1 gives 4. + finish_after_change(&mut panel, "unchanged", "Corner Radius", "3+", |_| {}); + assert_eq!(corner_radii(&panel.app(), a), [4.0; 4], "an unchanged target keeps the typed value"); + panel.app().run("object.setLiveShape", json!({"ids": [a], "radius": 0})).unwrap(); + panel.frame(); + + // A live rectangle's Corner Radius. + finish_after_change(&mut panel, "rectangle", "Corner Radius", "0 pt", |app| { + app.run("select.set", json!({"ids": [b]})).unwrap(); + }); + assert_eq!(two_corner_radii(&panel, a, b), (vec![0.0; 4], vec![1.0; 4]), "rectangle: b takes 1 pt, not 3+1"); + + // A live polygon's Corner Radius. + panel.app().run("select.set", json!({"ids": [p]})).unwrap(); + panel.frame(); + finish_after_change(&mut panel, "polygon", "Corner Radius", "0 pt", |app| { + app.run("select.set", json!({"ids": [q]})).unwrap(); + }); + assert_eq!(two_corner_radii(&panel, p, q), (vec![0.0; 5], vec![1.0; 5]), "polygon: q takes 1 pt, not 3+1"); + + // Each corner's field of a live rectangle, with Single Corner Radius off. + crate::panels::transform::set_pref(&mut panel.app(), "singleCornerRadius", false); + panel.app().run("object.setLiveShape", json!({"ids": [a, b], "radius": 0})).unwrap(); + panel.app().run("select.set", json!({"ids": [a]})).unwrap(); + panel.frame(); + finish_after_change(&mut panel, "Top Left", "Top Left", "0 pt", |app| { + app.run("select.set", json!({"ids": [b]})).unwrap(); + }); + assert_eq!(two_corner_radii(&panel, a, b), (vec![0.0; 4], vec![1.0, 0.0, 0.0, 0.0]), "Top Left: b takes 1 pt, not 3+1"); + + // Changing the selected rectangles drops the draft while their layer stays targeted. + panel.app().run("object.setLiveShape", json!({"ids": [a, b], "radius": 0})).unwrap(); + panel.app().run("select.set", json!({"ids": [a, b]})).unwrap(); + let layer = panel.app().session.active().unwrap().doc.default_layer().unwrap(); + panel.app().session.active_mut().unwrap().selection.target = Some(layer); + panel.frame(); + finish_after_change(&mut panel, "selected rectangles", "Top Left", "0 pt", |app| { + app.session.active_mut().unwrap().selection.objects.retain(|id| id.0 == b); + }); + assert_eq!(two_corner_radii(&panel, a, b), (vec![0.0; 4], vec![1.0, 0.0, 0.0, 0.0]), "selected rectangles: b takes 1 pt, not 3+1"); + panel.app().session.active_mut().unwrap().selection.target = None; + crate::panels::transform::set_pref(&mut panel.app(), "singleCornerRadius", true); + + // Direct-Selected corners: another corner of the same path, so the selected objects stay the same. + panel.app().select_tool("directSelection"); + panel.app().run("select.anchors", json!({"id": star, "anchors": [[0, 2]], "mode": "set"})).unwrap(); + panel.frame(); + let before = picked_corner(&panel.app()); + finish_after_change(&mut panel, "Direct-Selected", "Corner Radius", "0 pt", |app| { + app.run("select.anchors", json!({"id": star, "anchors": [[0, 4]], "mode": "set"})).unwrap(); + }); + let after = picked_corner(&panel.app()); + let radii = corner_radii(&panel.app(), star); + assert_eq!((radii[before], radii[after]), (0.0, 1.0), "Direct-Selected: the second corner takes 1 pt, not 3+1"); + + // The Corners pop-up's Radius. + panel.app().run("select.anchors", json!({"id": star, "anchors": [[0, 6]], "mode": "set"})).unwrap(); + panel.frame(); + let before = picked_corner(&panel.app()); + panel.click("Corners"); + finish_after_change(&mut panel, "pop-up", "Radius", "0 pt", |app| { + app.run("select.anchors", json!({"id": star, "anchors": [[0, 8]], "mode": "set"})).unwrap(); + }); + let after = picked_corner(&panel.app()); + let radii = corner_radii(&panel.app(), star); + assert_eq!((radii[before], radii[after]), (0.0, 1.0), "pop-up: the second corner takes 1 pt, not 3+1"); +} + +/// Rounding a Direct-Selected sharp corner gives it new anchors, and the corner fields' edit target +/// stays the same, so a scrub that rounds the corner goes on editing it. +#[test] +fn rounding_a_direct_selected_corner_keeps_the_corner_fields_target() { + let mut app = new_app(); + let star = app.run("shape.star", json!({"cx": 200, "cy": 150, "radius1": 80, "radius2": 40})).unwrap()["id"].as_u64().unwrap(); + app.select_tool("directSelection"); + app.run("select.anchors", json!({"id": star, "anchors": [[0, 2]], "mode": "set"})).unwrap(); + let anchors = app.session.active().unwrap().selection.anchors.clone(); + let target = |app: &VectorcraftApp| { + let picked = crate::panels::picked_corners(app).unwrap(); + crate::panels::corner_target(app, &picked.corners) + }; + let before = target(&app); + let picked = crate::panels::picked_corners(&app).unwrap(); + crate::panels::set_picked_corners(&mut app, &picked, json!({"radius": 5})); + assert_eq!(corner_radii(&app, star)[picked_corner(&app)], 5.0, "the corner is rounded"); + assert_ne!(app.session.active().unwrap().selection.anchors, anchors, "the rounded corner has other anchors"); + assert_eq!(target(&app), before, "the same corner is the target"); +} + +/// Reversing a path drops an unfinished radius when the picked index names a different corner: +/// an expanded rectangle, a path whose first corner and last corner have sides of the same +/// lengths and turn the same way, a crossing path whose corners share a position, a path whose +/// reversed corner has sides in the same directions, a path with two corners at the same point +/// whose sides lean the same ways, and a small subpath beside a very large one. +#[test] +fn an_unfinished_corner_radius_is_dropped_when_the_path_is_reversed() { + let mut app = new_app(); + let expanded = rect(&mut app, 20.0, 20.0, 60.0, 40.0).as_u64().unwrap(); + app.run("select.set", json!({"ids": [expanded]})).unwrap(); + app.run("object.expandShape", json!({})).unwrap(); + let kite = app.run("path.create", json!({"d": "M20 20 L26 12 L36 28 L30 20 Z"})).unwrap()["id"].as_u64().unwrap(); + let crossing = app.run("path.create", json!({"d": "M0 0 L20 20 L40 0 L40 40 L20 20 L0 40"})).unwrap()["id"].as_u64().unwrap(); + let parallel = app.run("path.create", json!({"d": "M0 0 L0 20 L20 20 L40 40 L20 40 L20 20"})).unwrap()["id"].as_u64().unwrap(); + let coincident = app.run("path.create", json!({"d": "M0 0 L20 20 L40 30 L50 40 L20 20 L0 10"})).unwrap()["id"].as_u64().unwrap(); + let small = + app.run("path.create", json!({"d": "M0 0 L0.2 0 L0.2 0.2 L0 0.2 Z M0 0 L1000000 0 L1000000 1000000 Z"})).unwrap()["id"].as_u64().unwrap(); + app.select_tool("directSelection"); + let mut panel = Panel::new(app, show); + panel.room(egui::vec2(420.0, 920.0)); + for (case, id, index) in [ + ("expanded rectangle", expanded, 0), + ("kite", kite, 0), + ("crossing path", crossing, 1), + ("parallel sides", parallel, 1), + ("coincident corners", coincident, 1), + ("small subpath", small, 1), + ] { + // Where the corner at `index` is and the directions its sides leave it in. + let corner = |app: &VectorcraftApp| { + let node = app.session.active().unwrap().doc.node(vectorcraft_doc::NodeId(id)).unwrap(); + let c = *vectorcraft_doc::LiveCorners::of(node).unwrap().corner(index).unwrap(); + (c.at, c.u, c.v) + }; + panel.app().run("select.anchors", json!({"id": id, "anchors": [[0, index]], "mode": "set"})).unwrap(); + panel.frame(); + let before = corner(&panel.app()); + finish_after_change(&mut panel, case, "Corner Radius", "0 pt", |app| { + app.run("path.reverse", json!({"ids": [id]})).unwrap(); + }); + let app = panel.app(); + assert_ne!(corner(&app), before, "{case}: index {index} names a different corner"); + let picked = picked_corner(&app); + assert_eq!(picked, index, "{case}: the picked corner index stays the same"); + let radii = corner_radii(&app, id); + assert_eq!(radii[picked], 1.0, "{case}: the corner now picked takes 1 pt"); + assert!(radii.iter().all(|&radius| radius != 4.0), "{case}: no corner takes the unfinished 3+1 draft"); + } +} + +/// Reversing another subpath keeps an unfinished radius for the picked corner of the first subpath. +#[test] +fn an_unfinished_corner_radius_stays_when_another_subpath_is_reversed() { + let mut app = new_app(); + let id = app.run("path.create", json!({"d": "M0 0 L40 0 L40 30 L0 30 Z M100 100 L140 100 L120 140 Z"})).unwrap()["id"].as_u64().unwrap(); + app.select_tool("directSelection"); + app.run("select.anchors", json!({"id": id, "anchors": [[0, 1]], "mode": "set"})).unwrap(); + let mut panel = Panel::new(app, show); + panel.room(egui::vec2(420.0, 920.0)); + finish_after_change(&mut panel, "second subpath reversed", "Corner Radius", "3+", |app| { + app.run("path.reverse", json!({"ids": [id], "subpath": 1})).unwrap(); + }); + let app = panel.app(); + assert_eq!(picked_corner(&app), 1, "the first subpath's corner stays picked"); + let radii = corner_radii(&app, id); + assert_eq!(radii[1], 4.0, "the picked corner takes 3+1"); + assert!(radii[..4].iter().enumerate().all(|(index, &radius)| index == 1 || radius == 0.0), "the other corners of the first subpath stay sharp"); +} + +/// Moving another subpath's bounds keeps an unfinished radius for the picked corner of the first subpath. +#[test] +fn an_unfinished_corner_radius_stays_when_another_subpath_moves_the_bounds() { + let mut app = new_app(); + let id = app.run("path.create", json!({"d": "M0 0 L40 0 L40 30 L0 30 Z M100 100 L140 100 L120 140 Z"})).unwrap()["id"].as_u64().unwrap(); + app.select_tool("directSelection"); + app.run("select.anchors", json!({"id": id, "anchors": [[0, 1]], "mode": "set"})).unwrap(); + let mut panel = Panel::new(app, show); + panel.room(egui::vec2(420.0, 920.0)); + finish_after_change(&mut panel, "second subpath moves the bounds", "Corner Radius", "3+", |app| { + app.run("path.moveAnchor", json!({"id": id, "subpath": 1, "anchor": 0, "x": -200, "y": -200})).unwrap(); + }); + let app = panel.app(); + assert_eq!(picked_corner(&app), 1, "the first subpath's corner stays picked"); + let radii = corner_radii(&app, id); + assert_eq!(radii[1], 4.0, "the picked corner takes 3+1"); + assert!(radii[..4].iter().enumerate().all(|(index, &radius)| index == 1 || radius == 0.0), "the other corners of the first subpath stay sharp"); +} + +/// Moving, rotating or scaling a live shape keeps an unfinished Corner Radius: Enter gives 3+1. +/// Moving a path keeps its corner target, and scaling a path drops it. +#[test] +fn an_unfinished_corner_radius_stays_while_the_shape_moves_rotates_or_scales() { + let mut app = new_app(); + let p = app.run("shape.polygon", json!({"cx": 100, "cy": 100, "radius": 40, "sides": 5})).unwrap()["id"].as_u64().unwrap(); + let r = rect(&mut app, 200.0, 20.0, 60.0, 40.0).as_u64().unwrap(); + let path = square_path(&mut app, 300.0); + let slanted = app.run("path.create", json!({"d": "M300 50 L360 70 L340 130 L290 110 Z"})).unwrap()["id"].as_u64().unwrap(); + let mut panel = Panel::new(app, show); + panel.room(egui::vec2(420.0, 920.0)); + let cases: [(&str, u64, usize, &str, Value); 4] = [ + ("polygon moved", p, 5, "object.move", json!({"dx": 12.25, "dy": -5})), + ("polygon rotated", p, 5, "object.rotate", json!({"angle": 30})), + ("rectangle scaled", r, 4, "object.scale", json!({"sx": 200, "sy": 100})), + ("rectangle rotated", r, 4, "object.rotate", json!({"angle": 30})), + ]; + for (case, id, corners, command, params) in cases { + panel.app().run("select.set", json!({"ids": [id]})).unwrap(); + panel.frame(); + finish_after_change(&mut panel, case, "Corner Radius", "3+", |app| { + app.run(command, params).unwrap(); + }); + assert_eq!(corner_radii(&panel.app(), id), vec![4.0; corners], "{case}: every corner takes 3+1"); + panel.app().run("object.setLiveShape", json!({"ids": [id], "radius": 0})).unwrap(); + panel.frame(); + } + // Moving a path keeps its Direct-Selected corner target; scaling the path drops it. + let mut app = panel.app(); + for path in [path, slanted] { + let target = |app: &mut VectorcraftApp| { + app.select_tool("directSelection"); + app.run("select.anchors", json!({"id": path, "anchors": [[0, 0]], "mode": "set"})).unwrap(); + let picked = crate::panels::picked_corners(app).unwrap(); + crate::panels::corner_target(app, &picked.corners) + }; + let before = target(&mut app); + let case = "path moved"; + app.select_tool("selection"); + app.run("select.set", json!({"ids": [path]})).unwrap(); + app.run("object.move", json!({"dx": 12.25, "dy": -5})).unwrap(); + assert_eq!(target(&mut app), before, "{case}: the same corner is the target"); + for (case, params) in [("path scaled", json!({"sx": 200, "sy": 200})), ("path scaled unevenly", json!({"sx": 200, "sy": 100}))] { + let before = target(&mut app); + app.select_tool("selection"); + app.run("select.set", json!({"ids": [path]})).unwrap(); + app.run("object.scale", params).unwrap(); + assert_ne!(target(&mut app), before, "{case}: scaling the path drops the value"); + } + } +} + +/// A Direct-Selected live star corner keeps an unfinished radius while the star rotates. +#[test] +fn an_unfinished_corner_radius_stays_while_a_star_rotates() { + let mut app = new_app(); + let id = app.run("shape.star", json!({"cx": 100, "cy": 100, "radius1": 40, "radius2": 20, "points": 5})).unwrap()["id"].as_u64().unwrap(); + app.select_tool("directSelection"); + app.run("select.anchors", json!({"id": id, "anchors": [[0, 0]], "mode": "set"})).unwrap(); + let mut panel = Panel::new(app, show); + panel.room(egui::vec2(420.0, 920.0)); + panel.frame(); + finish_after_change(&mut panel, "star rotated", "Corner Radius", "3+", |app| { + app.select_tool("selection"); + app.run("select.set", json!({"ids": [id]})).unwrap(); + app.run("object.rotate", json!({"angle": 30})).unwrap(); + app.select_tool("directSelection"); + app.run("select.anchors", json!({"id": id, "anchors": [[0, 0]], "mode": "set"})).unwrap(); + }); + let app = panel.app(); + let picked = picked_corner(&app); + assert_eq!(picked, 0, "the same star corner stays selected"); + assert_eq!(corner_radii(&app, id)[picked], 4.0, "the picked corner takes 3+1"); +} + +/// Selecting the same rectangles in another order keeps an unfinished radius for all their corners. +#[test] +fn an_unfinished_corner_radius_stays_when_the_same_objects_are_selected_in_another_order() { + let mut app = new_app(); + let a = rect(&mut app, 20.0, 20.0, 60.0, 40.0).as_u64().unwrap(); + let b = rect(&mut app, 200.0, 20.0, 60.0, 40.0).as_u64().unwrap(); + app.run("select.set", json!({"ids": [a, b]})).unwrap(); + let mut panel = Panel::new(app, show); + panel.room(egui::vec2(420.0, 920.0)); + finish_after_change(&mut panel, "selection order", "Corner Radius", "3+", |app| { + app.run("select.set", json!({"ids": [b, a]})).unwrap(); + }); + assert_eq!(corner_radii(&panel.app(), a), [4.0; 4], "every corner of the first rectangle takes 3+1"); + assert_eq!(corner_radii(&panel.app(), b), [4.0; 4], "every corner of the second rectangle takes 3+1"); +} + +/// A live polygon's Corner Radius typed and not yet entered is dropped when the polygon gains a +/// side, since Enter would then set a corner the value was not typed for. +#[test] +fn an_unfinished_corner_radius_is_dropped_when_a_polygon_gains_a_side() { + let mut app = new_app(); + let p = app.run("shape.polygon", json!({"cx": 100, "cy": 100, "radius": 40, "sides": 5})).unwrap()["id"].as_u64().unwrap(); + app.run("select.set", json!({"ids": [p]})).unwrap(); + let mut panel = Panel::new(app, show); + panel.room(egui::vec2(420.0, 920.0)); + finish_after_change(&mut panel, "sides", "Corner Radius", "0 pt", |app| { + app.run("object.setLiveShape", json!({"ids": [p], "sides": 6})).unwrap(); + }); + assert_eq!(corner_radii(&panel.app(), p), [1.0; 6], "every corner of the six takes 1 pt, not 3+1"); + + let p = panel.app().run("shape.polygon", json!({"cx": 200, "cy": 100, "radius": 40, "sides": 5})).unwrap()["id"].as_u64().unwrap(); + panel.app().select_tool("directSelection"); + panel.app().run("select.anchors", json!({"id": p, "anchors": [[0, 2]], "mode": "set"})).unwrap(); + panel.frame(); + finish_after_change(&mut panel, "Direct-Selected vertex", "Corner Radius", "0 pt", |app| { + app.run("object.setLiveShape", json!({"ids": [p], "sides": 6})).unwrap(); + app.run("select.anchors", json!({"id": p, "anchors": [[0, 2]], "mode": "set"})).unwrap(); + }); + assert_eq!(picked_corner(&panel.app()), 2, "the same anchor index stays selected"); + assert!(corner_radii(&panel.app(), p).iter().all(|&radius| radius != 4.0), "no corner takes the unfinished 3+1 draft"); +} + +/// A live shape's Corner Radius typed and not yet entered is dropped when Direct Selection moves +/// to another corner of the same shape: the selected objects stay the same, and the corner the +/// field sets changes. +#[test] +fn an_unfinished_live_shape_corner_radius_is_dropped_when_another_corner_is_direct_selected() { + let mut app = new_app(); + let a = rect(&mut app, 20.0, 20.0, 60.0, 40.0).as_u64().unwrap(); + let p = app.run("shape.polygon", json!({"cx": 200, "cy": 150, "radius": 40, "sides": 5})).unwrap()["id"].as_u64().unwrap(); + app.select_tool("directSelection"); + app.run("select.anchors", json!({"id": a, "anchors": [[0, 0]], "mode": "set"})).unwrap(); + let mut panel = Panel::new(app, show); + panel.room(egui::vec2(420.0, 920.0)); + panel.frame(); + finish_after_change(&mut panel, "rectangle corner", "Corner Radius", "0 pt", |app| { + app.run("select.anchors", json!({"id": a, "anchors": [[0, 1]], "mode": "set"})).unwrap(); + }); + assert_eq!(corner_radii(&panel.app(), a), [0.0, 1.0, 0.0, 0.0], "rectangle corner: the second corner takes 1 pt, not 3+1"); + + panel.app().run("select.anchors", json!({"id": p, "anchors": [[0, 0]], "mode": "set"})).unwrap(); + panel.frame(); + finish_after_change(&mut panel, "polygon corner", "Corner Radius", "0 pt", |app| { + app.run("select.anchors", json!({"id": p, "anchors": [[0, 2]], "mode": "set"})).unwrap(); + }); + assert_eq!(corner_radii(&panel.app(), p), [0.0, 0.0, 1.0, 0.0, 0.0], "polygon corner: the third corner takes 1 pt, not 3+1"); +} + +/// With Single Corner Radius off, a Top Left radius typed and not yet entered stays while Direct +/// Selection moves to another anchor of the same live rectangle: the field sets the top left +/// corner whichever anchors are selected. +#[test] +fn an_unfinished_rectangle_corner_radius_stays_while_its_anchors_change() { + let mut app = new_app(); + let a = rect(&mut app, 20.0, 20.0, 60.0, 40.0).as_u64().unwrap(); + app.run("select.set", json!({"ids": [a]})).unwrap(); + let mut panel = Panel::new(app, show); + panel.room(egui::vec2(420.0, 920.0)); + crate::panels::transform::set_pref(&mut panel.app(), "singleCornerRadius", false); + panel.app().select_tool("directSelection"); + panel.app().run("select.anchors", json!({"id": a, "anchors": [[0, 0]], "mode": "set"})).unwrap(); + panel.frame(); + finish_after_change(&mut panel, "anchors", "Top Left", "3+", |app| { + app.run("select.anchors", json!({"id": a, "anchors": [[0, 1]], "mode": "set"})).unwrap(); + }); + assert_eq!(corner_radii(&panel.app(), a), [4.0, 0.0, 0.0, 0.0], "the typed value is kept: 3+1 gives 4"); +} + +/// A corner radius typed and not yet entered is dropped when File › Revert replaces the document, +/// even where the same rectangle is selected again with the same radius. +#[test] +fn an_unfinished_corner_radius_is_dropped_by_file_revert() { + let mut app = new_app(); + let id = rect(&mut app, 20.0, 20.0, 60.0, 40.0).as_u64().unwrap(); + let dir = vectorcraft_testkit::TempDir::new("corner-radius-revert"); + app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|e| e.to_string()))); + app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|e| e.to_string()))); + app.run("file.saveAs", json!({"path": dir.join("Corners.vectorcraft")})).unwrap(); + // An unsaved change for Revert to undo, leaving the rectangle as saved. + rect(&mut app, 200.0, 200.0, 20.0, 20.0); + app.run("select.set", json!({"ids": [id]})).unwrap(); + let mut panel = Panel::new(app, show); + panel.room(egui::vec2(420.0, 920.0)); + finish_after_change(&mut panel, "File Revert", "Corner Radius", "0 pt", |app| { + app.run("file.revert", json!({"confirmed": true})).unwrap(); + app.run("select.set", json!({"ids": [id]})).unwrap(); + }); + assert_eq!(corner_radii(&panel.app(), id), [1.0; 4], "Revert drops the unfinished value"); +} + +/// A corner radius typed and not yet entered is dropped when Undo or Redo restores the document, +/// even where the same rectangle is selected again with the same radius. +#[test] +fn an_unfinished_corner_radius_is_dropped_by_undo_and_redo() { + for command in ["edit.undo", "edit.redo"] { + let mut app = new_app(); + let id = rect(&mut app, 20.0, 20.0, 60.0, 40.0).as_u64().unwrap(); + // A step for Undo to take back and Redo to make again, leaving the rectangle as it is. + rect(&mut app, 200.0, 200.0, 20.0, 20.0); + if command == "edit.redo" { + app.run("edit.undo", json!({})).unwrap(); + } + app.run("select.set", json!({"ids": [id]})).unwrap(); + let mut panel = Panel::new(app, show); + panel.room(egui::vec2(420.0, 920.0)); + finish_after_change(&mut panel, command, "Corner Radius", "0 pt", |app| { + app.run(command, json!({})).unwrap(); + app.run("select.set", json!({"ids": [id]})).unwrap(); + }); + assert_eq!(corner_radii(&panel.app(), id), [1.0; 4], "{command} drops the unfinished value"); + } +} + /// One effect on the one selected object shows its name and a Delete icon beside the fx button, /// which removes it in one undo step, the object's own or its stroke's. With several effects the /// icon activates the Appearance panel; without one, or with several objects selected, no name. diff --git a/crates/ui-egui/src/panels/properties/transform_section.rs b/crates/ui-egui/src/panels/properties/transform_section.rs index 042d6165dd..e944167e6d 100644 --- a/crates/ui-egui/src/panels/properties/transform_section.rs +++ b/crates/ui-egui/src/panels/properties/transform_section.rs @@ -116,10 +116,11 @@ pub(super) fn corner_rows(app: &mut VectorcraftApp, ui: &mut Ui) { panels::set_picked_corners(app, &picked, json!({"kind": kind})); } let lengths = crate::canvas::general_lengths(app); + let target = panels::corner_target(app, &picked.corners); let mut edit = None; Field::new(tl!("Corner Radius")).show(ui, |ui, _| { Row::new().show(ui, |ui, _| { - edit = Some(NumberField::length(tl!("Corner Radius"), Bind::given(picked.radius), lengths).show(ui).edit); + edit = Some(NumberField::length(tl!("Corner Radius"), Bind::given(picked.radius), lengths).edit_target(target).show(ui).edit); panels::corners_link(app, ui); }); });