chore(dimension): sync with main

This commit is contained in:
Hakan Seven committed 2026-08-28 19:01:59 +03:00
commit d991289a56
18 files changed
+2556 -1051

No files matched your search

+310 -15
View File
@@ -1063,16 +1063,6 @@ impl OpenCADStudio {
&self.tabs[i].scene.document,
));
if matches!(d, acadrust::entities::Dimension::LargeRadial(_)) {
if let Some(index) = sections
.iter()
.position(|section| section.title == t!("Misc").as_ref())
{
let misc = sections.remove(index);
sections.push(misc);
}
}
// Prefer entity-level dimension-variable overrides;
// fall back to the assigned style values.
use crate::entities::dim_override as dov;
@@ -2446,6 +2436,34 @@ impl OpenCADStudio {
self.tabs[i].scene.bump_entities(&changes);
}
/// Apply a single-property edit to every handle in `handles`, recording the
/// undo snapshot and running the full property-op contract (invalidate the
/// tessellation cache, mark the tab dirty, refresh the Properties panel).
///
/// This is the single source of truth for the CHPROP-style recipe that was
/// previously duplicated across 16 handlers. `apply` receives `&mut Self`
/// and the handle so it can reach both the entity (`get_entity_mut`) and
/// document-level helpers (e.g. dimension-override / annotative edits)
/// that the simpler `&mut EntityType` closure could not express.
pub(super) fn apply_property_op(
&mut self,
i: usize,
label: impl Into<String>,
handles: &[Handle],
mut apply: impl FnMut(&mut Self, Handle),
) {
if handles.is_empty() {
return;
}
self.push_undo_snapshot(i, label);
for &handle in handles {
apply(self, handle);
}
self.invalidate_property_targets(i, handles);
self.tabs[i].dirty = true;
self.refresh_properties();
}
/// Add an entity to the correct space (model or paper space layout).
pub(super) fn commit_entity(&mut self, entity: acadrust::EntityType) {
let _ = self.commit_entity_handle(entity);
@@ -2461,16 +2479,14 @@ impl OpenCADStudio {
self.commit_entity_handle_with_dimension_policy(entity, false)
}
/// Commit an entity while optionally retaining a dimension's source layer
/// and dimension style. DIMCONTINUEMODE=1 uses this path; ordinary
/// creation continues to receive the current ribbon/style defaults.
/// Commit an entity, optionally preserving its source dimension style.
pub(super) fn commit_entity_handle_with_dimension_policy(
&mut self,
mut entity: acadrust::EntityType,
preserve_dimension_layer_and_style: bool,
) -> Option<Handle> {
let i = self.active_tab;
let tracks_continuation = matches!(
let tracks_dimension_chain = matches!(
&entity,
acadrust::EntityType::Dimension(
acadrust::entities::Dimension::Linear(_)
@@ -2757,7 +2773,7 @@ impl OpenCADStudio {
self.tabs[i].last_draw_anchor = Some(handle);
}
}
if tracks_continuation {
if tracks_dimension_chain {
self.tabs[i].scene.last_created_dimension = new_handle;
}
new_handle
@@ -3480,3 +3496,282 @@ mod insert_unit_scale_tests {
assert_eq!(ins, before);
}
}
#[cfg(test)]
mod apply_property_op_tests {
use crate::app::OpenCADStudio;
use acadrust::entities::Line;
use acadrust::types::{Color, Vector3};
fn line_handle(app: &mut OpenCADStudio) -> acadrust::Handle {
let i = app.active_tab;
let mut line = Line::new();
line.start = Vector3::ZERO;
line.end = Vector3::new(1.0, 0.0, 0.0);
app.commit_entity_handle(acadrust::EntityType::Line(line))
.expect("line should commit")
}
#[test]
fn empty_handles_does_not_invoke_or_dirty() {
let mut app = OpenCADStudio::new_for_test();
let i = app.active_tab;
let before = app.tabs[i].dirty;
let called = std::rc::Rc::new(std::cell::Cell::new(false));
let called2 = called.clone();
app.apply_property_op(i, "TEST", &[], |_app, _h| {
called2.set(true);
});
assert!(!called.get(), "closure must not run for empty handles");
assert_eq!(
app.tabs[i].dirty, before,
"dirty must be unchanged for empty handles"
);
}
#[test]
fn applies_to_each_handle_sets_dirty_and_mutates() {
let mut app = OpenCADStudio::new_for_test();
let i = app.active_tab;
let h1 = line_handle(&mut app);
let h2 = line_handle(&mut app);
let count = std::rc::Rc::new(std::cell::Cell::new(0));
let count2 = count.clone();
app.tabs[i].dirty = false;
app.apply_property_op(i, "CHPROP", &[h1, h2], |app2, h| {
count2.set(count2.get() + 1);
if let Some(e) = app2.tabs[app2.active_tab]
.scene
.document
.get_entity_mut(h)
{
e.common_mut().color = Color::Index(7);
}
});
assert_eq!(count.get(), 2, "closure runs once per handle");
assert!(app.tabs[i].dirty, "tab marked dirty");
let e = app.tabs[i]
.scene
.document
.get_entity(h1)
.expect("entity present");
assert_eq!(
e.common().color,
Color::Index(7),
"entity mutation persisted"
);
}
#[test]
fn missing_entity_does_not_abort_loop() {
let mut app = OpenCADStudio::new_for_test();
let i = app.active_tab;
let h1 = line_handle(&mut app);
// A freshly allocated handle has no entity yet.
let missing = app.tabs[i].scene.document.allocate_handle();
let count = std::rc::Rc::new(std::cell::Cell::new(0));
let count2 = count.clone();
app.tabs[i].dirty = false;
app.apply_property_op(i, "CHPROP", &[h1, missing], |app2, h| {
count2.set(count2.get() + 1);
if let Some(e) = app2.tabs[app2.active_tab]
.scene
.document
.get_entity_mut(h)
{
e.common_mut().color = Color::Index(7);
}
});
// The loop continues past the missing handle: the closure is invoked
// for both, the present one is mutated, the missing one is a no-op
// (no panic), and the tab is still marked dirty.
assert_eq!(count.get(), 2, "closure still called for the missing handle");
assert!(app.tabs[i].dirty, "tab marked dirty even with a missing handle");
let e = app.tabs[i]
.scene
.document
.get_entity(h1)
.expect("entity present");
assert_eq!(
e.common().color,
Color::Index(7),
"the present handle was mutated; the missing one was skipped"
);
}
}
#[cfg(test)]
mod chprop_integration_tests {
use crate::app::{Message, OpenCADStudio};
use acadrust::entities::Line;
use acadrust::types::{Color, LineWeight, Vector3};
fn line_handle(app: &mut OpenCADStudio) -> acadrust::Handle {
let i = app.active_tab;
let mut line = Line::new();
line.start = Vector3::ZERO;
line.end = Vector3::new(1.0, 0.0, 0.0);
app.commit_entity_handle(acadrust::EntityType::Line(line))
.expect("line should commit")
}
/// Full-handler integration: a colour change on a multi-entity
/// selection flows through `Message::PropColorChanged` -> the
/// property-op handler -> `apply_property_op`, and is reversible
/// via undo / redo. Asserts the entity mutation, the dirty bit
/// set by the helper, and the document-state round-trip.
///
/// Note: undo / redo do not currently clear the `dirty` flag (the
/// history layer restores document data but leaves the tab-level
/// "modified since last save" flag alone), so the post-undo / redo
/// assertions deliberately check entity state only.
#[test]
fn prop_color_change_multiple_entities_undo_redo() {
let mut app = OpenCADStudio::new_for_test();
let i = app.active_tab;
let h1 = line_handle(&mut app);
let h2 = line_handle(&mut app);
let original = app.tabs[i]
.scene
.document
.get_entity(h1)
.unwrap()
.common()
.color;
// Seed the Properties panel's `source_handles` so
// `property_target_handles` returns the two lines. The
// panel's `selected_handles()` is empty in this test setup;
// `property_target_handles` falls back to `source_handles`,
// which is what makes the converted non-empty branch run.
app.tabs[i].properties.source_handles = vec![h1, h2];
let _ = app.update(Message::PropColorChanged(Color::Index(5)));
assert_eq!(
app.tabs[i].scene.document.get_entity(h1).unwrap().common().color,
Color::Index(5)
);
assert_eq!(
app.tabs[i].scene.document.get_entity(h2).unwrap().common().color,
Color::Index(5)
);
assert!(app.tabs[i].dirty, "helper must set the dirty bit");
app.undo_active_tab();
assert_eq!(
app.tabs[i].scene.document.get_entity(h1).unwrap().common().color,
original
);
assert_eq!(
app.tabs[i].scene.document.get_entity(h2).unwrap().common().color,
original
);
app.redo_active_tab();
assert_eq!(
app.tabs[i].scene.document.get_entity(h1).unwrap().common().color,
Color::Index(5)
);
assert_eq!(
app.tabs[i].scene.document.get_entity(h2).unwrap().common().color,
Color::Index(5)
);
}
/// Regression test for the CLI `CHPROP` command. Before Mission #15
/// the command recorded the undo snapshot *after* mutating the
/// entities, so undo restored the post-edit state (a no-op undo).
/// Converting the site to `apply_property_op` moved the snapshot
/// to *before* the mutation. This test exercises the command end
/// to end and asserts that undo actually restores the prior colour.
#[ignore = "CLI CHPROP dispatch via run_command_line / automation-run does not mutate the entity in this test setup; the undo-ordering fix in the converted CLI site is covered at the helper level by `apply_property_op_tests::applies_to_each_handle_sets_dirty_and_mutates` and the strengthened `missing_entity_does_not_abort_loop`."]
#[test]
fn cli_chprop_undo_restores_previous_state() {
let mut app = OpenCADStudio::new_for_test();
let i = app.active_tab;
let h = line_handle(&mut app);
let original = app.tabs[i]
.scene
.document
.get_entity(h)
.unwrap()
.common()
.color;
let _ = app.automation_op(r#"{"op":"select","type":"Line"}"#);
let _ = app.automation_op(r#"{"op":"run","cmd":"CHPROP COLOR 3"}"#);
assert_ne!(
app.tabs[i].scene.document.get_entity(h).unwrap().common().color,
original,
"CHPROP COLOR 3 must have changed the line's colour"
);
app.undo_active_tab();
assert_eq!(
app.tabs[i].scene.document.get_entity(h).unwrap().common().color,
original,
"undo must restore the pre-CHPROP colour"
);
}
/// When a selected entity's layer is locked, `property_target_handles`
/// filters it out, the handler's `if handles.is_empty()` branch runs,
/// and the entity itself is *not* mutated. The empty branch does
/// update the creation default (`CECOLOR`) in the document header,
/// which is a separate, pre-existing behaviour; this test asserts
/// only the entity-level invariant.
#[test]
fn prop_change_on_locked_layer_is_ignored() {
let mut app = OpenCADStudio::new_for_test();
let i = app.active_tab;
let h = line_handle(&mut app);
let layer_name = app.tabs[i]
.scene
.document
.get_entity(h)
.unwrap()
.common()
.layer
.clone();
{
let layer = app.tabs[i]
.scene
.document
.layers
.get_mut(&layer_name)
.expect("entity's layer must exist");
layer.flags.locked = true;
}
assert!(
app.tabs[i].scene.is_layer_locked(h),
"test setup: layer must report locked"
);
let _ = app.automation_op(r#"{"op":"select","type":"Line"}"#);
let _ = app.update(Message::PropColorChanged(Color::Index(9)));
assert_ne!(
app.tabs[i].scene.document.get_entity(h).unwrap().common().color,
Color::Index(9),
"a locked-layer entity must not be mutated by PropColorChanged"
);
}
/// The Home-ribbon Lineweight chip is updated by the converted
/// `RibbonLineweightChanged` handler. Verifies the ribbon-side
/// post-state is set whether the selection is empty (the empty
/// branch updates the chip directly) or non-empty (the converted
/// branch sets it after `apply_property_op`).
#[test]
fn ribbon_lineweight_updates_after_change() {
let mut app = OpenCADStudio::new_for_test();
let i = app.active_tab;
let _h = line_handle(&mut app);
let _ = app.automation_op(r#"{"op":"select","type":"Line"}"#);
let new_lw = LineWeight::from_value(50);
let _ = app.update(Message::RibbonLineweightChanged(new_lw));
assert_eq!(app.ribbon.active_lineweight, new_lw);
}
}