show pulsing buffering bar after 3 seconds

This commit is contained in:
Erica Z 2024-11-05 11:33:59 +01:00
parent 88962c629c
commit 38b3902f69
3 changed files with 190 additions and 146 deletions

View file

@ -2,6 +2,16 @@ using Gtk 4.0;
using Adw 1;
template $AudreyUiPlaybar: Adw.Bin {
child: Overlay {
[overlay]
ProgressBar pulse_bar {
styles [
"osd"
]
visible: bind template.show-pulse-bar;
valign: start;
}
child: CenterBox {
hexpand: true;
@ -84,6 +94,7 @@ template $AudreyUiPlaybar: Adw.Bin {
orientation: horizontal;
width-request: 400;
sensitive: bind template.idle-active inverted;
adjustment: Adjustment {
lower: 0;
value: bind template.position;
@ -172,4 +183,5 @@ template $AudreyUiPlaybar: Adw.Bin {
}
}
};
};
}

View file

@ -11,6 +11,10 @@ mod imp {
#[properties(wrapper_type = super::Playbar)]
#[template(resource = "/eu/callcc/audrey/playbar.ui")]
pub struct Playbar {
#[template_child]
#[property(get)]
pulse_bar: TemplateChild<gtk::ProgressBar>,
#[property(get, set)]
song: RefCell<Option<PlaybinSong>>,
#[property(get, set)]
@ -34,6 +38,9 @@ mod imp {
position: Cell<f64>,
#[property(get, set)]
duration: Cell<f64>,
#[property(get, set)]
_show_pulse_bar: Cell<bool>,
}
#[glib::object_subclass]

View file

@ -54,6 +54,8 @@ mod imp {
#[property(type = u32, get = Self::playlist_count)]
_playlist_count: (),
tick_callback: Cell<Option<gtk::TickCallbackId>>,
pub(super) queued_seek: Cell<Option<f64>>,
pub(crate) initial_setup_handle: RefCell<Option<JoinHandle<()>>>,
@ -103,7 +105,9 @@ mod imp {
_idle_active: (),
_playlist_count: (),
queued_seek: Cell::new(None),
tick_callback: Default::default(),
queued_seek: Default::default(),
initial_setup_handle: Default::default(),
mpv_event_loop_handle: Default::default(),
@ -202,7 +206,9 @@ mod imp {
},
Event::StartFile(_) => {
event!(Level::INFO, "start file event");
window.notify("song");
window.imp().buffering_start();
// TODO: load cover art
}
@ -258,12 +264,12 @@ mod imp {
Event::Seek => {
event!(Level::INFO, "seek event");
// TODO: if doing "buffering" progress bar, show it after a timeout here
window.imp().buffering_start();
}
Event::PlaybackRestart => {
event!(Level::INFO, "playback restart event");
// TODO: if doing "buffering" progress bar, hide it here
window.imp().buffering_end();
if let Some(queued_seek) = window.imp().queued_seek.take() {
// a seek was tried before and failed, try again now
@ -478,6 +484,25 @@ mod imp {
Some(song)
}
}
fn buffering_start(&self) {
let started_buffering = std::time::Instant::now();
self.tick_callback
.replace(Some(self.obj().add_tick_callback(move |window, _| {
// 3 second period from gnome hig
if started_buffering.elapsed() > std::time::Duration::from_secs(3) {
window.imp().playbar.set_show_pulse_bar(true);
window.imp().playbar.pulse_bar().pulse();
}
glib::ControlFlow::Continue
})))
.map(gtk::TickCallbackId::remove);
}
fn buffering_end(&self) {
self.tick_callback.take().map(gtk::TickCallbackId::remove);
self.playbar.set_show_pulse_bar(false);
}
}
impl Drop for Window {