From 2c877b9f9db7ac534d6052a427ef7f3cbd2729ec Mon Sep 17 00:00:00 2001 From: n0tfr3shm1lk Date: Sun, 18 Oct 2026 16:59:55 +0200 Subject: [PATCH] sketch: live preview for fillet and chamfer corners (#2044) Thanks @n0tfr3shm1lk. --- crates/ui-egui/src/dialogs.rs | 10 +++++ crates/ui-egui/src/preview.rs | 14 ++++++- crates/ui-egui/src/sketch_corner_ui_tests.rs | 40 ++++++++++++++++++++ crates/ui-egui/src/sketch_modify_ui.rs | 2 +- 4 files changed, 63 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index 67b270056..55b6712bf 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -1864,6 +1864,11 @@ fn error_rows(app: &mut SolveApp, ctx: &egui::Context, d: &mut Dialog, ui: &mut { d.refuse(app, error); } + // For Corner dialogs (fillet/chamfer), propagate preview errors to `d.error` + // so impossible sizes still show a refusal message and tests can check it. + if matches!(d.kind, Kind::SketchModify(crate::sketch_modify_ui::Mod::Corner { .. })) && app.preview.error.is_some() && d.error.is_none() { + d.error = app.preview.error.clone(); + } if let Some(error) = d.error.as_ref().or(app.preview.error.as_ref()).cloned() { if !full_width { row_label(ui, ""); @@ -2208,6 +2213,11 @@ pub fn show(app: &mut SolveApp, ctx: &egui::Context) { let has_ok = !matches!(d.kind, Kind::Sketch | Kind::Params { .. } | Kind::Measure { .. }); let valid = match &d.kind { Kind::SaveMesh(_) => crate::dialogs_save_mesh::ready(&d.inputs), + Kind::SketchModify(crate::sketch_modify_ui::Mod::Corner { .. }) => { + // Corner dialogs: preview errors are visual only; validation errors from + // `apply_commands` go to `d.error`. OK stays enabled when the preview fails. + !has_ok || (built.unwrap_or_else(|| apply_commands(app, &d).is_ok())) + } _ => !has_ok || !d.previews() || (built.unwrap_or_else(|| apply_commands(app, &d).is_ok()) && app.preview.error.is_none()), }; let width = if wide { crate::frame::Width::Wide } else { crate::frame::Width::Normal }; diff --git a/crates/ui-egui/src/preview.rs b/crates/ui-egui/src/preview.rs index 07098fa2f..355d760e8 100644 --- a/crates/ui-egui/src/preview.rs +++ b/crates/ui-egui/src/preview.rs @@ -351,8 +351,18 @@ fn spawn(pv: &mut PreviewState, key: u64, scratch: Session, cmds: Vec<(String, V /// kernel can't do yet), the tool body is still shown, with the error. pub fn compute(s: &Session, cmds: &[(String, Value)], colors: Colors) -> Job { if cmds.iter().any(|(command, params)| { - matches!(command.as_str(), "sketch.redefine" | "sketch.mirror" | "sketch.pattern.rectangular" | "sketch.pattern.circular" | "sketch.offset") - || command == "construct.plane.along_path" + matches!( + command.as_str(), + "sketch.redefine" + | "sketch.mirror" + | "sketch.pattern.rectangular" + | "sketch.pattern.circular" + | "sketch.offset" + | "sketch.fillet" + | "sketch.chamfer.equal_distance" + | "sketch.chamfer.two_distance" + | "sketch.chamfer.distance_angle" + ) || command == "construct.plane.along_path" || params.get("command").and_then(Value::as_str) == Some("construct.plane.along_path") }) { let preview = s.preview_visible_world(cmds).map_err(|error| error.to_string())?; diff --git a/crates/ui-egui/src/sketch_corner_ui_tests.rs b/crates/ui-egui/src/sketch_corner_ui_tests.rs index d5359c9b6..c80848f1d 100644 --- a/crates/ui-egui/src/sketch_corner_ui_tests.rs +++ b/crates/ui-egui/src/sketch_corner_ui_tests.rs @@ -475,3 +475,43 @@ fn chamfer_window_cuts_the_corner_between_two_picked_lines() { assert_eq!(sk.curves.len(), before + 1, "a chamfer line: {:?}", h.app.status); assert!(sk.curve_index("left").is_some(), "the third line stays"); } + +/// Corner previews draw new curves when a corner is picked, just like Mirror and Offset do. +/// This tests that `previews()` for Corner produces sketch preview curves visible in the viewport. +#[test] +fn fillet_chamfer_corner_preview_draws_curves() { + let mut h = crate::scenario::Harness::new(); + // Create a simple L-shape with two lines meeting at (0, 10). + h.app.run("sketch.create", json!({"plane":"XY"})).unwrap(); + h.app.run("sketch.line", json!({"points": [[-30,10],[0,10]], "ids": ["top"]})).unwrap(); + h.app.run("sketch.line", json!({"points": ["top.end",[0,-20]], "ids": ["right"]})).unwrap(); + h.frames(2); + + // Open the fillet tool and pick the corner at (0, 10). + h.step(&json!({"start":"sketch.fillet"})).unwrap(); + h.frames(2); + h.step(&json!({"click":{"world":[0.0,10.0,0.0]}})).unwrap(); + h.frames(3); + + // The preview should have drawn new curves (fillet arcs at picked corners). + h.step(&json!({"until":{"count":{"name":"sketch_preview_curves","value":1}}})).unwrap(); + + // Verify the sketch itself hasn't been modified yet. + let sid = h.app.session.active_sketch.unwrap(); + let sk = h.app.session.world_state().sketch(sid).unwrap().sketch.clone(); + assert_eq!(sk.curves.len(), 2, "fillet preview must not modify the sketch"); + + // Now test chamfer too. Close the fillet dialog first. + h.step(&json!({"key":"Escape"})).unwrap(); + h.frames(3); + h.step(&json!({"start":"sketch.chamfer.equal_distance"})).unwrap(); + h.frames(2); + h.step(&json!({"click":{"world":[0.0,10.0,0.0]}})).unwrap(); + h.frames(3); + + // Chamfer preview should also produce curves. + h.step(&json!({"until":{"count":{"name":"sketch_preview_curves","value":1}}})).unwrap(); + + let sk = h.app.session.world_state().sketch(sid).unwrap().sketch.clone(); + assert_eq!(sk.curves.len(), 2, "chamfer preview must not modify the sketch"); +} diff --git a/crates/ui-egui/src/sketch_modify_ui.rs b/crates/ui-egui/src/sketch_modify_ui.rs index 9d81ae570..a48324915 100644 --- a/crates/ui-egui/src/sketch_modify_ui.rs +++ b/crates/ui-egui/src/sketch_modify_ui.rs @@ -102,7 +102,7 @@ pub enum Mod { impl Mod { pub fn previews(&self) -> bool { - matches!(self, Self::Mirror | Self::PatternRect { .. } | Self::PatternCirc { .. } | Self::Offset { .. }) + matches!(self, Self::Mirror | Self::PatternRect { .. } | Self::PatternCirc { .. } | Self::Offset { .. } | Self::Corner { .. }) } pub fn title(&self) -> &'static str {