| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
use adw::prelude::*; |
| 21 |
use anyhow::Result; |
| 22 |
use async_channel::Sender; |
| 23 |
use chrono::prelude::*; |
| 24 |
use fragile::Fragile; |
| 25 |
use gio::File; |
| 26 |
use glib::clone; |
| 27 |
use glib::{SignalHandlerId, WeakRef}; |
| 28 |
use gst::ClockTime; |
| 29 |
use gtk::{gio, glib}; |
| 30 |
use mpris_server::{Metadata, PlaybackStatus, Player}; |
| 31 |
use std::cell::{RefCell, RefMut}; |
| 32 |
use std::ops::Deref; |
| 33 |
use std::path::Path; |
| 34 |
use std::rc::Rc; |
| 35 |
use std::sync::{LazyLock, Mutex}; |
| 36 |
use url::Url; |
| 37 |
|
| 38 |
use crate::app::Action; |
| 39 |
use crate::chapter_parser::Chapter; |
| 40 |
use crate::config::APP_ID; |
| 41 |
use crate::download_covers::load_widget_texture; |
| 42 |
use crate::i18n::i18n; |
| 43 |
use crate::widgets::SheetBase; |
| 44 |
use podcasts_data::{ |
| 45 |
Episode, EpisodeId, EpisodeModel, ShowCoverModel, ShowId, USER_AGENT, dbqueries, |
| 46 |
}; |
| 47 |
|
| 48 |
const RATE_MIN: f64 = 0.75; |
| 49 |
const RATE_MAX: f64 = 2.0; |
| 50 |
|
| 51 |
#[derive(Debug, Clone, Copy)] |
| 52 |
pub(crate) enum SeekDirection { |
| 53 |
Backwards, |
| 54 |
Forward, |
| 55 |
} |
| 56 |
|
| 57 |
pub(crate) trait PlayerExt { |
| 58 |
fn play(&self); |
| 59 |
fn pause(&mut self); |
| 60 |
fn toggle_pause(&mut self); |
| 61 |
fn stop(&mut self); |
| 62 |
fn seek(&self, offset: ClockTime, direction: SeekDirection) -> Option<()>; |
| 63 |
fn fast_forward(&self); |
| 64 |
fn rewind(&self); |
| 65 |
fn set_playback_rate(&self, _: f64); |
| 66 |
} |
| 67 |
|
| 68 |
#[derive(Debug, Clone)] |
| 69 |
struct PlayerInfo { |
| 70 |
show: gtk::Label, |
| 71 |
episode: gtk::Label, |
| 72 |
cover: gtk::Image, |
| 73 |
cover_button: gtk::Button, |
| 74 |
show_small: gtk::Label, |
| 75 |
episode_small: gtk::Label, |
| 76 |
cover_small: gtk::Image, |
| 77 |
cover_button_small: gtk::Button, |
| 78 |
mpris: Option<Rc<Player>>, |
| 79 |
restore_position: Option<i32>, |
| 80 |
finished_restore: bool, |
| 81 |
ep: Option<Episode>, |
| 82 |
episode_id: RefCell<Option<EpisodeId>>, |
| 83 |
|
| 84 |
chapters_signal_id: RefCell<Option<Rc<SignalHandlerId>>>, |
| 85 |
} |
| 86 |
|
| 87 |
impl PlayerInfo { |
| 88 |
fn create_bindings(&self) { |
| 89 |
self.show |
| 90 |
.bind_property("label", &self.show_small, "label") |
| 91 |
.flags(glib::BindingFlags::SYNC_CREATE) |
| 92 |
.build(); |
| 93 |
self.episode |
| 94 |
.bind_property("label", &self.episode_small, "label") |
| 95 |
.flags(glib::BindingFlags::SYNC_CREATE) |
| 96 |
.build(); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
fn init(&mut self, sender: &Sender<Action>, episode: &Episode, podcast: &ShowCoverModel) { |
| 101 |
self.ep = Some(episode.clone()); |
| 102 |
self.episode_id.replace(Some(episode.id())); |
| 103 |
self.init_cover_buttons(episode.id()); |
| 104 |
self.set_cover_image(podcast); |
| 105 |
self.set_show_title(podcast); |
| 106 |
self.set_episode_title(episode); |
| 107 |
|
| 108 |
let mut metadata = Metadata::new(); |
| 109 |
metadata.set_artist(Some(vec![podcast.title().to_string()])); |
| 110 |
metadata.set_title(Some(episode.title().to_string())); |
| 111 |
metadata.set_length( |
| 112 |
episode |
| 113 |
.duration() |
| 114 |
.map(|s| mpris_server::Time::from_secs(s as i64)), |
| 115 |
); |
| 116 |
|
| 117 |
|
| 118 |
let art_path = crate::download_covers::determin_cover_path(podcast, None); |
| 119 |
if art_path.exists() { |
| 120 |
metadata.set_art_url(Url::from_file_path(art_path).ok()); |
| 121 |
} else { |
| 122 |
|
| 123 |
let sender = sender.clone(); |
| 124 |
let podcast = podcast.clone(); |
| 125 |
crate::RUNTIME.spawn(async move { |
| 126 |
let id = podcast.id(); |
| 127 |
if let Err(err) = crate::download_covers::just_download(&podcast).await { |
| 128 |
error!("Cover download failed {err}"); |
| 129 |
send!(sender, Action::UpdateMprisCover(id, false)); |
| 130 |
} else { |
| 131 |
send!(sender, Action::UpdateMprisCover(id, true)); |
| 132 |
} |
| 133 |
}); |
| 134 |
} |
| 135 |
|
| 136 |
if let Some(mpris) = self.mpris.as_ref() { |
| 137 |
crate::MAINCONTEXT.spawn_local_with_priority( |
| 138 |
glib::source::Priority::LOW, |
| 139 |
clone!( |
| 140 |
#[weak] |
| 141 |
mpris, |
| 142 |
async move { |
| 143 |
if let Err(err) = mpris.set_metadata(metadata).await { |
| 144 |
warn!("Failed to set MPRIS metadata: {err:?}"); |
| 145 |
} |
| 146 |
if let Err(err) = mpris.set_can_pause(true).await { |
| 147 |
warn!("Failed to set MPRIS pause capability: {err:?}"); |
| 148 |
} |
| 149 |
if let Err(err) = mpris.set_can_play(true).await { |
| 150 |
warn!("Failed to set MPRIS play capability: {err:?}"); |
| 151 |
} |
| 152 |
if let Err(err) = mpris.set_can_seek(true).await { |
| 153 |
warn!("Failed to set MPRIS seek capability: {err:?}"); |
| 154 |
} |
| 155 |
} |
| 156 |
), |
| 157 |
); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
fn update_mpris_cover(&self, show_id: ShowId, dl_success: bool) -> Result<()> { |
| 163 |
if let Some(ep) = self.ep.as_ref() { |
| 164 |
if ep.show_id() != show_id { |
| 165 |
|
| 166 |
return Ok(()); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
if let Some(mpris) = self.mpris.as_ref() { |
| 171 |
let pd = dbqueries::get_podcast_cover_from_id(show_id)?; |
| 172 |
let mut metadata = mpris.metadata().clone(); |
| 173 |
if dl_success { |
| 174 |
let art_path = crate::download_covers::determin_cover_path(&pd, None); |
| 175 |
if art_path.exists() { |
| 176 |
metadata.set_art_url(Url::from_file_path(art_path).ok()); |
| 177 |
crate::MAINCONTEXT.spawn_local_with_priority( |
| 178 |
glib::source::Priority::LOW, |
| 179 |
clone!( |
| 180 |
#[weak] |
| 181 |
mpris, |
| 182 |
async move { |
| 183 |
if let Err(err) = mpris.set_metadata(metadata).await { |
| 184 |
error!("failed to update mpris metadata {err}"); |
| 185 |
} |
| 186 |
} |
| 187 |
), |
| 188 |
); |
| 189 |
return Ok(()); |
| 190 |
} else { |
| 191 |
error!( |
| 192 |
"cover does not exist after successful download? {}", |
| 193 |
art_path.display() |
| 194 |
); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
metadata.set_art_url(pd.image_uri()); |
| 200 |
crate::MAINCONTEXT.spawn_local_with_priority( |
| 201 |
glib::source::Priority::LOW, |
| 202 |
clone!( |
| 203 |
#[weak] |
| 204 |
mpris, |
| 205 |
async move { |
| 206 |
if let Err(err) = mpris.set_metadata(metadata).await { |
| 207 |
error!("failed to update mpris metadata {err}"); |
| 208 |
} |
| 209 |
} |
| 210 |
), |
| 211 |
); |
| 212 |
} |
| 213 |
Ok(()) |
| 214 |
} |
| 215 |
|
| 216 |
fn set_episode_title(&self, episode: &Episode) { |
| 217 |
self.episode.set_text(episode.title()); |
| 218 |
self.episode.set_tooltip_text(Some(episode.title())); |
| 219 |
} |
| 220 |
|
| 221 |
fn set_show_title(&self, show: &ShowCoverModel) { |
| 222 |
self.show.set_text(show.title()); |
| 223 |
self.show.set_tooltip_text(Some(show.title())); |
| 224 |
} |
| 225 |
|
| 226 |
fn init_cover_buttons(&self, id: EpisodeId) { |
| 227 |
self.cover_button.set_action_name(Some("app.go-to-episode")); |
| 228 |
self.cover_button |
| 229 |
.set_action_target_value(Some(&id.0.into())); |
| 230 |
self.cover_button_small |
| 231 |
.set_action_name(Some("app.go-to-episode")); |
| 232 |
self.cover_button_small |
| 233 |
.set_action_target_value(Some(&id.0.into())); |
| 234 |
} |
| 235 |
|
| 236 |
fn set_cover_image(&self, show: &ShowCoverModel) { |
| 237 |
load_widget_texture(&self.cover, show.id(), crate::Thumb64, false); |
| 238 |
load_widget_texture(&self.cover_small, show.id(), crate::Thumb64, false); |
| 239 |
} |
| 240 |
|
| 241 |
fn update_mpris_position(&self, position: Position) -> Option<()> { |
| 242 |
let time = mpris_server::Time::from_secs(position.seconds() as i64); |
| 243 |
self.mpris.as_ref()?.set_position(time); |
| 244 |
Some(()) |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
#[derive(Debug, Clone)] |
| 249 |
pub(crate) struct PlayerTimes { |
| 250 |
pub(crate) progressed: gtk::Label, |
| 251 |
pub(crate) duration: gtk::Label, |
| 252 |
pub(crate) slider: gtk::Scale, |
| 253 |
pub(crate) slider_update: Rc<SignalHandlerId>, |
| 254 |
pub(crate) progress_bar: gtk::ProgressBar, |
| 255 |
} |
| 256 |
|
| 257 |
#[derive(Debug, Clone, Copy)] |
| 258 |
pub(crate) struct Duration(ClockTime); |
| 259 |
|
| 260 |
impl Deref for Duration { |
| 261 |
type Target = ClockTime; |
| 262 |
fn deref(&self) -> &Self::Target { |
| 263 |
&self.0 |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
#[derive(Debug, Clone, Copy)] |
| 268 |
pub(crate) struct Position(ClockTime); |
| 269 |
|
| 270 |
impl Deref for Position { |
| 271 |
type Target = ClockTime; |
| 272 |
fn deref(&self) -> &Self::Target { |
| 273 |
&self.0 |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
impl PlayerTimes { |
| 278 |
|
| 279 |
pub(crate) fn on_duration_changed(&self, duration: Duration) { |
| 280 |
let seconds = duration.seconds(); |
| 281 |
|
| 282 |
self.slider.block_signal(&self.slider_update); |
| 283 |
self.slider.set_range(0.0, seconds as f64); |
| 284 |
self.slider.unblock_signal(&self.slider_update); |
| 285 |
|
| 286 |
self.duration.set_text(&format_duration(seconds as u32)); |
| 287 |
|
| 288 |
self.update_progress_bar(); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
pub(crate) fn on_position_updated(&self, position: Position) { |
| 293 |
let seconds = position.seconds(); |
| 294 |
|
| 295 |
self.slider.block_signal(&self.slider_update); |
| 296 |
self.slider.set_value(seconds as f64); |
| 297 |
self.slider.unblock_signal(&self.slider_update); |
| 298 |
|
| 299 |
self.progressed.set_text(&format_duration(seconds as u32)); |
| 300 |
|
| 301 |
self.update_progress_bar(); |
| 302 |
} |
| 303 |
|
| 304 |
fn update_progress_bar(&self) { |
| 305 |
let fraction = self.slider.value() / self.slider.adjustment().upper(); |
| 306 |
self.progress_bar.set_fraction(fraction); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
fn format_duration(seconds: u32) -> String { |
| 311 |
let time = NaiveTime::from_num_seconds_from_midnight_opt(seconds, 0); |
| 312 |
if time.is_none() { |
| 313 |
return "-".to_string(); |
| 314 |
} |
| 315 |
let time = time.unwrap(); |
| 316 |
|
| 317 |
if seconds >= 3600 { |
| 318 |
time.format("%T").to_string() |
| 319 |
} else { |
| 320 |
time.format("%M:%S").to_string() |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
#[derive(Debug, Clone)] |
| 325 |
pub(crate) struct PlayerRate { |
| 326 |
pub(crate) action: gio::SimpleAction, |
| 327 |
pub(crate) btn: gtk::MenuButton, |
| 328 |
} |
| 329 |
|
| 330 |
impl PlayerRate { |
| 331 |
fn new() -> Self { |
| 332 |
let builder = gtk::Builder::from_resource("/org/gnome/Podcasts/gtk/player_rate.ui"); |
| 333 |
|
| 334 |
|
| 335 |
let variant_type = glib::VariantTy::new("s").expect("Could not parse variant type"); |
| 336 |
let action = |
| 337 |
gio::SimpleAction::new_stateful("set", Some(variant_type), &"1.00".to_variant()); |
| 338 |
let btn: gtk::MenuButton = builder.object("rate_button").unwrap(); |
| 339 |
|
| 340 |
PlayerRate { action, btn } |
| 341 |
} |
| 342 |
|
| 343 |
fn connect_signals(&self, widget: &Rc<RefCell<PlayerWidget>>) { |
| 344 |
let group = gio::SimpleActionGroup::new(); |
| 345 |
self.action.connect_activate(clone!( |
| 346 |
#[weak] |
| 347 |
widget, |
| 348 |
move |action, rate_v| { |
| 349 |
let variant = rate_v.unwrap(); |
| 350 |
action.set_state(variant); |
| 351 |
let rate = variant |
| 352 |
.get::<String>() |
| 353 |
.expect("Could not get rate from variant") |
| 354 |
.parse::<f64>() |
| 355 |
.expect("Could not parse float from variant string"); |
| 356 |
widget.borrow().on_rate_changed(rate); |
| 357 |
} |
| 358 |
)); |
| 359 |
group.add_action(&self.action); |
| 360 |
widget |
| 361 |
.borrow() |
| 362 |
.container |
| 363 |
.insert_action_group("rate", Some(&group)); |
| 364 |
widget.borrow().sheet.connect_rate_actions(&group); |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
#[derive(Debug, Clone)] |
| 369 |
struct PlayerControls { |
| 370 |
play: gtk::Button, |
| 371 |
pause: gtk::Button, |
| 372 |
play_pause_big: gtk::Stack, |
| 373 |
play_small: gtk::Button, |
| 374 |
pause_small: gtk::Button, |
| 375 |
play_pause_small: gtk::Stack, |
| 376 |
forward: gtk::Button, |
| 377 |
rewind: gtk::Button, |
| 378 |
last_pause: RefCell<Option<DateTime<Local>>>, |
| 379 |
chapters_button: gtk::Button, |
| 380 |
} |
| 381 |
|
| 382 |
#[derive(Debug, Clone)] |
| 383 |
pub(crate) struct PlayerWidget { |
| 384 |
pub(crate) container: gtk::Box, |
| 385 |
player: gst_play::Play, |
| 386 |
player_signals: gst_play::PlaySignalAdapter, |
| 387 |
controls: PlayerControls, |
| 388 |
pub(crate) sheet: SheetBase, |
| 389 |
full: gtk::Box, |
| 390 |
small: gtk::Box, |
| 391 |
stack: gtk::Stack, |
| 392 |
timer: PlayerTimes, |
| 393 |
info: PlayerInfo, |
| 394 |
rate: PlayerRate, |
| 395 |
sender: Option<Sender<Action>>, |
| 396 |
} |
| 397 |
|
| 398 |
impl Default for PlayerWidget { |
| 399 |
fn default() -> Self { |
| 400 |
let player = gst_play::Play::default(); |
| 401 |
let player_signals = gst_play::PlaySignalAdapter::new(&player); |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
player.set_video_track_enabled(false); |
| 406 |
let mpris = crate::RUNTIME.block_on(async move { |
| 407 |
let mpris_result = Player::builder(APP_ID) |
| 408 |
.identity(i18n("Podcasts")) |
| 409 |
.desktop_entry(APP_ID) |
| 410 |
.can_raise(true) |
| 411 |
.can_pause(false) |
| 412 |
.can_play(false) |
| 413 |
.can_seek(false) |
| 414 |
.can_set_fullscreen(false) |
| 415 |
.can_go_next(false) |
| 416 |
.can_go_previous(false) |
| 417 |
.build() |
| 418 |
.await; |
| 419 |
match mpris_result { |
| 420 |
Err(e) => { |
| 421 |
error!("mpris initialization: {e}"); |
| 422 |
None |
| 423 |
} |
| 424 |
Ok(mpris) => Some(Rc::new(mpris)), |
| 425 |
} |
| 426 |
}); |
| 427 |
|
| 428 |
if let Some(mpris) = mpris.as_ref() { |
| 429 |
crate::MAINCONTEXT.spawn_local_with_priority( |
| 430 |
glib::source::Priority::LOW, |
| 431 |
clone!( |
| 432 |
#[weak] |
| 433 |
mpris, |
| 434 |
async move { |
| 435 |
let task = mpris.run(); |
| 436 |
task.await; |
| 437 |
} |
| 438 |
), |
| 439 |
); |
| 440 |
} |
| 441 |
|
| 442 |
let mut config = player.config(); |
| 443 |
config.set_user_agent(USER_AGENT); |
| 444 |
config.set_position_update_interval(250); |
| 445 |
player.set_config(config).unwrap(); |
| 446 |
|
| 447 |
let builder = gtk::Builder::from_resource("/org/gnome/Podcasts/gtk/player_toolbar.ui"); |
| 448 |
|
| 449 |
let play = builder.object("play_button").unwrap(); |
| 450 |
let pause = builder.object("pause_button").unwrap(); |
| 451 |
let play_small = builder.object("play_button_small").unwrap(); |
| 452 |
let pause_small = builder.object("pause_button_small").unwrap(); |
| 453 |
let forward: gtk::Button = builder.object("ff_button").unwrap(); |
| 454 |
let rewind: gtk::Button = builder.object("rewind_button").unwrap(); |
| 455 |
let play_pause_small = builder.object("play_pause_small").unwrap(); |
| 456 |
let play_pause_big = builder.object("play_pause_big").unwrap(); |
| 457 |
let chapters_button = builder.object("chapters_button").unwrap(); |
| 458 |
|
| 459 |
let controls = PlayerControls { |
| 460 |
play, |
| 461 |
pause, |
| 462 |
play_pause_big, |
| 463 |
play_small, |
| 464 |
pause_small, |
| 465 |
play_pause_small, |
| 466 |
forward, |
| 467 |
rewind, |
| 468 |
last_pause: RefCell::new(None), |
| 469 |
chapters_button, |
| 470 |
}; |
| 471 |
|
| 472 |
let progressed = builder.object("progress_time_label").unwrap(); |
| 473 |
let duration = builder.object("total_duration_label").unwrap(); |
| 474 |
let slider: gtk::Scale = builder.object("seek").unwrap(); |
| 475 |
slider.set_range(0.0, 1.0); |
| 476 |
let player_weak = player.downgrade(); |
| 477 |
let slider_update = Rc::new(Self::connect_update_slider(&slider, player_weak)); |
| 478 |
let progress_bar = builder.object("progress_bar").unwrap(); |
| 479 |
let timer = PlayerTimes { |
| 480 |
progressed, |
| 481 |
duration, |
| 482 |
slider, |
| 483 |
slider_update, |
| 484 |
progress_bar, |
| 485 |
}; |
| 486 |
|
| 487 |
let show = builder.object("show_label").unwrap(); |
| 488 |
let episode = builder.object("episode_label").unwrap(); |
| 489 |
let cover = builder.object("show_cover").unwrap(); |
| 490 |
let cover_button = builder.object("cover_button").unwrap(); |
| 491 |
let show_small = builder.object("show_label_small").unwrap(); |
| 492 |
let episode_small = builder.object("episode_label_small").unwrap(); |
| 493 |
let cover_small = builder.object("show_cover_small").unwrap(); |
| 494 |
let cover_button_small = builder.object("cover_button_small").unwrap(); |
| 495 |
let ep = None; |
| 496 |
let info = PlayerInfo { |
| 497 |
mpris, |
| 498 |
show, |
| 499 |
ep, |
| 500 |
episode, |
| 501 |
cover, |
| 502 |
cover_button, |
| 503 |
show_small, |
| 504 |
episode_small, |
| 505 |
cover_small, |
| 506 |
cover_button_small, |
| 507 |
restore_position: None, |
| 508 |
finished_restore: false, |
| 509 |
episode_id: RefCell::default(), |
| 510 |
chapters_signal_id: RefCell::default(), |
| 511 |
}; |
| 512 |
info.create_bindings(); |
| 513 |
|
| 514 |
let dialog_rate = PlayerRate::new(); |
| 515 |
let dialog = SheetBase::new(dialog_rate); |
| 516 |
|
| 517 |
let container = builder.object("container").unwrap(); |
| 518 |
let full: gtk::Box = builder.object("full").unwrap(); |
| 519 |
let small: gtk::Box = builder.object("small").unwrap(); |
| 520 |
let stack = builder.object("stack").unwrap(); |
| 521 |
|
| 522 |
let rate = PlayerRate::new(); |
| 523 |
full.append(&rate.btn); |
| 524 |
|
| 525 |
PlayerWidget { |
| 526 |
player, |
| 527 |
player_signals, |
| 528 |
container, |
| 529 |
controls, |
| 530 |
sheet: dialog, |
| 531 |
full, |
| 532 |
small, |
| 533 |
stack, |
| 534 |
timer, |
| 535 |
info, |
| 536 |
rate, |
| 537 |
sender: None, |
| 538 |
} |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
#[derive(PartialEq)] |
| 543 |
pub enum StreamMode { |
| 544 |
LocalOnly, |
| 545 |
StreamOnly, |
| 546 |
StreamFallback, |
| 547 |
} |
| 548 |
|
| 549 |
impl PlayerWidget { |
| 550 |
fn on_rate_changed(&self, rate: f64) { |
| 551 |
self.set_playback_rate(rate); |
| 552 |
self.rate.btn.set_label(&format!("{:.2}×", rate)); |
| 553 |
self.sheet.on_rate_changed(rate); |
| 554 |
} |
| 555 |
|
| 556 |
pub fn change_playback_rate(&self, difference: f64) { |
| 557 |
let rate = (self.player.rate() + difference).clamp(RATE_MIN, RATE_MAX); |
| 558 |
self.on_rate_changed(rate); |
| 559 |
} |
| 560 |
|
| 561 |
fn reveal(&self) { |
| 562 |
self.container.set_visible(true); |
| 563 |
} |
| 564 |
|
| 565 |
pub(crate) fn initialize_episode( |
| 566 |
&mut self, |
| 567 |
sender: &Sender<Action>, |
| 568 |
id: EpisodeId, |
| 569 |
stream: StreamMode, |
| 570 |
second: Option<i32>, |
| 571 |
) -> Result<()> { |
| 572 |
let ep = dbqueries::get_episode_from_id(id)?; |
| 573 |
let pd = dbqueries::get_podcast_cover_from_id(ep.show_id())?; |
| 574 |
let is_different_ep = self.info.episode_id.borrow().as_ref() != Some(&id); |
| 575 |
|
| 576 |
|
| 577 |
if is_different_ep { |
| 578 |
self.sheet.initialize_episode(&ep, &pd); |
| 579 |
} |
| 580 |
|
| 581 |
self.info.restore_position = second.or_else(|| { |
| 582 |
let episode_position = ep.play_position(); |
| 583 |
if episode_position == 0 { |
| 584 |
None |
| 585 |
} else { |
| 586 |
Some(episode_position) |
| 587 |
} |
| 588 |
}); |
| 589 |
let last_id = self.id(); |
| 590 |
self.info.finished_restore = false; |
| 591 |
self.info.init(sender, &ep, &pd); |
| 592 |
|
| 593 |
if let Some(id) = last_id { |
| 594 |
|
| 595 |
send_blocking!(sender, Action::RefreshEpisode(id)); |
| 596 |
} |
| 597 |
|
| 598 |
if stream == StreamMode::StreamOnly { |
| 599 |
if let Some(uri) = ep.uri() { |
| 600 |
self.init_uri(sender, id, uri, second); |
| 601 |
return Ok(()); |
| 602 |
} else { |
| 603 |
error!("No uri for episode"); |
| 604 |
} |
| 605 |
|
| 606 |
|
| 607 |
} else if let Some(ref path) = ep.local_uri() { |
| 608 |
if Path::new(path).exists() { |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
let uri = File::for_path(path).uri(); |
| 613 |
self.init_uri(sender, id, uri.as_str(), second); |
| 614 |
return Ok(()); |
| 615 |
} else { |
| 616 |
error!("failed to create path for episode {:#?}", ep); |
| 617 |
} |
| 618 |
} else if stream == StreamMode::StreamFallback { |
| 619 |
if let Some(uri) = ep.uri() { |
| 620 |
self.init_uri(sender, id, uri, second); |
| 621 |
return Ok(()); |
| 622 |
} else { |
| 623 |
error!("No uri for episode"); |
| 624 |
} |
| 625 |
} else { |
| 626 |
error!("Episode not downloaded yet."); |
| 627 |
} |
| 628 |
|
| 629 |
Ok(()) |
| 630 |
} |
| 631 |
|
| 632 |
fn init_uri(&mut self, sender: &Sender<Action>, id: EpisodeId, uri: &str, second: Option<i32>) { |
| 633 |
|
| 634 |
if self.player.uri().is_none_or(|s| s != uri) { |
| 635 |
self.player.set_uri(Some(uri)); |
| 636 |
|
| 637 |
|
| 638 |
let uri_string = uri.to_owned(); |
| 639 |
crate::RUNTIME.spawn_blocking(clone!( |
| 640 |
#[strong] |
| 641 |
sender, |
| 642 |
move || match crate::chapter_parser::load_chapters(&uri_string) { |
| 643 |
Ok(chapters) => { |
| 644 |
send_blocking!(sender, Action::ChaptersAvailable(id, chapters)); |
| 645 |
} |
| 646 |
Err(e) => { |
| 647 |
error!("Failed to get chapters: {e:#?}"); |
| 648 |
} |
| 649 |
} |
| 650 |
)); |
| 651 |
} else if second.is_some() { |
| 652 |
|
| 653 |
self.restore_play_position(); |
| 654 |
} else { |
| 655 |
|
| 656 |
self.info.finished_restore = true; |
| 657 |
} |
| 658 |
|
| 659 |
self.play(); |
| 660 |
} |
| 661 |
|
| 662 |
fn connect_update_slider( |
| 663 |
slider: >k::Scale, |
| 664 |
player: WeakRef<gst_play::Play>, |
| 665 |
) -> SignalHandlerId { |
| 666 |
slider.connect_value_changed(move |slider| { |
| 667 |
let player = match player.upgrade() { |
| 668 |
Some(p) => p, |
| 669 |
None => return, |
| 670 |
}; |
| 671 |
|
| 672 |
let value = slider.value() as u64; |
| 673 |
player.seek(ClockTime::from_seconds(value)); |
| 674 |
}) |
| 675 |
} |
| 676 |
|
| 677 |
pub fn update_mpris_cover(&self, show_id: ShowId, dl_success: bool) -> Result<()> { |
| 678 |
self.info.update_mpris_cover(show_id, dl_success) |
| 679 |
} |
| 680 |
|
| 681 |
fn smart_rewind(&self) -> Option<()> { |
| 682 |
static LAST_KNOWN_EPISODE: LazyLock<Mutex<Option<EpisodeId>>> = |
| 683 |
LazyLock::new(|| Mutex::new(None)); |
| 684 |
|
| 685 |
|
| 686 |
let now = Local::now(); |
| 687 |
let last: &Option<DateTime<_>> = &*self.controls.last_pause.borrow(); |
| 688 |
let delta = (now - (*last)?).num_seconds(); |
| 689 |
|
| 690 |
|
| 691 |
let seconds_passed = self.player.position()?.seconds(); |
| 692 |
|
| 693 |
let mut last = LAST_KNOWN_EPISODE.lock().unwrap(); |
| 694 |
|
| 695 |
let current_id = *self.info.episode_id.borrow(); |
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
if seconds_passed >= 90 && delta >= 60 && current_id == *last { |
| 700 |
self.seek(ClockTime::from_seconds(5), SeekDirection::Backwards); |
| 701 |
} |
| 702 |
|
| 703 |
|
| 704 |
*last = current_id; |
| 705 |
|
| 706 |
Some(()) |
| 707 |
} |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
fn restore_play_position(&self) -> Option<()> { |
| 712 |
debug!("Attempting to restore playback position"); |
| 713 |
let pos = self.info.restore_position; |
| 714 |
if let Some(pos) = pos { |
| 715 |
let s: u64 = pos.try_into().ok()?; |
| 716 |
debug!("Seeking stream at second: {}", s); |
| 717 |
self.player.seek(ClockTime::from_seconds(s)); |
| 718 |
return Some(()); |
| 719 |
} |
| 720 |
|
| 721 |
debug!("No playback position to restore"); |
| 722 |
None |
| 723 |
} |
| 724 |
|
| 725 |
pub fn set_small(&self, small: bool) { |
| 726 |
if small { |
| 727 |
self.stack.set_visible_child(&self.small); |
| 728 |
} else { |
| 729 |
self.stack.set_visible_child(&self.full); |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
pub(crate) fn id(&self) -> Option<EpisodeId> { |
| 734 |
*self.info.episode_id.borrow() |
| 735 |
} |
| 736 |
|
| 737 |
pub(crate) fn is_playing(&self) -> bool { |
| 738 |
self.info.mpris.as_ref().map(|m| m.playback_status()) == Some(PlaybackStatus::Playing) |
| 739 |
} |
| 740 |
|
| 741 |
pub(crate) fn sheet(&self) -> &SheetBase { |
| 742 |
&self.sheet |
| 743 |
} |
| 744 |
|
| 745 |
pub(crate) fn slider(&self) -> >k::Scale { |
| 746 |
&self.timer.slider |
| 747 |
} |
| 748 |
|
| 749 |
pub fn chapters_available(&self, id: EpisodeId, chapters: Vec<Chapter>) { |
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
if self.id() != Some(id) { |
| 755 |
return; |
| 756 |
} |
| 757 |
|
| 758 |
self.controls |
| 759 |
.chapters_button |
| 760 |
.set_visible(!chapters.is_empty()); |
| 761 |
|
| 762 |
self.sheet.chapters_available(id, chapters.clone()); |
| 763 |
|
| 764 |
if let Some(sender) = self.sender.as_ref() { |
| 765 |
|
| 766 |
let sender2 = sender.clone(); |
| 767 |
let new_controls_id = self.controls.chapters_button.connect_clicked(move |_| { |
| 768 |
send_blocking!(sender2, Action::GoToChaptersPage(id, chapters.clone())); |
| 769 |
}); |
| 770 |
|
| 771 |
|
| 772 |
let old_signal_id = self |
| 773 |
.info |
| 774 |
.chapters_signal_id |
| 775 |
.replace(Some(Rc::new(new_controls_id))); |
| 776 |
if let Some(chapters_signal_id) = old_signal_id { |
| 777 |
if let Ok(controls_signal) = Rc::try_unwrap(chapters_signal_id) { |
| 778 |
self.controls.chapters_button.disconnect(controls_signal); |
| 779 |
} |
| 780 |
} |
| 781 |
} |
| 782 |
} |
| 783 |
} |
| 784 |
|
| 785 |
impl PlayerExt for PlayerWidget { |
| 786 |
fn play(&self) { |
| 787 |
self.sheet.on_play(); |
| 788 |
|
| 789 |
self.reveal(); |
| 790 |
|
| 791 |
self.controls |
| 792 |
.play_pause_big |
| 793 |
.set_visible_child(&self.controls.pause); |
| 794 |
self.controls |
| 795 |
.play_pause_small |
| 796 |
.set_visible_child(&self.controls.pause_small); |
| 797 |
|
| 798 |
self.smart_rewind(); |
| 799 |
self.player.play(); |
| 800 |
if let Some(mpris) = self.info.mpris.as_ref() { |
| 801 |
crate::MAINCONTEXT.spawn_local_with_priority( |
| 802 |
glib::source::Priority::LOW, |
| 803 |
clone!( |
| 804 |
#[weak] |
| 805 |
mpris, |
| 806 |
#[strong(rename_to = this)] |
| 807 |
self, |
| 808 |
async move { |
| 809 |
if let Err(err) = mpris.set_playback_status(PlaybackStatus::Playing).await { |
| 810 |
warn!("Failed to set MPRIS playback status: {err:?}"); |
| 811 |
} |
| 812 |
if let Some(sender) = &this.sender { |
| 813 |
if let Some(id) = this.id() { |
| 814 |
send!(sender, Action::RefreshEpisode(id)); |
| 815 |
} |
| 816 |
} |
| 817 |
} |
| 818 |
), |
| 819 |
); |
| 820 |
} |
| 821 |
if let Some(sender) = &self.sender { |
| 822 |
send_blocking!(sender, Action::InhibitSuspend); |
| 823 |
} |
| 824 |
} |
| 825 |
|
| 826 |
fn pause(&mut self) { |
| 827 |
self.sheet.on_pause(); |
| 828 |
|
| 829 |
self.controls |
| 830 |
.play_pause_big |
| 831 |
.set_visible_child(&self.controls.play); |
| 832 |
self.controls |
| 833 |
.play_pause_small |
| 834 |
.set_visible_child(&self.controls.play_small); |
| 835 |
|
| 836 |
self.player.pause(); |
| 837 |
if let Some(mpris) = self.info.mpris.as_ref() { |
| 838 |
crate::MAINCONTEXT.spawn_local_with_priority( |
| 839 |
glib::source::Priority::LOW, |
| 840 |
clone!( |
| 841 |
#[weak] |
| 842 |
mpris, |
| 843 |
#[strong(rename_to = this)] |
| 844 |
self, |
| 845 |
async move { |
| 846 |
if let Err(err) = mpris.set_playback_status(PlaybackStatus::Paused).await { |
| 847 |
warn!("Failed to set MPRIS playback status: {err:?}"); |
| 848 |
} |
| 849 |
if let Some(sender) = &this.sender { |
| 850 |
if let Some(id) = this.id() { |
| 851 |
send!(sender, Action::RefreshEpisode(id)); |
| 852 |
} |
| 853 |
} |
| 854 |
} |
| 855 |
), |
| 856 |
); |
| 857 |
} |
| 858 |
if let Some(sender) = &self.sender { |
| 859 |
send_blocking!(sender, Action::UninhibitSuspend); |
| 860 |
} |
| 861 |
|
| 862 |
self.controls.last_pause.replace(Some(Local::now())); |
| 863 |
let pos = self.player.position(); |
| 864 |
self.info.ep.as_mut().map(|ep| { |
| 865 |
ep.set_play_position(pos.and_then(|s| s.seconds().try_into().ok()).unwrap_or(0)) |
| 866 |
}); |
| 867 |
} |
| 868 |
|
| 869 |
fn stop(&mut self) { |
| 870 |
|
| 871 |
let is_focus = self.controls.pause.is_focus(); |
| 872 |
self.controls |
| 873 |
.play_pause_big |
| 874 |
.set_visible_child(&self.controls.play); |
| 875 |
if is_focus { |
| 876 |
self.controls.play.grab_focus(); |
| 877 |
} |
| 878 |
|
| 879 |
let is_focus = self.controls.pause_small.is_focus(); |
| 880 |
self.controls |
| 881 |
.play_pause_small |
| 882 |
.set_visible_child(&self.controls.play_small); |
| 883 |
if is_focus { |
| 884 |
self.controls.play_small.grab_focus(); |
| 885 |
} |
| 886 |
|
| 887 |
self.sheet.on_stop(); |
| 888 |
|
| 889 |
self.info.ep = None; |
| 890 |
self.info.restore_position = None; |
| 891 |
self.player.stop(); |
| 892 |
if let Some(mpris) = self.info.mpris.as_ref() { |
| 893 |
crate::MAINCONTEXT.spawn_local_with_priority( |
| 894 |
glib::source::Priority::LOW, |
| 895 |
clone!( |
| 896 |
#[weak] |
| 897 |
mpris, |
| 898 |
#[strong(rename_to = this)] |
| 899 |
self, |
| 900 |
async move { |
| 901 |
if let Err(err) = mpris.set_playback_status(PlaybackStatus::Paused).await { |
| 902 |
warn!("Failed to set MPRIS playback status: {err:?}"); |
| 903 |
} |
| 904 |
if let Some(sender) = &this.sender { |
| 905 |
if let Some(id) = this.id() { |
| 906 |
send!(sender, Action::RefreshEpisode(id)); |
| 907 |
} |
| 908 |
} |
| 909 |
} |
| 910 |
), |
| 911 |
); |
| 912 |
} |
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
self.timer |
| 917 |
.on_position_updated(Position(ClockTime::from_seconds(0))); |
| 918 |
if let Some(sender) = &self.sender { |
| 919 |
send_blocking!(sender, Action::UninhibitSuspend); |
| 920 |
} |
| 921 |
} |
| 922 |
|
| 923 |
fn toggle_pause(&mut self) { |
| 924 |
if let Some(mpris) = self.info.mpris.as_ref() { |
| 925 |
match mpris.playback_status() { |
| 926 |
PlaybackStatus::Paused => self.play(), |
| 927 |
PlaybackStatus::Stopped => self.play(), |
| 928 |
_ => self.pause(), |
| 929 |
}; |
| 930 |
} |
| 931 |
} |
| 932 |
|
| 933 |
|
| 934 |
fn seek(&self, offset: ClockTime, direction: SeekDirection) -> Option<()> { |
| 935 |
|
| 936 |
let position = self.player.position()?; |
| 937 |
if offset.is_zero() { |
| 938 |
return Some(()); |
| 939 |
} |
| 940 |
|
| 941 |
|
| 942 |
let duration = self.player.duration()?; |
| 943 |
let destination = match direction { |
| 944 |
|
| 945 |
SeekDirection::Backwards if position >= offset => position.checked_sub(offset), |
| 946 |
|
| 947 |
SeekDirection::Backwards if position < offset => Some(ClockTime::from_seconds(0)), |
| 948 |
|
| 949 |
SeekDirection::Forward if !duration.is_zero() && position + offset <= duration => { |
| 950 |
position.checked_add(offset) |
| 951 |
} |
| 952 |
|
| 953 |
SeekDirection::Forward if !duration.is_zero() && position + offset > duration => { |
| 954 |
Some(duration) |
| 955 |
} |
| 956 |
_/¯ |
| 957 |
_ => None, |
| 958 |
}; |
| 959 |
|
| 960 |
|
| 961 |
if let Some(destination) = destination { |
| 962 |
self.player.seek(destination) |
| 963 |
} |
| 964 |
|
| 965 |
Some(()) |
| 966 |
} |
| 967 |
|
| 968 |
fn rewind(&self) { |
| 969 |
let r = self.seek(ClockTime::from_seconds(10), SeekDirection::Backwards); |
| 970 |
if r.is_none() { |
| 971 |
warn!("Failed to rewind"); |
| 972 |
} |
| 973 |
} |
| 974 |
|
| 975 |
fn fast_forward(&self) { |
| 976 |
let r = self.seek(ClockTime::from_seconds(10), SeekDirection::Forward); |
| 977 |
if r.is_none() { |
| 978 |
warn!("Failed to fast-forward"); |
| 979 |
} |
| 980 |
} |
| 981 |
|
| 982 |
fn set_playback_rate(&self, rate: f64) { |
| 983 |
self.player.set_rate(rate); |
| 984 |
} |
| 985 |
} |
| 986 |
|
| 987 |
#[derive(Debug, Clone)] |
| 988 |
pub(crate) struct PlayerWrapper(pub Rc<RefCell<PlayerWidget>>); |
| 989 |
|
| 990 |
impl Default for PlayerWrapper { |
| 991 |
fn default() -> Self { |
| 992 |
PlayerWrapper(Rc::new(RefCell::new(PlayerWidget::default()))) |
| 993 |
} |
| 994 |
} |
| 995 |
|
| 996 |
impl Deref for PlayerWrapper { |
| 997 |
type Target = Rc<RefCell<PlayerWidget>>; |
| 998 |
fn deref(&self) -> &Self::Target { |
| 999 |
&self.0 |
| 1000 |
} |
| 1001 |
} |
| 1002 |
|
| 1003 |
impl PlayerWrapper { |
| 1004 |
pub(crate) fn borrow_mut(&self) -> RefMut<'_, PlayerWidget> { |
| 1005 |
self.0.borrow_mut() |
| 1006 |
} |
| 1007 |
pub(crate) fn new(sender: &Sender<Action>) -> Self { |
| 1008 |
let w = PlayerWrapper::default(); |
| 1009 |
w.init(sender); |
| 1010 |
w |
| 1011 |
} |
| 1012 |
|
| 1013 |
fn init(&self, sender: &Sender<Action>) { |
| 1014 |
self.borrow_mut().sender = Some(sender.clone()); |
| 1015 |
self.connect_control_buttons(); |
| 1016 |
self.connect_rate_buttons(); |
| 1017 |
self.connect_mpris_buttons(sender); |
| 1018 |
self.connect_gst_signals(sender); |
| 1019 |
self.connect_sheet(sender); |
| 1020 |
} |
| 1021 |
|
| 1022 |
fn connect_sheet(&self, sender: &Sender<Action>) { |
| 1023 |
let widget = self.borrow(); |
| 1024 |
widget.sheet.init(sender, &self.borrow().timer.slider); |
| 1025 |
widget.sheet.connect(&self.borrow().timer); |
| 1026 |
} |
| 1027 |
|
| 1028 |
|
| 1029 |
fn connect_control_buttons(&self) { |
| 1030 |
let this = self.deref(); |
| 1031 |
let widget = self.borrow(); |
| 1032 |
|
| 1033 |
widget.controls.play.connect_clicked(clone!( |
| 1034 |
#[weak] |
| 1035 |
this, |
| 1036 |
move |_| { |
| 1037 |
this.borrow().play(); |
| 1038 |
this.borrow().controls.pause.grab_focus(); |
| 1039 |
} |
| 1040 |
)); |
| 1041 |
|
| 1042 |
|
| 1043 |
widget.controls.pause.connect_clicked(clone!( |
| 1044 |
#[weak] |
| 1045 |
this, |
| 1046 |
move |_| { |
| 1047 |
this.borrow_mut().pause(); |
| 1048 |
this.borrow().controls.play.grab_focus(); |
| 1049 |
} |
| 1050 |
)); |
| 1051 |
|
| 1052 |
|
| 1053 |
widget.controls.play_small.connect_clicked(clone!( |
| 1054 |
#[weak] |
| 1055 |
this, |
| 1056 |
move |_| { |
| 1057 |
this.borrow().play(); |
| 1058 |
this.borrow().controls.pause_small.grab_focus(); |
| 1059 |
} |
| 1060 |
)); |
| 1061 |
|
| 1062 |
|
| 1063 |
widget.controls.pause_small.connect_clicked(clone!( |
| 1064 |
#[weak] |
| 1065 |
this, |
| 1066 |
move |_| { |
| 1067 |
this.borrow_mut().pause(); |
| 1068 |
this.borrow().controls.play_small.grab_focus(); |
| 1069 |
} |
| 1070 |
)); |
| 1071 |
|
| 1072 |
|
| 1073 |
widget.controls.rewind.connect_clicked(clone!( |
| 1074 |
#[weak] |
| 1075 |
this, |
| 1076 |
move |_| { |
| 1077 |
this.borrow().rewind(); |
| 1078 |
} |
| 1079 |
)); |
| 1080 |
|
| 1081 |
|
| 1082 |
widget.controls.forward.connect_clicked(clone!( |
| 1083 |
#[weak] |
| 1084 |
this, |
| 1085 |
move |_| { |
| 1086 |
this.borrow().fast_forward(); |
| 1087 |
} |
| 1088 |
)); |
| 1089 |
|
| 1090 |
widget.sheet.connect_control_buttons(this); |
| 1091 |
} |
| 1092 |
|
| 1093 |
fn connect_gst_signals(&self, sender: &Sender<Action>) { |
| 1094 |
let signal_adapter = &self.borrow().player_signals; |
| 1095 |
|
| 1096 |
|
| 1097 |
signal_adapter |
| 1098 |
.connect_warning(move |_, warn, details| warn!("gst warning: {} {:#?}", warn, details)); |
| 1099 |
|
| 1100 |
|
| 1101 |
signal_adapter.connect_error(clone!( |
| 1102 |
#[strong] |
| 1103 |
sender, |
| 1104 |
move |_, _error, details| { |
| 1105 |
error!("gstreamer error: {} {:#?}", _error, details); |
| 1106 |
send_blocking!( |
| 1107 |
sender, |
| 1108 |
Action::ErrorNotification(format!("Player Error: {}", _error)) |
| 1109 |
); |
| 1110 |
let s = i18n("The media player was unable to execute an action."); |
| 1111 |
send_blocking!(sender, Action::ErrorNotification(s)); |
| 1112 |
} |
| 1113 |
)); |
| 1114 |
|
| 1115 |
|
| 1116 |
let weak = Fragile::new(Rc::downgrade(self)); |
| 1117 |
|
| 1118 |
signal_adapter.connect_uri_loaded(clone!( |
| 1119 |
#[strong] |
| 1120 |
weak, |
| 1121 |
move |_, _| { |
| 1122 |
if let Some(player_widget) = weak.get().upgrade() { |
| 1123 |
player_widget.borrow().restore_play_position(); |
| 1124 |
player_widget.borrow_mut().info.finished_restore = true; |
| 1125 |
} |
| 1126 |
} |
| 1127 |
)); |
| 1128 |
|
| 1129 |
|
| 1130 |
signal_adapter.connect_duration_changed(clone!( |
| 1131 |
#[strong] |
| 1132 |
weak, |
| 1133 |
move |_, clock| { |
| 1134 |
if let Some(player_widget) = weak.get().upgrade() { |
| 1135 |
if let Some(c) = clock { |
| 1136 |
player_widget |
| 1137 |
.borrow() |
| 1138 |
.timer |
| 1139 |
.on_duration_changed(Duration(c)); |
| 1140 |
} |
| 1141 |
} |
| 1142 |
} |
| 1143 |
)); |
| 1144 |
|
| 1145 |
|
| 1146 |
signal_adapter.connect_position_updated(clone!( |
| 1147 |
#[strong] |
| 1148 |
weak, |
| 1149 |
move |_, clock| { |
| 1150 |
if let Some(player_widget) = weak.get().upgrade() { |
| 1151 |
|
| 1152 |
if let Some(c) = clock { |
| 1153 |
let pos = Position(c); |
| 1154 |
let finished_restore = player_widget.borrow().info.finished_restore; |
| 1155 |
player_widget.borrow_mut().info.ep.as_mut().map(|ep| { |
| 1156 |
if finished_restore { |
| 1157 |
ep.set_play_position_if_divergent(pos.seconds() as i32) |
| 1158 |
} else { |
| 1159 |
Ok(()) |
| 1160 |
} |
| 1161 |
}); |
| 1162 |
player_widget.borrow().timer.on_position_updated(pos); |
| 1163 |
player_widget.borrow().info.update_mpris_position(pos); |
| 1164 |
} |
| 1165 |
} |
| 1166 |
} |
| 1167 |
)); |
| 1168 |
|
| 1169 |
|
| 1170 |
signal_adapter.connect_end_of_stream(clone!( |
| 1171 |
#[strong] |
| 1172 |
sender, |
| 1173 |
#[strong] |
| 1174 |
weak, |
| 1175 |
move |_| { |
| 1176 |
if let Some(player_widget) = weak.get().upgrade() { |
| 1177 |
|
| 1178 |
player_widget.borrow_mut().info.ep.as_mut().map(|ep| { |
| 1179 |
ep.set_play_position(0)?; |
| 1180 |
send_blocking!(sender, Action::MarkAsPlayed(true, ep.id())); |
| 1181 |
let ok: Result<(), podcasts_data::errors::DataError> = Ok(()); |
| 1182 |
ok |
| 1183 |
}); |
| 1184 |
|
| 1185 |
player_widget.borrow_mut().stop() |
| 1186 |
} |
| 1187 |
} |
| 1188 |
)); |
| 1189 |
} |
| 1190 |
|
| 1191 |
fn connect_rate_buttons(&self) { |
| 1192 |
self.deref().borrow().rate.connect_signals(self.deref()); |
| 1193 |
self.deref() |
| 1194 |
.borrow() |
| 1195 |
.sheet |
| 1196 |
.rate() |
| 1197 |
.connect_signals(self.deref()); |
| 1198 |
} |
| 1199 |
|
| 1200 |
fn connect_mpris_buttons(&self, sender: &Sender<Action>) { |
| 1201 |
let widget = self.borrow(); |
| 1202 |
|
| 1203 |
if let Some(mpris) = widget.info.mpris.as_ref() { |
| 1204 |
mpris.connect_play_pause(clone!( |
| 1205 |
#[strong(rename_to = player)] |
| 1206 |
self, |
| 1207 |
move |mpris| { |
| 1208 |
match mpris.playback_status() { |
| 1209 |
PlaybackStatus::Paused => player.borrow().play(), |
| 1210 |
PlaybackStatus::Stopped => player.borrow().play(), |
| 1211 |
_ => player.borrow_mut().pause(), |
| 1212 |
}; |
| 1213 |
} |
| 1214 |
)); |
| 1215 |
mpris.connect_play(clone!( |
| 1216 |
#[strong(rename_to = player)] |
| 1217 |
self, |
| 1218 |
move |_| { |
| 1219 |
player.borrow().play(); |
| 1220 |
} |
| 1221 |
)); |
| 1222 |
|
| 1223 |
mpris.connect_pause(clone!( |
| 1224 |
#[strong(rename_to = player)] |
| 1225 |
self, |
| 1226 |
move |_| { |
| 1227 |
player.borrow_mut().pause(); |
| 1228 |
} |
| 1229 |
)); |
| 1230 |
|
| 1231 |
mpris.connect_seek(clone!( |
| 1232 |
#[strong(rename_to = player)] |
| 1233 |
self, |
| 1234 |
move |_, offset: mpris_server::Time| { |
| 1235 |
let direction = if offset.is_positive() { |
| 1236 |
SeekDirection::Forward |
| 1237 |
} else { |
| 1238 |
SeekDirection::Backwards |
| 1239 |
}; |
| 1240 |
player.borrow().seek( |
| 1241 |
ClockTime::from_useconds(offset.as_micros().unsigned_abs()), |
| 1242 |
direction, |
| 1243 |
); |
| 1244 |
} |
| 1245 |
)); |
| 1246 |
|
| 1247 |
mpris.connect_raise(clone!( |
| 1248 |
#[strong] |
| 1249 |
sender, |
| 1250 |
move |_| { |
| 1251 |
send_blocking!(sender, Action::RaiseWindow); |
| 1252 |
} |
| 1253 |
)); |
| 1254 |
}; |
| 1255 |
} |
| 1256 |
} |