Turn Node into a gtk::Frame subclass.

This commit is contained in:
Tom A. Wagner
2021-01-07 17:42:37 +01:00
parent d5283c803b
commit f1b66d9c53
2 changed files with 132 additions and 68 deletions

View File

@@ -1,13 +1,13 @@
use super::Node; use super::Node;
use gtk::{glib, prelude::*, subclass::prelude::ObjectSubclass, WidgetExt}; use gtk::{glib, prelude::*, subclass::prelude::*, WidgetExt};
use std::collections::HashMap; use std::collections::HashMap;
mod imp { mod imp {
use super::*; use super::*;
use gtk::{gdk, graphene, gsk, subclass::prelude::*, WidgetExt}; use gtk::{gdk, graphene, gsk, WidgetExt};
use std::{cell::RefCell, rc::Rc}; use std::{cell::RefCell, rc::Rc};
@@ -70,7 +70,7 @@ mod imp {
self.nodes self.nodes
.borrow() .borrow()
.values() .values()
.for_each(|node| node.widget.unparent()) .for_each(|node| node.unparent())
} }
} }
@@ -132,7 +132,7 @@ mod imp {
self.nodes self.nodes
.borrow() .borrow()
.values() .values()
.for_each(|node| self.get_instance().snapshot_child(&node.widget, snapshot)); .for_each(|node| self.get_instance().snapshot_child(node, snapshot));
} }
} }
@@ -143,7 +143,7 @@ mod imp {
let x = (self.nodes.borrow().len() / 4) as f32 * 400.0; let x = (self.nodes.borrow().len() / 4) as f32 * 400.0;
let y = self.nodes.borrow().len() as f32 % 4.0 * 100.0; let y = self.nodes.borrow().len() as f32 % 4.0 * 100.0;
self.move_node(&node.widget.clone().upcast(), x, y); self.move_node(&node.clone().upcast(), x, y);
self.nodes.borrow_mut().insert(id, node); self.nodes.borrow_mut().insert(id, node);
} }
@@ -209,7 +209,9 @@ mod imp {
width: fw, width: fw,
height: fh, height: fh,
} = from_port.get_allocation(); } = from_port.get_allocation();
let from_node = from_port.get_ancestor(gtk::Grid::static_type()).unwrap(); let from_node = from_port
.get_ancestor(Node::static_type())
.expect("Port is not a child of a node");
let gtk::Allocation { x: fnx, y: fny, .. } = from_node.get_allocation(); let gtk::Allocation { x: fnx, y: fny, .. } = from_node.get_allocation();
fx += fnx + fw; fx += fnx + fw;
fy += fny + (fh / 2); fy += fny + (fh / 2);
@@ -221,7 +223,9 @@ mod imp {
height: th, height: th,
.. ..
} = to_port.get_allocation(); } = to_port.get_allocation();
let to_node = to_port.get_ancestor(gtk::Grid::static_type()).unwrap(); let to_node = to_port
.get_ancestor(Node::static_type())
.expect("Port is not a child of a node");
let gtk::Allocation { x: tnx, y: tny, .. } = to_node.get_allocation(); let gtk::Allocation { x: tnx, y: tny, .. } = to_node.get_allocation();
tx += tnx; tx += tnx;
ty += tny + (th / 2); ty += tny + (th / 2);
@@ -242,7 +246,7 @@ impl GraphView {
} }
pub fn add_node(&self, id: u32, node: Node) { pub fn add_node(&self, id: u32, node: Node) {
node.widget.set_parent(self); node.set_parent(self);
imp::GraphView::from_instance(self).add_node(id, node) imp::GraphView::from_instance(self).add_node(id, node)
} }

View File

@@ -1,85 +1,145 @@
use super::graph_view::GraphView; use super::graph_view::GraphView;
use gtk::prelude::*; use gtk::{glib, prelude::*, subclass::prelude::*, WidgetExt};
use pipewire::port::Direction; use pipewire::port::Direction;
use std::collections::HashMap; use std::{collections::HashMap, rc::Rc};
pub struct Node { mod imp {
pub(super) widget: gtk::Grid, use super::*;
label: gtk::Label,
ports: HashMap<u32, super::port::Port>,
num_ports_in: u32,
num_ports_out: u32,
}
impl Node { use std::cell::{Cell, RefCell};
pub fn new(name: &str) -> Self {
let result = Self { pub struct Node {
widget: gtk::Grid::new(), pub(super) grid: gtk::Grid,
label: gtk::Label::new(Some(name)), pub(super) label: gtk::Label,
ports: HashMap::new(), pub(super) ports: RefCell<HashMap<u32, Rc<crate::view::port::Port>>>,
num_ports_in: 0, pub(super) num_ports_in: Cell<u32>,
num_ports_out: 0, pub(super) num_ports_out: Cell<u32>,
}; }
impl ObjectSubclass for Node {
const NAME: &'static str = "Node";
type Type = super::Node;
type ParentType = gtk::Frame;
type Instance = glib::subclass::simple::InstanceStruct<Self>;
type Class = glib::subclass::simple::ClassStruct<Self>;
glib::object_subclass!();
fn class_init(klass: &mut Self::Class) {
klass.set_layout_manager_type::<gtk::BinLayout>();
}
fn new() -> Self {
let grid = gtk::Grid::new();
let label = gtk::Label::new(None);
grid.attach(&label, 0, 0, 2, 1);
let motion_controller = gtk::EventControllerMotion::new(); let motion_controller = gtk::EventControllerMotion::new();
// Tell the graphview that the Node is the target of a drag when the mouse enters its label
motion_controller.connect_enter(|controller, _, _| { motion_controller.connect_enter(|controller, _, _| {
// Tell the graphview that the Node is the target of a drag when the mouse enters its label
let widget = controller let widget = controller
.get_widget() .get_widget()
.expect("Controller with enter event has no widget") .expect("Controller with enter event has no widget")
.get_ancestor(gtk::Grid::static_type()) .get_ancestor(super::Node::static_type())
.unwrap(); .expect("Node label does not have a node ancestor widget");
widget widget
.get_ancestor(GraphView::static_type()) .get_ancestor(GraphView::static_type())
.unwrap() .expect("Node with enter event is not on graph")
.dynamic_cast::<GraphView>() .dynamic_cast::<GraphView>()
.unwrap() .unwrap()
.set_dragged(Some(widget)); .set_dragged(Some(widget));
}); });
// Tell the graphview that the Node is no longer the target of a drag when the mouse leaves.
motion_controller.connect_leave(|controller| { motion_controller.connect_leave(|controller| {
// Tell the graphview that the Node is no longer the target of a drag when the mouse leaves.
// FIXME: Check that we are the current target before setting none. // FIXME: Check that we are the current target before setting none.
controller controller
.get_widget() .get_widget()
.expect("Controller with leave event has no widget") .expect("Controller with leave event has no widget")
.get_ancestor(GraphView::static_type()) .get_ancestor(GraphView::static_type())
.unwrap() .expect("Node with leave event is not on graph")
.dynamic_cast::<GraphView>() .dynamic_cast::<GraphView>()
.unwrap() .unwrap()
.set_dragged(None); .set_dragged(None);
}); });
result.label.add_controller(&motion_controller); label.add_controller(&motion_controller);
// Display a grab cursor when the mouse is over the label so the user knows the node can be dragged. // Display a grab cursor when the mouse is over the label so the user knows the node can be dragged.
result label.set_cursor(gtk::gdk::Cursor::from_name("grab", None).as_ref());
.label
.set_cursor(gtk::gdk::Cursor::from_name("grab", None).as_ref());
result.widget.attach(&result.label, 0, 0, 2, 1); Self {
grid,
label,
ports: RefCell::new(HashMap::new()),
num_ports_in: Cell::new(0),
num_ports_out: Cell::new(0),
}
}
}
result impl ObjectImpl for Node {
fn constructed(&self, obj: &Self::Type) {
self.parent_constructed(obj);
self.grid.set_parent(obj);
}
fn dispose(&self, _obj: &Self::Type) {
self.grid.unparent();
}
}
impl FrameImpl for Node {}
impl WidgetImpl for Node {}
}
glib::wrapper! {
pub struct Node(ObjectSubclass<imp::Node>)
@extends gtk::Widget;
}
impl Node {
pub fn new(name: &str) -> Self {
let res: Self = glib::Object::new(&[]).expect("Failed to create Node");
let private = imp::Node::from_instance(&res);
private.label.set_text(name);
res
} }
pub fn add_port(&mut self, id: u32, port: super::port::Port) { pub fn add_port(&mut self, id: u32, port: super::port::Port) {
let private = imp::Node::from_instance(self);
match port.direction { match port.direction {
Direction::Input => { Direction::Input => {
self.widget private
.attach(&port.widget, 0, (self.num_ports_in + 1) as i32, 1, 1); .grid
self.num_ports_in += 1; .attach(&port.widget, 0, private.num_ports_in.get() as i32 + 1, 1, 1);
private.num_ports_in.set(private.num_ports_in.get() + 1);
} }
Direction::Output => { Direction::Output => {
self.widget private.grid.attach(
.attach(&port.widget, 1, (self.num_ports_out + 1) as i32, 1, 1); &port.widget,
self.num_ports_out += 1; 1,
private.num_ports_out.get() as i32 + 1,
1,
1,
);
private.num_ports_out.set(private.num_ports_out.get() + 1);
} }
} }
self.ports.insert(id, port); private.ports.borrow_mut().insert(id, Rc::new(port));
} }
pub fn get_port(&self, id: u32) -> Option<&super::port::Port> { pub fn get_port(&self, id: u32) -> Option<Rc<super::port::Port>> {
self.ports.get(&id) let private = imp::Node::from_instance(self);
private
.ports
.borrow_mut()
.get(&id)
.map(|port_rc| port_rc.clone())
} }
} }