From 4744a1a647559727ba1e535ee4f43d651a744d26 Mon Sep 17 00:00:00 2001 From: "Tom A. Wagner" Date: Fri, 8 Jan 2021 09:57:25 +0100 Subject: [PATCH] Check that a widget is being dragged before checking the event in graphview. A bug in gtk4-rs makes the library print errors when accessing an event, this change reduces the amount of errors, as they are only printed when the mouse is over a node now. --- src/view/graph_view.rs | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/view/graph_view.rs b/src/view/graph_view.rs index 7a049af..e10d0e2 100644 --- a/src/view/graph_view.rs +++ b/src/view/graph_view.rs @@ -44,24 +44,26 @@ mod imp { fn constructed(&self, obj: &Self::Type) { self.parent_constructed(obj); + // Move the Node that is currently being dragged to the cursor position as long as Mouse Button 1 is held. let motion_controller = gtk::EventControllerMotion::new(); motion_controller.connect_motion(|controller, x, y| { - if controller - .get_current_event() + let instance = controller + .get_widget() .unwrap() - .get_modifier_state() - .contains(gdk::ModifierType::BUTTON1_MASK) - { - let instance = controller - .get_widget() + .dynamic_cast::() + .unwrap(); + let this = imp::GraphView::from_instance(&instance); + + if let Some(ref widget) = *this.dragged.borrow() { + if controller + .get_current_event() .unwrap() - .dynamic_cast::() - .unwrap(); - let this = imp::GraphView::from_instance(&instance); - if let Some(ref widget) = *this.dragged.borrow() { - this.move_node(&widget, x as f32, y as f32); - }; - } + .get_modifier_state() + .contains(gdk::ModifierType::BUTTON1_MASK) + { + instance.move_node(&widget, x as f32, y as f32); + } + }; }); obj.add_controller(&motion_controller); }