fun with actions

This commit is contained in:
Erica Z 2024-11-13 19:40:04 +01:00
parent 3940392f2d
commit 2e0a0ef40f
3 changed files with 63 additions and 26 deletions

View file

@ -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 {

View file

@ -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: &gtk::Button) {
if self.window().idle_active() {

View file

@ -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::<super::Window>("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();
}