fix: a failed or panicking edit marks the restored document's tiles dirty again

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
SteamSmithandClaude Sonnet 5.5 committed 2026-11-28 20:55:04 -05:00
1 parent fd54b7819a
commit aabf1c06dc
2 files changed
+48 -7

No files matched your search

+11 -7
View File
@@ -694,9 +694,7 @@ impl Session {
let result = match ran {
Ok(result) => result,
Err(panic) => {
self.doc = before;
// As for an error below: a repaint may have cached tiles from the discarded document
self.path_cache = None;
self.restore_after_failed_edit(before);
std::panic::resume_unwind(panic);
}
};
@@ -716,15 +714,21 @@ impl Session {
Ok(value)
}
Err(error) => {
self.doc = before;
// A failed repaint may have cached tiles from the discarded document
self.path_cache = None;
self.restore_after_failed_edit(before);
Err(error)
}
}
}
/// Put `before` back after an edit failed or panicked. The edit may have recomposited the
/// discarded document (draining the dirty tiles) or cached path tiles from it, so mark what
/// differs between the two dirty again, as undo does.
fn restore_after_failed_edit(&mut self, before: Document) {
let discarded = std::mem::replace(&mut self.doc, before);
self.path_cache = None;
self.invalidate_changed(&discarded);
}
pub(crate) fn push_history(&mut self, label: &str, before: Document) {
self.record(label, before, false);
}
+37
View File
@@ -536,6 +536,43 @@ fn a_panic_inside_an_edit_restores_the_document() {
assert_eq!(s.history_labels(), history, "no undo step was recorded");
}
/// An edit that recomposites the discarded document and then fails or panics: the display shows the
/// restored document again after the next `update`, not the discarded one.
#[test]
fn a_failed_or_panicking_edit_recomposites_the_restored_document() {
for panics in [false, true] {
let mut s = session();
let hit = s.pick(texturecraft_engine::geom::glam::Vec2::new(200.0, 200.0), s.viewport).unwrap();
let (x, y) = ((hit.uv.x * 256.0) as u32, (hit.uv.y * 256.0) as u32);
let red_at = |s: &mut Session| {
s.update();
s.caches[0].composite.value(Channel::BaseColor, x, y)[0]
};
execute(&mut s, "tool.material", &json!({"channel": "baseColor", "color": [1.0, 0.0, 0.0]})).unwrap();
execute(&mut s, "tool.brush", &json!({"size": 20, "hardness": 90})).unwrap();
execute(&mut s, "tool.stroke", &json!({"points": [[180, 200], [220, 200]]})).unwrap();
let painted = red_at(&mut s);
assert!(painted > 0.9, "the stroke painted: {painted}");
let edit = |s: &mut Session| {
let _ = s.edit("Hide everything", |s| -> texturecraft_engine::Result<()> {
s.doc.project.texture_sets[0].stack.clear();
s.touched();
s.update();
assert!(s.caches[0].composite.value(Channel::BaseColor, x, y)[0] < 0.9, "the edit shows the empty stack");
if panics {
panic!("boom");
}
Err(texturecraft_engine::EngineError::Other("nope".into()))
});
};
let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| edit(&mut s)));
assert_eq!(r.is_err(), panics);
let shown = red_at(&mut s);
assert!(shown > 0.9, "panics={panics}: the restored paint is shown again: {shown}");
}
}
/// A command that fails leaves the document and history as they were (commands are atomic).
#[test]
fn failed_commands_leave_the_document_unchanged() {