//! Pictures linked in chat, shown inline: a link to a PNG, JPEG, GIF or //! WebP file (by name) up to `ui.image_preview_kb` is downloaded into //! memory once (`Command::DownloadChatFile` with `DownloadTo::Memory`, //! voice connections only) and decoded once into the image cache //! (`images.rs`); the chat shows it as a picture card, and a click opens //! it larger. A picture on the web a message shows (`[img]`) comes the way //! banners do, into the engine's cache (`Command::FetchPicture`, up to the //! same size, only with `cache.fetch_images`), and until it is there the //! message shows its address. use std::borrow::Cow; use crate::app::App; use crate::bind::convert; use crate::vm; impl App { /// Download the pictures linked in the current chat of `session` that /// are not there yet ([`vm::previews::fetch_previews`]). pub(crate) fn fetch_previews(&mut self, session: i64) { self.transition(|state, cx| vm::previews::fetch_previews(state, cx, session)); } /// The picture of link `index` of a message of the current chat (its /// files, then its pictures on the web), for the large view. pub(crate) fn open_preview(&self, key: i32, index: i32) -> slint::Image { let Some(view) = self.view() else { return slint::Image::default() }; let Some(tab) = view.tabs.get(view.current_tab) else { return slint::Image::default() }; let message = match tab.message(key) { Some(message) => Cow::Borrowed(&message.message), // A line of the server. None => match vm::chat::server_text(view, tab, key) { Some(text) => Cow::Owned(vm::chat::server_message("", text)), None => return slint::Image::default(), }, }; let index = index.max(0) as usize; match message.file_refs().get(index) { Some(file) => { vm::previews::image_of(view, file).map(|i| convert::image(&i)).unwrap_or_default() } None => vm::chat::picture_link(&message, index) .and_then(|url| view.pictures.get(&url)) .map(|path| crate::images::file(path)) .unwrap_or_default(), } } }