diff --git a/crates/ui-egui/src/dialogs/freehand.rs b/crates/ui-egui/src/dialogs/freehand.rs index 78fdd8bdd1..aa793afef2 100644 --- a/crates/ui-egui/src/dialogs/freehand.rs +++ b/crates/ui-egui/src/dialogs/freehand.rs @@ -280,6 +280,7 @@ fn body(_app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool { variation: (0.0, 100.0), }; varied(ui, d, &roundness, &Variation::ALL); + tip_preview(ui, d, roundness.range.0); size_rows(ui, d, &Variation::ALL); } else { if d.fields.contains_key("angle") { @@ -291,7 +292,7 @@ fn body(_app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool { } } if d.str("tool") == "blobBrush" { - blob_tip_preview(ui, d); + tip_preview(ui, d, 0.0); } // The Eraser's Size rows are above. if !eraser && d.fields.contains_key("sizeMode") { @@ -331,26 +332,26 @@ fn body(_app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool { false } -/// Side of the Blob Brush tip preview's square, in points. +/// Side of the Blob Brush's and the Eraser's tip preview square, in points. const TIP_PREVIEW: f32 = 64.0; -/// Half the length of the Blob Brush tip in its preview, in points. +/// Half the length of the tip in its preview, in points. const TIP_LONG: f32 = 18.0; /// How far from a Roundness dot of the tip preview a press still takes the dot, in points. const TIP_DOT_REACH: f32 = 8.0; -/// The Blob Brush's tip preview: a square with the tip at its Angle and Roundness, a line with an -/// arrowhead along its long axis and a black dot at each end of its short axis, and beside it three -/// dabs of the tip, the middle one darker. A click or a drag that starts on a dot sets the -/// Roundness from the pointer's distance from the center along the short axis -/// ([`blob_tip_roundness_at`]) until the release, inside the square or out of it, and gives the +/// The Blob Brush's and the Eraser's tip preview: a square with the tip at its Angle and +/// Roundness, a line with an arrowhead along its long axis and a black dot at each end of its short +/// axis, and beside it three dabs of the tip, the middle one darker. A click or a drag that starts +/// on a dot sets the Roundness from the pointer's distance from the center along the short axis +/// ([`tip_roundness_at`]) until the release, inside the square or out of it, and gives the /// Roundness slider the keyboard focus. A click or a drag that starts anywhere else in the square /// points the Angle at the pointer and gives the Angle slider the focus. Screen readers get the /// square as the Angle slider and then the Roundness slider, and their keys step the Angle within -/// -180° to 180° and the Roundness within 0% to 100% as a dialog slider's do +/// -180° to 180° and the Roundness within `min_roundness` to 100% as a dialog slider's do /// ([`widgets::stepped_area`]). -fn blob_tip_preview(ui: &mut egui::Ui, d: &mut Dialog) { +fn tip_preview(ui: &mut egui::Ui, d: &mut Dialog, min_roundness: f64) { let t = Tokens::get(ui.ctx()); let (rect, _) = ui.allocate_exact_size(egui::vec2(CONTENT_W, TIP_PREVIEW), egui::Sense::hover()); let square = egui::Rect::from_min_size(rect.min, egui::vec2(TIP_PREVIEW, TIP_PREVIEW)); @@ -359,7 +360,7 @@ fn blob_tip_preview(ui: &mut egui::Ui, d: &mut Dialog) { // Whether the press that started this click or drag was on a dot: it is kept until the release. let on_dot = response.id.with("on-dot"); let near_dot = |p: egui::Pos2, d: &Dialog| { - blob_tip_dots(center, d.f64("angle", 0.0), d.f64("roundness", 100.0)).iter().any(|dot| dot.distance(p) <= TIP_DOT_REACH) + tip_dots(center, d.f64("angle", 0.0), d.f64("roundness", 100.0)).iter().any(|dot| dot.distance(p) <= TIP_DOT_REACH) }; let mut started = ui.data(|m| m.get_temp::(on_dot)); if started.is_none() && response.is_pointer_button_down_on() { @@ -383,7 +384,7 @@ fn blob_tip_preview(ui: &mut egui::Ui, d: &mut Dialog) { if let Some((p, _)) = widgets::pointer_phase(&response) { let delta = p - center; if dot_drag { - d.fields.insert("roundness".into(), json!(blob_tip_roundness_at(delta, d.f64("angle", 0.0)))); + d.fields.insert("roundness".into(), json!(tip_roundness_at(delta, d.f64("angle", 0.0), min_roundness))); } else if delta.length_sq() > 1.0 { d.fields.insert("angle".into(), json!(f64::from((-delta.y).atan2(delta.x)).to_degrees().round())); } @@ -406,13 +407,13 @@ fn blob_tip_preview(ui: &mut egui::Ui, d: &mut Dialog) { if dot_drag && (response.drag_started() || response.clicked()) { widgets::focus_by_pointer(ui, roundness.id); } - let shown = widgets::SliderValue::field(d.f64("roundness", 100.0), 0.0..=100.0, "%", 0); + let shown = widgets::SliderValue::field(d.f64("roundness", 100.0), min_roundness..=100.0, "%", 0); if let Some(r) = widgets::stepped_area(ui, &roundness, tl!("Roundness"), Some(shown)) { d.fields.insert("roundness".into(), json!(r)); } - let (u, v) = blob_tip_axes(d.f64("angle", 0.0)); + let (u, v) = tip_axes(d.f64("angle", 0.0)); let long = TIP_LONG; - let short = long * blob_tip_short_ratio(d.f64("roundness", 100.0)); + let short = long * tip_short_ratio(d.f64("roundness", 100.0)); let dab = |center: egui::Pos2, color: egui::Color32| { let points = (0..32) .map(|i| { @@ -427,7 +428,7 @@ fn blob_tip_preview(ui: &mut egui::Ui, d: &mut Dialog) { let arrowhead = vec![center + u * 28.0, center + u * 22.0 + v * 3.0, center + u * 22.0 - v * 3.0]; painter.add(egui::Shape::convex_polygon(arrowhead, t.accent, egui::Stroke::NONE)); // Black with a white rim, so a dot shows on the tip and on the square in either theme. - for dot in blob_tip_dots(center, d.f64("angle", 0.0), d.f64("roundness", 100.0)) { + for dot in tip_dots(center, d.f64("angle", 0.0), d.f64("roundness", 100.0)) { painter.circle(dot, 3.0, egui::Color32::BLACK, egui::Stroke::new(1.0, egui::Color32::WHITE)); } let strip = square.right() + 2.5 * long; @@ -438,7 +439,7 @@ fn blob_tip_preview(ui: &mut egui::Ui, d: &mut Dialog) { /// The tip preview's long axis on screen at Angle `angle` (degrees, y down: a positive angle tilts /// it up to the right), and its short axis. -fn blob_tip_axes(angle: f64) -> (egui::Vec2, egui::Vec2) { +fn tip_axes(angle: f64) -> (egui::Vec2, egui::Vec2) { let a = angle.clamp(-180.0, 180.0).to_radians(); let u = egui::vec2(a.cos() as f32, -a.sin() as f32); (u, egui::vec2(-u.y, u.x)) @@ -446,23 +447,23 @@ fn blob_tip_axes(angle: f64) -> (egui::Vec2, egui::Vec2) { /// The two Roundness dots of the tip preview centered on `center`: the ends of the tip's short /// axis at Angle `angle` and Roundness `roundness`. -fn blob_tip_dots(center: egui::Pos2, angle: f64, roundness: f64) -> [egui::Pos2; 2] { - let (_, v) = blob_tip_axes(angle); - let short = v * (TIP_LONG * blob_tip_short_ratio(roundness)); +fn tip_dots(center: egui::Pos2, angle: f64, roundness: f64) -> [egui::Pos2; 2] { + let (_, v) = tip_axes(angle); + let short = v * (TIP_LONG * tip_short_ratio(roundness)); [center + short, center - short] } -/// The Roundness, in whole percent from 0 to 100, that a dot of the tip preview dragged to `offset` -/// from the preview's center gives at Angle `angle`: the distance along the tip's short axis, on -/// either side of the center, as a percentage of half the tip's length. -fn blob_tip_roundness_at(offset: egui::Vec2, angle: f64) -> f64 { - let (_, v) = blob_tip_axes(angle); - (f64::from(offset.dot(v).abs() / TIP_LONG) * 100.0).round().clamp(0.0, 100.0) +/// The Roundness, in whole percent from `min` to 100, that a dot of the tip preview dragged to +/// `offset` from the preview's center gives at Angle `angle`: the distance along the tip's short +/// axis, on either side of the center, as a percentage of half the tip's length. +fn tip_roundness_at(offset: egui::Vec2, angle: f64, min: f64) -> f64 { + let (_, v) = tip_axes(angle); + (f64::from(offset.dot(v).abs() / TIP_LONG) * 100.0).round().clamp(min, 100.0) } /// The preview tip's short axis as a fraction of its long axis: Roundness in percent, with 0 -/// percent drawn as 0.1 percent, the minimum the brush draws with. -fn blob_tip_short_ratio(roundness: f64) -> f32 { +/// percent drawn as 0.1 percent, the minimum the Blob Brush draws with. +fn tip_short_ratio(roundness: f64) -> f32 { (roundness.clamp(0.0, 100.0) as f32 / 100.0).max(0.001) } diff --git a/crates/ui-egui/src/dialogs/freehand/tests.rs b/crates/ui-egui/src/dialogs/freehand/tests.rs index 6ec010a8bd..e324bbbd45 100644 --- a/crates/ui-egui/src/dialogs/freehand/tests.rs +++ b/crates/ui-egui/src/dialogs/freehand/tests.rs @@ -50,7 +50,12 @@ fn a_refused_fidelity_keeps_the_dialog_open() { fn the_tip_preview_is_as_flat_as_the_tip_the_brush_draws() { for roundness in [-5.0, 0.0, 0.05, 0.1, 1.0, 50.0, 100.0, 120.0] { let drawn = vectorcraft_tools::draw2::blob_tip(0.0, roundness).as_coeffs()[3] as f32; - assert!((blob_tip_short_ratio(roundness) - drawn).abs() < 1e-6, "roundness {roundness}: drawn {drawn}"); + assert!((tip_short_ratio(roundness) - drawn).abs() < 1e-6, "roundness {roundness}: drawn {drawn}"); + } + // The Eraser's Roundness runs from 1% to 100%, where its tip and the Blob Brush's are the same. + for roundness in [1.0, 2.0, 50.0, 100.0] { + let drawn = vectorcraft_tools::draw2::eraser_tip(0.0, roundness).as_coeffs()[3] as f32; + assert!((tip_short_ratio(roundness) - drawn).abs() < 1e-6, "Eraser roundness {roundness}: drawn {drawn}"); } } @@ -61,23 +66,35 @@ fn the_tip_preview_is_as_flat_as_the_tip_the_brush_draws() { #[test] fn a_roundness_dot_sets_its_distance_along_the_short_axis() { for angle in [0.0, 30.0, 90.0, -135.0, 180.0] { - let (u, v) = blob_tip_axes(angle); + let (u, v) = tip_axes(angle); for (along, want) in [(0.0, 0.0), (4.5, 25.0), (-9.0, 50.0), (18.0, 100.0), (-18.0, 100.0), (40.0, 100.0), (-400.0, 100.0)] { for sideways in [0.0, 7.0, -30.0] { - assert_eq!(blob_tip_roundness_at(v * along + u * sideways, angle), want, "angle {angle}, {along} along, {sideways} sideways"); + assert_eq!(tip_roundness_at(v * along + u * sideways, angle, 0.0), want, "angle {angle}, {along} along, {sideways} sideways"); } } let center = egui::pos2(32.0, 32.0); for roundness in [0.0, 1.0, 37.0, 50.0, 99.0, 100.0] { - let [a, b] = blob_tip_dots(center, angle, roundness); + let [a, b] = tip_dots(center, angle, roundness); assert!(((a - center) + (b - center)).length() < 1e-4, "the dots face each other across the center"); for dot in [a, b] { - assert_eq!(blob_tip_roundness_at(dot - center, angle), roundness, "angle {angle}"); + assert_eq!(tip_roundness_at(dot - center, angle, 0.0), roundness, "angle {angle}"); } } } } +/// With the Eraser's 1% minimum, a Roundness dot dragged to the center or near it gives 1%, and +/// any farther distance gives the same Roundness as with no minimum. +#[test] +fn a_roundness_dot_keeps_the_eraser_at_one_percent_or_more() { + for angle in [0.0, 45.0, -90.0] { + let (u, v) = tip_axes(angle); + for (along, want) in [(0.0, 1.0), (0.05, 1.0), (-0.1, 1.0), (0.18, 1.0), (0.36, 2.0), (-9.0, 50.0), (30.0, 100.0)] { + assert_eq!(tip_roundness_at(v * along + u * 5.0, angle, 1.0), want, "angle {angle}, {along} along"); + } + } +} + /// The labels the open dialog paints. fn painted(app: &mut VectorcraftApp) -> String { crate::tests_labels::painted_text(app, |app, ui| super::super::show(app, ui.ctx())) diff --git a/crates/ui-egui/src/dialogs/tests.rs b/crates/ui-egui/src/dialogs/tests.rs index 5cd84a8ae3..de651d45cb 100644 --- a/crates/ui-egui/src/dialogs/tests.rs +++ b/crates/ui-egui/src/dialogs/tests.rs @@ -2965,6 +2965,62 @@ fn a_drag_on_a_blob_brush_tip_dot_sets_the_roundness() { assert_eq!(values(&app), (46.0, 1.0), "Tab goes on to the Roundness slider"); } +/// The Eraser Tool Options show the same tip preview as the Blob Brush, with the Angle and +/// Roundness sliders for screen readers. A drag on the arrowhead sets only the Angle, and a drag on +/// a black dot sets only the Roundness, which stays within the Eraser's 1% to 100% for a drag and +/// for the keys. The fields show the new values, OK sets the tool's options to them, and Cancel +/// leaves the options as they were. +#[test] +fn the_eraser_tip_preview_sets_the_angle_and_roundness_by_dragging() { + use egui::Key::{ArrowDown, Home}; + use egui::Modifiers as M; + let mut app = app(); + app.run("tool.options", json!({"tool": "eraser"})).unwrap(); + let values = |app: &VectorcraftApp| { + let d = app.ui.dialog.as_ref().expect("Eraser Tool Options is open"); + (d.f64("angle", f64::NAN), d.f64("roundness", f64::NAN)) + }; + let mut f = Frames::new(); + f.run(&mut app, vec![]); + let out = f.run(&mut app, vec![]); + let (_, value, text) = slider_node(&out, "Angle"); + assert_eq!((value, text.as_deref()), (Some(0.0), Some("0°"))); + let (_, value, text) = slider_node(&out, "Roundness"); + assert_eq!((value, text.as_deref()), (Some(100.0), Some("100%"))); + let center = f.control(&mut app, "Angle", egui::accesskit::Role::Slider); + // At 0° the arrowhead points right, 22 pt to 28 pt from the center. + f.drag(&mut app, center + egui::vec2(25.0, 0.0), egui::vec2(-5.0, -20.0)); + assert_eq!(values(&app), (45.0, 100.0), "the arrowhead dragged up to the right"); + // At 45° and 100% the dots are 18 pt from the center, up to the left and down to the right. + let v = egui::vec2(1.0, 1.0).normalized(); + f.drag(&mut app, center + v * 18.0, -v * 9.0); + assert_eq!(values(&app), (45.0, 50.0), "the dot dragged half way to the center"); + f.drag(&mut app, center + v * 9.0, -v * 9.0); + assert_eq!(values(&app), (45.0, 1.0), "the dot dragged to the center stops at 1%"); + f.key(&mut app, ArrowDown, M::NONE); + f.key(&mut app, Home, M::NONE); + assert_eq!(values(&app), (45.0, 1.0), "the keys stop at 1%"); + let out = f.run(&mut app, vec![]); + assert_eq!(slider_node(&out, "Roundness").2.as_deref(), Some("1%")); + let d = app.ui.dialog.as_mut().expect("Eraser Tool Options is open"); + d.fields.insert("roundness".into(), json!(30)); + let out = f.run(&mut app, vec![]); + assert_eq!(slider_node(&out, "Roundness").2.as_deref(), Some("30%"), "the preview follows the field"); + confirm(&mut app).unwrap(); + let o = app.session.tool_options_of("eraser"); + assert_eq!((o["angle"].as_f64(), o["roundness"].as_f64()), (Some(45.0), Some(30.0))); + app.run("tool.options", json!({"tool": "eraser"})).unwrap(); + let mut f = Frames::new(); + f.run(&mut app, vec![]); + f.run(&mut app, vec![]); + let center = f.control(&mut app, "Angle", egui::accesskit::Role::Slider); + f.drag(&mut app, center + egui::vec2(18.0, 18.0), egui::vec2(2.0, 2.0)); + assert_eq!(values(&app), (-45.0, 30.0), "the drag points the Angle down to the right"); + app.ui.dialog = None; + let o = app.session.tool_options_of("eraser"); + assert_eq!((o["angle"].as_f64(), o["roundness"].as_f64()), (Some(45.0), Some(30.0)), "Cancel keeps the options"); +} + /// Screen readers can set the Blob Brush tip preview's Angle to a number: the slider offers the /// set-value action, a fraction rounds to the whole degrees the field shows, a number past -180° to /// 180° sets the nearer end, the last request in a frame wins, and a value that is not a finite diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index 71889c86e9..d192e3a99e 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1352,6 +1352,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur `path.eraseRegion` refuses an Angle outside −180° to 180° and a Roundness outside 1% to 100% with an error and leaves the document unchanged; a key left out or null takes its default (0° and 100%). The help page gives no range for the two options, so the ranges are VectorCraft's own values. `path.split` refuses a `t` outside 0 to 1 in the same way. `path.eraseSegments` (the Path Eraser) refuses a `width` outside 0.001 to 1000 pt with an error and leaves the document unchanged; left out or null, the width is 8 pt. The help page gives the Path Eraser no width option, so the range is VectorCraft's own value. Eraser Tool Options gives Angle, Roundness and Size each a menu of Fixed, Random, Pressure, Tilt, Bearing and Rotation with a Variation field, as the same page describes: the tip varies by up to the Variation either way, Random once per drag and the others with what the pen reads at each point of the drag, and the tip outline at the pointer follows the pen while it erases. The outline of the area a drag shows takes, at each step, the tip the pen's readings give halfway along it, with a Random value shown as the value itself, and a mouse drag erases at the set values (`path.eraseRegion` `angleMode`, `roundnessMode`, `sizeMode` and their variations). + Eraser Tool Options show the Blob Brush's tip preview under the Roundness rows. A drag on the arrowhead turns the Angle, and a drag that starts on a black dot moves it toward the center for a flatter tip or away from it for a rounder one, as the Eraser tool options section of the [help page](https://web.archive.org/web/20210601162814/https://helpx.adobe.com/pdf/illustrator_reference.pdf#page=133) describes; the fields show the new values, and OK keeps them. The drag's mapping and the Angle and Roundness sliders are the Blob Brush preview's, which are VectorCraft's own, with the Eraser's Roundness range of 1% to 100% for the drag and the keys. The preview shows the set Angle and Roundness, not a Random or pen-driven variation of them. Regression tests: `dialogs::tests::the_eraser_tip_preview_sets_the_angle_and_roundness_by_dragging`, `dialogs::freehand::tests::a_roundness_dot_keeps_the_eraser_at_one_percent_or_more`. The outline a Blob Brush or Eraser drag shows is built a bounded amount per frame, so a long stroke does not slow the canvas: each frame takes at most 256 new points of the drag and outlines again only the pieces of up to 32 segments they change, and the parts of a piece's outline inside the rest of the area are removed a limited amount at a time. While work is left, the canvas draws again with the pointer still, the points not outlined yet show as a thin line, and the tip shows at the pointer. When the tip varies with the pen (Size set to Pressure, or an Eraser mode that follows the pen), each step is its own piece and the outline also shows only the edge of the whole area. The release sends the whole stroke as before (`path.blob`, `path.eraseRegion`). The settled part of that outline is kept in paths of up to 64 pieces each. A path is shared from one frame to the next until a piece in it changes, and the canvas keeps the triangles it drew the path with while the view stays the same, so a frame does not copy, flatten or tessellate a settled path again; a pan, zoom or rotation draws each one once more. This is VectorCraft's own rule. The line that a drag of more than 64 segments shows with a round Eraser tip, the Knife, the Path Eraser or the Pencil with Live preview off is kept the same way, in pieces of 64 segments: a frame builds again only the last piece, and the Knife's dashes run on from one piece to the next. With the Eraser's Size set to a mode that follows the pen, each step of such a drag is kept as its own path and a frame builds only a new step. A smoothing drag's band is kept until it changes. The canvas still copies the kept triangles of every piece into each frame. - **Transform:** Rotate, Reflect, Scale, Shear (click or Alt-click snaps the reference point to anchors and centres; Alt-click opens the tool's dialog, and a drag with Alt down at the release, from the press or pressed mid-drag, transforms a copy), Reshape, Free Transform (free and perspective distort and shear, switched by the keys held during a handle drag).