feat(qt): QbzPlayer.seekBy with the seekbar's buffered-edge clamp

The keyboard seek never clamped to npSeekableMax; the shared helper now
does, so keys and the coming ±10 s buttons behave like a click on the bar.

cc
This commit is contained in:
vicrodh committed 2026-09-13 03:50:28 -06:00
1 parent 42d6087c6c
commit 3ddb7cd083
4 files changed
+57 -7

No files matched your search

+40 -7
View File
@@ -245,16 +245,36 @@ fn select_all_active() -> bool {
true
}
/// §1.3 seek math, verbatim from keybindings.rs:572-581:
/// `clamp(npElapsedSecs ± d, 0, npDurationSecs) / npDurationSecs`, same 1 Hz
/// base (the now_playing model is fed by the poll pump — immersive D14).
pub(crate) fn seek_relative(delta: i32) {
let (pos, duration) = crate::now_playing::position();
/// §1.3 seek math (`clamp(npElapsedSecs ± d, 0, npDurationSecs) /
/// npDurationSecs`) plus the clamp every seekbar applies on click/drag and the
/// keyboard path never had: not past the buffered edge of a stream.
pub(crate) fn relative_seek_fraction(
pos: i32,
duration: i32,
delta: i32,
seekable_max: f32,
) -> Option<f32> {
if duration <= 0 {
return;
return None;
}
let target = (pos + delta).clamp(0, duration);
crate::transport_seek(target as f32 / duration as f32);
let frac = target as f32 / duration as f32;
Some(if seekable_max > 0.0 {
frac.min(seekable_max)
} else {
frac
})
}
/// Same 1 Hz base as the seekbars (the now_playing model is fed by the poll
/// pump — immersive D14). Shared by the hotkeys and the ±10 s buttons.
pub(crate) fn seek_relative(delta: i32) {
let (pos, duration) = crate::now_playing::position();
if let Some(frac) =
relative_seek_fraction(pos, duration, delta, crate::now_playing::seekable_max())
{
crate::transport_seek(frac);
}
}
/// Keyboard volume step — the same 0.05 the player bars' +/- steppers use.
@@ -572,6 +592,19 @@ impl qbz_hotkeys::QbzHotkeys {
mod tests {
use super::*;
#[test]
fn relative_seek_clamps_to_the_track_and_to_the_buffered_edge() {
use super::relative_seek_fraction as f;
assert_eq!(f(10, 100, 10, 1.0), Some(0.2));
assert_eq!(f(5, 100, -10, 1.0), Some(0.0));
assert_eq!(f(95, 100, 10, 1.0), Some(1.0));
// A stream with only half the track buffered: never past the edge.
assert_eq!(f(45, 100, 10, 0.5), Some(0.5));
// No seek lock published yet (0.0) means "no limit".
assert_eq!(f(95, 100, 10, 0.0), Some(1.0));
assert_eq!(f(10, 0, 10, 1.0), None);
}
#[test]
fn groups_default_is_full_shape() {
// trap 15: the construction-seeded default parses and carries all
+4
View File
@@ -2578,6 +2578,10 @@ pub(crate) fn transport_seek(frac: f32) {
spawn(async move { playback_qt::seek_frac(&runtime, frac).await });
}
pub(crate) fn transport_seek_by(delta_secs: i32) {
hotkeys_bridge::seek_relative(delta_secs);
}
pub(crate) fn transport_set_volume(volume: f32) {
// Local model first (instant UI), then the engine.
now_playing::set_volume(volume);
+5
View File
@@ -296,6 +296,11 @@ pub(crate) fn position() -> (i32, i32) {
with_model(|m| (m.elapsed_secs, m.duration_secs)).0
}
/// The published seek lock (`np_seekable_max`, 0.0 = not yet published).
pub(crate) fn seekable_max() -> f32 {
with_model(|m| m.seekable_max).0
}
/// (artist_id, title) of the current track — the read the immersive
/// Suggestions loader makes off the Slint `NowPlayingState`
/// (`main.rs:16699-16702`: the panel only has the track id, the seed artist
+8
View File
@@ -181,6 +181,10 @@ pub mod qbz_player {
fn previous(self: Pin<&mut QbzPlayer>);
#[qinvokable]
fn seek(self: Pin<&mut QbzPlayer>, frac: f32);
/// Relative seek in whole seconds (the ±10 s buttons); clamps to the
/// track and to the buffered edge like the seekbars do.
#[qinvokable]
fn seek_by(self: Pin<&mut QbzPlayer>, delta_secs: i32);
#[qinvokable]
fn set_volume(self: Pin<&mut QbzPlayer>, volume: f32);
/// Persist the SETTLED volume (drag-end only — see QbzSlider.released).
@@ -464,6 +468,10 @@ impl qbz_player::QbzPlayer {
crate::transport_seek(frac);
}
pub fn seek_by(self: Pin<&mut Self>, delta_secs: i32) {
crate::transport_seek_by(delta_secs);
}
pub fn persist_volume(self: Pin<&mut Self>, fraction: f32) {
crate::settings_qt::save_pref("volume", serde_json::json!(fraction.clamp(0.0, 1.0)));
}