From 7a9bc84b8b53686a346dda7eb5e89d436915f75e Mon Sep 17 00:00:00 2001 From: "Tom A. Wagner" Date: Tue, 1 Aug 2023 09:09:57 +0200 Subject: [PATCH] port: Use glib::properties derive macro for properties --- src/ui/graph/port.rs | 61 +++++++++++++------------------------------- 1 file changed, 18 insertions(+), 43 deletions(-) diff --git a/src/ui/graph/port.rs b/src/ui/graph/port.rs index f414eb0..152787f 100644 --- a/src/ui/graph/port.rs +++ b/src/ui/graph/port.rs @@ -38,16 +38,25 @@ struct ForwardLink(u32); struct ReversedLink(u32); mod imp { - use glib::ParamFlags; + use super::*; + use once_cell::{sync::Lazy, unsync::OnceCell}; use pipewire::spa::Direction; - use super::*; - /// Graphical representation of a pipewire port. - #[derive(Default)] + #[derive(Default, glib::Properties)] + #[properties(wrapper_type = super::Port)] pub struct Port { + #[property(get, set, construct_only)] pub(super) pipewire_id: OnceCell, + #[property( + name = "name", type = String, + get = |this: &Self| this.label.text().to_string(), + set = |this: &Self, val| { + this.label.set_text(val); + this.label.set_tooltip_text(Some(val)); + } + )] pub(super) label: gtk::Label, pub(super) direction: OnceCell, } @@ -82,35 +91,15 @@ mod imp { } fn properties() -> &'static [glib::ParamSpec] { - static PROPERTIES: Lazy> = Lazy::new(|| { - vec![ - glib::ParamSpecUInt::builder("pipewire-id") - .flags(ParamFlags::READWRITE | ParamFlags::CONSTRUCT_ONLY) - .build(), - glib::ParamSpecString::builder("name").build(), - ] - }); - - PROPERTIES.as_ref() + Self::derived_properties() } - fn property(&self, _id: usize, pspec: &glib::ParamSpec) -> glib::Value { - match pspec.name() { - "pipewire-id" => self.pipewire_id.get().unwrap().to_value(), - "name" => self.label.text().to_value(), - _ => unimplemented!(), - } + fn property(&self, id: usize, pspec: &glib::ParamSpec) -> glib::Value { + Self::derived_property(self, id, pspec) } - fn set_property(&self, _id: usize, value: &glib::Value, pspec: &glib::ParamSpec) { - match pspec.name() { - "name" => { - self.label.set_text(value.get().unwrap()); - self.label.set_tooltip_text(value.get().ok()); - } - "pipewire-id" => self.pipewire_id.set(value.get().unwrap()).unwrap(), - _ => unimplemented!(), - } + fn set_property(&self, id: usize, value: &glib::Value, pspec: &glib::ParamSpec) { + Self::derived_set_property(self, id, value, pspec) } fn signals() -> &'static [Signal] { @@ -228,20 +217,6 @@ impl Port { res } - pub fn pipewire_id(&self) -> u32 { - self.property("pipewire-id") - } - - /// Get the nodes `name` property, which represents the displayed name. - pub fn name(&self) -> String { - self.property("name") - } - - /// Set the nodes `name` property, which represents the displayed name. - pub fn set_name(&self, name: &str) { - self.set_property("name", name); - } - pub fn direction(&self) -> &Direction { self.imp() .direction