update to gtk 0.9.1

Change the glib::clone macros usage
Unwrap the the <Option>Widget
This commit is contained in:
Stéphane Cerveau 2024-09-10 15:30:12 +02:00
parent 7b6c351033
commit f15b76ac6f

View file

@ -99,6 +99,7 @@ mod imp {
drag_controller.connect_drag_begin(|drag_controller, x, y| {
let widget = drag_controller
.widget()
.unwrap()
.dynamic_cast::<super::GraphView>()
.expect("drag-begin event is not on the GraphView");
let mut dragged_node = widget.imp().dragged_node.borrow_mut();
@ -138,6 +139,7 @@ mod imp {
drag_controller.connect_drag_update(|drag_controller, x, y| {
let widget = drag_controller
.widget()
.unwrap()
.dynamic_cast::<super::GraphView>()
.expect("drag-update event is not on the GraphView");
let dragged_node = widget.imp().dragged_node.borrow();
@ -167,6 +169,7 @@ mod imp {
drag_controller.connect_drag_end(|drag_controller, _x, _y| {
let widget = drag_controller
.widget()
.unwrap()
.dynamic_cast::<super::GraphView>()
.expect("drag-update event is not on the GraphView");
widget.graph_updated();
@ -174,65 +177,131 @@ mod imp {
let gesture = gtk::GestureClick::new();
gesture.set_button(0);
gesture.connect_pressed(
clone!(@weak obj, @weak drag_controller => move |gesture, _n_press, x, y| {
gesture.connect_pressed(clone!(
#[weak]
obj,
#[weak]
drag_controller,
move |gesture, _n_press, x, y| {
if gesture.current_button() == gdk::BUTTON_SECONDARY {
let widget = drag_controller.widget()
let widget = drag_controller
.widget()
.unwrap()
.dynamic_cast::<Self::Type>()
.expect("click event is not on the GraphView");
let target = widget.pick(x, y, gtk::PickFlags::DEFAULT).expect("port pick() did not return a widget");
let target = widget
.pick(x, y, gtk::PickFlags::DEFAULT)
.expect("port pick() did not return a widget");
if let Some(target) = target.ancestor(Port::static_type()) {
let port = target.dynamic_cast::<Port>().expect("click event is not on the Port");
let node = port.ancestor(Node::static_type()).expect("Unable to reach parent").dynamic_cast::<Node>().expect("Unable to cast to Node");
obj.emit_by_name::<()>("port-right-clicked", &[&port.id(), &node.id(), &graphene::Point::new(x as f32,y as f32)]);
let port = target
.dynamic_cast::<Port>()
.expect("click event is not on the Port");
let node = port
.ancestor(Node::static_type())
.expect("Unable to reach parent")
.dynamic_cast::<Node>()
.expect("Unable to cast to Node");
obj.emit_by_name::<()>(
"port-right-clicked",
&[
&port.id(),
&node.id(),
&graphene::Point::new(x as f32, y as f32),
],
);
} else if let Some(target) = target.ancestor(Node::static_type()) {
let node = target.dynamic_cast::<Node>().expect("click event is not on the Node");
let node = target
.dynamic_cast::<Node>()
.expect("click event is not on the Node");
widget.unselect_all();
node.set_selected(true);
obj.emit_by_name::<()>("node-right-clicked", &[&node.id(), &graphene::Point::new(x as f32,y as f32)]);
obj.emit_by_name::<()>(
"node-right-clicked",
&[&node.id(), &graphene::Point::new(x as f32, y as f32)],
);
} else {
widget.unselect_all();
obj.emit_by_name::<()>("graph-right-clicked", &[&graphene::Point::new(x as f32,y as f32)]);
obj.emit_by_name::<()>(
"graph-right-clicked",
&[&graphene::Point::new(x as f32, y as f32)],
);
}
} else if gesture.current_button() == gdk::BUTTON_PRIMARY {
let widget = drag_controller.widget()
let widget = drag_controller
.widget()
.unwrap()
.dynamic_cast::<Self::Type>()
.expect("click event is not on the GraphView");
let target = widget.pick(x, y, gtk::PickFlags::DEFAULT).expect("port pick() did not return a widget");
let target = widget
.pick(x, y, gtk::PickFlags::DEFAULT)
.expect("port pick() did not return a widget");
if let Some(target) = target.ancestor(Port::static_type()) {
let port = target.dynamic_cast::<Port>().expect("click event is not on the Node");
let port = target
.dynamic_cast::<Port>()
.expect("click event is not on the Node");
widget.unselect_all();
port.toggle_selected();
} else if let Some(target) = target.ancestor(Node::static_type()) {
let node = target.dynamic_cast::<Node>().expect("click event is not on the Node");
let node = target
.dynamic_cast::<Node>()
.expect("click event is not on the Node");
widget.unselect_all();
node.toggle_selected();
}
else {
widget.point_on_link(&graphene::Point::new(x.floor() as f32,y.floor() as f32));
} else {
widget.point_on_link(&graphene::Point::new(
x.floor() as f32,
y.floor() as f32,
));
}
}
}),
);
}
));
gesture.connect_released(clone!(@weak gesture, @weak obj, @weak drag_controller => move |_gesture, _n_press, x, y| {
gesture.connect_released(clone!(
#[weak]
gesture,
#[weak]
obj,
#[weak]
drag_controller,
move |_gesture, _n_press, x, y| {
if gesture.current_button() == gdk::BUTTON_PRIMARY {
let widget = drag_controller
.widget()
.unwrap()
.dynamic_cast::<Self::Type>()
.expect("click event is not on the GraphView");
if let Some(target) = widget.pick(x, y, gtk::PickFlags::DEFAULT) {
if let Some(target) = target.ancestor(Port::static_type()) {
let port_clicked = target.dynamic_cast::<Port>().expect("click event is not on the Port");
let port_clicked = target
.dynamic_cast::<Port>()
.expect("click event is not on the Port");
if widget.port_is_linked(port_clicked.id()).is_none() {
let selected_port = widget.selected_port().to_owned();
if let Some(mut port_from) = selected_port {
debug!("Port {} is clicked at {}:{}", port_clicked.id(), x, y);
debug!(
"Port {} is clicked at {}:{}",
port_clicked.id(),
x,
y
);
let mut port_to = port_clicked;
if widget.ports_compatible(&port_to) {
let mut node_from = port_from.ancestor(Node::static_type()).expect("Unable to reach parent").dynamic_cast::<Node>().expect("Unable to cast to Node");
let mut node_to = port_to.ancestor(Node::static_type()).expect("Unable to reach parent").dynamic_cast::<Node>().expect("Unable to cast to Node");
info!("add link from port {} to {} ", port_from.id(), port_to.id());
let mut node_from = port_from
.ancestor(Node::static_type())
.expect("Unable to reach parent")
.dynamic_cast::<Node>()
.expect("Unable to cast to Node");
let mut node_to = port_to
.ancestor(Node::static_type())
.expect("Unable to reach parent")
.dynamic_cast::<Node>()
.expect("Unable to cast to Node");
info!(
"add link from port {} to {} ",
port_from.id(),
port_to.id()
);
if port_to.direction() == PortDirection::Output {
debug!("swap ports and nodes to create the link");
std::mem::swap(&mut node_from, &mut node_to);
@ -254,22 +323,38 @@ mod imp {
// click to a linked port
widget.set_selected_port(None);
}
}
else {
} else {
if let Some(target) = target.ancestor(Node::static_type()) {
let node = target.dynamic_cast::<Node>().expect("click event is not on the Node");
let node = target
.dynamic_cast::<Node>()
.expect("click event is not on the Node");
info!(" node id {}", node.id());
if _n_press % 2 == 0 {
info!("double clicked node id {}", node.id());
obj.emit_by_name::<()>("node-double-clicked", &[&node.id(), &graphene::Point::new(x as f32,y as f32)]);
obj.emit_by_name::<()>(
"node-double-clicked",
&[
&node.id(),
&graphene::Point::new(x as f32, y as f32),
],
);
}
} else if _n_press % 2 == 0 {
if let Some(link) = widget.point_on_link(&graphene::Point::new(x.floor() as f32,y.floor() as f32)) {
if let Some(link) = widget.point_on_link(&graphene::Point::new(
x.floor() as f32,
y.floor() as f32,
)) {
info!("double clicked link id {}", link.id());
obj.emit_by_name::<()>("link-double-clicked", &[&link.id(), &graphene::Point::new(x as f32,y as f32)]);
obj.emit_by_name::<()>(
"link-double-clicked",
&[
&link.id(),
&graphene::Point::new(x as f32, y as f32),
],
);
}
} else {
info!("double click {}",widget.width());
info!("double click {}", widget.width());
}
// Click to something else than a port
@ -277,19 +362,23 @@ mod imp {
}
}
}
}));
}
));
obj.add_controller(drag_controller);
obj.add_controller(gesture);
let event_motion = gtk::EventControllerMotion::new();
event_motion.connect_motion(glib::clone!(@weak obj => move |_e, x, y| {
event_motion.connect_motion(glib::clone!(
#[weak]
obj,
move |_e, x, y| {
let graphview = obj;
if graphview.selected_port().is_some() {
graphview.set_mouse_position(x,y);
graphview.set_mouse_position(x, y);
graphview.queue_allocate();
}
}));
}
));
obj.add_controller(event_motion);
let scroll_controller =
@ -304,6 +393,7 @@ mod imp {
{
let widget = eventcontroller
.widget()
.unwrap()
.downcast::<super::GraphView>()
.unwrap();
widget.set_zoom_factor(widget.zoom_factor() + (0.1 * -delta_y), None);
@ -1564,7 +1654,11 @@ impl GraphView {
}
if let Some(adjustment) = adjustment {
adjustment.connect_value_changed(clone!(@weak obj => move |_| obj.queue_allocate() ));
adjustment.connect_value_changed(clone!(
#[weak]
obj,
move |_| obj.queue_allocate()
));
}
}