Files
cadcraft/crates/engine/tests/it/insert_enter.rs
T
Obtrusive8276andClaude Opus 5.5 65ed2fb7f0 Blocks palette: previews, block libraries and placement (#2122)
* Blocks palette: list/medium/large icon modes, visible-only preview cache, grab-and-drop placement

* Address CodeRabbit review: repeat-placement tracking, Arc<Drawing> clone, contains_pointer, docstrings

* Keep repeat INSERT inside engine state machine and leave canvas hover check intact

* Handle Repeat/NoRepeat across all INSERT stages and bound preview work budget

* Fix Blocks palette review findings and preflight polyline previews

* Fix Blocks palette review findings and menu click ownership

* Restore bounded hatch thumbnails and exclude navigation clicks

* Support faithful hatch previews with shared work budget and bounded contour fallback

* Allow checking Repeat placement while block insertion is already in progress

* Address PR 2122 review feedback on hatch budget, ViewCube hit-testing and preview test

* Charge leader and wipeout expansion and bound MLeader array inserts in preview preflight

* Charge MLeader paths in preview preflight and match ViewCube hit-testing to displayed sheet

* Allow block insertion while rename editor is open and clarify hatch preview docs

* Limit palette repeat placement to palette-started insertions

* Account hatch preview comparisons as work occurs

* Bound attribute thumbnail work and preserve palette rotation

* Charge spline sampling work to the block preview budget

* Charge splines at 1/32 of their sampling cap so ordinary blocks still preview

* Charge text lengths in block preview budget and preserve inline INSERT arguments

* Bound MText column metadata in block previews

* Allow detailed block palette previews

* Explain refused block previews and keep them cached while scrolling

Previews now return Ready, Empty or Unavailable(reason) instead of an
ambiguous empty display list, so a refused thumbnail is no longer shown
like an empty block. Preflight separates input, computation and retained
output, and reuses the geometry/render sampling rules at a thumbnail
tolerance instead of a 4,000-sample worst case. A 64 MiB / 512-result LRU
replaces retain_visible. Insertion never depends on preview success.

Also: invalid-but-finite splines draw as their control polygon, and the
insertion-point prompt advertises REpeat so R keeps meaning Rotate.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* Remove changes unrelated to the Blocks palette

Drop the contributor entry and the two xtask test adjustments so the PR
only contains palette, preview and insertion changes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* Document the functions touched by the Blocks palette preview work

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* Keep placement on unchanged rename and tighten palette click ownership

Confirming a block name unchanged no longer cancels the INSERT in progress.
Command-line releases are not placed as points when two arrive in one frame,
and menu ownership of a press is taken from the press position of the last
press in the frame.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* Translate palette refusal messages and track canvas click ownership per release

Mark the five preview refusal texts for translation and add the palette status
texts to the other catalogs. Give each primary release in a frame the owner of
its own press, and place a release at its own position when the cursor has
moved onto a menu or a new press has begun.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* Treat navigation controls as release owners in batched canvas clicks

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* Track INSERT ownership and refuse palette edits during a foreign INSERT

The palette now cancels only the INSERT it started (identified by a per-run serial), advertises REpeat/NoRepeat at every numeric and point prompt, and refuses Edit, Create, Rename and Delete while an INSERT it did not start is running, so undoing that insertion cannot also undo a palette edit.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* Keep clicks on command-line keywords from placing canvas points

The command line now records one rectangle covering the bar and every inline keyword, and the canvas treats a press, release or ordinary click there as the command line's, so a keyword pushed past the bar by a long prompt cannot also send a point.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* Decide whether the ViewCube is shown from the current space, not the display list

The display list is rebuilt after a frame's clicks are handled, so right after a layout switch it was stale or cleared. A click where the ViewCube had just disappeared was dropped, and a click on one that had just appeared reached the drawing as a point. Painting and hit-testing now share viewcube_shown, which follows the layout space.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* Treat only a refused charge as hatch budget exhaustion

Spending the hatch budget exactly left zero remaining, which hatch::draw and the preview builder read as exhaustion, so a valid preview could lose its hatches or be refused. HatchBudget now records a refused reservation separately and both checks use it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* Keep CLOSEALL's confirmation, widen the command-line click area, persist the Blocks window choice

Only {json} calls and INSERT with inline arguments bypass the UI router, so CLOSEALL Y still asks before discarding drawings. The command line's click rectangle now includes the history lines or window, the resize grip and the AutoComplete rows. The floating-window preference of the Blocks palette is saved and restored with the other preferences.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* Update Z and axis presets with a comma scale; grey the palette's Y scale when exploding

A comma-separated scale preset now sets Z to the size of X and the axis presets to X, Y and that Z, so an earlier Scale cannot leave a stale Z. The Blocks palette disables its Y scale while Explode is on, with a tooltip saying the exploded insertion scales both axes by X.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* Fix doc comments on the BLOCK and ATTDEF input methods and the INSERT tests

BlockM::input and AttdefM::input named the INSERT state machine; the NoRepeat description sat on the comma-scale test instead of the repeat test, and the two newer INSERT tests had none of their own.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* Reset the Verify counter between repeated INSERT placements

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* Blocks palette: previews, block libraries and placement

Rewrite this PR on a smaller base. The Blocks window becomes a dockable
palette with a Current Drawing tab and a Libraries tab, icon and list views,
a kept list of block libraries (folders and DWG/DXF files) and one-click
placement through the existing interactive INSERT.

- Previews are built only for the visible tiles, a few per frame, under a
  time and point budget, in a bounded least-recently-used cache.
- BLOCKLIBRARY, BLOCKLIBIMPORT and BLOCKLIBRARIES are commands; importing a
  library block brings only the layers, linetypes, styles and blocks it needs.
- INSERT: Enter at the insertion point ends the command, as in AutoCAD.
- Hostile library files and block data are bounded; nothing panics.
- The canvas, ViewCube, command-line and interactive INSERT changes of the
  earlier version are gone.
- Translations in all interface languages.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* Blocks palette: read libraries off the UI thread, bound preview work

CodeRabbit's reviews of f2ae408 and 62bff60:

- A library is listed on a worker thread (the web reads it at once), with
  a "Reading the library..." message in every catalog. Refresh waits for
  the read, and now parses the file again even when its size and time
  look the same. `list_library` is what BLOCKLIBRARY answers, without a
  session.
- A preview's work is counted before anything is drawn: vertices, text,
  and the blocks references, dimensions and multileaders draw. An object
  with nothing in it still costs one visit. The pass that finds the size
  draws no dashes, and a block of points no longer scans the drawing's
  extents.
- A block listing keeps the first blocks by name without sorting the
  whole table, for BLOCKSPALETTE too.
- Merging a drawing removes nothing from a drawing too big for the purge
  scan: past its limit, a layer or style in use can look unused.

Also fixed: a block with more than 100,000 references could lose the
blocks it needs on import, because the walk counted repeated names. The
same count in `used_blocks`, which PURGE shares, could drop the arrowhead
of a merged dimension style, so that limit goes too: each block is walked
once, so the walk ends, cycles included.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Blocks palette: repeat a placement only after its own INSERT placed

The palette took any new handle as proof that its INSERT had placed the
block. Queued control requests can cancel that INSERT and run another
command to its end between two frames, and that command's handles then
started a placement nobody asked for.

The session now says how the last command ended: `commands_started`
numbers the commands, nested transparent ones left out, and
`last_finished` holds the number of the last one that ended by itself and
whether it added to the drawing. The palette repeats only when that is its
own INSERT, which also removes its handle counter.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Blocks palette: a command started after a placement ends the repeat

A command with no prompt, such as REGEN, started right after INSERT placed
the block left `last_finished` on that INSERT, so the palette started
placing again after the user had moved on. The palette now also requires
that no command has started since.

A JSON command still does not end the repeat, on purpose: it never stops a
running command, so one that lands a frame later finds the next INSERT
running and leaves it alone. Ending the repeat only when it lands before
the frame would make the result depend on timing. A test pins this.

Also adds the doc comments CodeRabbit's coverage check asks for, on this
PR's own functions and tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* INSERT: a cancelled placement takes no undo step

INSERT wrote INSNAME when the name was given, so cancelling it left a
changed drawing and an undo step that undid nothing visible. Repeat
Placement, on by default, always ends with a cancelled INSERT, so the first
undo after placing a block from the palette appeared to do nothing.

INSNAME is now written when the block is placed. A cancelled INSERT no
longer offers its name as the next default.

Tests go through the palette where they only imitated it: the scale set in
the palette, and the two undo steps of a block from a library. The palette
places with two clicks, so the wording about a drag is gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Block import: keep what a block names and says about itself

BLOCKLIBIMPORT defined a new block from the chosen one's objects and base
point, so its description, annotative flag, units and explodable flag were
lost. They now come along.

The import's clean-up uses PURGE's scan, which only read the linetypes of
objects and layers. A linetype named only by a dimension style, a dimension
override, a multiline style, a multiline's copy of its style or an attribute
override looked unused, so the import dropped it and PURGE removed it while
the style still named it. The scan reads them all now.

A reference to a renamed anonymous block kept the drawing's block of that
name when it spelled the name in another case, or when it was a multileader's
block content. Both now take the new name, found as Drawing::block finds a
block.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* INSERT: Enter asks again at the insertion point, arrowheads follow renames

This PR made Enter and Space at the insertion point end INSERT, "as in
AutoCAD". AutoCAD 2027 does not: from -INSERT, from its palette and during
repeated placement it answers that a point or option is needed and asks
again. Only Escape ends it, so that change is gone.

A file named at the block name prompt defines its block before the insertion
point is asked for, and Escape keeps that definition, which is what AutoCAD
2027 does too. A test pins it.

An anonymous arrowhead block renamed by a merge kept its old name in the
dimension styles that came along and in dimension overrides, so they pointed
at this drawing's block. Both follow the rename now; a style this drawing
already had keeps its own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Block import and palette: keep every named style and layer, skip unused counts

Selective import uses PURGE's scans, which missed dimension styles named by
leaders, text styles named by linetype pattern elements, table cell styles,
dimension DIMTXSTY overrides and multiline or multileader-content attributes,
plus layers and linetypes named by attributes on multileader block content.
They all count as in use now.

The palette asks BLOCKSPALETTE for no reference counts so it does not read
every object after each edit, and cancels any running command before importing
a library block so the import keeps its own undo step.

* Blocks palette: a quick right-click at the insertion point cancels the placement

A placement started from the palette had no way out with the mouse: Enter at
the insertion point asks again, as AutoCAD 2027 does, and a quick right-click
is Enter. At that prompt only, for an INSERT the palette started, a quick
right-click now ends the command like Escape, which also ends Repeat
Placement. A typed INSERT and the scale and rotation prompts keep Enter, and
holding the button still opens the command menu.

Escape, the menu's Cancel and this right-click share one function that also
clears the half-typed text and the IME preedit; the menu's Cancel left the
preedit behind.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* Blocks palette: a transparent command no longer stops Repeat Placement

A 'ZOOM or 'PAN run while the palette's INSERT asks for the insertion point
shows the prompt `>>ZOOM`, which the palette took for another command and so
stopped repeating the placement. A transparent command inside the INSERT does
not start a command; it is now the count of commands started that tells
whether the user moved on. A test pins it.

The blocklibrary params string names the `truncated` field both results
return.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* Blocks palette: a transparent command ending inside INSERT is not its end; bound library reads

A transparent command that finishes inside a running one ('ZOOM in an INSERT)
recorded its end under the outer command's number. With an object added
meanwhile, cancelling the resumed INSERT looked like a placement and the
palette started another. Only a top-level command records its end now.

Switching quickly between big libraries started a reading thread for each. At
most four libraries are read at once; a further one waits, and the palette
says it is reading until a place comes back.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* Blocks palette: BLOCKLIBIMPORT never replaces a block; BLOCKSPALETTE counts every object that draws one

BLOCKLIBIMPORT refuses a name the drawing already has (any letter case),
the rule the palette already follows, so a direct JSON call can no longer
change existing references. BLOCKSPALETTE counts block references,
dimensions and multileader block content through `referenced_block`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Blocks palette: a library file must be named .dwg or .dxf

cached_drawing read any regular path, and the reader sniffs the bytes, so a
DXF named part.bin was listed by BLOCKLIBRARY, imported by BLOCKLIBIMPORT and
loaded by the palette. It now refuses other names before the cache lookup, so
the three callers share the check. Regression test with a part.bin.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* Blocks palette: library listing needs no drawing; a preview's two passes share one deadline

BLOCKLIBRARY and BLOCKLIBRARIES ignore the session but had the has_doc guard,
so the palette opened on the Start tab got "no drawing open", kept an empty
library list and never reloaded it. They are now always enabled; BLOCKLIBIMPORT
still needs a drawing.

outline() gave each of its two build passes the full PREVIEW_DEADLINE, so one
preview could take twice the limit. The detailed pass now gets what the coarse
pass left.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* Blocks palette: a dimension override given as a number names its style, linetype or arrowhead

DIMOVERRIDE keeps a number as given (DIMTXSTY 123) and the dimension is
drawn with it as text, but the in-use scans only read strings, so PURGE,
style deletion and import cleanup could drop a style, linetype or
arrowhead block still in use. `purge::override_name` reads either, for
text styles, linetypes, arrowheads (main's `override_arrows` had the same
defect) and the arrowhead rename at import, which now rewrites an
override only when its block is really renamed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Blocks palette: put show()'s doc comment back on show()

right_click_cancels_placing had been inserted between show()'s doc comment
(the cursor-visibility contract, #1701) and show() itself, so the contract
documented the wrong function. Moved, no code change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* i18n(ka): add the 27 Blocks palette strings

Georgian rows for the strings this PR adds to the other catalogs (cargo test -p cadcraft-ui-egui translation_gaps -- --ignored --nocapture lists none of them any more).

---------

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-18 08:43:17 +07:00

79 lines
3.4 KiB
Rust

//! Enter or Space at INSERT's insertion point prompt asks again, as in AutoCAD 2027; Escape ends
//! the command (the Blocks palette's Repeat Placement stops this way).
use cadcraft_engine::geom::Vec2;
use cadcraft_engine::{Input, Session};
use serde_json::json;
#[test]
fn enter_at_the_insertion_point_asks_again() {
let mut s = Session::new();
s.execute("line", &json!({"points": [[0, 0], [10, 0]]})).unwrap();
let hs: Vec<String> = s.doc().unwrap().model.iter().map(|e| e.handle.hex()).collect();
s.execute("block", &json!({"name": "A", "base": [0, 0], "handles": hs, "keep": "delete"})).unwrap();
s.start("insert").unwrap();
s.input(Input::Text("A".into())).unwrap();
assert!(s.prompt_text().starts_with("INSERT Specify insertion point"), "{}", s.prompt_text());
s.input(Input::Enter).unwrap();
assert!(s.prompt_text().starts_with("INSERT Specify insertion point"), "{}", s.prompt_text());
s.input(Input::Cancel).unwrap();
assert_eq!(s.prompt_text(), "Command:");
assert_eq!(s.doc().unwrap().model.iter().count(), 0, "nothing is placed");
assert_eq!(s.last_finished, None, "ending it this way is cancelling it");
}
/// A cancelled INSERT leaves the drawing as it was: it takes no undo step, or the next undo would
/// seem to do nothing (Repeat Placement always ends this way). A placed block keeps its name.
#[test]
fn a_cancelled_insert_takes_no_undo_step() {
let mut s = Session::new();
s.execute("line", &json!({"points": [[0, 0], [10, 0]]})).unwrap();
let hs: Vec<String> = s.doc().unwrap().model.iter().map(|e| e.handle.hex()).collect();
s.execute("block", &json!({"name": "A", "base": [0, 0], "handles": hs, "keep": "delete"})).unwrap();
let steps = s.state().unwrap().undo.len();
s.start("insert").unwrap();
s.input(Input::Text("A".into())).unwrap();
s.input(Input::Cancel).unwrap();
assert_eq!(s.state().unwrap().undo.len(), steps);
assert_eq!(s.doc().unwrap().header.str("INSNAME", ""), "");
s.start("insert").unwrap();
s.input(Input::Text("A".into())).unwrap();
s.input(Input::Keyword("Scale".into())).unwrap();
s.input(Input::Text("1".into())).unwrap();
s.input(Input::Keyword("Rotate".into())).unwrap();
s.input(Input::Text("0".into())).unwrap();
s.input(Input::Point(Vec2::new(1.0, 1.0))).unwrap();
assert_eq!(s.state().unwrap().undo.len(), steps + 1);
assert_eq!(s.doc().unwrap().header.str("INSNAME", ""), "A");
}
/// How the last command ended: by itself, with what it added; a cancelled one changes nothing.
#[test]
fn the_last_finished_command_says_whether_it_added_objects() {
let mut s = Session::new();
s.start("line").unwrap();
s.input(Input::Point(Vec2::new(0.0, 0.0))).unwrap();
s.input(Input::Point(Vec2::new(1.0, 1.0))).unwrap();
s.input(Input::Enter).unwrap();
let line = (s.commands_started, true);
assert_eq!(s.last_finished, Some(line));
s.start("circle").unwrap();
s.input(Input::Cancel).unwrap();
assert_eq!(s.last_finished, Some(line));
}
/// A transparent command runs inside the current one: it does not count as a new command.
#[test]
fn only_a_command_of_its_own_counts_as_started() {
let mut s = Session::new();
s.start("line").unwrap();
let started = s.commands_started;
s.start("'zoom").unwrap();
assert_eq!(s.commands_started, started);
s.start("circle").unwrap();
assert_eq!(s.commands_started, started + 1);
}