remove some boilerplate

This commit is contained in:
Erica Z 2024-11-02 08:54:44 +01:00
parent 3b3e3166ca
commit 173494c71f
2 changed files with 49 additions and 40 deletions

View file

@ -121,52 +121,44 @@ impl Player {
.interface::<_, Self>("/org/mpris/MediaPlayer2") .interface::<_, Self>("/org/mpris/MediaPlayer2")
.await?; .await?;
playbin.connect_closure( playbin.connect_new_track(glib::clone!(
"new-track", #[strong]
false, player_ref,
glib::closure_local!( move |_, song| {
#[strong] let metadata = MetadataMap::from_playbin_song(Some(song));
player_ref,
move |_playbin: &crate::Playbin, song: &crate::playbin::Song| {
let metadata = MetadataMap::from_playbin_song(Some(song));
let player_ref = player_ref.clone(); let player_ref = player_ref.clone();
glib::spawn_future_local(async move { glib::spawn_future_local(async move {
let mut player = player_ref.get_mut().await; let mut player = player_ref.get_mut().await;
player.metadata = metadata; player.metadata = metadata;
player player
.metadata_changed(player_ref.signal_emitter()) .metadata_changed(player_ref.signal_emitter())
.await .await
.unwrap(); .unwrap();
}); });
} }
), ));
);
playbin.connect_closure( playbin.connect_seeked(glib::clone!(
"seeked", #[strong]
false, player_ref,
glib::closure_local!( move |_, position| {
#[strong] let player_ref = player_ref.clone();
player_ref, glib::spawn_future_local(async move {
move |_playbin: &crate::Playbin, position: f64| { player_ref
let player_ref = player_ref.clone(); .seeked((position * MICROSECONDS) as i64)
glib::spawn_future_local(async move { .await
player_ref .unwrap();
.seeked((position * MICROSECONDS) as i64) });
.await }
.unwrap(); ));
});
}
),
);
playbin.connect_notify_local( playbin.connect_notify_local(
Some("play-queue-length"), Some("play-queue-length"),
glib::clone!( glib::clone!(
#[strong] #[strong]
player_ref, player_ref,
move |_playbin: &crate::Playbin, _| { move |_, _| {
let player_ref = player_ref.clone(); let player_ref = player_ref.clone();
glib::spawn_future_local(async move { glib::spawn_future_local(async move {
let player = player_ref.get_mut().await; let player = player_ref.get_mut().await;
@ -193,7 +185,7 @@ impl Player {
glib::clone!( glib::clone!(
#[strong] #[strong]
player_ref, player_ref,
move |_playbin: &crate::Playbin, _| { move |_, _| {
let player_ref = player_ref.clone(); let player_ref = player_ref.clone();
glib::spawn_future_local(async move { glib::spawn_future_local(async move {
let player = player_ref.get_mut().await; let player = player_ref.get_mut().await;
@ -212,7 +204,7 @@ impl Player {
glib::clone!( glib::clone!(
#[strong] #[strong]
player_ref, player_ref,
move |_playbin: &crate::Playbin, _| { move |_, _| {
let player_ref = player_ref.clone(); let player_ref = player_ref.clone();
glib::spawn_future_local(async move { glib::spawn_future_local(async move {
let player = player_ref.get_mut().await; let player = player_ref.get_mut().await;

View file

@ -49,6 +49,7 @@ pub mod ffi {
} }
} }
use adw::prelude::*;
use glib::translate::{from_glib, from_glib_none, IntoGlib, ToGlibPtr}; use glib::translate::{from_glib, from_glib_none, IntoGlib, ToGlibPtr};
use gtk::{gio, glib}; use gtk::{gio, glib};
@ -136,4 +137,20 @@ impl Playbin {
pub fn play_queue(&self) -> gio::ListModel { pub fn play_queue(&self) -> gio::ListModel {
unsafe { from_glib_none(ffi::audrey_playbin_get_play_queue(self.to_glib_none().0)) } unsafe { from_glib_none(ffi::audrey_playbin_get_play_queue(self.to_glib_none().0)) }
} }
pub fn connect_new_track<F: Fn(&Self, &Song) + 'static>(&self, f: F) -> glib::SignalHandlerId {
self.connect_closure(
"new-track",
false,
glib::closure_local!(|playbin, song| f(playbin, song)),
)
}
pub fn connect_seeked<F: Fn(&Self, f64) + 'static>(&self, f: F) -> glib::SignalHandlerId {
self.connect_closure(
"seeked",
false,
glib::closure_local!(|playbin, position| f(playbin, position)),
)
}
} }