cad-editor/src/app/history.rs
Hakan Seven da2c600953 feat(sysvars): persist Ortho, running OSNAP and Isolate in the drawing
Ortho and the running object-snap set were app-global (settings.txt only) and
Isolate/Hide was a session-only view state, so none survived save/reopen and
none were per-drawing. Bind all three to the file like AutoCAD:

- Ortho -> header.ortho_mode ($ORTHOMODE); running OSNAP <-> header
  .object_snap_mode ($OSMODE bitmask) via a new SnapType<->bit map in settings
  (osmode_from_snaps / snaps_from_osmode, round-trip tested). The header is the
  per-drawing store, live state mirrors the active drawing: adopt header->live
  on open and tab-switch, stamp live->header on save and when leaving a tab, a
  fresh drawing inherits the current values, and SETVAR ORTHOMODE/OSMODE now
  also update the live state. settings.txt stays as the boot/new-drawing
  default; the per-file header overrides it on open.
- Isolate/Hide now set each entity's persisted invisible flag (DXF code 60,
  which acadrust round-trips in DXF and DWG) in addition to the session set;
  End Isolation clears it. The set is reseeded from invisible entities on load
  and after undo/redo, the commands are undoable and mark the tab dirty. The
  renderer already skipped invisible entities, so no render change was needed.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-10 15:45:08 +03:00

129 lines
4.7 KiB
Rust

use super::{document::HistorySnapshot, OpenCADStudio};
use rustc_hash::FxHashSet as HashSet;
impl OpenCADStudio {
pub(super) fn history_label_from_active_cmd(&self, i: usize, fallback: &'static str) -> String {
self.tabs[i]
.active_cmd
.as_ref()
.map(|cmd| cmd.name().to_string())
.unwrap_or_else(|| fallback.to_string())
}
pub(super) fn capture_history_snapshot(
&self,
i: usize,
label: impl Into<String>,
) -> HistorySnapshot {
HistorySnapshot {
document: self.tabs[i].scene.document.clone(),
current_layout: self.tabs[i].scene.current_layout.clone(),
selected: self.tabs[i].scene.selected.iter().copied().collect(),
dirty: self.tabs[i].dirty,
label: label.into(),
}
}
pub(super) fn push_undo_snapshot(&mut self, i: usize, label: impl Into<String>) {
let snapshot = self.capture_history_snapshot(i, label);
self.tabs[i].history.undo_stack.push(snapshot);
self.tabs[i].history.redo_stack.clear();
}
pub(super) fn restore_history_snapshot(&mut self, i: usize, snapshot: HistorySnapshot) {
self.tabs[i].scene.document = snapshot.document;
self.tabs[i].scene.set_current_layout(snapshot.current_layout);
// Force a re-tessellation: the cached wires were keyed against the
// outgoing document / layout and would be returned unchanged
// otherwise (`set_current_layout` only bumps on actual change).
self.tabs[i].scene.bump_geometry();
self.tabs[i].scene.selected = snapshot
.selected
.into_iter()
.filter(|h| self.tabs[i].scene.document.get_entity(*h).is_some())
.collect::<HashSet<_>>();
self.tabs[i].scene.populate_hatches_from_document();
self.tabs[i].scene.populate_images_from_document();
self.tabs[i].scene.populate_meshes_from_document();
// Keep the Isolate/Hide set in step with the restored per-entity
// visibility so End Isolation stays correct after undo/redo.
self.tabs[i].scene.sync_hidden_from_invisible();
self.tabs[i].scene.clear_preview_wire();
self.tabs[i].scene.images.clear();
self.tabs[i].active_cmd = None;
self.tabs[i].snap_result = None;
self.tabs[i].active_grip = None;
self.tabs[i].dirty = snapshot.dirty;
let doc_layers = self.tabs[i].scene.document.layers.clone();
let vp_info = self.tabs[i].scene.viewport_list();
self.tabs[i]
.layers
.sync_with_viewports(&doc_layers, vp_info);
self.sync_ribbon_layers();
self.refresh_properties();
}
pub(super) fn undo_active_tab(&mut self) {
self.undo_steps(1);
}
pub(super) fn redo_active_tab(&mut self) {
self.redo_steps(1);
}
pub(super) fn undo_steps(&mut self, steps: usize) {
let i = self.active_tab;
let available = self.tabs[i].history.undo_stack.len();
let steps = steps.min(available);
if steps == 0 {
self.command_line.push_info("Nothing to undo.");
return;
}
let mut last_label = String::new();
for _ in 0..steps {
let Some(snapshot) = self.tabs[i].history.undo_stack.pop() else {
break;
};
let label = snapshot.label.clone();
let current = self.capture_history_snapshot(i, label.clone());
self.tabs[i].history.redo_stack.push(current);
self.restore_history_snapshot(i, snapshot);
last_label = label;
}
self.command_line
.push_output(&format!("Undo: {last_label}"));
}
pub(super) fn redo_steps(&mut self, steps: usize) {
let i = self.active_tab;
let available = self.tabs[i].history.redo_stack.len();
let steps = steps.min(available);
if steps == 0 {
self.command_line.push_info("Nothing to redo.");
return;
}
let mut last_label = String::new();
for _ in 0..steps {
let Some(snapshot) = self.tabs[i].history.redo_stack.pop() else {
break;
};
let label = snapshot.label.clone();
let current = self.capture_history_snapshot(i, label.clone());
self.tabs[i].history.undo_stack.push(current);
self.restore_history_snapshot(i, snapshot);
last_label = label;
}
self.command_line
.push_output(&format!("Redo: {last_label}"));
}
}
pub(super) fn history_dropdown_labels(stack: &[HistorySnapshot]) -> Vec<String> {
stack
.iter()
.rev()
.map(|snapshot| snapshot.label.clone())
.collect()
}