From 2e0a0ef40f4aa8f70681c6abc2110fbec90143d3 Mon Sep 17 00:00:00 2001 From: Erica Z Date: Wed, 13 Nov 2024 19:40:04 +0100 Subject: [PATCH] fun with actions --- resources/playbar.blp | 6 ++--- src/ui/playbar.rs | 22 ---------------- src/ui/window.rs | 61 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 26 deletions(-) diff --git a/resources/playbar.blp b/resources/playbar.blp index 4e0bc78..e6a9ed3 100644 --- a/resources/playbar.blp +++ b/resources/playbar.blp @@ -130,8 +130,7 @@ template $AudreyUiPlaybar: Adw.Bin { Button { icon-name: "media-seek-backward"; valign: center; - sensitive: bind template.idle-active inverted; - clicked => $seek_backward() swapped; + action-name: "app.seek-backward"; } Button { @@ -144,8 +143,7 @@ template $AudreyUiPlaybar: Adw.Bin { Button { icon-name: "media-seek-forward"; valign: center; - sensitive: bind template.idle-active inverted; - clicked => $seek_forward() swapped; + action-name: "app.seek-forward"; } Button { diff --git a/src/ui/playbar.rs b/src/ui/playbar.rs index f178a8d..08e312b 100644 --- a/src/ui/playbar.rs +++ b/src/ui/playbar.rs @@ -111,28 +111,6 @@ mod imp { } } - #[template_callback] - fn seek_backward(&self) { - // 10 seconds - let mut new_position = self.window().time_pos() - 10.0; - if new_position < 0.0 { - new_position = 0.0; - } - self.window().seek(new_position); - } - - #[template_callback] - fn seek_forward(&self) { - // 10 seconds - let new_position = self.window().time_pos() + 10.0; - if new_position > self.window().duration() { - // just seek to the next track - self.on_skip_forward_clicked(); - } else { - self.window().seek(new_position); - } - } - #[template_callback] fn on_play_pause_clicked(&self, _button: >k::Button) { if self.window().idle_active() { diff --git a/src/ui/window.rs b/src/ui/window.rs index 21a3ff4..c42ec48 100644 --- a/src/ui/window.rs +++ b/src/ui/window.rs @@ -170,6 +170,66 @@ mod imp { fn constructed(&self) { self.parent_constructed(); + use gio::ActionEntry; + + let action_seek_backward = ActionEntry::builder("seek-backward") + .activate(glib::clone!( + #[weak(rename_to = window)] + self.obj(), + move |_, _, _| { + let new_position = window.time_pos() - 10.0; + if new_position < 0.0 { + window.seek(0.0); + } else { + window.seek(new_position); + } + }, + )) + .build(); + + let action_seek_forward = ActionEntry::builder("seek-forward") + .activate(glib::clone!( + #[weak(rename_to = window)] + self.obj(), + move |_, _, _| { + let new_position = window.time_pos() + 10.0; + if new_position > window.duration() { + // just seek to the next track + if window.playlist_pos() + 1 < window.playlist_count() { + window.playlist_next(); + } else { + window.playlist_play_index(None); + } + } else { + window.seek(new_position); + } + }, + )) + .build(); + + let actions = gio::SimpleActionGroup::new(); + actions.add_action_entries([action_seek_backward, action_seek_forward]); + self.obj().insert_action_group("app", Some(&actions)); + + let playlist_not_empty = gtk::ClosureExpression::with_callback( + [gtk::ObjectExpression::new(self.obj().as_ref()) + .chain_property::("playlist-count")], + |values| { + let playlist_count: i64 = values[1].get().unwrap(); + playlist_count > 0 + }, + ); + playlist_not_empty.bind( + &actions.lookup_action("seek-backward").unwrap(), + "enabled", + None::<&glib::Object>, + ); + playlist_not_empty.bind( + &actions.lookup_action("seek-forward").unwrap(), + "enabled", + None::<&glib::Object>, + ); + let settings = gio::Settings::new(crate::APP_ID); settings.bind("mute", self.obj().as_ref(), "mute").build(); settings @@ -650,6 +710,7 @@ mod imp { } self.state.set(State::Seeking); + self.obj().notify("time-pos"); self.buffering_start(); }