Undo restores what a hide put away (#2219) (#4780)

* Undo: what is hidden lives in the snapshot, so hiding is a step (#2219)

`Session::execute` records a step only when the document `Arc` changes, and what is
hidden lives in `Session::visibility` — saved with the design, but outside the
document. `browser.visibility` was also declared `.noundo()`, and `Snapshot` had no
field for it, so even a recorded step could not put a hidden body back: undo went to
the command before the hide and left it hidden (#2219).

`Snapshot` and `Checkpoint` now carry the `Visibility`, `go_to` restores it, and
`Session::execute` treats a visibility change as a change of its own. That change is
display-only, so it refreshes the view with a revision bump (`refresh_display`)
instead of evaluating a model a hide does not touch.

The command also takes `folder: "all"` (every body, sketch, construction item, origin
item and joint, plus the browser folders and occurrence hides) so Show All can be one
step, and asking for the state an item already has no longer reorders the hidden
lists: a redundant hide is not a step and leaves no unsaved changes.

* Show/Hide: the menus run browser.visibility, so the history has it (#2219)

The hide and show actions wrote `session.visibility` directly instead of running the
command: the body and face menus (`ui.hide` / `ui.show`), the `V` key
(`context_menu::toggle_visibility`), the browser rows' eyes, the Origin rows and
`ui.originAll` / `ui.originPart`. The engine could record a step and there was
nothing to record — which is why the release still answered "nothing to undo" after
hiding from the Browser.

They all run `browser.visibility` now (the repo rule that the UI acts through
commands), and `ui.showAll` is that one `folder: "all"` call, so Show All is one undo
step rather than three. `docs/occurrence-workflows.md` says what visibility does in
the history.
This commit is contained in:
imanu86 authored and GitHub committed 2026-10-24 22:12:17 +00:00
1 parent 1dbb13cad2
commit dbdeeec2e9
11 files changed
+225 -88

No files matched your search

+4 -3
View File
@@ -2556,14 +2556,15 @@ pub struct Document {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub named_views: Vec<NamedView>,
/// What was hidden when the design was saved. A session takes it from here when the design
/// opens and keeps the live state itself (`browser.visibility`: saved, not an undo step), so
/// a document in a session leaves this empty; the session puts it back when it saves.
/// opens and keeps the live state itself (`browser.visibility`: saved, and undone like any
/// other change), so a document in a session leaves this empty; the session puts it back when
/// it saves.
#[serde(default, skip_serializing_if = "Visibility::is_empty")]
pub visibility: Visibility,
}
/// The browser's view of a design: items and folders hidden or shown, sketch profiles and
/// dimensions shown, bodies locked. Saved with the design, but not an undo step.
/// dimensions shown, bodies locked. Saved with the design and an undo step of its own.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct Visibility {
+119 -19
View File
@@ -42,12 +42,12 @@ pub static COMMANDS: &[CommandSpec] = &[
"view?: front|back|top|bottom|left|right|iso|home (frames the model) | a named view's name; or camera?: {target: [x,y,z], yaw, pitch, distance, fov?, roll?} (radians, mm; roll defaults to 0, must be finite and is clamped to ±π; up is refused: the up axis comes from the design settings) → camera. The viewport (and the MCP screenshot) moves there. View state: not saved, not an undo step",
),
CommandSpec::new("browser.order", "Order Browser Items", order).icon("reorder").params("folder, component?, order: [keys] (the folder's items in display order)"),
CommandSpec::new("browser.visibility", "Show/Hide", visibility).icon("eye").noundo().params(
CommandSpec::new("browser.visibility", "Show/Hide", visibility).icon("eye").params(
"visible: bool; items?: [sketch id or name | body name | construction name | joint name | O|X|Y|Z|XY|XZ|YZ]; \
or joints?: [joint id or name] (their glyphs; they still constrain and move parts); \
or folder?: bodies|sketches|construction|origin|joints (all of it; showing also shows its browser folders and groups); \
or folder?: bodies|sketches|construction|origin|joints|all (all of it; showing also shows its browser folders and groups, and `all` takes in the occurrence hides too); \
or tree_folders?: [browser folder or group key: c0/bodies, o:4/sketches, c0/g7, c0/joints, c0/canvases] (the folder or group itself: its items keep their own state); \
or paths?: [[occurrence ids]] (exact placements and descendants), isolate?: bool (requires paths and visible:true). Saved with the design; not an undo step",
or paths?: [[occurrence ids]] (exact placements and descendants), isolate?: bool (requires paths and visible:true). Saved with the design and undone like any other change (the camera is not)",
),
CommandSpec::new("browser.expand_all", "Expand All", expand_all).icon("folder").noundo().params(
"path?: [occurrence ids] (that placement and its descendants); component?: id (that component's own folders); \
@@ -331,7 +331,17 @@ fn visibility(s: &mut Session, p: &Value) -> Result<Value> {
(Some("construction"), None) => s.doc.features.iter().filter(|f| is_construction(&f.kind)).map(|f| Shown::Origin(f.name.clone())).collect(),
(Some("origin"), None) => ORIGIN.iter().map(|o| Shown::Origin(o.to_string())).collect(),
(Some("joints"), None) => s.doc.assembly.joints.iter().map(|j| Shown::Joint(j.id)).collect(),
(Some(f), None) => return Err(bad(cmd, format!("unknown folder `{f}` (bodies, sketches, construction, origin, joints)"))),
// Everything at once, for a single "show all" step: every body, sketch, construction
// item, origin item and joint.
(Some("all"), None) => {
let mut all: Vec<Shown> = s.world_state().bodies.iter().map(|b| Shown::Body(b.name.clone())).collect();
all.extend(s.model.state().sketches.iter().map(|x| Shown::Sketch(x.feature)));
all.extend(s.doc.features.iter().filter(|f| is_construction(&f.kind)).map(|f| Shown::Origin(f.name.clone())));
all.extend(ORIGIN.iter().map(|o| Shown::Origin(o.to_string())));
all.extend(s.doc.assembly.joints.iter().map(|j| Shown::Joint(j.id)));
all
}
(Some(f), None) => return Err(bad(cmd, format!("unknown folder `{f}` (bodies, sketches, construction, origin, joints, all)"))),
(None, Some(_)) => {
let keys = keys(p, "items", cmd)?;
if keys.is_empty() {
@@ -346,39 +356,58 @@ fn visibility(s: &mut Session, p: &Value) -> Result<Value> {
};
// Showing all of a kind also shows the browser folders and groups that hold it.
if visible && let Some(f) = str_(p, "folder") {
let groups: Vec<String> = s.doc.browser_groups.iter().filter(|g| g.folder == f).map(|g| format!("/g{}", g.id)).collect();
let own = format!("/{f}");
s.visibility.hidden_folders.retain(|k| !k.ends_with(&own) && !groups.iter().any(|g| k.ends_with(g.as_str())));
if f == "all" {
// Everything includes the folders and groups themselves.
s.visibility.hidden_folders.clear();
} else {
let groups: Vec<String> = s.doc.browser_groups.iter().filter(|g| g.folder == f).map(|g| format!("/g{}", g.id)).collect();
let own = format!("/{f}");
s.visibility.hidden_folders.retain(|k| !k.ends_with(&own) && !groups.iter().any(|g| k.ends_with(g.as_str())));
}
}
let v = &mut s.visibility;
match str_(p, "folder") {
Some("bodies") if visible => v.hidden_occurrences.clear(),
Some("bodies" | "all") if visible => v.hidden_occurrences.clear(),
// Showing a whole folder shows the folder too.
Some("origin") if visible => v.origin_hidden = Some(false),
_ => {}
}
for it in &items {
// Each item ends in the state asked for, and asking for the state it already has leaves
// the lists exactly as they were: no reordering, so a redundant hide shows up as no
// change at all (no undo step, no unsaved changes).
match it {
Shown::Sketch(id) => {
v.hidden_sketches.retain(|x| x != id);
v.shown_sketches.retain(|x| x != id);
if visible { v.shown_sketches.push(*id) } else { v.hidden_sketches.push(*id) }
if visible {
v.hidden_sketches.retain(|x| x != id);
if !v.shown_sketches.contains(id) {
v.shown_sketches.push(*id);
}
} else {
v.shown_sketches.retain(|x| x != id);
if !v.hidden_sketches.contains(id) {
v.hidden_sketches.push(*id);
}
}
}
Shown::Body(n) => {
v.hidden_bodies.retain(|x| x != n);
if !visible {
if visible {
v.hidden_bodies.retain(|x| x != n);
} else if !v.hidden_bodies.contains(n) {
v.hidden_bodies.push(n.clone());
}
}
Shown::Origin(n) => {
v.hidden_origin.retain(|x| x != n);
if !visible {
if visible {
v.hidden_origin.retain(|x| x != n);
} else if !v.hidden_origin.contains(n) {
v.hidden_origin.push(n.clone());
}
}
Shown::Joint(id) => {
v.hidden_joints.retain(|x| x != id);
if !visible {
if visible {
v.hidden_joints.retain(|x| x != id);
} else if !v.hidden_joints.contains(id) {
v.hidden_joints.push(*id);
}
}
@@ -1327,12 +1356,14 @@ mod tests {
};
let (n0, undo0, doc0) = (lines(&s), s.undo.len(), s.doc.clone());
assert_eq!(shown(&mut s), 2);
// By id and by name; the view loses their lines, the design and its history don't change.
// By id and by name; the view loses their lines, the design still doesn't change: hiding
// is an undo step of its own (#2219), and the step carries no document change.
let r = run(&mut s, "browser.visibility", json!({"items": [floor, drain_name], "visible": false}));
assert_eq!(r["changed"], 2);
assert_eq!(shown(&mut s), 0);
assert!(lines(&s) < n0);
assert!(std::sync::Arc::ptr_eq(&s.doc, &doc0) && s.undo.len() == undo0, "view state, not a design change");
assert!(std::sync::Arc::ptr_eq(&s.doc, &doc0), "view state, not a design change");
assert_eq!(s.undo.len(), undo0 + 1, "hiding is one undo step");
run(&mut s, "browser.visibility", json!({"folder": "sketches", "visible": true}));
assert_eq!(lines(&s), n0);
// The browser's Sketches folder hidden: sketches shown one by one aren't drawn either,
@@ -1369,6 +1400,75 @@ mod tests {
assert_eq!(s.visibility, before);
}
/// #2219: hiding is a step of its own, so Ctrl+Z brings the body back instead of undoing the
/// command before the hide, and Ctrl+Y hides it again.
#[test]
fn hiding_a_body_is_undone_and_redone() {
let mut s = Session::default();
run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10, "body_name": "Box"}));
s.mark_saved();
let cam = solidcraft_render::Camera::default();
let drawn = |s: &Session| crate::view::scene(s, &cam).meshes.len();
assert!(drawn(&s) > 0, "the box is drawn");
assert!(!s.is_dirty());
let steps = s.undo.len();
// Hide: one step, no document change, the view loses the body, the design has changes.
let doc = s.doc.clone();
run(&mut s, "browser.visibility", json!({"items": ["Box"], "visible": false}));
assert_eq!(s.undo.len(), steps + 1, "hiding is one undo step");
assert!(std::sync::Arc::ptr_eq(&s.doc, &doc), "hiding leaves the document alone");
assert!(!s.body_visible("Box") && drawn(&s) == 0);
assert!(s.is_dirty(), "hiding is a change to save");
// Undo: the hide is what goes back, not the box the hide came after.
let r = run(&mut s, "edit.undo", json!({}));
assert_eq!(r["undone"], "Show/Hide", "{r}");
assert!(s.body_visible("Box") && drawn(&s) > 0);
assert!(s.model.state().bodies.iter().any(|b| b.name == "Box"), "the box itself stays");
assert!(!s.is_dirty(), "back to what the file holds");
// Redo hides it again, and undoing twice more leaves the design as it was.
run(&mut s, "edit.redo", json!({}));
assert!(!s.body_visible("Box") && s.is_dirty());
run(&mut s, "edit.undo", json!({}));
assert!(s.body_visible("Box") && !s.is_dirty());
// Hiding a whole folder is one step too, and undoing it brings the body back.
run(&mut s, "browser.visibility", json!({"folder": "bodies", "visible": false}));
assert!(!s.body_visible("Box"));
run(&mut s, "edit.undo", json!({}));
assert!(s.body_visible("Box") && s.visibility.is_empty() && !s.is_dirty());
// Hiding or showing what is already so changes nothing: no step, and the lists are not
// even reordered, so the design is not left with unsaved changes either.
let steps = s.undo.len();
run(&mut s, "browser.visibility", json!({"items": ["Box"], "visible": true}));
assert_eq!(s.undo.len(), steps, "showing a body that is already shown is no change");
run(&mut s, "browser.visibility", json!({"items": ["Box"], "visible": false}));
run(&mut s, "browser.visibility", json!({"items": ["Box"], "visible": false}));
assert_eq!(s.undo.len(), steps + 1, "the first hide is one step, hiding it again is none");
assert_eq!(s.visibility.hidden_bodies, vec!["Box".to_string()], "no duplicate, no reordering");
assert!(s.is_dirty(), "the first hide is still a change to save");
run(&mut s, "browser.visibility", json!({"items": ["Box"], "visible": true}));
assert_eq!(s.undo.len(), steps + 2, "showing it again is one step");
assert!(s.visibility.hidden_bodies.is_empty() && !s.is_dirty());
// `all` shows everything (folders and occurrence hides included) as one step.
run(&mut s, "solid.box", json!({"length": 4, "width": 4, "height": 4, "corner": [30, 0, 0], "operation": "new", "body_name": "Second"}));
run(&mut s, "browser.visibility", json!({"folder": "bodies", "visible": false}));
run(&mut s, "browser.visibility", json!({"tree_folders": ["c0/bodies"], "visible": false}));
assert_eq!(s.visibility.hidden_bodies.len(), 2);
assert_eq!(s.visibility.hidden_folders, vec!["c0/bodies".to_string()]);
let steps = s.undo.len();
run(&mut s, "browser.visibility", json!({"folder": "all", "visible": true}));
assert!(s.visibility.is_empty(), "all shows everything: {:?}", s.visibility);
assert_eq!(s.undo.len(), steps + 1, "one step for all of it");
run(&mut s, "edit.undo", json!({}));
assert_eq!(s.visibility.hidden_bodies.len(), 2, "and one undo puts it back");
assert_eq!(s.visibility.hidden_folders, vec!["c0/bodies".to_string()]);
}
/// A design file can carry any id, the largest a `u64` holds too: a new group then has no id
/// to take. That is an error, not an overflow (a panic where overflow is checked, a duplicate
/// id where it is not).
+15 -2
View File
@@ -92,15 +92,28 @@ fn redo(s: &mut Session, _p: &Value) -> Result<Value> {
/// Put the session at `snap`; returns where it was, under the same label.
fn go_to(s: &mut Session, snap: Snapshot) -> Snapshot {
let was = Snapshot { label: snap.label, doc: s.doc.clone(), active_sketch: s.active_sketch, pending_moves: s.pending_moves.clone() };
let was = Snapshot {
label: snap.label,
doc: s.doc.clone(),
active_sketch: s.active_sketch,
pending_moves: s.pending_moves.clone(),
visibility: s.visibility.clone(),
};
let same = std::sync::Arc::ptr_eq(&s.doc, &snap.doc);
// Undoing a hide (or a show) leaves the document alone: the model needs no evaluating.
let display_only = same && s.visibility != snap.visibility;
s.doc = snap.doc;
s.active_sketch = snap.active_sketch;
s.pending_moves = snap.pending_moves;
s.visibility = snap.visibility;
s.selection.clear();
// A new document is evaluated by `execute`.
if same {
s.refresh();
if display_only {
s.refresh_display();
} else {
s.refresh();
}
}
was
}
+4 -4
View File
@@ -1019,9 +1019,9 @@ fn joint(s: &Session, name: &str) -> u64 {
s.doc.assembly.joints.iter().find(|j| j.name == name).map(|j| j.id).unwrap()
}
/// #1160: hiding a joint hides only its glyph. It is saved with the design (not an undo step,
/// like other visibility), the hinge still drives its leaf while hidden, and the folder hides
/// and shows every joint at once.
/// #1160: hiding a joint hides only its glyph. It is saved with the design and is an undo step
/// of its own (#2219, like other visibility), the hinge still drives its leaf while hidden, and
/// the folder hides and shows every joint at once.
#[test]
fn hidden_joints_still_drive_and_are_saved_with_the_design() {
let (dir, path) = temp_design("saved", "json");
@@ -1032,7 +1032,7 @@ fn hidden_joints_still_drive_and_are_saved_with_the_design() {
let r = run(&mut s, "browser.visibility", json!({"joints": ["Hinge"], "visible": false}));
assert_eq!(r["changed"], 1, "{r}");
assert!(!s.joint_visible(hinge) && s.joint_visible(glue));
assert_eq!(s.undo.len(), undo, "hiding is not an undo step");
assert_eq!(s.undo.len(), undo + 1, "hiding is one undo step");
assert!(s.is_dirty(), "hiding leaves unsaved changes");
let l = run(&mut s, "joint.list", json!({}));
assert_eq!(l["joints"].as_array().unwrap().iter().map(|j| j["visible"].clone()).collect::<Vec<_>>(), [json!(false), json!(true)]);
+4 -4
View File
@@ -490,10 +490,10 @@ fn temp_design(tag: &str, ext: &str) -> (std::path::PathBuf, String) {
}
/// #531: hiding a placement and a body is saved with the design and comes back when it opens.
/// It is not an undo step (undoing a later feature leaves it hidden), but the design then has
/// unsaved changes, and showing it again is saved the same way.
/// Each hide is an undo step of its own (#2219, so undoing a later feature still leaves it
/// hidden), the design then has unsaved changes, and showing it again is saved the same way.
#[test]
fn hidden_placements_and_bodies_are_saved_with_the_design_but_not_undone() {
fn hidden_placements_and_bodies_are_saved_with_the_design() {
let (dir, path) = temp_design("saved", "solidcraft");
let mut s = Session::default();
run(&mut s, "solid.box", json!({"length":4,"width":4,"height":4,"body_name":"Base"}));
@@ -507,7 +507,7 @@ fn hidden_placements_and_bodies_are_saved_with_the_design_but_not_undone() {
let undo = s.undo.len();
run(&mut s, "browser.visibility", json!({"visible":false,"paths":[[second]]}));
run(&mut s, "browser.visibility", json!({"visible":false,"items":["Base"]}));
assert_eq!(s.undo.len(), undo, "hiding is not an undo step");
assert_eq!(s.undo.len(), undo + 2, "each hide is one undo step");
assert!(s.is_dirty(), "hiding leaves the design with unsaved changes");
run(&mut s, "solid.box", json!({"length":1,"width":1,"height":1,"corner":[50,0,0],"operation":"new","body_name":"Later"}));
run(&mut s, "edit.undo", json!({}));
+40 -7
View File
@@ -169,6 +169,9 @@ pub struct Snapshot {
pub doc: Arc<Document>,
pub active_sketch: Option<u64>,
pub pending_moves: std::collections::BTreeMap<u64, solidcraft_doc::Mat>,
/// What was hidden or shown then (`browser.visibility`). It is saved with the design, so
/// hiding a body is undone and redone like any other change.
pub visibility: Visibility,
}
/// Where the open sketch was entered (`sketch.create`, `sketch.edit`): the design before it and
@@ -311,8 +314,9 @@ pub struct Session {
/// The seams `inspect.continuity` last measured, for the viewport to shade. View state: not
/// part of the design and not an undo step.
pub continuity: Vec<ContinuitySeam>,
/// What is hidden or shown in the view (`browser.visibility`): saved with the design, not an
/// undo step. A design brings its saved visibility when it opens (`Session::new`).
/// What is hidden or shown in the view (`browser.visibility`): saved with the design and an
/// undo step of its own ([`Snapshot::visibility`]). A design brings its saved visibility when
/// it opens (`Session::new`).
pub visibility: Visibility,
/// The camera `view.apply` asked the view to use (view state, not saved). The desktop app
/// moves its viewport there and takes it; headless renders keep using it until a standard
@@ -354,6 +358,7 @@ pub(crate) struct Checkpoint {
pub doc: Arc<Document>,
pub active_sketch: Option<u64>,
pub pending_moves: std::collections::BTreeMap<u64, solidcraft_doc::Mat>,
pub visibility: Visibility,
pub undo: Vec<Snapshot>,
pub redo: Vec<Snapshot>,
}
@@ -423,7 +428,7 @@ impl Session {
/// `doc`'s evaluation ([`Model::adopt`]); otherwise the timeline is evaluated.
/// `SOLIDCRAFT_NO_MODEL_CACHE` set always evaluates.
pub fn opened(mut doc: Document, saved: Option<&solidcraft_doc::SavedModel>) -> Self {
// The session keeps what is hidden (not an undo step); the design file carries it.
// The session keeps what is hidden (an undo step of its own); the design file carries it.
let visibility = std::mem::take(&mut doc.visibility);
let doc = Arc::new(doc);
let adopted = saved
@@ -784,6 +789,12 @@ impl Session {
&& (self.doc.differs_only_in_render(&before.doc) || self.doc.differs_only_in_part_properties(&before.doc))
}
/// Only what is hidden or shown changed (`browser.visibility`): the document and the model
/// are untouched, so the view refreshes without evaluating anything again.
pub(crate) fn visibility_only_change(&self, before: &Snapshot) -> bool {
Arc::ptr_eq(&self.doc, &before.doc) && self.visibility != before.visibility
}
fn refresh_render(&mut self, previous: &Arc<Document>) {
self.revision += 1;
// Geometry/placements have been proved identical. Retarget the cache's document key
@@ -796,6 +807,12 @@ impl Session {
}
}
/// Only what is hidden or shown changed: the model is untouched, so only the view revision
/// moves (the visibility caches are keyed on it) and the viewport redraws.
pub(crate) fn refresh_display(&mut self) {
self.revision += 1;
}
/// Show the sketch a live frame changed, leaving the features after it as they were last
/// evaluated (marked stale); a full `refresh` when the model cannot show it that way.
fn refresh_live(&mut self, edit: LiveEdit) {
@@ -889,6 +906,7 @@ impl Session {
doc: self.doc.clone(),
active_sketch: self.active_sketch,
pending_moves: self.pending_moves.clone(),
visibility: self.visibility.clone(),
};
// Where new geometry goes: not in the design, but a replay needs it (component.activate).
let active_before = (self.active_component, self.active_occurrence_path.clone());
@@ -939,11 +957,15 @@ impl Session {
crash::command_done(self.crash_journal, changed, &before.doc, after, spec.id, given, failed.map_or(Ok(()), Err));
}
let changed = !Arc::ptr_eq(&self.doc, &before.doc);
// Hiding or showing (`browser.visibility`) leaves the document alone, so it needs its own
// test: it is saved with the design and has an undo step of its own.
let hidden = self.visibility != before.visibility;
match r {
Ok((v, live_edit)) => {
let v = if changed { self.with_created(spec.id, params, &before, v) } else { v };
if changed {
if changed || hidden {
let render_only = self.render_only_change(&before);
let display_only = !changed && hidden;
let previous_doc = before.doc.clone();
if spec.undoable {
self.undo.push(before);
@@ -959,16 +981,18 @@ impl Session {
self.live_links = Some((sketch, self.doc.clone(), self.revision));
}
None if render_only => self.refresh_render(&previous_doc),
None if display_only => self.refresh_display(),
None => self.refresh_from(&previous_doc),
}
}
Ok(v)
}
Err(e) => {
if changed || self.pending_moves != before.pending_moves {
if changed || hidden || self.pending_moves != before.pending_moves {
self.doc = before.doc;
self.active_sketch = before.active_sketch;
self.pending_moves = before.pending_moves;
self.visibility = before.visibility;
self.refresh();
}
Err(e)
@@ -1007,6 +1031,7 @@ impl Session {
active_sketch: self.active_sketch,
undo: self.undo.clone(),
pending_moves: self.pending_moves.clone(),
visibility: self.visibility.clone(),
redo: self.redo.clone(),
});
if self.checkpoints.len() > MAX_CHECKPOINTS {
@@ -1020,16 +1045,24 @@ impl Session {
pub fn restore_checkpoint(&mut self, id: u64) -> Result<()> {
let i = self.checkpoints.iter().position(|c| c.id == id).ok_or_else(|| EngineError::Other(format!("no checkpoint {id}")))?;
let Some(c) = self.checkpoints.drain(i..).next() else { return Err(EngineError::Other(format!("no checkpoint {id}"))) };
let before =
Snapshot { label: String::new(), doc: self.doc.clone(), active_sketch: self.active_sketch, pending_moves: self.pending_moves.clone() };
let before = Snapshot {
label: String::new(),
doc: self.doc.clone(),
active_sketch: self.active_sketch,
pending_moves: self.pending_moves.clone(),
visibility: self.visibility.clone(),
};
self.doc = c.doc;
self.active_sketch = c.active_sketch;
self.pending_moves = c.pending_moves;
self.visibility = c.visibility;
self.undo = c.undo;
self.redo = c.redo;
self.selection.clear();
if self.render_only_change(&before) {
self.refresh_render(&before.doc);
} else if self.visibility_only_change(&before) {
self.refresh_display();
} else if !Arc::ptr_eq(&self.doc, &before.doc) || self.active_sketch != before.active_sketch || self.pending_moves != before.pending_moves {
self.refresh();
}
+1
View File
@@ -175,6 +175,7 @@ fn merging_finds_the_turns_start_when_the_history_was_trimmed() {
doc: d.clone(),
active_sketch: None,
pending_moves: Default::default(),
visibility: Default::default(),
};
let docs: Vec<_> = (0..6).map(doc).collect();
// The turn began at depth 3 with docs[3]; the history then lost its oldest step.
+8 -20
View File
@@ -182,13 +182,8 @@ pub fn sketch_visible(app: &SolveApp, id: u64) -> bool {
}
pub fn set_sketch_visible(app: &mut SolveApp, id: u64, on: bool) {
app.session.visibility.hidden_sketches.retain(|x| *x != id);
app.session.visibility.shown_sketches.retain(|x| *x != id);
if on {
app.session.visibility.shown_sketches.push(id);
} else {
app.session.visibility.hidden_sketches.push(id);
}
// Through the command, so showing or hiding a sketch is an undo step too (#2219).
let _ = app.run("browser.visibility", json!({ "items": [id], "visible": on }));
}
pub fn redefine_sketch(app: &mut SolveApp, id: u64) {
@@ -1392,11 +1387,8 @@ fn origin_rows(app: &mut SolveApp, ui: &mut egui::Ui, depth: usize, acts: &Actio
&Row { depth: depth + 1, eye: Some(visible), icon, label, selected, dim: !visible || !app.origin_shown(), mark, ..Default::default() },
);
if r.eye {
if visible {
app.session.visibility.hidden_origin.push(k.into());
} else {
app.session.visibility.hidden_origin.retain(|h| h != k);
}
// Through the command, so the Origin row's eye is an undo step (#2219).
let _ = app.run("browser.visibility", json!({ "items": [k], "visible": !visible }));
}
if let Some(p) = r.secondary
&& icon == "plane"
@@ -1852,14 +1844,13 @@ fn arrange(app: &SolveApp, comp: u64, folder: &str, all: Vec<Entry>) -> (Vec<(so
(grouped, loose)
}
/// Show or hide items of a folder.
/// Show or hide items of a folder, through `browser.visibility` so each is an undo step (#2219).
fn set_visible(app: &mut SolveApp, folder: &str, items: &[&Entry], on: bool) {
for e in items {
match folder {
"bodies" => {
app.session.visibility.hidden_bodies.retain(|b| !e.bodies.contains(b));
if !on {
app.session.visibility.hidden_bodies.extend(e.bodies.clone());
if !e.bodies.is_empty() {
let _ = app.run("browser.visibility", json!({ "items": e.bodies.clone(), "visible": on }));
}
}
"sketches" => {
@@ -1868,10 +1859,7 @@ fn set_visible(app: &mut SolveApp, folder: &str, items: &[&Entry], on: bool) {
}
}
_ => {
app.session.visibility.hidden_origin.retain(|p| *p != e.label);
if !on {
app.session.visibility.hidden_origin.push(e.label.clone());
}
let _ = app.run("browser.visibility", json!({ "items": [e.label], "visible": on }));
}
}
}
+26 -26
View File
@@ -1073,22 +1073,25 @@ pub fn run_item(app: &mut SolveApp, item: &Item, at: Pos2) {
}
}
"ui.hide" => {
for b in bodies {
if !app.session.visibility.hidden_bodies.contains(&b) {
app.session.visibility.hidden_bodies.push(b);
}
}
// Through the command, so hiding is one undo step (#2219).
let _ = app.run("browser.visibility", json!({ "items": bodies, "visible": false }));
}
"ui.show" => {
let _ = app.run("browser.visibility", json!({ "items": bodies, "visible": true }));
}
"ui.show" => app.session.visibility.hidden_bodies.retain(|b| !bodies.contains(b)),
"ui.showAll" => {
let _ = app.run("browser.visibility", json!({"folder":"bodies", "visible":true}));
app.session.visibility.hidden_sketches.clear();
// Hidden folders and groups too, so everything really shows.
app.session.visibility.hidden_folders.clear();
// One command and so one undo step: everything shown, folders and placements included.
let _ = app.run("browser.visibility", json!({ "folder": "all", "visible": true }));
}
"ui.isolate" => {
app.session.visibility.hidden_bodies =
app.session.world_state().bodies.iter().map(|b| b.name.clone()).filter(|b| !bodies.contains(b)).collect();
// The picked bodies stay (shown first if they were hidden), everything else goes.
let others: Vec<String> = app.session.world_state().bodies.iter().map(|b| b.name.clone()).filter(|b| !bodies.contains(b)).collect();
if !bodies.is_empty() {
let _ = app.run("browser.visibility", json!({ "items": bodies, "visible": true }));
}
if !others.is_empty() {
let _ = app.run("browser.visibility", json!({ "items": others, "visible": false }));
}
}
"ui.lock" => {
let locked = bodies.iter().all(|b| app.session.visibility.locked_bodies.contains(b));
@@ -1286,15 +1289,13 @@ pub fn run_item(app: &mut SolveApp, item: &Item, at: Pos2) {
}
"ui.originAll" => {
app.set_origin_shown(true);
app.session.visibility.hidden_origin.retain(|h| !["O", "X", "Y", "Z", "XY", "XZ", "YZ"].contains(&h.as_str()));
let _ = app.run("browser.visibility", json!({ "items": ["O", "X", "Y", "Z", "XY", "XZ", "YZ"], "visible": true }));
}
"ui.originPart" => {
let show = p.get("show").and_then(Value::as_bool).unwrap_or(true);
for k in strs(&p, "keys") {
app.session.visibility.hidden_origin.retain(|h| *h != k);
if !show {
app.session.visibility.hidden_origin.push(k);
}
let keys = strs(&p, "keys");
if !keys.is_empty() {
let _ = app.run("browser.visibility", json!({ "items": keys, "visible": show }));
}
}
"ui.editFeature" => {
@@ -1426,14 +1427,13 @@ pub fn toggle_visibility(app: &mut SolveApp) {
|| origin.iter().any(|o| !app.session.visibility.hidden_origin.contains(o))
|| app.session.doc.canvases.iter().any(|c| canvases.contains(&c.id) && c.visible);
let hide = any_visible;
app.session.visibility.hidden_bodies.retain(|b| !bodies.contains(b));
app.session.visibility.hidden_origin.retain(|o| !origin.contains(o));
if hide {
app.session.visibility.hidden_bodies.extend(bodies);
app.session.visibility.hidden_origin.extend(origin);
}
for s in sketches {
crate::browser::set_sketch_visible(app, s, !hide);
// One command for the picked bodies, construction items and sketches: the same step the
// menus run, so hiding from the viewport (V) is undone like any other hide (#2219).
let mut items: Vec<Value> = bodies.iter().map(|b| json!(b)).collect();
items.extend(origin.iter().map(|o| json!(o)));
items.extend(sketches.iter().map(|s| json!(s)));
if !items.is_empty() {
let _ = app.run("browser.visibility", json!({ "items": items, "visible": !hide }));
}
if !joints.is_empty() {
let _ = app.run("browser.visibility", json!({ "joints": joints, "visible": !hide }));
@@ -63,8 +63,6 @@ fn lost_attachment_ghost_rebind_preview_cancel_apply_and_undo_preserve_sketch()
assert!(h.app.session.world_state().sketch(sketch).is_none(), "saved repair guides are not solved sketch geometry");
let raw = raw_sketch(&h, sketch);
let before = h.app.session.doc.to_json();
let undo = h.app.session.undo.len();
let redo = h.app.session.redo.len();
h.app.edit_sketch(sketch);
h.frames(4);
assert_eq!(h.app.tree.redefine, Some(sketch));
@@ -82,6 +80,9 @@ fn lost_attachment_ghost_rebind_preview_cancel_apply_and_undo_preserve_sketch()
assert!(h.app.session.selection.is_empty(), "saved lost-plane guides must not be selectable geometry");
assert!(h.app.session.active_sketch.is_none());
h.app.run("browser.visibility", json!({"items":["ControlBox"],"visible":true})).unwrap();
// After the setup's two hide/show commands: each is an undo step of its own now (#2219).
let undo = h.app.session.undo.len();
let redo = h.app.session.redo.len();
h.frames(4);
h.step(&json!({"click":{"world":[3,4,30]}})).unwrap();
assert!(matches!(h.app.session.selection.first(), Some(solidcraft_engine::Sel::Face { body, .. }) if body=="ControlBox"));
+1 -1
View File
@@ -40,7 +40,7 @@ Visibility is an independent view setting for exact placements. Hide a path and
{"visible":false,"paths":[[2,4]]}
```
`visible:true` removes that path's own hide setting. A hidden ancestor still hides its descendants. Add `isolate:true` to a visible path list to hide other branches. Show the bodies folder to clear occurrence hides. Selection and activation paths are session state. Visibility is saved with the design (hidden bodies, placements, sketches and origin items) but is not an undo step; the saved document also retains occurrence IDs and transforms. Undo follows the existing behavior of clearing selection.
`visible:true` removes that path's own hide setting. A hidden ancestor still hides its descendants. Add `isolate:true` to a visible path list to hide other branches. Show the bodies folder to clear occurrence hides. Selection and activation paths are session state. Visibility is saved with the design (hidden bodies, placements, sketches and origin items) and is an undo step of its own, so undo brings a hidden body or placement back; the saved document also retains occurrence IDs and transforms. Undo follows the existing behavior of clearing selection.
Paths are checked for missing links, repeated IDs, component mismatches and a maximum depth of 64. Descendant expansion rejects more than 100,000 placements or bodies instead of returning incomplete results. Computed body names normally retain their readable occurrence labels; collisions use full occurrence IDs so visibility and body commands still address the correct source.