From f4a90bc221466037c2d026919e70b6ecf3a4b32b Mon Sep 17 00:00:00 2001 From: Nicolai Syvertsen Date: Sat, 11 May 2024 00:53:17 +0200 Subject: [PATCH] Remove PwNodeModel, use gio::ListStore instead. --- src/backend/manager.rs | 24 ++++++++--- src/backend/mod.rs | 2 - src/backend/pwnodemodel.rs | 83 -------------------------------------- src/ui/volumebox.rs | 6 --- 4 files changed, 19 insertions(+), 96 deletions(-) delete mode 100644 src/backend/pwnodemodel.rs diff --git a/src/backend/manager.rs b/src/backend/manager.rs index caacff2..35a8461 100644 --- a/src/backend/manager.rs +++ b/src/backend/manager.rs @@ -2,7 +2,7 @@ use crate::macros::*; use crate::{ - backend::NodeType, backend::PwDeviceObject, backend::PwNodeFilterModel, backend::PwNodeModel, backend::PwNodeObject, ui::PwvucontrolWindow, + backend::NodeType, backend::PwDeviceObject, backend::PwNodeFilterModel, backend::PwNodeObject, ui::PwvucontrolWindow, ui::PwvucontrolWindowView, PwvucontrolApplication, }; use gtk::{ @@ -32,7 +32,7 @@ mod imp { pub wp_object_manager: OnceCell, #[property(get)] - pub(crate) node_model: PwNodeModel, + pub(crate) node_model: gio::ListStore, #[property(get)] pub(crate) stream_output_model: PwNodeFilterModel, @@ -67,7 +67,7 @@ mod imp { Self { wp_core: Default::default(), wp_object_manager: Default::default(), - node_model: Default::default(), + node_model: gio::ListStore::new::(), stream_input_model: PwNodeFilterModel::new(NodeType::StreamInput, None::), stream_output_model: PwNodeFilterModel::new(NodeType::StreamOutput, None::), source_model: PwNodeFilterModel::new(NodeType::Source, None::), @@ -215,8 +215,7 @@ mod imp { wp_om.connect_object_removed(clone!(@weak self as imp => move |_, object| { if let Some(node) = object.dynamic_cast_ref::() { pwvucontrol_info!("removed: {:?} id: {}", node.name(), node.bound_id()); - let model = &imp.node_model; - model.remove(node.bound_id()); + imp.obj().remove_node_by_id(node.bound_id()); } else if let Some(device) = object.dynamic_cast_ref::() { imp.obj().remove_device_by_id(device.bound_id()); } else { @@ -344,6 +343,21 @@ impl PwvucontrolManager { None } + pub fn remove_node_by_id(&self, id: u32) { + let nodemodel = &self.imp().node_model; + + for (i, item) in (0..).zip(nodemodel.iter::()) { + if let Ok(item) = item { + if item.wpnode().bound_id() == id { + nodemodel.remove(i); + break; + } + } else { + panic!("Device model mutated during iteration!"); + } + } + } + pub fn get_model_for_nodetype(&self, nodetype: NodeType) -> PwNodeFilterModel { match nodetype { NodeType::Sink => self.sink_model(), diff --git a/src/backend/mod.rs b/src/backend/mod.rs index e5da25f..ce1bd94 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -3,7 +3,6 @@ mod pwchannelobject; mod manager; mod pwdeviceobject; mod pwprofileobject; -mod pwnodemodel; mod pwnodeobject; mod pwrouteobject; mod routedirection; @@ -15,7 +14,6 @@ pub use pwchannelobject::PwChannelObject; pub use manager::PwvucontrolManager; pub use pwdeviceobject::PwDeviceObject; pub use pwprofileobject::PwProfileObject; -pub use pwnodemodel::PwNodeModel; pub use pwnodeobject::{PwNodeObject, NodeType}; pub use pwrouteobject::PwRouteObject; pub use routedirection::RouteDirection; diff --git a/src/backend/pwnodemodel.rs b/src/backend/pwnodemodel.rs deleted file mode 100644 index 2968b0e..0000000 --- a/src/backend/pwnodemodel.rs +++ /dev/null @@ -1,83 +0,0 @@ -// SPDX-License-Identifier: GPL-3.0-or-later - -use crate::backend::PwNodeObject; -use gtk::{gio, glib, prelude::*, subclass::prelude::*}; -use std::cell::RefCell; -use imbl::Vector; - -mod imp { - use super::*; - - #[derive(Debug, Default)] - pub struct PwNodeModel(pub(super) RefCell>); - - #[glib::object_subclass] - impl ObjectSubclass for PwNodeModel { - const NAME: &'static str = "PwNodeModel"; - type Type = super::PwNodeModel; - type Interfaces = (gio::ListModel,); - } - - impl ObjectImpl for PwNodeModel {} - - impl ListModelImpl for PwNodeModel { - fn item_type(&self) -> glib::Type { - PwNodeObject::static_type() - } - fn n_items(&self) -> u32 { - self.0.borrow().len() as u32 - } - fn item(&self, position: u32) -> Option { - self.0 - .borrow() - .get(position as usize) - .map(|o| o.clone().upcast::()) - } - } -} - -glib::wrapper! { - pub struct PwNodeModel(ObjectSubclass) @implements gio::ListModel; -} - -impl PwNodeModel { - pub(crate) fn new() -> PwNodeModel { - glib::Object::new() - } - - pub(crate) fn append(&self, obj: &PwNodeObject) { - let imp = self.imp(); - let index = { - // Borrow the data only once and ensure the borrow guard is dropped - // before we emit the items_changed signal because the view - // could call get_item / get_n_item from the signal handler to update its state - let mut data = imp.0.borrow_mut(); - data.push_back(obj.clone()); - data.len() - 1 - }; - // Emits a signal that 1 item was added, 0 removed at the position index - self.items_changed(index as u32, 0, 1); - } - - pub(crate) fn remove(&self, id: u32) { - let imp = self.imp(); - let mut vector = imp.0.borrow_mut(); - for (i,v) in (0..).zip(vector.iter()) { - if id == v.boundid() { - vector.remove(i as usize); - drop(vector); - // Emits a signal that 1 item was removed, 0 added at the position index - self.items_changed(i, 1, 0); - break; - } - } - - } - -} - -impl Default for PwNodeModel { - fn default() -> Self { - Self::new() - } -} \ No newline at end of file diff --git a/src/ui/volumebox.rs b/src/ui/volumebox.rs index fdea091..6c72a39 100644 --- a/src/ui/volumebox.rs +++ b/src/ui/volumebox.rs @@ -235,12 +235,6 @@ glib::wrapper! { } impl PwVolumeBox { - pub(crate) fn new(channel_object: &impl glib::IsA) -> Self { - glib::Object::builder() - .property("node-object", channel_object) - //.property("channelmodel", gio::ListStore::new::()) - .build() - } pub(crate) fn set_level(&self, level: f32) { self.imp().level.set(level);