From d3641a6f339e56048d3a090a00be618708e942d5 Mon Sep 17 00:00:00 2001 From: Erica Z Date: Tue, 5 Nov 2024 10:48:50 +0100 Subject: [PATCH] attempt at queued seeks --- src/ui/window.rs | 44 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/src/ui/window.rs b/src/ui/window.rs index 7cdfed5..0c73099 100644 --- a/src/ui/window.rs +++ b/src/ui/window.rs @@ -52,6 +52,8 @@ mod imp { _idle_active: (), #[property(type = u32, get = Self::playlist_count)] _playlist_count: (), + + pub(super) queued_seek: Cell>, } impl Default for Window { @@ -95,6 +97,8 @@ mod imp { _duration: (), _idle_active: (), _playlist_count: (), + + queued_seek: Cell::new(None), } } } @@ -242,6 +246,23 @@ mod imp { // ^ ignore } + Event::PlaybackRestart => { + if let Some(queued_seek) = window.imp().queued_seek.take() { + // a seek was tried before and failed, try again now + event!(Level::INFO, "performing queued seek to {queued_seek}"); + window.seek(queued_seek); + } + } + + Event::EndFile(event) => { + if let Err(err) = event.reason { + event!(Level::ERROR, "end file error: {err}"); + } + + // cancel queued seek always + window.imp().queued_seek.set(None); + } + _ => event!(Level::DEBUG, "unhandled {event:?}"), } } @@ -369,12 +390,17 @@ mod imp { } fn time_pos(&self) -> f64 { - match self.mpv.get_property("time-pos") { - Ok(time_pos) => Ok(time_pos), - Err(err) if err.is_property_unavailable() => Ok(0.0), //placeholder - Err(err) => Err(err), + if let Some(queued_seek) = self.queued_seek.get() { + // counterfeit time-pos while the seek is ongoing + queued_seek + } else { + match self.mpv.get_property("time-pos") { + Ok(time_pos) => Ok(time_pos), + Err(err) if err.is_property_unavailable() => Ok(0.0), //placeholder + Err(err) => Err(err), + } + .unwrap() } - .unwrap() } fn duration(&self) -> f64 { @@ -498,9 +524,11 @@ impl Window { .command(["seek", &new_position.to_string(), "absolute", "exact"]) { Ok(()) => {} - // TODO: buffer this on fail (usually happens when the file isn't done loading) - // to repro, mash the seek forward button - Err(err) => event!(Level::ERROR, "could seek to {new_position}: {err}"), + Err(err) => { + event!(Level::INFO, "queuing seek to {new_position}: {err}"); + self.imp().queued_seek.set(Some(new_position)); + self.notify("time-pos"); + } } } }