From ab354a35c08ccf8f7d5e46828cd5eef48d20d564 Mon Sep 17 00:00:00 2001 From: Erica Z Date: Thu, 31 Oct 2024 07:59:09 +0100 Subject: [PATCH] unfuck playbar --- src/playbin.rs | 49 +++++++++++++++++++++++++++++------------ src/playbin/song.rs | 6 ++--- src/ui/playbar.rs | 53 ++++++++++++++++++++++++++------------------- 3 files changed, 69 insertions(+), 39 deletions(-) diff --git a/src/playbin.rs b/src/playbin.rs index 43799ec..7c521ef 100644 --- a/src/playbin.rs +++ b/src/playbin.rs @@ -25,9 +25,18 @@ mod ffi { pub fn audrey_playbin_get_volume(self_: *mut AudreyPlaybin) -> std::ffi::c_int; pub fn audrey_playbin_set_volume(self_: *mut AudreyPlaybin, volume: std::ffi::c_int); pub fn audrey_playbin_seek(self_: *mut AudreyPlaybin, position: f64); + pub fn audrey_playbin_go_to_next_track(self_: *mut AudreyPlaybin); + pub fn audrey_playbin_go_to_prev_track(self_: *mut AudreyPlaybin); + pub fn audrey_playbin_get_position(self_: *mut AudreyPlaybin) -> f64; + pub fn audrey_playbin_get_duration(self_: *mut AudreyPlaybin) -> f64; + pub fn audrey_playbin_get_mute(self_: *mut AudreyPlaybin) -> glib::ffi::gboolean; + pub fn audrey_playbin_set_mute(self_: *mut AudreyPlaybin, mute: glib::ffi::gboolean); } } +use glib::translate::from_glib; +use glib::translate::IntoGlib; +use glib::translate::ToGlibPtr; use gtk::glib; glib::wrapper! { @@ -39,39 +48,51 @@ glib::wrapper! { } impl Playbin { - pub fn get_state(&self) -> State { - use glib::translate::ToGlibPtr; - + pub fn state(&self) -> State { unsafe { glib::translate::from_glib(ffi::audrey_playbin_get_state(self.to_glib_none().0)) } } pub fn pause(&self) { - use glib::translate::ToGlibPtr; - unsafe { ffi::audrey_playbin_pause(self.to_glib_none().0) } } pub fn play(&self) { - use glib::translate::ToGlibPtr; - unsafe { ffi::audrey_playbin_play(self.to_glib_none().0) } } - pub fn get_volume(&self) -> i32 { - use glib::translate::ToGlibPtr; - + pub fn volume(&self) -> i32 { unsafe { ffi::audrey_playbin_get_volume(self.to_glib_none().0) } } pub fn set_volume(&self, value: i32) { - use glib::translate::ToGlibPtr; - unsafe { ffi::audrey_playbin_set_volume(self.to_glib_none().0, value) } } pub fn seek(&self, position: f64) { - use glib::translate::ToGlibPtr; - unsafe { ffi::audrey_playbin_seek(self.to_glib_none().0, position) } } + + pub fn go_to_next_track(&self) { + unsafe { ffi::audrey_playbin_go_to_next_track(self.to_glib_none().0) } + } + + pub fn go_to_prev_track(&self) { + unsafe { ffi::audrey_playbin_go_to_prev_track(self.to_glib_none().0) } + } + + pub fn position(&self) -> f64 { + unsafe { ffi::audrey_playbin_get_position(self.to_glib_none().0) } + } + + pub fn duration(&self) -> f64 { + unsafe { ffi::audrey_playbin_get_duration(self.to_glib_none().0) } + } + + pub fn mute(&self) -> bool { + unsafe { from_glib(ffi::audrey_playbin_get_mute(self.to_glib_none().0)) } + } + + pub fn set_mute(&self, mute: bool) { + unsafe { ffi::audrey_playbin_set_mute(self.to_glib_none().0, mute.into_glib()) } + } } diff --git a/src/playbin/song.rs b/src/playbin/song.rs index 3547de2..fa33a47 100644 --- a/src/playbin/song.rs +++ b/src/playbin/song.rs @@ -36,7 +36,7 @@ glib::wrapper! { } impl Song { - pub fn get_title(&self) -> std::borrow::Cow<'_, str> { + pub fn title(&self) -> std::borrow::Cow<'_, str> { use glib::translate::ToGlibPtr; // TODO: memory management.... @@ -46,7 +46,7 @@ impl Song { }) } - pub fn get_artist(&self) -> std::borrow::Cow<'_, str> { + pub fn artist(&self) -> std::borrow::Cow<'_, str> { use glib::translate::ToGlibPtr; // TODO: memory management.... @@ -56,7 +56,7 @@ impl Song { }) } - pub fn get_album(&self) -> std::borrow::Cow<'_, str> { + pub fn album(&self) -> std::borrow::Cow<'_, str> { use glib::translate::ToGlibPtr; // TODO: memory management.... diff --git a/src/ui/playbar.rs b/src/ui/playbar.rs index 576be40..fe60865 100644 --- a/src/ui/playbar.rs +++ b/src/ui/playbar.rs @@ -18,8 +18,8 @@ mod imp { #[property(get, set, default = true)] show_cover_art: Cell, - #[property(get = Self::get_volume, set = Self::set_volume)] - _volume: i32, + #[property(type = i32, get = Self::volume, set = Self::set_volume)] + _volume: (), } #[glib::object_subclass] @@ -51,7 +51,7 @@ mod imp { fn song_title(&self, song: Option<&crate::playbin::Song>) -> String { match song { None => "".to_owned(), - Some(song) => song.get_title().to_string(), + Some(song) => song.title().to_string(), } } @@ -59,7 +59,7 @@ mod imp { fn song_artist(&self, song: Option<&crate::playbin::Song>) -> String { match song { None => "".to_owned(), - Some(song) => song.get_artist().to_string(), + Some(song) => song.artist().to_string(), } } @@ -67,7 +67,7 @@ mod imp { fn song_album(&self, song: Option<&crate::playbin::Song>) -> String { match song { None => "".to_owned(), - Some(song) => song.get_album().to_string(), + Some(song) => song.album().to_string(), } } @@ -120,32 +120,40 @@ mod imp { #[template_callback] fn on_skip_forward_clicked(&self) { - // this.playbin.go_to_next_track (); - todo!() + let playbin = self.playbin.borrow(); + let playbin = playbin.as_ref().unwrap(); + playbin.go_to_next_track(); } #[template_callback] fn on_skip_backward_clicked(&self) { - // this.playbin.go_to_prev_track (); - todo!() + let playbin = self.playbin.borrow(); + let playbin = playbin.as_ref().unwrap(); + playbin.go_to_prev_track(); } #[template_callback] fn seek_backward(&self) { + let playbin = self.playbin.borrow(); + let playbin = playbin.as_ref().unwrap(); // 10 seconds - // double new_position = playbin.position - 10.0; - // if (new_position < 0.0) new_position = 0.0; - // this.playbin.seek (new_position); - todo!() + let mut new_position = playbin.position() - 10.0; + if new_position < 0.0 { + new_position = 0.0; + } + playbin.seek(new_position); } #[template_callback] fn seek_forward(&self) { + let playbin = self.playbin.borrow(); + let playbin = playbin.as_ref().unwrap(); // 10 seconds - // double new_position = playbin.position + 10.0; - // if (new_position > this.playbin.duration) new_position = this.playbin.duration; - // this.playbin.seek (new_position); - todo!() + let mut new_position = playbin.position() + 10.0; + if new_position > playbin.duration() { + new_position = playbin.duration(); + } + playbin.seek(new_position); } #[template_callback] @@ -153,7 +161,7 @@ mod imp { let playbin = self.playbin.borrow(); let playbin = playbin.as_ref().unwrap(); - if playbin.get_state() == crate::playbin::State::Playing { + if playbin.state() == crate::playbin::State::Playing { playbin.pause(); } else { playbin.play(); @@ -162,16 +170,17 @@ mod imp { #[template_callback] fn on_mute_toggle(&self) { - //this.playbin.mute = !this.playbin.mute; - todo!() + let playbin = self.playbin.borrow(); + let playbin = playbin.as_ref().unwrap(); + playbin.set_mute(!playbin.mute()); } - fn get_volume(&self) -> i32 { + fn volume(&self) -> i32 { let playbin = self.playbin.borrow(); match playbin.as_ref() { None => 100, - Some(playbin) => playbin.get_volume(), + Some(playbin) => playbin.volume(), } }