mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-19 17:58:10 +00:00
Tile: the kept pane outlines hold a weak document reference
The outline cache identified its document by address, so a new document allocated at a freed document's address could show the freed document's outlines when the revision, selection and screen mapping matched. The cache now holds a Weak reference and compares it with Weak::ptr_eq, which keeps the address reserved while the outlines exist.
This commit is contained in:
@@ -52,7 +52,9 @@ pub struct Panes {
|
||||
/// selection and screen mapping they were made for.
|
||||
struct Outlines {
|
||||
revision: u64,
|
||||
doc: usize,
|
||||
/// A weak reference to the document: while it exists, no other document can take the
|
||||
/// allocation's address.
|
||||
doc: std::sync::Weak<vectorcraft_doc::Document>,
|
||||
selection: Vec<vectorcraft_doc::NodeId>,
|
||||
affine: [f64; 6],
|
||||
shapes: Vec<egui::Shape>,
|
||||
@@ -61,15 +63,10 @@ struct Outlines {
|
||||
impl Outlines {
|
||||
/// Whether these outlines show `st`'s selection through the screen mapping `affine`.
|
||||
fn shows(&self, st: &vectorcraft_engine::DocState, affine: &[f64; 6]) -> bool {
|
||||
self.revision == st.revision && self.doc == doc_address(st) && self.affine == *affine && self.selection == st.selection.objects
|
||||
self.revision == st.revision && self.doc.ptr_eq(&Arc::downgrade(&st.doc)) && self.affine == *affine && self.selection == st.selection.objects
|
||||
}
|
||||
}
|
||||
|
||||
/// The address of `st`'s document, which tells two documents with the same revision apart.
|
||||
fn doc_address(st: &vectorcraft_engine::DocState) -> usize {
|
||||
Arc::as_ptr(&st.doc) as usize
|
||||
}
|
||||
|
||||
impl Panes {
|
||||
/// Forget every render and pane.
|
||||
fn clear(&mut self) {
|
||||
@@ -514,7 +511,7 @@ fn selected_objects(panes: &mut Panes, slot: Slot, st: &vectorcraft_engine::DocS
|
||||
let affine = xf.affine().as_coeffs();
|
||||
if !panes.outlines.get(&slot).is_some_and(|o| o.shows(st, &affine)) {
|
||||
let shapes = outline_shapes(st, xf);
|
||||
let kept = Outlines { revision: st.revision, doc: doc_address(st), selection: st.selection.objects.clone(), affine, shapes };
|
||||
let kept = Outlines { revision: st.revision, doc: Arc::downgrade(&st.doc), selection: st.selection.objects.clone(), affine, shapes };
|
||||
panes.outlines.insert(slot, kept);
|
||||
panes.outlines_made += 1;
|
||||
}
|
||||
|
||||
@@ -586,6 +586,23 @@ fn in_first(app: &mut VectorcraftApp, cmd: &str, args: serde_json::Value) -> ser
|
||||
out
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn kept_outlines_do_not_show_for_a_new_document_that_takes_a_freed_documents_address() {
|
||||
let affine = [1.0, 0.0, 0.0, 1.0, 0.0, 0.0];
|
||||
let st = vectorcraft_engine::DocState::new(vectorcraft_doc::Document::new(400.0, 300.0), None);
|
||||
let kept = Outlines { revision: st.revision, doc: Arc::downgrade(&st.doc), selection: vec![], affine, shapes: vec![] };
|
||||
assert!(kept.shows(&st, &affine), "the outlines show the document they were made for");
|
||||
let freed = Arc::as_ptr(&st.doc);
|
||||
drop(st);
|
||||
let mut reused = false;
|
||||
for _ in 0..64 {
|
||||
let other = vectorcraft_engine::DocState::new(vectorcraft_doc::Document::new(400.0, 300.0), None);
|
||||
reused |= Arc::as_ptr(&other.doc) == freed;
|
||||
assert!(!kept.shows(&other, &affine), "a different document with the same revision, selection and mapping is not the one shown");
|
||||
}
|
||||
assert!(!reused, "the weak reference keeps the freed document's address from being taken");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn other_panes_keep_their_selected_outlines_until_the_document_selection_or_view_changes() {
|
||||
let mut app = app_with(2);
|
||||
|
||||
Reference in New Issue
Block a user