Fix DCEL selection bugs
This commit is contained in:
@@ -194,17 +194,23 @@ fn main() -> eframe::Result {
|
||||
let test_mode_is_replaying: std::sync::Arc<std::sync::atomic::AtomicBool> =
|
||||
std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
|
||||
#[cfg(debug_assertions)]
|
||||
let test_mode_pending_geometry: std::sync::Arc<std::sync::Mutex<Option<serde_json::Value>>> =
|
||||
std::sync::Arc::new(std::sync::Mutex::new(None));
|
||||
#[cfg(debug_assertions)]
|
||||
let test_mode_panic_snapshot_for_app = test_mode_panic_snapshot.clone();
|
||||
#[cfg(debug_assertions)]
|
||||
let test_mode_pending_event_for_app = test_mode_pending_event.clone();
|
||||
#[cfg(debug_assertions)]
|
||||
let test_mode_is_replaying_for_app = test_mode_is_replaying.clone();
|
||||
#[cfg(debug_assertions)]
|
||||
let test_mode_pending_geometry_for_app = test_mode_pending_geometry.clone();
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
let panic_snapshot = test_mode_panic_snapshot.clone();
|
||||
let pending_event = test_mode_pending_event.clone();
|
||||
let is_replaying = test_mode_is_replaying.clone();
|
||||
let pending_geometry = test_mode_pending_geometry.clone();
|
||||
let test_dir = directories::ProjectDirs::from("", "", "lightningbeam")
|
||||
.map(|dirs| dirs.data_dir().join("test_cases"))
|
||||
.unwrap_or_else(|| std::path::PathBuf::from("test_cases"));
|
||||
@@ -219,7 +225,7 @@ fn main() -> eframe::Result {
|
||||
format!("{}", info)
|
||||
};
|
||||
let backtrace = format!("{}", std::backtrace::Backtrace::force_capture());
|
||||
test_mode::TestModeState::record_panic(&panic_snapshot, &pending_event, &is_replaying, msg, backtrace, &test_dir);
|
||||
test_mode::TestModeState::record_panic(&panic_snapshot, &pending_event, &is_replaying, &pending_geometry, msg, backtrace, &test_dir);
|
||||
default_hook(info);
|
||||
}));
|
||||
}
|
||||
@@ -229,7 +235,7 @@ fn main() -> eframe::Result {
|
||||
options,
|
||||
Box::new(move |cc| {
|
||||
#[cfg(debug_assertions)]
|
||||
let app = EditorApp::new(cc, layouts, theme, test_mode_panic_snapshot_for_app, test_mode_pending_event_for_app, test_mode_is_replaying_for_app);
|
||||
let app = EditorApp::new(cc, layouts, theme, test_mode_panic_snapshot_for_app, test_mode_pending_event_for_app, test_mode_is_replaying_for_app, test_mode_pending_geometry_for_app);
|
||||
#[cfg(not(debug_assertions))]
|
||||
let app = EditorApp::new(cc, layouts, theme);
|
||||
Ok(Box::new(app))
|
||||
@@ -921,6 +927,7 @@ impl EditorApp {
|
||||
#[cfg(debug_assertions)] panic_snapshot: std::sync::Arc<std::sync::Mutex<Option<lightningbeam_core::test_mode::TestCase>>>,
|
||||
#[cfg(debug_assertions)] pending_event: std::sync::Arc<std::sync::Mutex<Option<lightningbeam_core::test_mode::TestEvent>>>,
|
||||
#[cfg(debug_assertions)] is_replaying: std::sync::Arc<std::sync::atomic::AtomicBool>,
|
||||
#[cfg(debug_assertions)] pending_geometry: std::sync::Arc<std::sync::Mutex<Option<serde_json::Value>>>,
|
||||
) -> Self {
|
||||
let current_layout = layouts[0].layout.clone();
|
||||
|
||||
@@ -1114,7 +1121,7 @@ impl EditorApp {
|
||||
|
||||
// Debug test mode (F5)
|
||||
#[cfg(debug_assertions)]
|
||||
test_mode: test_mode::TestModeState::new(panic_snapshot, pending_event, is_replaying),
|
||||
test_mode: test_mode::TestModeState::new(panic_snapshot, pending_event, is_replaying, pending_geometry),
|
||||
|
||||
// Debug overlay (F3)
|
||||
cursor_cache: custom_cursor::CursorCache::new(),
|
||||
|
||||
@@ -4203,9 +4203,28 @@ impl StagePane {
|
||||
return;
|
||||
}
|
||||
|
||||
// Capture DCEL snapshot + region path for crash diagnosis (debug builds only)
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
use vello::kurbo::PathEl;
|
||||
let path_elems: Vec<serde_json::Value> = region_path.elements().iter().map(|el| match el {
|
||||
PathEl::MoveTo(p) => serde_json::json!({"type": "M", "x": p.x, "y": p.y}),
|
||||
PathEl::LineTo(p) => serde_json::json!({"type": "L", "x": p.x, "y": p.y}),
|
||||
PathEl::QuadTo(p1, p2) => serde_json::json!({"type": "Q", "x1": p1.x, "y1": p1.y, "x2": p2.x, "y2": p2.y}),
|
||||
PathEl::CurveTo(p1, p2, p3) => serde_json::json!({"type": "C", "x1": p1.x, "y1": p1.y, "x2": p2.x, "y2": p2.y, "x3": p3.x, "y3": p3.y}),
|
||||
PathEl::ClosePath => serde_json::json!({"type": "Z"}),
|
||||
}).collect();
|
||||
let geom = serde_json::json!({
|
||||
"region_path": path_elems,
|
||||
"dcel_snapshot": serde_json::to_value(&snapshot).unwrap_or(serde_json::Value::Null),
|
||||
});
|
||||
shared.test_mode.set_pending_geometry(geom);
|
||||
}
|
||||
|
||||
// Insert region boundary as invisible edges (no stroke style/color)
|
||||
let stroke_result = dcel.insert_stroke(&segments, None, None, 1.0);
|
||||
let boundary_verts = stroke_result.new_vertices;
|
||||
let boundary_verts: Vec<_> = stroke_result.new_vertices.clone();
|
||||
let region_edge_ids: Vec<_> = stroke_result.new_edges.clone();
|
||||
|
||||
// Extract the inside portion; self (dcel) keeps the outside + boundary.
|
||||
let mut selected_dcel = dcel.extract_region(®ion_path, &boundary_verts);
|
||||
@@ -4224,9 +4243,25 @@ impl StagePane {
|
||||
if !has_visible {
|
||||
// Nothing visible inside — restore snapshot and bail
|
||||
*dcel = snapshot;
|
||||
#[cfg(debug_assertions)]
|
||||
shared.test_mode.clear_pending_geometry();
|
||||
return;
|
||||
}
|
||||
|
||||
// Compute inside_vertices: non-deleted verts in selected_dcel that aren't boundary.
|
||||
let inside_vertices: Vec<_> = selected_dcel
|
||||
.vertices
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(i, v)| {
|
||||
if v.deleted { return None; }
|
||||
let vid = lightningbeam_core::dcel::VertexId(i as u32);
|
||||
if !boundary_verts.contains(&vid) { Some(vid) } else { None }
|
||||
})
|
||||
.collect();
|
||||
|
||||
let action_epoch = shared.action_executor.epoch();
|
||||
|
||||
shared.selection.clear();
|
||||
|
||||
// Store region selection state with extracted DCEL
|
||||
@@ -4238,7 +4273,14 @@ impl StagePane {
|
||||
selected_dcel,
|
||||
transform: vello::kurbo::Affine::IDENTITY,
|
||||
committed: false,
|
||||
inside_vertices,
|
||||
boundary_vertices: boundary_verts,
|
||||
region_edge_ids,
|
||||
action_epoch_at_selection: action_epoch,
|
||||
});
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
shared.test_mode.clear_pending_geometry();
|
||||
}
|
||||
|
||||
/// Revert an uncommitted region selection, restoring the DCEL from snapshot
|
||||
@@ -4255,11 +4297,26 @@ impl StagePane {
|
||||
return;
|
||||
}
|
||||
|
||||
// Restore the DCEL from the snapshot taken before boundary insertion
|
||||
let no_actions_taken =
|
||||
shared.action_executor.epoch() == region_sel.action_epoch_at_selection;
|
||||
|
||||
let doc = shared.action_executor.document_mut();
|
||||
if let Some(AnyLayer::Vector(vl)) = doc.get_layer_mut(®ion_sel.layer_id) {
|
||||
if let Some(dcel) = vl.dcel_at_time_mut(region_sel.time) {
|
||||
*dcel = region_sel.dcel_snapshot;
|
||||
if no_actions_taken {
|
||||
// Nothing changed: restore snapshot cleanly (undo boundary insertion)
|
||||
*dcel = region_sel.dcel_snapshot;
|
||||
} else {
|
||||
// Actions were applied to the selection: merge selected_dcel back
|
||||
let mut merged = region_sel.dcel_snapshot;
|
||||
merged.merge_back_from_selected(
|
||||
®ion_sel.selected_dcel,
|
||||
®ion_sel.inside_vertices,
|
||||
®ion_sel.boundary_vertices,
|
||||
®ion_sel.region_edge_ids,
|
||||
);
|
||||
*dcel = merged;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -192,6 +192,9 @@ pub struct TestModeState {
|
||||
/// Current in-flight event, set before processing. If a panic occurs during
|
||||
/// processing, the panic hook appends this to the saved test case.
|
||||
pub pending_event: Arc<Mutex<Option<TestEvent>>>,
|
||||
/// Geometry context set before risky operations (e.g. region select).
|
||||
/// Shared with the panic hook so crashes include the relevant geometry.
|
||||
pub pending_geometry: Arc<Mutex<Option<serde_json::Value>>>,
|
||||
/// Always-on ring buffer of last N events (for crash capture outside test mode)
|
||||
pub event_ring: VecDeque<TestEvent>,
|
||||
pub ring_start_time: Instant,
|
||||
@@ -205,7 +208,7 @@ pub struct TestModeState {
|
||||
}
|
||||
|
||||
impl TestModeState {
|
||||
pub fn new(panic_snapshot: Arc<Mutex<Option<TestCase>>>, pending_event: Arc<Mutex<Option<TestEvent>>>, is_replaying: Arc<AtomicBool>) -> Self {
|
||||
pub fn new(panic_snapshot: Arc<Mutex<Option<TestCase>>>, pending_event: Arc<Mutex<Option<TestEvent>>>, is_replaying: Arc<AtomicBool>, pending_geometry: Arc<Mutex<Option<serde_json::Value>>>) -> Self {
|
||||
let test_dir = directories::ProjectDirs::from("", "", "lightningbeam")
|
||||
.map(|dirs| dirs.data_dir().join("test_cases"))
|
||||
.unwrap_or_else(|| PathBuf::from("test_cases"));
|
||||
@@ -219,6 +222,7 @@ impl TestModeState {
|
||||
status_message: None,
|
||||
panic_snapshot,
|
||||
pending_event,
|
||||
pending_geometry,
|
||||
event_ring: VecDeque::with_capacity(RING_BUFFER_SIZE),
|
||||
ring_start_time: Instant::now(),
|
||||
ring_event_count: 0,
|
||||
@@ -228,6 +232,22 @@ impl TestModeState {
|
||||
}
|
||||
}
|
||||
|
||||
/// Store geometry context for panic capture.
|
||||
/// Called before risky operations (e.g. region select) so the panic hook
|
||||
/// can include it in the crash file for easier reproduction.
|
||||
pub fn set_pending_geometry(&self, context: serde_json::Value) {
|
||||
if let Ok(mut guard) = self.pending_geometry.try_lock() {
|
||||
*guard = Some(context);
|
||||
}
|
||||
}
|
||||
|
||||
/// Clear the pending geometry context (call after the operation succeeds).
|
||||
pub fn clear_pending_geometry(&self) {
|
||||
if let Ok(mut guard) = self.pending_geometry.try_lock() {
|
||||
*guard = None;
|
||||
}
|
||||
}
|
||||
|
||||
/// Store the current in-flight event for panic capture.
|
||||
/// Called before processing so the panic hook can grab it if processing panics.
|
||||
pub fn set_pending_event(&self, kind: TestEventKind) {
|
||||
@@ -339,6 +359,7 @@ impl TestModeState {
|
||||
panic_snapshot: &Arc<Mutex<Option<TestCase>>>,
|
||||
pending_event: &Arc<Mutex<Option<TestEvent>>>,
|
||||
is_replaying: &Arc<AtomicBool>,
|
||||
pending_geometry: &Arc<Mutex<Option<serde_json::Value>>>,
|
||||
msg: String,
|
||||
backtrace: String,
|
||||
test_dir: &PathBuf,
|
||||
@@ -372,6 +393,11 @@ impl TestModeState {
|
||||
}
|
||||
}
|
||||
|
||||
// Attach geometry context if one was set before the crash
|
||||
if let Ok(mut geom_guard) = pending_geometry.try_lock() {
|
||||
test_case.geometry_context = geom_guard.take();
|
||||
}
|
||||
|
||||
test_case.ended_with_panic = true;
|
||||
test_case.panic_message = Some(msg);
|
||||
test_case.panic_backtrace = Some(backtrace);
|
||||
|
||||
Reference in New Issue
Block a user