Skip to main content

cosmic_applet_avestruz/handlers/
video.rs

1// SPDX-License-Identifier: MIT
2
3//! Video message handlers for Avestruz.
4//!
5//! Most of what a video is told is told to the model at once (see
6//! [`crate::video`]). The one thing that takes time is a YouTube video:
7//! what there is to play of it has to be found first, by yt-dlp (see
8//! [`crate::youtube`]).
9
10use cosmic::prelude::*;
11
12use crate::fl;
13use crate::messages::Message;
14use crate::state::AppModel;
15use crate::youtube::{self, FindError};
16
17impl AppModel {
18    /// The video at `url` was pressed: play it, or hold it if it plays. A
19    /// YouTube video is on its way from here, while yt-dlp is asked what
20    /// there is to play of it.
21    pub fn handle_video_press(&mut self, url: String) -> Task<cosmic::Action<Message>> {
22        match self.videos.press(&url) {
23            Some(video) => Task::perform(youtube::find_stream(video), move |result| {
24                cosmic::Action::App(Message::VideoFound(url.clone(), result))
25            }),
26            None => Task::none(),
27        }
28    }
29
30    /// yt-dlp answered for the YouTube video at `url`: with the address of
31    /// a stream, which is played, or with why there is none, which is said
32    /// where the video would have played.
33    pub fn handle_video_found(&mut self, url: &str, result: Result<String, FindError>) {
34        let stream = result.map_err(|e| {
35            tracing::warn!("no stream of a YouTube video was found: {e:?}");
36            match e {
37                FindError::NotInstalled => fl!("video-ytdlp-missing"),
38                FindError::TimedOut => fl!("video-ytdlp-slow"),
39                FindError::NoRuntime => fl!("video-ytdlp-runtime"),
40                FindError::Failed(why) => fl!("video-ytdlp-failed", error = why),
41            }
42        });
43        self.videos.found(url, stream);
44    }
45}