Skip to content

Commit

Permalink
Implement PwVolumeBoxExt trait so subclasses can directly call into p…
Browse files Browse the repository at this point in the history
…arent methods
  • Loading branch information
saivert committed Dec 25, 2023
1 parent c9bd60d commit 73f77df
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
12 changes: 6 additions & 6 deletions src/ui/outputbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use glib::{closure_local, clone};
use gtk::{prelude::*, subclass::prelude::*};
use wireplumber as wp;

use super::volumebox::PwVolumeBoxExt;

mod imp {
use super::*;

Expand Down Expand Up @@ -42,10 +44,9 @@ mod imp {
let manager = PwvucontrolManager::default();

let obj = self.obj();
let parent: &PwVolumeBox = obj.upcast_ref();
let item = parent.node_object().expect("nodeobj");
let item = obj.node_object().expect("nodeobj");

parent.add_default_node_change_handler(clone!(@weak self as widget => move || {
obj.set_default_node_change_handler(clone!(@weak self as widget => move || {
widget.obj().update_output_device_dropdown();
}));

Expand Down Expand Up @@ -103,11 +104,10 @@ impl PwOutputBox {
let sinkmodel = &manager.imp().sinkmodel;

let imp = self.imp();
let parent: &PwVolumeBox = self.upcast_ref();

let output_dropdown = imp.output_dropdown.get();

let id = parent.imp().default_node.get();
let id = self.default_node();

let string = if let Ok(node) = sinkmodel.get_node(id) {
format!("Default ({})", node.name())
Expand All @@ -116,7 +116,7 @@ impl PwOutputBox {
};
output_dropdown.set_default_text(&string);

let item = parent.node_object().expect("nodeobj");
let item = self.node_object().expect("nodeobj");

if let Some(deftarget) = item.default_target() {
// let model: gio::ListModel = imp
Expand Down
4 changes: 2 additions & 2 deletions src/ui/sinkbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use glib::clone;
use gtk::{prelude::*, subclass::prelude::*};
use std::cell::Cell;
use wireplumber as wp;
use super::volumebox::PwVolumeBoxExt;

mod imp {
use super::*;
Expand Down Expand Up @@ -43,9 +44,8 @@ mod imp {
self.parent_constructed();

let obj = self.obj();
let parent: &PwVolumeBox = obj.upcast_ref();

parent.add_default_node_change_handler(clone!(@weak self as widget => move || {
obj.set_default_node_change_handler(clone!(@weak self as widget => move || {
widget.obj().default_node_changed();
}));

Expand Down
30 changes: 23 additions & 7 deletions src/ui/volumebox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ mod imp {
timeoutid: Cell<Option<glib::SourceId>>,
pub(super) level: Cell<f32>,
pub default_node: Cell<u32>,
pub(super) default_node_changed_handlers: RefCell<Vec<Box<dyn Fn()>>>,
pub(super) default_node_changed_handler: RefCell<Option<Box<dyn Fn()>>>,

// Template widgets
#[template_child]
Expand Down Expand Up @@ -167,8 +167,8 @@ mod imp {
wp::info!("default-nodes-api changed: new id {id}");
widget.imp().default_node.set(id);

let list = widget.imp().default_node_changed_handlers.borrow();
for cb in list.iter() {
let handler = widget.imp().default_node_changed_handler.borrow();
if let Some(cb) = handler.as_ref() {
cb();
}
});
Expand Down Expand Up @@ -269,14 +269,30 @@ impl PwVolumeBox {
self.imp().level.set(level);
}

pub fn add_default_node_change_handler(&self, c: impl Fn() + 'static) {
pub fn set_default_node_change_handler(&self, c: impl Fn() + 'static) {
let imp = self.imp();

let mut list = imp.default_node_changed_handlers.borrow_mut();
list.push(Box::new(c));
let mut handler = imp.default_node_changed_handler.borrow_mut();
handler.replace(Box::new(c));
}
}
pub trait PwVolumeBoxImpl: ListBoxRowImpl + ObjectImpl + 'static {}
pub trait PwVolumeBoxImpl: ListBoxRowImpl {}

pub trait PwVolumeBoxExt: IsA<PwVolumeBox> {
fn default_node(&self) -> u32 {
self.upcast_ref::<PwVolumeBox>().imp().default_node.get()
}

fn node_object(&self) -> Option<PwNodeObject> {
self.upcast_ref::<PwVolumeBox>().node_object()
}

fn set_default_node_change_handler(&self, c: impl Fn() + 'static) {
self.upcast_ref::<PwVolumeBox>().set_default_node_change_handler(c);
}
}

impl<O: IsA<PwVolumeBox>> PwVolumeBoxExt for O {}

unsafe impl<T: PwVolumeBoxImpl> IsSubclassable<T> for PwVolumeBox {
fn class_init(class: &mut glib::Class<Self>) {
Expand Down

0 comments on commit 73f77df

Please sign in to comment.