From ed3b837c79a2e779c4726085725cbcbab32f1ffa Mon Sep 17 00:00:00 2001 From: Erica Z Date: Sat, 2 Nov 2024 09:42:11 +0100 Subject: [PATCH] more attempts at boilerplate removal --- src/mpris/player.rs | 24 ++++++++++++------------ src/playbin.rs | 14 ++++++++++++++ 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/mpris/player.rs b/src/mpris/player.rs index 62c9ae7..fdb8d3a 100644 --- a/src/mpris/player.rs +++ b/src/mpris/player.rs @@ -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(); - }); + } } ), ); diff --git a/src/playbin.rs b/src/playbin.rs index 6d6ce73..7ea2f26 100644 --- a/src/playbin.rs +++ b/src/playbin.rs @@ -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 + 'static, + >( + &self, + name: &str, + f: F, + ) { + self.connect_notify_local(Some(name), move |self_, param_spec| { + glib::spawn_future_local(f(self_, param_spec)); + }); + } }