blitzarx1 / egui_graphs

Interactive graph visualization widget for rust powered by egui and petgraph
https://docs.rs/crate/egui_graphs
MIT License
417 stars 30 forks source link

unwrap panic in graph.rs, `edge_by_screen_pos` #166

Closed XertroV closed 3 months ago

XertroV commented 9 months ago

edge_by_screen_pos panics if you are clicking and holding a node while extending the graph. Seems to be because self.g.edge_endpoints(e.id()) returns None.

The following seems to work as a fix, but not sure if this is a bigger bug.

    pub fn edge_by_screen_pos(&self, meta: &Metadata, screen_pos: Pos2) -> Option<EdgeIndex<Ix>> {
        let pos_in_graph = meta.screen_to_canvas_pos(screen_pos);
        for (idx, e) in self.edges_iter() {
            let (idx_start, idx_end) = match self.g.edge_endpoints(e.id()) {
                Some((start, end)) => (start, end),
                None => continue,
            };
            let start = self.g.node_weight(idx_start).unwrap();
            let end = self.g.node_weight(idx_end).unwrap();
            if e.display().is_inside(start, end, pos_in_graph) {
                return Some(idx);
            }
        }

        None
    }
blitzarx1 commented 8 months ago

I am not sure if this is reproducable during the intended usage.