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

View file

@ -153,4 +153,18 @@ impl Playbin {
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));
});
}
}