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