From e29ffdeea847a93f6d52ad2b2bf97927f19085d7 Mon Sep 17 00:00:00 2001 From: "Tom A. Wagner" Date: Tue, 6 Jul 2021 11:01:20 +0200 Subject: [PATCH] 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 --- src/view/port.rs | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/view/port.rs b/src/view/port.rs index 1b3808f..b7ee171 100644 --- a/src/view/port.rs +++ b/src/view/port.rs @@ -30,6 +30,7 @@ mod imp { /// Graphical representation of a pipewire port. #[derive(Default)] pub struct Port { + pub(super) label: OnceCell, pub(super) id: OnceCell, pub(super) direction: OnceCell, } @@ -38,10 +39,23 @@ mod imp { impl ObjectSubclass for Port { const NAME: &'static str = "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::(); + + // Make it look like a GTK button. + klass.set_css_name("button"); + } } impl ObjectImpl for Port { + fn dispose(&self, _obj: &Self::Type) { + if let Some(label) = self.label.get() { + label.unparent() + } + } + fn signals() -> &'static [Signal] { static SIGNALS: Lazy> = Lazy::new(|| { vec![Signal::builder( @@ -58,18 +72,18 @@ mod imp { } } impl WidgetImpl for Port {} - impl ButtonImpl for Port {} } glib::wrapper! { pub struct Port(ObjectSubclass) - @extends gtk::Button, gtk::Widget; + @extends gtk::Widget; } impl Port { pub fn new(id: u32, name: &str, direction: Direction, media_type: Option) -> Self { // Create the widget and initialize needed fields let res: Self = glib::Object::new(&[]).expect("Failed to create Port"); + let private = imp::Port::from_instance(&res); private.id.set(id).expect("Port id already set"); private @@ -77,7 +91,12 @@ impl Port { .set(direction) .expect("Port direction already set"); - res.set_child(Some(>k::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, // they will be responsible for link creation by dragging an output port onto an input port or the other way around.