From 0f2351ae0164ae879d03870f428266425cdb18bd Mon Sep 17 00:00:00 2001 From: Erica Z Date: Wed, 30 Oct 2024 12:59:16 +0100 Subject: [PATCH] make the play pause button work again --- resources/playbar.blp | 2 +- src/playbin.rs | 23 +++++++++++++++++++++++ src/playbin/state.rs | 26 +++++++++++++++++--------- src/ui/playbar.rs | 31 +++++++++++++++++++++---------- 4 files changed, 62 insertions(+), 20 deletions(-) diff --git a/resources/playbar.blp b/resources/playbar.blp index 890ab67..91fa730 100644 --- a/resources/playbar.blp +++ b/resources/playbar.blp @@ -121,7 +121,7 @@ template $AudreyUiPlaybar: Adw.Bin { valign: center; sensitive: bind $can_press_play (template.playbin as <$AudreyPlaybin>.state as <$AudreyPlaybinState>, template.playbin as <$AudreyPlaybin>.play-queue-length) as ; - clicked => $on_play_pause_clicked (); + clicked => $on_play_pause_clicked (template); } Button { diff --git a/src/playbin.rs b/src/playbin.rs index fd41c69..14a0d6c 100644 --- a/src/playbin.rs +++ b/src/playbin.rs @@ -19,6 +19,9 @@ mod ffi { extern "C" { pub fn audrey_playbin_get_type() -> glib::ffi::GType; + pub fn audrey_playbin_get_state(self_: *mut AudreyPlaybin) -> super::state::ffi::State; + pub fn audrey_playbin_pause(self_: *mut AudreyPlaybin); + pub fn audrey_playbin_play(self_: *mut AudreyPlaybin); } } @@ -31,3 +34,23 @@ glib::wrapper! { type_ => || ffi::audrey_playbin_get_type(), } } + +impl Playbin { + pub fn get_state(&self) -> State { + use glib::translate::ToGlibPtr; + + 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) } + } +} diff --git a/src/playbin/state.rs b/src/playbin/state.rs index ef09b6e..7379bc4 100644 --- a/src/playbin/state.rs +++ b/src/playbin/state.rs @@ -1,13 +1,15 @@ -mod ffi { +pub mod ffi { use gtk::glib; + pub type State = std::ffi::c_int; + extern "C" { pub fn audrey_playbin_state_get_type() -> glib::ffi::GType; } } -use gtk::glib; use glib::prelude::*; +use gtk::glib; #[derive(Copy, Clone, PartialEq, Eq)] pub enum State { @@ -16,13 +18,9 @@ pub enum State { Playing, } -unsafe impl<'a> glib::value::FromValue<'a> for State { - type Checker = glib::value::GenericValueTypeChecker; - - unsafe fn from_value(value: &'a glib::Value) -> Self { - use glib::translate::ToGlibPtr; - - match glib::gobject_ffi::g_value_get_enum(value.to_glib_none().0) { +impl glib::translate::FromGlib for State { + unsafe fn from_glib(value: ffi::State) -> Self { + match value { 0 => Self::Stopped, 1 => Self::Paused, 2 => Self::Playing, @@ -31,6 +29,16 @@ unsafe impl<'a> glib::value::FromValue<'a> for State { } } +unsafe impl<'a> glib::value::FromValue<'a> for State { + type Checker = glib::value::GenericValueTypeChecker; + + unsafe fn from_value(value: &'a glib::Value) -> Self { + use glib::translate::ToGlibPtr; + + glib::translate::from_glib(glib::gobject_ffi::g_value_get_enum(value.to_glib_none().0)) + } +} + impl StaticType for State { fn static_type() -> glib::Type { unsafe { glib::translate::from_glib(ffi::audrey_playbin_state_get_type()) } diff --git a/src/ui/playbar.rs b/src/ui/playbar.rs index fb8bee1..584ad54 100644 --- a/src/ui/playbar.rs +++ b/src/ui/playbar.rs @@ -96,11 +96,20 @@ mod imp { #[template_callback] fn mute_button_icon_name(&self, mute: bool) -> &'static str { - if mute { "audio-volume-muted" } else { "audio-volume-high" } + if mute { + "audio-volume-muted" + } else { + "audio-volume-high" + } } #[template_callback] - fn on_play_position_seek(&self, _range: >k::Range, _scroll_type: gtk::ScrollType, _value: f64) -> bool { + fn on_play_position_seek( + &self, + _range: >k::Range, + _scroll_type: gtk::ScrollType, + _value: f64, + ) -> bool { /* if (range.adjustment.lower < range.adjustment.upper) { this.playbin.seek ((int64) value); @@ -139,15 +148,17 @@ mod imp { // this.playbin.seek (new_position); todo!() } - + #[template_callback] - fn on_play_pause_clicked(&self) { - // if (this.playbin.state == PlaybinState.PLAYING) { - // this.playbin.pause(); - // } else { - // this.playbin.play(); - // } - todo!() + fn on_play_pause_clicked(&self, _button: >k::Button) { + let playbin = self.playbin.borrow(); + let playbin = playbin.as_ref().unwrap(); + + if playbin.get_state() == crate::playbin::State::Playing { + playbin.pause(); + } else { + playbin.play(); + } } #[template_callback]