From 173494c71f1943fe845dea5bcfe8066bfac90eb6 Mon Sep 17 00:00:00 2001 From: Erica Z Date: Sat, 2 Nov 2024 08:54:44 +0100 Subject: [PATCH] remove some boilerplate --- src/mpris/player.rs | 72 ++++++++++++++++++++------------------------- src/playbin.rs | 17 +++++++++++ 2 files changed, 49 insertions(+), 40 deletions(-) diff --git a/src/mpris/player.rs b/src/mpris/player.rs index 3202f90..3868395 100644 --- a/src/mpris/player.rs +++ b/src/mpris/player.rs @@ -121,52 +121,44 @@ impl Player { .interface::<_, Self>("/org/mpris/MediaPlayer2") .await?; - playbin.connect_closure( - "new-track", - false, - glib::closure_local!( - #[strong] - player_ref, - move |_playbin: &crate::Playbin, song: &crate::playbin::Song| { - let metadata = MetadataMap::from_playbin_song(Some(song)); + playbin.connect_new_track(glib::clone!( + #[strong] + player_ref, + move |_, song| { + let metadata = MetadataMap::from_playbin_song(Some(song)); - let player_ref = player_ref.clone(); - glib::spawn_future_local(async move { - let mut player = player_ref.get_mut().await; - player.metadata = metadata; - player - .metadata_changed(player_ref.signal_emitter()) - .await - .unwrap(); - }); - } - ), - ); + let player_ref = player_ref.clone(); + glib::spawn_future_local(async move { + let mut player = player_ref.get_mut().await; + player.metadata = metadata; + player + .metadata_changed(player_ref.signal_emitter()) + .await + .unwrap(); + }); + } + )); - playbin.connect_closure( - "seeked", - false, - glib::closure_local!( - #[strong] - player_ref, - move |_playbin: &crate::Playbin, position: f64| { - let player_ref = player_ref.clone(); - glib::spawn_future_local(async move { - player_ref - .seeked((position * MICROSECONDS) as i64) - .await - .unwrap(); - }); - } - ), - ); + playbin.connect_seeked(glib::clone!( + #[strong] + player_ref, + move |_, position| { + let player_ref = player_ref.clone(); + glib::spawn_future_local(async move { + player_ref + .seeked((position * MICROSECONDS) as i64) + .await + .unwrap(); + }); + } + )); playbin.connect_notify_local( Some("play-queue-length"), glib::clone!( #[strong] player_ref, - move |_playbin: &crate::Playbin, _| { + move |_, _| { let player_ref = player_ref.clone(); glib::spawn_future_local(async move { let player = player_ref.get_mut().await; @@ -193,7 +185,7 @@ impl Player { glib::clone!( #[strong] player_ref, - move |_playbin: &crate::Playbin, _| { + move |_, _| { let player_ref = player_ref.clone(); glib::spawn_future_local(async move { let player = player_ref.get_mut().await; @@ -212,7 +204,7 @@ impl Player { glib::clone!( #[strong] player_ref, - move |_playbin: &crate::Playbin, _| { + move |_, _| { let player_ref = player_ref.clone(); glib::spawn_future_local(async move { let player = player_ref.get_mut().await; diff --git a/src/playbin.rs b/src/playbin.rs index 75891ad..6d6ce73 100644 --- a/src/playbin.rs +++ b/src/playbin.rs @@ -49,6 +49,7 @@ pub mod ffi { } } +use adw::prelude::*; use glib::translate::{from_glib, from_glib_none, IntoGlib, ToGlibPtr}; use gtk::{gio, glib}; @@ -136,4 +137,20 @@ impl Playbin { pub fn play_queue(&self) -> gio::ListModel { unsafe { from_glib_none(ffi::audrey_playbin_get_play_queue(self.to_glib_none().0)) } } + + pub fn connect_new_track(&self, f: F) -> glib::SignalHandlerId { + self.connect_closure( + "new-track", + false, + glib::closure_local!(|playbin, song| f(playbin, song)), + ) + } + + pub fn connect_seeked(&self, f: F) -> glib::SignalHandlerId { + self.connect_closure( + "seeked", + false, + glib::closure_local!(|playbin, position| f(playbin, position)), + ) + } }