feat(block-editor): add Test Block preview

Preview the current unsaved block in a disposable drawing with independent history. Keep normal Draw tools available, append Close Test Block, and return to the original editor without saving. Guard persistence paths and handle refresh and parent-close lifecycle by stable document identity.
This commit is contained in:
Karim Jerbi committed 2026-10-14 14:44:58 +01:00
1 parent 0a29be07f7
commit 9632979d09
22 files changed
+801 -65

No files matched your search

+32 -1
View File
@@ -160,7 +160,7 @@ impl OpenCADStudio {
if self.tabs[idx].is_start {
continue;
}
if self.tabs[idx].active_block_edit.is_some() {
if self.tabs[idx].active_block_edit.is_some() || self.tabs[idx].block_test.is_some() {
tasks.push(self.on_tab_close(idx));
if self.pending_block_close.is_some() || self.pending_close.is_some() {
break;
@@ -178,6 +178,27 @@ impl OpenCADStudio {
}
pub(in crate::app) fn on_tab_close(&mut self, idx: usize) -> Task<Message> {
let test_return_id = self
.tabs
.get(idx)
.and_then(|tab| tab.block_test.as_ref())
.map(|test| {
if idx == self.active_tab {
test.source_id
} else {
self.tabs[self.active_tab].id
}
});
if test_return_id.is_some() {
if idx == self.active_tab {
self.cancel_active_grip_edit();
self.grip_pending = None;
self.visibility_popup = None;
self.grip_popup = None;
self.grip_hover = None;
}
self.tabs[idx].dirty = false;
}
// Start tab is fixed — close requests on it are no-ops.
if self.tabs.get(idx).map_or(false, |t| t.is_start) {
return Task::none();
@@ -249,6 +270,16 @@ impl OpenCADStudio {
// The active tab is now either a brand-new blank or a
// different existing tab; in both cases the ribbon needs
// to track that doc's defaults / selection. #21.
if let Some(index) =
test_return_id.and_then(|id| self.tabs.iter().position(|tab| tab.id == id))
{
self.active_tab = index;
self.adopt_header_sysvars(index);
}
self.ribbon
.set_block_testing(self.tabs[self.active_tab].block_test.is_some());
self.ribbon
.set_block_editing(self.tabs[self.active_tab].active_block_edit.is_some());
self.sync_ribbon_layers();
self.sync_ribbon_styles();
self.sync_ribbon_from_selection();
+3
View File
@@ -22,6 +22,9 @@ use iced::{mouse, Point, Task};
impl OpenCADStudio {
pub(in crate::app) fn open_save_dialog_window(&mut self, tab_idx: usize) -> Task<Message> {
if self.refuse_block_test_save(tab_idx) {
return Task::none();
}
// Every entry here is Save-As semantics (the SAVEAS command, the
// recovery save-as, the native failure redirects): the user is
// choosing a destination, so on the web the FS Access picker must
+21 -4
View File
@@ -1759,6 +1759,11 @@ impl OpenCADStudio {
version: codec::DxfVersion,
set_current_path: bool,
) -> Result<(), crate::io::SaveFailure> {
if self.tabs[i].block_test.is_some() {
return Err(crate::io::SaveFailure::other(
"Test Block is temporary and cannot be saved",
));
}
let previous_autosave = self.autosave_target(i);
if self.tabs[i].recovery_save_as_required
&& self.tabs[i]
@@ -2423,7 +2428,7 @@ impl OpenCADStudio {
let mut skipped = 0usize;
let mut pending: Vec<(crate::app::WebSaveallEntry, codec::CadDocument)> = Vec::new();
for t in 0..self.tabs.len() {
if self.tabs[t].is_start {
if self.tabs[t].is_start || self.tabs[t].block_test.is_some() {
continue;
}
let Some(path) = self.tabs[t].current_path.clone() else {
@@ -3083,6 +3088,9 @@ impl OpenCADStudio {
set_current_path: bool,
check_external_change: bool,
) -> Task<Message> {
if self.refuse_block_test_save(i) {
return Task::none();
}
let tab_id = self.tabs[i].id;
if self
.pending_native_thumbnail_save
@@ -3751,6 +3759,9 @@ impl OpenCADStudio {
}
pub(super) fn on_save_file(&mut self) -> Task<Message> {
if self.refuse_block_test_save(self.active_tab) {
return Task::none();
}
if self.read_only {
self.command_line.push_error(
crate::t!("Read-only session (--read-only): saving is disabled.").as_ref(),
@@ -3807,6 +3818,9 @@ impl OpenCADStudio {
/// Used by plain Save (QSAVE) on an as-yet-unsaved drawing and by the
/// save-before-close flow — the version picker is reserved for Save As.
pub(in crate::app) fn save_with_default_format(&mut self, tab_idx: usize) -> Task<Message> {
if self.refuse_block_test_save(tab_idx) {
return Task::none();
}
self.active_tab = tab_idx;
// Every caller of this entry (Save/QSAVE, the save-before-close
// flow) is plain-save semantics: on the web, the tab's retained
@@ -4199,7 +4213,10 @@ impl OpenCADStudio {
pub(super) fn on_autosave(&mut self) -> Task<Message> {
let mut tasks = Vec::new();
for i in 0..self.tabs.len() {
if !self.tabs[i].dirty || self.active_save_jobs.contains_key(&self.tabs[i].id) {
if self.tabs[i].block_test.is_some()
|| !self.tabs[i].dirty
|| self.active_save_jobs.contains_key(&self.tabs[i].id)
{
continue;
}
self.prepare_snapshot_save(i);
@@ -4234,7 +4251,7 @@ impl OpenCADStudio {
}
let mut pending = Vec::new();
for i in 0..self.tabs.len() {
if !self.tabs[i].dirty {
if self.tabs[i].block_test.is_some() || !self.tabs[i].dirty {
continue;
}
// Same pre-serialize state sync the web Save flow performs
@@ -4561,7 +4578,7 @@ impl OpenCADStudio {
let mut names: Vec<String> = Vec::new();
let mut active = 0;
for (index, tab) in self.tabs.iter().enumerate() {
if tab.is_start {
if tab.is_start || tab.block_test.is_some() {
continue;
}
if index == self.active_tab {
+16 -2
View File
@@ -346,7 +346,17 @@ impl OpenCADStudio {
.tabs
.get(self.active_tab)
.is_some_and(|tab| tab.active_block_edit.is_some());
self.ribbon.set_block_editing(block_editing);
let testing = self
.tabs
.get(self.active_tab)
.is_some_and(|tab| tab.block_test.is_some());
if testing {
self.ribbon.set_block_editing(false);
self.ribbon.set_block_testing(true);
} else {
self.ribbon.set_block_testing(false);
self.ribbon.set_block_editing(block_editing);
}
task
}
@@ -595,7 +605,11 @@ impl OpenCADStudio {
}
}
#[cfg(target_arch = "wasm32")]
crate::sys::set_unsaved_changes_warning(self.tabs.iter().any(|tab| tab.dirty));
crate::sys::set_unsaved_changes_warning(
self.tabs
.iter()
.any(|tab| tab.dirty && tab.block_test.is_none()),
);
if self.tabs[i].active_cmd.is_none() {
self.block_palette.placing = None;
}