trim/extend: Shift toggles mode with live preview (partial #3569)

Holding Shift during TRIM/EXTEND temporarily switches to the opposite command (standard CAD behavior). Session.shift_down is written by the UI and TrimM uses extend XOR shift_down for execution, prompt, Fence/Crossing preview and the single-entity rollover preview. The UI repaints and the preview cache key includes shift/ctrl, so a modifier-only change refreshes the preview.

BLOCK/INSERT geometry is still ignored as cutting/boundary edges in TRIM/EXTEND; that remains for a follow-up.
This commit is contained in:
kc8a
2026-11-30 18:32:57 +00:00
parent a3ed0682ab
commit 544dea24d1
6 changed files with 113 additions and 10 deletions
+24 -9
View File
@@ -397,7 +397,8 @@ impl TrimM {
let e = d.entity(h)?;
// Half a pixel: finer than the eye can tell apart, coarse enough to stay quick.
let tol = (s.pixel_size() / 2.0).max(1e-9);
let part = if self.extend {
let extending = self.extend ^ s.shift_down;
let part = if extending {
let grown = Entity { kind: extended(s, h, cursor, cut).ok()?, ..(**e).clone() };
uncovered(d, &grown, std::slice::from_ref(&e.kind), tol)
} else {
@@ -414,7 +415,8 @@ impl TrimM {
Edges { only: self.edges.as_deref(), implied: !quick && implied }
}
fn select_edges(&mut self, s: &mut Session) {
s.echo(if self.extend { "Select boundary edges ..." } else { "Select cutting edges ..." });
let extending = self.extend ^ s.shift_down;
s.echo(if extending { "Select boundary edges ..." } else { "Select cutting edges ..." });
s.set_selection(Vec::new());
self.stage = Stage::Edges(Vec::new());
}
@@ -503,16 +505,27 @@ impl Interactive for TrimM {
fn prompt(&self, s: &Session) -> Prompt {
match &self.stage {
Stage::Pick => {
let (msg, first) = if self.extend {
("Select object to extend or shift-select to trim", "Boundary edges")
let extending = self.extend ^ s.shift_down;
let (msg, first) = if extending {
let msg = if s.shift_down {
"Select object to extend or release Shift to trim"
} else {
"Select object to extend or shift-select to trim"
};
(msg, "Boundary edges")
} else {
("Select object to trim or shift-select to extend", "cuTting edges")
let msg = if s.shift_down {
"Select object to trim or release Shift to extend"
} else {
"Select object to trim or shift-select to extend"
};
(msg, "cuTting edges")
};
let mut kw = vec![first, "Fence", "Crossing", "mOde", "Project"];
if !quick_mode(s) {
kw.push("Edge");
}
if !self.extend {
if !extending {
kw.push("eRase");
}
kw.push("Undo");
@@ -528,7 +541,8 @@ impl Interactive for TrimM {
Stage::Crossing(None) => Prompt::new("Specify first corner", Accept::POINT),
Stage::Crossing(Some(_)) => Prompt::new("Specify opposite corner", Accept::POINT),
Stage::Mode => {
let msg = if self.extend { "Enter an extend mode option" } else { "Enter a trim mode option" };
let extending = self.extend ^ s.shift_down;
let msg = if extending { "Enter an extend mode option" } else { "Enter a trim mode option" };
Prompt::new(msg, KW).kw(&["Quick", "Standard"]).default(if quick_mode(s) { "Quick" } else { "Standard" })
}
Stage::Project => {
@@ -551,7 +565,7 @@ impl Interactive for TrimM {
self.cut.take();
let quick = quick_mode(s);
let implied = edge_mode(s);
let extending = self.extend;
let extending = self.extend ^ s.shift_down;
match self.stage.clone() {
Stage::Mode | Stage::Project | Stage::Edge => self.option(s, i),
Stage::Edges(mut picked) => {
@@ -719,7 +733,8 @@ impl Interactive for TrimM {
return None;
}
let px = (s.pixel_size() / 2.0).max(1e-9);
Some(picks_preview(s, &work, self.extend, self.edges(quick, implied), quick, tol, px))
let extending = self.extend ^ s.shift_down;
Some(picks_preview(s, &work, extending, self.edges(quick, implied), quick, tol, px))
}
fn preview(&self, _s: &Session, c: Vec2) -> Vec<EntityKind> {
match &self.stage {
+4
View File
@@ -154,6 +154,9 @@ pub struct Session {
/// change behavior on Ctrl — currently only ARC's "hold Ctrl to switch direction" — read
/// this instead of receiving modifiers through `Input`, which stays free of UI state.
pub ctrl_down: bool,
/// Whether Shift is held right now (from the UI, overwritten every frame). TRIM/EXTEND read
/// this to temporarily toggle the active mode, as in AutoCAD.
pub shift_down: bool,
/// Origin of the point currently being fed by `input_snapped`; never reused by typed points.
picked_viewport: Option<snap::ViewportPoint>,
/// The last drawing comparison (COMPARE), drawn over the drawing it was made for.
@@ -236,6 +239,7 @@ impl Session {
cursor: Vec2::ZERO,
cursor_deferred: None,
ctrl_down: false,
shift_down: false,
picked_viewport: None,
compare: None,
tracking: snap::tracking::Tracker::default(),
+37
View File
@@ -310,3 +310,40 @@ fn a_fence_across_more_objects_than_the_preview_cap_shows_no_part() {
s.input(Input::Enter).unwrap();
assert!((before - drawn(&s) - (CAP + 1) as f64 * 10.0).abs() < 1e-6, "every rung lost its middle");
}
#[test]
fn trim_shift_shows_the_opposite_piece_and_applies_it() {
let mut s = Session::new();
line(&mut s, [4.0, -5.0], [4.0, 5.0]);
line(&mut s, [10.0, -5.0], [10.0, 5.0]);
let l = line(&mut s, [0.0, 0.0], [8.0, 0.0]);
s.zoom_extents().unwrap();
s.start("trim").unwrap();
assert_eq!(ends(&s, l, [6.0, 0.0]), Some(vec![seg([4.0, 0.0], [8.0, 0.0])]), "shift up: the piece a click removes");
s.shift_down = true;
assert_eq!(ends(&s, l, [6.0, 0.0]), Some(vec![seg([8.0, 0.0], [10.0, 0.0])]), "shift down: the opposite (extend) preview");
s.shift_down = false;
assert_eq!(ends(&s, l, [6.0, 0.0]), Some(vec![seg([4.0, 0.0], [8.0, 0.0])]), "shift released: back to the trim preview");
s.shift_down = true;
s.input(Input::Point(Vec2::new(6.0, 0.0))).unwrap();
let EntityKind::Line(x) = &s.doc().unwrap().entity(l).unwrap().kind else { panic!() };
assert_eq!((x.b.x, x.b.y), (10.0, 0.0), "the shift pick applies the previewed extend");
}
#[test]
fn extend_shift_shows_the_opposite_piece_and_applies_it() {
let mut s = Session::new();
line(&mut s, [4.0, -5.0], [4.0, 5.0]);
line(&mut s, [10.0, -5.0], [10.0, 5.0]);
let l = line(&mut s, [0.0, 0.0], [8.0, 0.0]);
s.zoom_extents().unwrap();
s.start("extend").unwrap();
assert_eq!(ends(&s, l, [6.0, 0.0]), Some(vec![seg([8.0, 0.0], [10.0, 0.0])]), "shift up: the stretch a click adds");
s.shift_down = true;
assert_eq!(ends(&s, l, [6.0, 0.0]), Some(vec![seg([4.0, 0.0], [8.0, 0.0])]), "shift down: the opposite (trim) preview");
s.shift_down = false;
assert_eq!(ends(&s, l, [6.0, 0.0]), Some(vec![seg([8.0, 0.0], [10.0, 0.0])]), "shift released: back to the extend preview");
s.shift_down = true;
s.input(Input::Point(Vec2::new(6.0, 0.0))).unwrap();
let EntityKind::Line(x) = &s.doc().unwrap().entity(l).unwrap().kind else { panic!() };
assert_eq!((x.b.x, x.b.y), (4.0, 0.0), "the shift pick applies the previewed trim");
}
+4
View File
@@ -287,7 +287,11 @@ fn navigate_canvas(app: &mut CadApp, ui: &mut egui::Ui, nav: &Navigation<'_>) ->
let p = nav.pointer;
let inside = p.hover_pos.is_some_and(|at| nav.rect.contains(at) && ui.ctx().layer_id_at(at) == Some(ui.layer_id()) && nav.response.hovered());
let on_menu = p.hover_pos.is_some_and(|at| over_canvas_menu(app, at));
if p.mods.shift != app.session.shift_down || p.mods.ctrl != app.session.ctrl_down {
ui.ctx().request_repaint();
}
app.session.ctrl_down = p.mods.ctrl;
app.session.shift_down = p.mods.shift;
ui.input(|i| {
for event in &i.raw.events {
if let egui::Event::MouseWheel { unit, .. } = event {
+31
View File
@@ -537,3 +537,34 @@ fn a_fence_point_taken_back_with_undo_updates_the_highlight_at_once() {
painted_text(&mut app, &ctx, below);
assert!(app.canvas.hover_part.list.as_ref().is_none_or(|l| l.prims.is_empty()), "nothing is crossed");
}
/// #1118: holding Shift at TRIM's prompt flips the cached rollover highlight to the opposite
/// edit and back, recomputing it even though the cursor has not moved.
#[test]
fn trim_shift_flips_the_cached_highlight_without_moving_the_cursor() {
let mut app = CadApp::new(cadcraft_engine::Session::new(), crate::Services::default());
for points in [[[4, -5], [4, 5]], [[10, -5], [10, 5]], [[0, 0], [8, 0]]] {
app.run("line", serde_json::json!({ "points": points })).unwrap();
}
app.session.zoom_extents().unwrap();
let ctx = egui::Context::default();
crate::framework::install_headless(&ctx);
painted_text(&mut app, &ctx, pos2(700.0, 450.0));
app.session.cmdline("trim").unwrap();
let cursor = app.canvas.xf.unwrap().to_screen(Vec2::new(6.0, 0.0));
painted_text(&mut app, &ctx, cursor);
let part = app.canvas.hover_part.list.as_ref().expect("a part of the line is highlighted");
assert!((part.bounds.min.x - 4.0).abs() < 1e-6 && (part.bounds.max.x - 8.0).abs() < 1e-6, "the piece a click removes: {:?}", part.bounds);
let (hover, cursor, scale) = (app.canvas.hover, app.canvas.cursor, app.canvas.xf.unwrap().scale);
let computed = app.canvas.hover_part.list_gen;
app.session.shift_down = true;
app.canvas.hover_part.update(&app.session, hover, cursor, scale);
assert_ne!(app.canvas.hover_part.list_gen, computed, "shift recomputes the highlight at a resting cursor");
let part = app.canvas.hover_part.list.as_ref().expect("the opposite part is highlighted");
assert!((part.bounds.min.x - 8.0).abs() < 1e-6 && (part.bounds.max.x - 10.0).abs() < 1e-6, "the stretch a shift-click adds: {:?}", part.bounds);
let flipped = app.canvas.hover_part.list_gen;
app.session.shift_down = false;
app.canvas.hover_part.update(&app.session, hover, cursor, scale);
assert_ne!(app.canvas.hover_part.list_gen, flipped, "releasing shift recomputes it again");
let part = app.canvas.hover_part.list.as_ref().expect("the original part is highlighted");
assert!((part.bounds.min.x - 4.0).abs() < 1e-6 && (part.bounds.max.x - 8.0).abs() < 1e-6, "back to the trim piece: {:?}", part.bounds);
}
+13 -1
View File
@@ -333,7 +333,19 @@ impl HoverPart {
// Every answer the running command is given counts, not only those that change the
// prompt: a fence point taken back with Undo leaves the same prompt but a shorter fence.
let answers = (session.commands_started, session.inputs);
(hover, c.x.to_bits(), c.y.to_bits(), scale.to_bits(), doc, answers, session.prompt_text(), super::list::font_generation()).hash(&mut hs);
(
hover,
c.x.to_bits(),
c.y.to_bits(),
scale.to_bits(),
doc,
answers,
session.shift_down,
session.ctrl_down,
session.prompt_text(),
super::list::font_generation(),
)
.hash(&mut hs);
hs.finish()
};
if self.key == Some(key) {