Files
cadcraft/crates/engine/src/cmd/edit.rs
T
Dimitrios Vidalis e43c39c55e Selection and grips respect the layers frozen in the active viewport (#3055) (#3124)
Fixes #3055.

## What
Inside an MSPACE viewport an object on a layer frozen in that viewport (VPLAYER, VP Freeze) is not drawn, but it
could still be picked, window-selected and edited through the viewport, and a selected one kept its grips.
`Drawing::is_visible` has no viewport, and selection and grips asked only that.

Three questions now take the active viewport into account:
- `select::selectable(d, e, frozen)`: what a selection can take. `frozen` is `DocState::viewport_frozen()`, the
  layers frozen in the active MSPACE viewport (empty in paper space and on the Model tab). The selection functions
  have `_in` variants that take it (`pick_in`, `pick_all_in`, `select_window_in`, `select_fence_in`,
  `select_polygon_in`, `select_group_in`, `SelectMode::select_in`); the existing functions keep their signatures
  and select as before.
- `DocState::shows(e)`: what the view being edited shows, `is_visible` plus the active viewport. Grips ask it where
  #3048 made them ask `is_visible`.
- `Session::selection()`: the selection as the view being edited has it, without the model-space objects the active
  viewport does not draw (`DocState::shown_selection`). The stored selection, `DocState::selection`, keeps them.

**Selecting.** Every selection made in the space being edited passes the active viewport's frozen layers:
- idle clicks and windows, "Select objects" (a pick, the implied window, W, C, BOX, F, WP, CP, ALL, G, and L and P,
  which take the last object the viewport draws and the part of the Previous set it draws), selection cycling, and
  prompts that pick one object;
- `select`, `selectall`, `selectsimilar`, and the group members a pick brings (PICKSTYLE);
- the commands that select for themselves: STRETCH (JSON and typed, with its own All, Last and Previous and its
  multiline vertex rule), TRIM and EXTEND by fence and by crossing window;
- AutoLISP `ssget` (W, C, F, WP, CP, a point, P, L) and `entsel`;
- in the UI, the rollover highlight and the hatch under the cursor that a double-click edits. The object under
  the cursor and selection cycling's badge are worked out again when the active viewport's frozen layers change
  under a pointer that does not move.

**Already selected.** An object that is selected when its layer is frozen in the active viewport stays in the
stored selection until the selection is replaced or cleared, and counts as selected again once the layer is thawed
there. Until then `Session::selection()` leaves it out, and with it everything that starts from the selection: the
selection made before a command (PICKFIRST), a JSON command that names no objects, Delete, the grip edits that
cover the selection, AutoLISP's implied selection (`ssget`, `ssget "I"`, `ssgetfirst`), the palettes, and what the
session reports as the selection (`summary`, `drawing.inspect`). Two UI functions read the stored selection
themselves and now read `shown_selection` too: the hatch editor's edit mode and BEDIT's choice between editor and
dialog. The selection highlight inside the active viewport leaves the object out as well.

Adding to the selection or removing from it (a click, a window, `select` with `add` or with nothing to select
with, QSELECT appending) starts from the stored selection (`Session::stored_selection()`), so such an object is not
dropped while the selection changes around it. A command that takes the selection, or a new selection, replaces
it as before. `Session::selection()` gives the stored selection unchanged wherever the active viewport hides
none of the selected objects: in paper space, on the Model tab, and in a viewport that freezes no layer.

The UI asks for the selection several times a frame, so `DocState::drawn_in_viewport` does not look every selected
object up: a few handles are looked up one by one, and beyond that it passes over model space once.

**Grips.** Static grip painting and hit testing, hot grips (`click_grip`), the grip edit's targets and the typed
GRIP command (`griphot.rs`), and dynamic grips ask `shows`. The dynamic-grip cache key now holds the active
viewport's frozen layers: entering another viewport changes which references are shown without changing the
drawing revision, so the key did not change. The grip code asks for itself, as it does for `is_visible` since
#3048, so it does not depend on who hands it the objects.

A viewport's frozen layers hide model-space objects only, never an object on the sheet. Layer names are compared
the way the renderer compares them. Outside a viewport, and in a viewport that freezes no layer, nothing changes.

## Test
The tests build two viewports on one layout, one of which freezes a layer. Most take the same path through both:
through the viewport that freezes the layer the object on it is left out, through the other one it is taken as
before. The ones about an object that is already selected freeze and thaw its layer in the viewport being edited.

`crates/engine/tests/it/viewport_frozen_selection.rs`, 22 tests:
- selecting: a pick, window / crossing / fence, idle clicks and windows, selection cycling, every answer to
  "Select objects" (with ERASE), group members, `selectall` and `selectsimilar`, STRETCH (JSON; typed crossing
  window, All, a pick, Last, Previous; the multiline vertex rule), TRIM by fence and by crossing window, a prompt
  for one object (BREAK), LEADER's Copy, AutoLISP `ssget` and `entsel`;
- already selected: a typed command with PICKFIRST, a JSON command without handles and Delete; Previous and Last;
  the grip edits that cover the selection; what the session reports, with paper space and the Model tab unchanged;
  adding to and removing from the selection; a small and a large selection (the two paths of
  `drawn_in_viewport`, in a drawing whose draw order is not the order of its handles);
- grips: a shared grip, the grips of an object whose layer is frozen and thawed again while it is selected, and
  what the edited view shows (model-space objects only, never an object on the sheet).

`crates/ui-egui`, 9 unit tests (hot grips, the grip and the hatch under the cursor, the viewport highlight, dynamic
grips and their cache key, BEDIT's choice, the hatch editor's edit mode, selection cycling's badge under a resting
pointer) and `tests/viewport_frozen_grips.rs`, 3 tests (no grips painted for a selected object the viewport does
not draw; the rollover highlight; the highlight under a resting pointer while the layer is frozen and thawed).

EXTEND runs TRIM's selection code and has no test of its own.

Without the change these tests fail, checked call by call with a temporary switch that is not in this PR: each of
the 60 calls of `viewport_frozen`, `shows`, `hidden_in_viewport`, `drawn_in_viewport`, `shown_selection`, `last_in`
and `stored_selection` outside tests was put back to the old behaviour on its own. 51 of them made at least one
test fail directly; 5 grip checks that sit behind `Session::selection()` did so with the getter switched back as
well; the other 4 are calls inside `DocState` that the same tests reach through their callers. Each path of
`drawn_in_viewport`, and its sort, fails a test when switched off. Of the 42 calls of `Session::selection()`, 10
have a test that fails when that one call reads the stored selection; the other 32 receive the same list and have
no test of their own. The second commit's two conditions (the hover and the badge under a resting pointer) each
have a test that fails without them.

## What ran
The seven steps of `cargo xtask ci`, one by one, on this branch at 6754b960 (main at ddfa833b plus its two commits):
fmt, clippy (workspace, all targets, `-D warnings`), test (3725 passed, 0 failed, 26 ignored; the doctest
step ran, the workspace has no doctests), the i18n report, assets, layers and wasm. All pass.

## Not in this PR
- A selected object whose layer is turned off or frozen for the whole drawing is still taken by a command that
  starts from the selection, as before (ran: ERASE and MOVE typed and as JSON, and Delete, edit it). The issue asks
  to keep the drawing-wide behaviour, so this PR leaves it alone.
- A command that is given object handles (`handles` in JSON, an entity name from AutoLISP) acts on them as before,
  whatever the viewport draws. QSELECT and `ssget "X"` look at the whole drawing; what they put in the stored
  selection is then left out like any other selected object the viewport does not draw.
- `ssget "L"` takes the last object of any space, as before; inside a viewport it now passes over the objects
  that viewport does not draw.
- Object snap, hatch boundary detection, the implied cutting edges of TRIM and EXTEND, constraint inference, and
  the Z that ID and DIST read from an object under the point are not selections and are unchanged.
- A block reference on a layer the viewport draws, with parts on a layer frozen in the viewport: the parts are not
  drawn, but the hit geometry of a reference is built without the viewport, so a pick where such a part would be
  still finds the reference. Ran on eaae189f with a test that is not in the PR: a pick, a crossing window and a
  fence on such a part select the reference, and a window around the drawn part alone does not; with the layer
  frozen for the whole drawing it is the other way round. It needs extents that know the viewport, which
  `crates/doc` does not have, so it is left for its own change.
- Visibility that changes while an interactive command is already waiting, or while a grip list is open, is #3056.

## Compared with AutoCAD 2022
Observed as a black box, in a drawing made there for this and driven by a script: two lines, one of them on a layer
frozen (VPLAYER) in one of two layout viewports. No file from it is in this PR.

Inside the viewport that freezes the layer, the same there and here: a pick, a window, a crossing window, a fence
and a crossing polygon (`ssget`), ALL, Last, a crossing window and a fence at "Select objects", `ssget "L"`, select
all, and STRETCH's crossing window leave the object out; `ssget "X"` returns it; a command that is given the object
itself (its entity name there, `handles` here) changes it. Through the other viewport the pick, the windows, the
fence, ALL, Last and STRETCH took it.

Different, where the issue asks for more ("cannot be edited through that viewport, including when they were
already selected"):
- An object selected through the other viewport stays in the implied selection when the viewport that freezes its
  layer becomes the active one (`ssgetfirst`, `ssget "I"`), and ERASE typed there erased it. Here the commands leave
  it out.
- Previous: `ssget "P"` returned the set made through the other viewport whole, and CHPROP with Previous changed
  the object. Here Previous takes the part of that set the viewport draws.

A comment on the issue asks which of the two is wanted. This PR follows the issue as written; if AutoCAD's
behaviour is preferred for these two cases, I will change them here.

Not observed there: grips (a script cannot read them), and freezing the layer while objects are selected (VPLAYER
is a command and cleared the selection).

## Parity rows
U5 (grips) and F15 (viewports) in docs/gaps.md. No docs edited.

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->

## Summary by CodeRabbit

* **New Features**
  * Selection, picking, hover highlighting, and grip interactions now respect layers frozen in the active model-space viewport.
  * Selection reports and editing tools show only objects visible in the active viewport, while stored selections retain hidden objects.
  * Window, crossing, fence, point, and other selection modes now apply viewport visibility consistently.
* **Bug Fixes**
  * Changing viewports or freezing and thawing layers now updates selection highlights, hover targets, and grips to match what the active viewport displays.
  * Selection commands and object inspection now consistently reflect viewport visibility.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-11-15 12:11:56 +01:00

754 lines
29 KiB
Rust

//! Edit menu: undo/redo, clipboard, selection.
use cadcraft_doc::{Entity, Handle, entity_bounds};
use cadcraft_geom::{Bounds2, Mat3, Vec2};
use serde_json::{Value, json};
use super::machines::{SelOutcome, SelectPhase, SelectRun};
use super::{CommandSpec, EngineError, bad, bool_or, clipboard, f64_or, has_selection, point_param, point_req, points_param, str_param, targets};
use crate::undoctl::Control;
use crate::{Accept, Input, Interactive, Prompt, Result, Session, Step};
pub fn specs() -> Vec<CommandSpec> {
vec![
// U undoes the last operation at once (Edit > Undo, Cmd+Z); UNDO asks how many.
CommandSpec::new("u", "Undo", run_u).menu(&["Edit", "Undo"]).key("Cmd+Z").params("{}").noundo(),
CommandSpec::new("undo", "Undo", run_undo)
.params(
"{count?: n} | {mode: mark|begin|end} | {mode: back, confirm?: bool (no mark: undo everything; default true)} | {mode: auto, on: bool} | {mode: control, control: all|none|one|combine|layer, on?: bool (combine/layer)}",
)
.noundo()
.interactive(|_| Ok(Box::new(UndoM::default()))),
CommandSpec::new("redo", "Redo", run_redo).menu(&["Edit", "Redo"]).key("Cmd+Shift+Z").params("{count?: n}").noundo(),
CommandSpec::new("mredo", "Redo", run_mredo).params("{count?: n} | {all: true}").noundo().interactive(|_| Ok(Box::new(MredoM))),
CommandSpec::new("cutclip", "Cut", run_cut)
.menu(&["Edit", "Cut"])
.key("Cmd+X")
.params("{handles?}")
.interactive(|_| Ok(Box::new(SelectRun::new("cutclip", "CUTCLIP")))),
CommandSpec::new("cutbase", "Cut with Base Point", run_cutbase)
.menu(&["Edit", "Cut with Base Point"])
.params("{handles?, base: [x,y]}")
.interactive(|_| Ok(Box::new(CopyBaseM { cut: true, ..CopyBaseM::default() }))),
CommandSpec::new("copyclip", "Copy", run_copyclip)
.menu(&["Edit", "Copy"])
.key("Cmd+C")
.params("{handles?}")
.noundo()
.interactive(|_| Ok(Box::new(SelectRun::new("copyclip", "COPYCLIP")))),
CommandSpec::new("copybase", "Copy with Base Point", run_copybase)
.menu(&["Edit", "Copy with Base Point"])
.key("Cmd+Shift+C")
.params("{handles?, base: [x,y]}")
.noundo()
.interactive(|_| Ok(Box::new(CopyBaseM::default()))),
CommandSpec::new("pasteclip", "Paste", run_paste)
.menu(&["Edit", "Paste"])
.key("Cmd+V")
.params("{at?: [x,y]} (where the base point lands; default: the original coordinates)")
.enabled(clipboard::has_clip)
.interactive(|_| Ok(Box::new(PasteM { mode: PasteMode::Clip }))),
CommandSpec::new("pasteblock", "Paste as Block", run_pasteblock)
.menu(&["Edit", "Paste as Block"])
.key("Cmd+Shift+V")
.params("{at?: [x,y]} (insertion point; default: the original coordinates)")
.enabled(clipboard::has_clip)
.interactive(|_| Ok(Box::new(PasteM { mode: PasteMode::Block }))),
CommandSpec::new("pasteorig", "Paste to Original Coordinates", run_pasteorig)
.menu(&["Edit", "Paste to Original Coordinates"])
.params("{} (only into a drawing other than the one copied from)")
.enabled(clipboard::can_paste_orig)
.interactive(|_| Ok(Box::new(PasteM { mode: PasteMode::Orig }))),
CommandSpec::new("erase.selection", "Clear", run_clear).menu(&["Edit", "Clear"]).key("Delete").enabled(has_selection).params("{}"),
CommandSpec::new("selectall", "Select All", run_selectall).menu(&["Edit", "Select All"]).key("Cmd+A").alias(&["ai_selall"]).noundo(),
CommandSpec::new("ai_deselect", "Deselect All", run_deselect).params("{}").noundo(),
CommandSpec::new("select", "Select", run_select)
.params("{handles: [hex]} | {window: [[x,y],[x,y]], crossing?: bool} | {at: [x,y]} | {clear: true} | {add?: bool}")
.noundo()
.interactive(|_| Ok(Box::new(SelectM::default()))),
]
}
/// A programmatic undo/redo (`engine.execute`, MCP, control channel, scripts) first ends a running
/// command the way Esc does, exactly as `Session::start` does for Cmd+Z, the menu or the command
/// line. Otherwise it would swap the drawing under the command and pop the previous command's step.
fn end_running_command(s: &mut Session) {
if s.running.is_some() {
s.cancel();
}
}
/// The UNDO options of the JSON form (`mode`) and the command line (keywords).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum UndoMode {
Count,
Mark,
Back,
Begin,
End,
Auto,
Control,
}
fn undo_mode(m: &str) -> Option<UndoMode> {
Some(match m.trim().to_ascii_lowercase().as_str() {
"" | "count" => UndoMode::Count,
"mark" => UndoMode::Mark,
"back" => UndoMode::Back,
"begin" | "group" => UndoMode::Begin,
"end" => UndoMode::End,
"auto" => UndoMode::Auto,
"control" => UndoMode::Control,
_ => return None,
})
}
fn on_param(p: &Value, key: &str) -> Option<bool> {
let v = p.get(key)?;
v.as_bool().or_else(|| v.as_i64().map(|i| i != 0)).or_else(|| match v.as_str()?.trim().to_ascii_lowercase().as_str() {
"on" | "yes" | "y" | "true" | "1" => Some(true),
"off" | "no" | "n" | "false" | "0" => Some(false),
_ => None,
})
}
/// The JSON result of an UNDO option: `message` plus the new UNDOCTL bits and mark count.
fn undo_state(s: &Session, message: impl Into<String>) -> Result<Value> {
let st = s.state()?;
Ok(json!({ "message": message.into(), "undoctl": st.undo_ctl.bits(), "marks": st.undo_marks() }))
}
fn undo_off() -> EngineError {
EngineError::Other("Undo is turned off (UNDO Control None); UNDO Control All turns it on.".into())
}
fn run_undo(s: &mut Session, p: &Value) -> Result<Value> {
end_running_command(s);
let mode = match p.get("mode") {
None | Some(Value::Null) => UndoMode::Count,
Some(v) => v.as_str().and_then(undo_mode).ok_or_else(|| bad("undo", "mode: count, mark, back, begin, end, auto or control"))?,
};
let control = s.state()?.undo_ctl.control;
if control != Control::All && !matches!(mode, UndoMode::Count | UndoMode::Control | UndoMode::Auto) {
return Err(EngineError::Other(format!("UNDO {mode:?} needs UNDO Control All.")));
}
match mode {
UndoMode::Count => {
if control == Control::None {
return Err(undo_off());
}
let n = p.get("count").and_then(Value::as_u64).unwrap_or(1).clamp(1, 1000);
// Undoing inside a BEgin group ends it: the whole group is undone as one step.
s.state_mut()?.end_undo_group();
let mut labels = Vec::new();
for _ in 0..n {
match s.undo()? {
Some(l) => labels.push(l),
None => break,
}
}
if labels.is_empty() {
return Ok(json!({ "message": "Everything has been undone" }));
}
Ok(json!({ "undone": labels, "message": labels.join(", ").to_string() }))
}
UndoMode::Mark => {
s.state_mut()?.undo_mark();
undo_state(s, "Mark set")
}
UndoMode::Back => undo_back(s, on_param(p, "confirm").unwrap_or(true)),
UndoMode::Begin => {
let ended = s.state_mut()?.begin_undo_group();
undo_state(s, if ended { "Group ended; new group begun" } else { "Group begun" })
}
UndoMode::End => {
let ended = s.state_mut()?.end_undo_group();
undo_state(s, if ended { "Group ended" } else { "No group to end" })
}
UndoMode::Auto => {
let on = on_param(p, "on").ok_or_else(|| bad("undo", "`on` (true/false) is required for mode auto"))?;
s.state_mut()?.undo_ctl.auto = on;
undo_state(s, format!("UNDO Auto {}", if on { "on" } else { "off" }))
}
UndoMode::Control => {
let c = str_param(p, "control").unwrap_or("all").trim().to_ascii_lowercase();
let st = s.state_mut()?;
let msg = match c.as_str() {
"all" => {
st.set_undo_control(Control::All);
"UNDO Control All"
}
"none" => {
st.set_undo_control(Control::None);
"UNDO Control None: undo is off"
}
"one" => {
st.set_undo_control(Control::One);
"UNDO Control One: only the last step can be undone"
}
"combine" => {
st.undo_ctl.combine_view = on_param(p, "on").unwrap_or(true);
"UNDO Control Combine set"
}
"layer" => {
st.undo_ctl.combine_layer = on_param(p, "on").unwrap_or(true);
"UNDO Control Layer set"
}
_ => return Err(bad("undo", "control: all, none, one, combine or layer")),
};
undo_state(s, msg)
}
}
}
/// UNDO Back: undo to the last mark (which goes with it), or with no mark everything when
/// `confirm` is true.
fn undo_back(s: &mut Session, confirm: bool) -> Result<Value> {
let st = s.state_mut()?;
st.end_undo_group();
let depth = st.undo.len();
st.undo_ctl.marks.retain(|m| *m <= depth);
let target = match st.undo_ctl.marks.pop() {
Some(m) => m,
None if confirm => 0,
None => return undo_state(s, "Nothing undone"),
};
let mut labels = Vec::new();
// Each step pops one entry, so this ends within the stack's depth.
while s.state()?.undo.len() > target {
match s.undo()? {
Some(l) => labels.push(l),
None => break,
}
}
let msg = match (labels.is_empty(), target) {
(true, 0) => "Everything has been undone".to_string(),
(true, _) => "Back at the mark".to_string(),
(false, _) => labels.join(", "),
};
let mut r = undo_state(s, msg)?;
r["undone"] = json!(labels);
Ok(r)
}
fn run_u(s: &mut Session, _p: &Value) -> Result<Value> {
run_undo(s, &json!({}))
}
/// What the UNDO prompt machine asks.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
enum UndoAsk {
/// "Enter the number of operations to undo or [Auto/Control/BEgin/End/Mark/Back]".
#[default]
Main,
Auto,
Control,
Combine,
Layer,
/// Back with no mark: undo everything?
BackAll,
}
/// UNDO at the command line: a number of operations (default 1) or Auto, Control (All, None, One,
/// Combine, Layer), BEgin, End, Mark or Back. With Control None only Control is offered, with One
/// a count or Control.
#[derive(Default)]
struct UndoM {
ask: UndoAsk,
}
impl UndoM {
fn finish(s: &mut Session, r: Result<Value>) -> Result<Step> {
match r {
Ok(v) => {
if let Some(m) = v.get("message").and_then(Value::as_str) {
s.echo(m.to_string());
}
}
Err(e) => s.echo(e.to_string()),
}
Ok(Step::Done)
}
}
impl Interactive for UndoM {
fn name(&self) -> &'static str {
"UNDO"
}
fn prompt(&self, s: &Session) -> Prompt {
let control = s.state().map(|st| st.undo_ctl.control).unwrap_or_default();
let ask = if control == Control::None && self.ask == UndoAsk::Main { UndoAsk::Control } else { self.ask };
let yes_no = |b: bool| if b { "Yes" } else { "No" };
let ctl = s.state().map(|st| st.undo_ctl.clone()).unwrap_or_default();
match ask {
UndoAsk::Auto => Prompt::new("Enter UNDO Auto mode", Accept::TEXT).kw(&["ON", "OFF"]).default(if ctl.auto { "ON" } else { "OFF" }),
UndoAsk::Control => {
Prompt::new("Enter an UNDO control option", Accept::TEXT).kw(&["All", "None", "One", "Combine", "Layer"]).default("All")
}
UndoAsk::Combine => {
Prompt::new("Combine zooms and pans into one undo step?", Accept::TEXT).kw(&["Yes", "No"]).default(yes_no(ctl.combine_view))
}
UndoAsk::Layer => {
Prompt::new("Combine layer property changes into one undo step?", Accept::TEXT).kw(&["Yes", "No"]).default(yes_no(ctl.combine_layer))
}
UndoAsk::BackAll => Prompt::new("No mark: undo everything?", Accept::TEXT).kw(&["Yes", "No"]).default("Yes"),
UndoAsk::Main if control == Control::One => {
Prompt::new("Enter the number of operations to undo", Accept::NUMBER).kw(&["Control"]).default("1")
}
UndoAsk::Main => Prompt::new("Enter the number of operations to undo", Accept::NUMBER)
.kw(&["Auto", "Control", "BEgin", "End", "Mark", "Back"])
.default("1"),
}
}
fn input(&mut self, s: &mut Session, i: Input) -> Result<Step> {
let prompt = self.prompt(s);
if s.state()?.undo_ctl.control == Control::None && self.ask == UndoAsk::Main {
self.ask = UndoAsk::Control;
}
let kw = match &i {
Input::Cancel => return Ok(Step::Cancel),
Input::Enter => prompt.default.clone(),
Input::Keyword(k) | Input::Text(k) => prompt.match_keyword(k),
_ => return Ok(Step::Continue),
};
let yes = kw.as_deref() == Some("Yes");
match self.ask {
UndoAsk::Main => {
// Enter takes the default count, not a keyword.
if let Some(k) = kw.as_deref().filter(|_| !matches!(i, Input::Enter)) {
match k {
"Auto" => self.ask = UndoAsk::Auto,
"Control" => self.ask = UndoAsk::Control,
"Back" if s.state()?.undo_marks() == 0 => self.ask = UndoAsk::BackAll,
_ => {
let r = run_undo(s, &json!({ "mode": k.to_ascii_lowercase() }));
return Self::finish(s, r);
}
}
return Ok(Step::Continue);
}
let n = match &i {
Input::Text(t) => {
t.trim().parse::<u64>().ok().filter(|n| *n >= 1).ok_or_else(|| EngineError::Other("Requires a positive integer.".into()))?
}
_ => 1,
};
let r = run_undo(s, &json!({ "count": n }));
Self::finish(s, r)
}
UndoAsk::Auto => {
let on = match kw.as_deref() {
Some("ON") => true,
Some("OFF") => false,
_ => return Err(EngineError::Other("Enter ON or OFF.".into())),
};
let r = run_undo(s, &json!({ "mode": "auto", "on": on }));
Self::finish(s, r)
}
UndoAsk::Control => match kw.as_deref() {
Some("Combine") => {
self.ask = UndoAsk::Combine;
Ok(Step::Continue)
}
Some("Layer") => {
self.ask = UndoAsk::Layer;
Ok(Step::Continue)
}
Some(c) => {
let r = run_undo(s, &json!({ "mode": "control", "control": c.to_ascii_lowercase() }));
Self::finish(s, r)
}
None => Err(EngineError::Other("Enter All, None, One, Combine or Layer.".into())),
},
UndoAsk::Combine | UndoAsk::Layer => {
if kw.is_none() {
return Err(EngineError::Other("Answer Yes or No.".into()));
}
let control = if self.ask == UndoAsk::Combine { "combine" } else { "layer" };
let r = run_undo(s, &json!({ "mode": "control", "control": control, "on": yes }));
Self::finish(s, r)
}
UndoAsk::BackAll => {
if kw.is_none() {
return Err(EngineError::Other("Answer Yes or No.".into()));
}
let r = run_undo(s, &json!({ "mode": "back", "confirm": yes }));
Self::finish(s, r)
}
}
}
}
/// Redo up to `n` undone steps.
fn redo_steps(s: &mut Session, n: u64) -> Result<Value> {
end_running_command(s);
let mut labels = Vec::new();
for _ in 0..n {
match s.redo()? {
Some(l) => labels.push(l),
None => break,
}
}
Ok(json!({ "redone": labels, "message": if labels.is_empty() { "Nothing to redo".to_string() } else { labels.join(", ") } }))
}
fn run_redo(s: &mut Session, p: &Value) -> Result<Value> {
redo_steps(s, p.get("count").and_then(Value::as_u64).unwrap_or(1).clamp(1, 1000))
}
/// MREDO: `{count}` steps, or `{all: true}` every undone step.
fn run_mredo(s: &mut Session, p: &Value) -> Result<Value> {
if p.get("all").and_then(Value::as_bool).unwrap_or(false) {
let n = s.state()?.redo.len() as u64;
return redo_steps(s, n.max(1));
}
run_redo(s, p)
}
/// MREDO at the command line: a number of actions, All or Last.
struct MredoM;
impl Interactive for MredoM {
fn name(&self) -> &'static str {
"MREDO"
}
fn prompt(&self, _s: &Session) -> Prompt {
Prompt::new("Enter number of actions", Accept::NUMBER).kw(&["All", "Last"])
}
fn input(&mut self, s: &mut Session, i: Input) -> Result<Step> {
let p = match &i {
Input::Cancel => return Ok(Step::Cancel),
Input::Enter => return Ok(Step::Done),
Input::Keyword(k) | Input::Text(k) => match self.prompt(s).match_keyword(k).as_deref() {
Some("All") => json!({ "all": true }),
Some(_) => json!({ "count": 1 }),
None => {
let n =
k.trim().parse::<u64>().ok().filter(|n| *n >= 1).ok_or_else(|| EngineError::Other("Requires a positive integer.".into()))?;
json!({ "count": n })
}
},
_ => return Ok(Step::Continue),
};
let r = run_mredo(s, &p)?;
if let Some(m) = r.get("message").and_then(Value::as_str) {
s.echo(m.to_string());
}
Ok(Step::Done)
}
}
/// Copy the objects of `hs` to the clipboard, each once however often `hs` lists it.
fn copy_to_clip(s: &mut Session, hs: &[Handle], base: Option<Vec2>) -> Result<usize> {
let d = s.doc()?;
let mut seen = std::collections::HashSet::new();
let ents: Vec<Entity> = hs.iter().filter(|h| seen.insert(**h)).filter_map(|h| d.entity(*h).map(|e| (**e).clone())).collect();
let b = ents.iter().fold(Bounds2::EMPTY, |acc, e| acc.union(&entity_bounds(d, e, 0)));
let base = base.unwrap_or(if b.min.is_finite() { b.min } else { Vec2::ZERO });
let n = ents.len();
clipboard::copy(s, ents, base)?;
Ok(n)
}
fn run_copyclip(s: &mut Session, p: &Value) -> Result<Value> {
let hs = targets(s, p)?;
let n = copy_to_clip(s, &hs, None)?;
Ok(json!({ "copied": n }))
}
fn run_copybase(s: &mut Session, p: &Value) -> Result<Value> {
let base = point_req("copybase", p, "base")?;
let hs = targets(s, p)?;
let n = copy_to_clip(s, &hs, Some(base))?;
Ok(json!({ "copied": n }))
}
/// Copy the objects that are not on locked layers to the clipboard and erase them. Returns how
/// many were cut and the message to show: how many were on a locked layer, and that nothing was
/// cut when that is all of them (the clipboard then keeps what it held).
fn cut_to_clip(s: &mut Session, hs: &[Handle], base: Option<Vec2>) -> Result<(usize, Option<String>)> {
let mut seen = std::collections::HashSet::new();
let live: Vec<Handle> = {
let d = s.doc()?;
hs.iter().copied().filter(|h| seen.insert(*h) && d.entity(*h).is_some()).collect()
};
let (hs, locked): (Vec<Handle>, Vec<Handle>) = live.into_iter().partition(|h| !super::curves::is_locked(s, *h));
let note = match locked.len() {
0 => None,
1 => Some("1 object was on a locked layer.".to_string()),
n => Some(format!("{n} objects were on a locked layer.")),
};
if hs.is_empty() {
let msg = note.map_or_else(|| "Nothing was cut.".to_string(), |m| format!("{m} Nothing was cut."));
return Ok((0, Some(msg)));
}
let n = copy_to_clip(s, &hs, base)?;
let d = s.doc_mut()?;
for h in &hs {
d.remove_entity(*h);
}
s.set_selection(Vec::new());
Ok((n, note))
}
fn cut_result(n: usize, msg: Option<String>) -> Value {
let mut r = json!({ "cut": n });
if let Some(m) = msg {
r["message"] = json!(m);
}
r
}
fn run_cut(s: &mut Session, p: &Value) -> Result<Value> {
let hs = targets(s, p)?;
let (n, msg) = cut_to_clip(s, &hs, None)?;
Ok(cut_result(n, msg))
}
fn run_cutbase(s: &mut Session, p: &Value) -> Result<Value> {
let base = point_req("cutbase", p, "base")?;
let hs = targets(s, p)?;
let (n, msg) = cut_to_clip(s, &hs, Some(base))?;
Ok(cut_result(n, msg))
}
/// Paste the clipboard moved by `offset` (the clipboard brings the layers, styles and blocks its
/// objects need).
fn paste(s: &mut Session, offset: Vec2) -> Result<Vec<Handle>> {
clipboard::paste(s, &Mat3::translate(offset))
}
fn run_paste(s: &mut Session, p: &Value) -> Result<Value> {
if s.clipboard.is_empty() {
return Err(bad("pasteclip", "the clipboard is empty"));
}
let off = match point_param(p, "at") {
Some(at) => at - s.clipboard_base,
None => Vec2::ZERO,
};
let r = paste(s, off)?;
Ok(json!({ "handles": r.iter().map(|h| h.hex()).collect::<Vec<_>>() }))
}
fn run_pasteblock(s: &mut Session, p: &Value) -> Result<Value> {
let at = point_param(p, "at").unwrap_or(s.clipboard_base);
let h = clipboard::paste_block(s, at)?;
let block = match s.doc()?.entity(h).map(|e| &e.kind) {
Some(cadcraft_doc::EntityKind::Insert(i)) => i.block.clone(),
_ => String::new(),
};
Ok(json!({ "handle": h.hex(), "block": block }))
}
fn run_pasteorig(s: &mut Session, _p: &Value) -> Result<Value> {
let r = paste(s, Vec2::ZERO)?;
Ok(json!({ "handles": r.iter().map(|h| h.hex()).collect::<Vec<_>>() }))
}
fn run_clear(s: &mut Session, _p: &Value) -> Result<Value> {
let mut hs = s.selection();
hs.retain(|h| !super::curves::is_locked(s, *h));
let n = super::modify::erase_kept(s, &hs)?;
s.set_selection(Vec::new());
Ok(json!({ "erased": n }))
}
fn run_selectall(s: &mut Session, _p: &Value) -> Result<Value> {
let space = s.space();
let d = s.doc()?;
let frozen = s.viewport_frozen();
let hs: Vec<Handle> = d
.space(&space)
.map(|st| {
st.iter()
.filter(|e| crate::select::selectable(d, e, frozen) && d.layer(&e.common.layer).is_none_or(|l| !l.locked))
.map(|e| e.handle)
.collect()
})
.unwrap_or_default();
let n = hs.len();
s.set_selection(hs);
Ok(json!({ "selected": n }))
}
fn run_deselect(s: &mut Session, _p: &Value) -> Result<Value> {
s.set_selection(Vec::new());
Ok(json!({ "selected": 0 }))
}
fn run_select(s: &mut Session, p: &Value) -> Result<Value> {
if bool_or(p, "clear", false) {
s.set_selection(Vec::new());
return Ok(json!({ "selected": 0 }));
}
let space = s.space();
let add = bool_or(p, "add", false);
let picked: Vec<Handle> = if let Some(w) = points_param(p, "window") {
let (Some(a), Some(b)) = (w.first(), w.get(1)) else { return Err(bad("select", "window needs 2 corners")) };
crate::select::select_window_in(s.doc()?, &space, s.viewport_frozen(), Bounds2::new(*a, *b), bool_or(p, "crossing", false))
} else if let Some(at) = point_param(p, "at") {
let ap = f64_or(p, "aperture", s.pixel_size() * s.settings.pickbox * 1.5);
crate::select::pick_in(s.doc()?, &space, s.viewport_frozen(), at, ap).into_iter().collect()
} else if let Some(f) = points_param(p, "fence") {
crate::select::select_fence_in(s.doc()?, &space, s.viewport_frozen(), &f)
} else if ["handles", "handle"].iter().any(|k| p.get(*k).is_some_and(|v| !v.is_null())) {
targets(s, p)?
} else {
// Nothing to select with: the selection stays, also what the active viewport does not draw.
s.stored_selection()
};
let mut sel = if add { s.stored_selection() } else { Vec::new() };
sel.extend(picked);
s.set_selection(sel);
let sel = s.selection();
Ok(json!({ "selected": sel.len(), "handles": sel.iter().map(|h| h.hex()).collect::<Vec<_>>() }))
}
#[derive(Clone, Copy, PartialEq)]
enum PasteMode {
/// PASTECLIP: at an insertion point.
Clip,
/// PASTEBLOCK: as a block, at an insertion point.
Block,
/// PASTEORIG: at the original coordinates, without asking.
Orig,
}
struct PasteM {
mode: PasteMode,
}
impl Interactive for PasteM {
fn name(&self) -> &'static str {
match self.mode {
PasteMode::Clip => "PASTECLIP",
PasteMode::Block => "PASTEBLOCK",
PasteMode::Orig => "PASTEORIG",
}
}
fn begin(&mut self, s: &mut Session) -> Result<Step> {
if s.clipboard.is_empty() {
s.echo("Clipboard is empty.");
return Ok(Step::Done);
}
if self.mode == PasteMode::Orig {
paste(s, Vec2::ZERO)?;
return Ok(Step::Done);
}
Ok(Step::Continue)
}
fn prompt(&self, _s: &Session) -> Prompt {
Prompt::new("Specify insertion point", Accept::POINT)
}
fn input(&mut self, s: &mut Session, i: Input) -> Result<Step> {
match i {
Input::Point(p) if self.mode == PasteMode::Block => {
clipboard::paste_block(s, p)?;
Ok(Step::Done)
}
Input::Point(p) => {
let base = s.clipboard_base;
paste(s, p - base)?;
Ok(Step::Done)
}
Input::Enter => Ok(Step::Done),
_ => Ok(Step::Continue),
}
}
fn drag(&self, s: &Session, c: Vec2) -> Option<crate::prompt::Drag> {
if self.mode == PasteMode::Orig || s.clipboard.is_empty() {
return None;
}
Some(crate::prompt::Drag { handles: Vec::new(), matrix: Mat3::translate(c - s.clipboard_base), extra: Vec::new(), clipboard: true })
}
fn preview(&self, s: &Session, c: Vec2) -> Vec<cadcraft_doc::EntityKind> {
let m = Mat3::translate(c - s.clipboard_base);
s.clipboard
.iter()
.take(500)
.map(|e| {
let mut k = e.kind.clone();
k.transform(&m);
k
})
.collect()
}
}
#[derive(Default)]
struct SelectM {
sel: SelectPhase,
}
impl Interactive for SelectM {
fn name(&self) -> &'static str {
"SELECT"
}
fn prompt(&self, _s: &Session) -> Prompt {
self.sel.prompt()
}
fn input(&mut self, s: &mut Session, i: Input) -> Result<Step> {
match self.sel.feed(s, &i)? {
SelOutcome::More => Ok(Step::Continue),
_ => Ok(Step::Done),
}
}
}
/// COPYBASE and CUTBASE: base point first, then "Select objects:" (skipped with a pickfirst
/// selection). CUTBASE also erases the objects it copies.
#[derive(Default)]
struct CopyBaseM {
base: Option<Vec2>,
sel: SelectPhase,
cut: bool,
}
impl CopyBaseM {
fn copy(&self, s: &mut Session, hs: &[Handle]) -> Result<Step> {
if self.cut {
if let (_, Some(m)) = cut_to_clip(s, hs, self.base)? {
s.echo(m);
}
} else {
copy_to_clip(s, hs, self.base)?;
}
Ok(Step::Done)
}
}
impl Interactive for CopyBaseM {
fn name(&self) -> &'static str {
if self.cut { "CUTBASE" } else { "COPYBASE" }
}
fn begin(&mut self, s: &mut Session) -> Result<Step> {
self.sel = SelectPhase::begin(s);
Ok(Step::Continue)
}
fn prompt(&self, _s: &Session) -> Prompt {
if self.base.is_none() { Prompt::new("Specify base point", Accept::POINT) } else { self.sel.prompt() }
}
fn input(&mut self, s: &mut Session, i: Input) -> Result<Step> {
if self.base.is_none() {
if let Input::Point(p) = i {
self.base = Some(p);
if self.sel.done {
let hs = self.sel.picked.clone();
return self.copy(s, &hs);
}
}
return Ok(Step::Continue);
}
match self.sel.feed(s, &i)? {
SelOutcome::More => Ok(Step::Continue),
SelOutcome::Empty => Ok(Step::Done),
SelOutcome::Done(hs) => {
let step = self.copy(s, &hs)?;
s.set_selection(Vec::new());
Ok(step)
}
}
}
}