more attempts at boilerplate removal

This commit is contained in:
Erica Z 2024-11-02 09:42:11 +01:00
parent 9ecb0db1f8
commit ed3b837c79
2 changed files with 26 additions and 12 deletions

View file

@ -153,14 +153,14 @@ impl Player {
} }
)); ));
playbin.connect_notify_local( playbin.connect_notify_future_local(
Some("play-queue-length"), "play-queue-length",
glib::clone!( glib::clone!(
#[strong] #[strong]
player_ref, player_ref,
move |_, _| { move |_, _| {
let player_ref = player_ref.clone(); let player_ref = player_ref.clone();
glib::spawn_future_local(async move { async move {
let player = player_ref.get_mut().await; let player = player_ref.get_mut().await;
// properties that depend on the play queue length // properties that depend on the play queue length
player player
@ -175,44 +175,44 @@ impl Player {
.can_play_changed(player_ref.signal_emitter()) .can_play_changed(player_ref.signal_emitter())
.await .await
.unwrap(); .unwrap();
}); }
} }
), ),
); );
playbin.connect_notify_local( playbin.connect_notify_future_local(
Some("state"), "state",
glib::clone!( glib::clone!(
#[strong] #[strong]
player_ref, player_ref,
move |_, _| { move |_, _| {
let player_ref = player_ref.clone(); let player_ref = player_ref.clone();
glib::spawn_future_local(async move { async move {
let player = player_ref.get_mut().await; let player = player_ref.get_mut().await;
// properties that depend on the playbin state // properties that depend on the playbin state
player player
.playback_status_changed(player_ref.signal_emitter()) .playback_status_changed(player_ref.signal_emitter())
.await .await
.unwrap(); .unwrap();
}); }
} }
), ),
); );
playbin.connect_notify_local( playbin.connect_notify_future_local(
Some("volume"), "volume",
glib::clone!( glib::clone!(
#[strong] #[strong]
player_ref, player_ref,
move |_, _| { move |_, _| {
let player_ref = player_ref.clone(); let player_ref = player_ref.clone();
glib::spawn_future_local(async move { async move {
let player = player_ref.get_mut().await; let player = player_ref.get_mut().await;
player player
.volume_changed(player_ref.signal_emitter()) .volume_changed(player_ref.signal_emitter())
.await .await
.unwrap(); .unwrap();
}); }
} }
), ),
); );

View file

@ -153,4 +153,18 @@ impl Playbin {
glib::closure_local!(|playbin, position| f(playbin, position)), glib::closure_local!(|playbin, position| f(playbin, position)),
) )
} }
// FIXME: this would be useful in other places, probably
pub fn connect_notify_future_local<
F: Fn(&Self, &glib::ParamSpec) -> U + 'static,
U: std::future::Future<Output = ()> + 'static,
>(
&self,
name: &str,
f: F,
) {
self.connect_notify_local(Some(name), move |self_, param_spec| {
glib::spawn_future_local(f(self_, param_spec));
});
}
} }