fix playbar seek position

This commit is contained in:
Erica Z 2024-11-03 19:25:32 +01:00
parent 7da4c6cea3
commit 9357c98521
6 changed files with 69 additions and 13 deletions

View file

@ -66,7 +66,7 @@ template $AudreyUiPlaybar: Adw.Bin {
"numeric", "numeric",
] ]
label: bind $format_timestamp (template.playbin as <$AudreyPlaybin>.position) as <string>; label: bind $format_timestamp (template.position) as <string>;
} }
[center] [center]
@ -74,12 +74,12 @@ template $AudreyUiPlaybar: Adw.Bin {
name: "seek-scale"; name: "seek-scale";
orientation: horizontal; orientation: horizontal;
width-request: 400; width-request: 400;
sensitive: bind $playbin_active (template.playbin as <$AudreyPlaybin>.state as <$AudreyPlaybinState>) as <bool>; //sensitive: bind $playbin_active (template.playbin as <$AudreyPlaybin>.state as <$AudreyPlaybinState>) as <bool>;
adjustment: Adjustment { adjustment: Adjustment {
lower: 0; lower: 0;
value: bind template.playbin as <$AudreyPlaybin>.position; value: bind template.position;
upper: bind template.playbin as <$AudreyPlaybin>.duration; upper: bind template.duration;
}; };
change-value => $on_play_position_seek () swapped; change-value => $on_play_position_seek () swapped;
@ -92,7 +92,7 @@ template $AudreyUiPlaybar: Adw.Bin {
"numeric", "numeric",
] ]
label: bind $format_timestamp (template.playbin as <$AudreyPlaybin>.duration) as <string>; label: bind $format_timestamp (template.duration) as <string>;
} }
} }
@ -103,7 +103,7 @@ template $AudreyUiPlaybar: Adw.Bin {
Button { Button {
icon-name: "media-skip-backward"; icon-name: "media-skip-backward";
valign: center; valign: center;
sensitive: bind $playbin_active (template.playbin as <$AudreyPlaybin>.state as <$AudreyPlaybinState>) as <bool>; //sensitive: bind $playbin_active (template.playbin as <$AudreyPlaybin>.state as <$AudreyPlaybinState>) as <bool>;
clicked => $on_skip_backward_clicked () swapped; clicked => $on_skip_backward_clicked () swapped;
} }
@ -111,15 +111,16 @@ template $AudreyUiPlaybar: Adw.Bin {
Button { Button {
icon-name: "media-seek-backward"; icon-name: "media-seek-backward";
valign: center; valign: center;
sensitive: bind $playbin_active (template.playbin as <$AudreyPlaybin>.state as <$AudreyPlaybinState>) as <bool>; //sensitive: bind $playbin_active (template.playbin as <$AudreyPlaybin>.state as <$AudreyPlaybinState>) as <bool>;
clicked => $seek_backward () swapped; clicked => $seek_backward () swapped;
} }
Button { Button {
icon-name: bind $play_pause_icon_name (template.playbin as <$AudreyPlaybin>.state as <$AudreyPlaybinState>) as <string>; icon-name: "media-playback-start";
//icon-name: bind $play_pause_icon_name (template.playbin as <$AudreyPlaybin>.state as <$AudreyPlaybinState>) as <string>;
valign: center; valign: center;
sensitive: bind $can_press_play (template.playbin as <$AudreyPlaybin>.state as <$AudreyPlaybinState>, template.playbin as <$AudreyPlaybin>.play-queue-length) as <bool>; //sensitive: bind $can_press_play (template.playbin as <$AudreyPlaybin>.state as <$AudreyPlaybinState>, template.playbin as <$AudreyPlaybin>.play-queue-length) as <bool>;
clicked => $on_play_pause_clicked () swapped; clicked => $on_play_pause_clicked () swapped;
} }
@ -127,7 +128,7 @@ template $AudreyUiPlaybar: Adw.Bin {
Button { Button {
icon-name: "media-seek-forward"; icon-name: "media-seek-forward";
valign: center; valign: center;
sensitive: bind $playbin_active (template.playbin as <$AudreyPlaybin>.state as <$AudreyPlaybinState>) as <bool>; //sensitive: bind $playbin_active (template.playbin as <$AudreyPlaybin>.state as <$AudreyPlaybinState>) as <bool>;
clicked => $seek_forward () swapped; clicked => $seek_forward () swapped;
} }
@ -135,7 +136,7 @@ template $AudreyUiPlaybar: Adw.Bin {
Button { Button {
icon-name: "media-skip-forward"; icon-name: "media-skip-forward";
valign: center; valign: center;
sensitive: bind $playbin_active (template.playbin as <$AudreyPlaybin>.state as <$AudreyPlaybinState>) as <bool>; //sensitive: bind $playbin_active (template.playbin as <$AudreyPlaybin>.state as <$AudreyPlaybinState>) as <bool>;
clicked => $on_skip_forward_clicked () swapped; clicked => $on_skip_forward_clicked () swapped;
} }

View file

@ -3,7 +3,7 @@ use std::ffi::{c_int, CStr};
use std::fmt; use std::fmt;
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct Error(c_int); pub struct Error(pub(super) c_int);
impl Error { impl Error {
pub(super) fn from_return_code(rc: c_int) -> Result<(), Self> { pub(super) fn from_return_code(rc: c_int) -> Result<(), Self> {

View file

@ -122,6 +122,19 @@ impl GetProperty for i64 {
} }
} }
impl GetProperty for f64 {
unsafe fn get_property(ctx: *mut ffi::mpv_handle, name: *const c_char) -> Result<Self, Error> {
let mut value: f64 = 0.0;
Error::from_return_code(ffi::mpv_get_property(
ctx,
name,
ffi::mpv_format_MPV_FORMAT_DOUBLE,
std::ptr::from_mut::<f64>(&mut value) as *mut c_void,
))?;
Ok(value)
}
}
impl GetProperty for String { impl GetProperty for String {
unsafe fn get_property(ctx: *mut ffi::mpv_handle, name: *const c_char) -> Result<Self, Error> { unsafe fn get_property(ctx: *mut ffi::mpv_handle, name: *const c_char) -> Result<Self, Error> {
let mut value: *mut c_char = std::ptr::null_mut(); let mut value: *mut c_char = std::ptr::null_mut();
@ -136,3 +149,13 @@ impl GetProperty for String {
Ok(result) Ok(result)
} }
} }
impl<T: GetProperty> GetProperty for Option<T> {
unsafe fn get_property(ctx: *mut ffi::mpv_handle, name: *const c_char) -> Result<Self, Error> {
match T::get_property(ctx, name) {
Ok(t) => Ok(Some(t)),
Err(Error(ffi::mpv_error_MPV_ERROR_PROPERTY_UNAVAILABLE)) => Ok(None),
Err(err) => Err(err),
}
}
}

View file

@ -95,7 +95,7 @@ where
} }
pub fn position(&self) -> Option<f64> { pub fn position(&self) -> Option<f64> {
todo!() self.mpv.get_property("time-pos").unwrap()
} }
pub fn current_entry(&self) -> Option<usize> { pub fn current_entry(&self) -> Option<usize> {

View file

@ -23,6 +23,10 @@ mod imp {
volume: Cell<i32>, volume: Cell<i32>,
#[property(get, set)] #[property(get, set)]
mute: Cell<bool>, mute: Cell<bool>,
#[property(get, set)]
position: Cell<f64>,
#[property(get, set)]
duration: Cell<f64>,
} }
#[glib::object_subclass] #[glib::object_subclass]

View file

@ -188,6 +188,7 @@ mod imp {
use adw::prelude::*; use adw::prelude::*;
use adw::subclass::prelude::*; use adw::subclass::prelude::*;
use gtk::{gdk, gio, glib}; use gtk::{gdk, gio, glib};
use std::rc::Rc;
glib::wrapper! { glib::wrapper! {
pub struct Window(ObjectSubclass<imp::Window>) pub struct Window(ObjectSubclass<imp::Window>)
@ -237,6 +238,33 @@ impl Window {
), ),
); );
window.imp().playbin2.file_started().connect_object(
&*window.imp().playbar,
|playbin, playbar, ()| {
let entry = &playbin.entries()[playbin.current_entry().unwrap()];
playbar.set_duration(entry.duration() as f64);
true
},
);
// update position every 100 ms
glib::source::timeout_add_local(std::time::Duration::from_millis(100), {
let playbar = window.imp().playbar.downgrade();
let playbin = Rc::downgrade(&window.imp().playbin2);
move || {
let playbar = match playbar.upgrade() {
None => return glib::ControlFlow::Break,
Some(playbar) => playbar,
};
let playbin = match playbin.upgrade() {
None => return glib::ControlFlow::Break,
Some(playbin) => playbin,
};
playbar.set_position(playbin.position().unwrap_or(0.0));
glib::ControlFlow::Continue
}
});
window window
.imp() .imp()
.setup .setup