view: Port: Do not inherit from gtk::Button

We do not need any button functionality, so it is more appropriate to inherit directly from gtk::Widget
This commit is contained in:
Tom A. Wagner
2021-07-06 11:01:20 +02:00
parent add1a96b75
commit e29ffdeea8

View File

@@ -30,6 +30,7 @@ mod imp {
/// Graphical representation of a pipewire port. /// Graphical representation of a pipewire port.
#[derive(Default)] #[derive(Default)]
pub struct Port { pub struct Port {
pub(super) label: OnceCell<gtk::Label>,
pub(super) id: OnceCell<u32>, pub(super) id: OnceCell<u32>,
pub(super) direction: OnceCell<Direction>, pub(super) direction: OnceCell<Direction>,
} }
@@ -38,10 +39,23 @@ mod imp {
impl ObjectSubclass for Port { impl ObjectSubclass for Port {
const NAME: &'static str = "Port"; const NAME: &'static str = "Port";
type Type = super::Port; type Type = super::Port;
type ParentType = gtk::Button; type ParentType = gtk::Widget;
fn class_init(klass: &mut Self::Class) {
klass.set_layout_manager_type::<gtk::BinLayout>();
// Make it look like a GTK button.
klass.set_css_name("button");
}
} }
impl ObjectImpl for Port { impl ObjectImpl for Port {
fn dispose(&self, _obj: &Self::Type) {
if let Some(label) = self.label.get() {
label.unparent()
}
}
fn signals() -> &'static [Signal] { fn signals() -> &'static [Signal] {
static SIGNALS: Lazy<Vec<Signal>> = Lazy::new(|| { static SIGNALS: Lazy<Vec<Signal>> = Lazy::new(|| {
vec![Signal::builder( vec![Signal::builder(
@@ -58,18 +72,18 @@ mod imp {
} }
} }
impl WidgetImpl for Port {} impl WidgetImpl for Port {}
impl ButtonImpl for Port {}
} }
glib::wrapper! { glib::wrapper! {
pub struct Port(ObjectSubclass<imp::Port>) pub struct Port(ObjectSubclass<imp::Port>)
@extends gtk::Button, gtk::Widget; @extends gtk::Widget;
} }
impl Port { impl Port {
pub fn new(id: u32, name: &str, direction: Direction, media_type: Option<MediaType>) -> Self { pub fn new(id: u32, name: &str, direction: Direction, media_type: Option<MediaType>) -> Self {
// Create the widget and initialize needed fields // Create the widget and initialize needed fields
let res: Self = glib::Object::new(&[]).expect("Failed to create Port"); let res: Self = glib::Object::new(&[]).expect("Failed to create Port");
let private = imp::Port::from_instance(&res); let private = imp::Port::from_instance(&res);
private.id.set(id).expect("Port id already set"); private.id.set(id).expect("Port id already set");
private private
@@ -77,7 +91,12 @@ impl Port {
.set(direction) .set(direction)
.expect("Port direction already set"); .expect("Port direction already set");
res.set_child(Some(&gtk::Label::new(Some(name)))); let label = gtk::Label::new(Some(name));
label.set_parent(&res);
private
.label
.set(label)
.expect("Port label was already set");
// Add a drag source and drop target controller with the type depending on direction, // Add a drag source and drop target controller with the type depending on direction,
// they will be responsible for link creation by dragging an output port onto an input port or the other way around. // they will be responsible for link creation by dragging an output port onto an input port or the other way around.