//! Native chat image presentation. Network/decode work runs away from the event loop. use super::{chat_images, slint::MainWindow}; use slint::{ComponentHandle, Model, SharedPixelBuffer}; use std::{cell::RefCell, collections::HashMap, rc::Rc, sync::mpsc, time::Instant}; use vnox_client::net::NetCommand; struct Animation { frames: Vec<(slint::Image, u32)>, ratio: f32, started: Instant, bytes: usize, } impl Animation { fn new(image: chat_images::DecodedImage) -> Self { let bytes = image.frames.iter().map(|f| f.rgba.len()).sum(); Self { frames: image .frames .into_iter() .map(|f| { ( slint::Image::from_rgba8(SharedPixelBuffer::clone_from_slice( &f.rgba, image.width, image.height, )), f.delay_ms.max(20), ) }) .collect(), ratio: image.width as f32 / image.height as f32, started: Instant::now(), bytes, } } fn frame(&self) -> slint::Image { let duration: u128 = self.frames.iter().map(|f| u128::from(f.1)).sum(); let mut position = self.started.elapsed().as_millis() % duration.max(1); for (image, delay) in &self.frames { if position < u128::from(*delay) { return image.clone(); } position -= u128::from(*delay); } self.frames[0].0.clone() } } struct Entry { image: Option, error: String, started: Instant, } struct Ready { key: String, result: Result, clipboard: Option>, } struct Pending { bytes: Vec, image: Animation, channel: String, generation: u64, request: Option, started: Instant, } struct Media { cache: HashMap, tx: mpsc::Sender, rx: mpsc::Receiver, pending: Option, clipboard_key: Option, generation: u64, workers: usize, } impl Default for Media { fn default() -> Self { let (tx, rx) = mpsc::channel(); Self { cache: HashMap::new(), tx, rx, pending: None, clipboard_key: None, generation: 0, workers: 0, } } } thread_local! { static MEDIA: RefCell = RefCell::new(Media::default()); } pub fn wire(window: &MainWindow, state: &Rc>) { let weak = window.as_weak(); let paste_state = state.clone(); window.on_paste_image(move || { let Some(win)=weak.upgrade() else { return false }; let s=paste_state.borrow(); // A failed image lookup must leave normal text paste to TextInput. let Ok(clipboard)=chat_images::clipboard_image() else { return false }; if MEDIA.with(|m| {let m=m.borrow();m.workers+m.cache.iter().filter(|(key,entry)|key.starts_with("attachment:") && entry.error=="Loading image…").count()>=3 || m.pending.as_ref().is_some_and(|p|p.request.is_some())}) {win.set_image_status("Wait for the current image operation to finish.".into());return true;} if s.ui.active_dm_id.is_some() || s.ui.active_channel.is_none() { win.set_image_status("Image attachments are available in channels. You can send an HTTPS image link here.".into()); return true; } let channel=s.ui.active_channel.clone().unwrap_or_default(); let generation=s.connection_generation; MEDIA.with(|m| { let mut m=m.borrow_mut(); if m.pending.as_ref().is_some_and(|p|p.request.is_some()) {return} let tx=m.tx.clone(); m.workers+=1;let clipboard_key=format!("clipboard:{generation}:{channel}:{}",uuid::Uuid::new_v4());m.clipboard_key=Some(clipboard_key.clone()); tokio::task::spawn_blocking(move || { let result=chat_images::encode_clipboard_png(clipboard).and_then(|bytes|chat_images::decode(&bytes).map(|image|(bytes,image))); match result {Ok((bytes,image))=>{let _=tx.send(Ready {key:clipboard_key,result:Ok(image),clipboard:Some(bytes)});},Err(error)=>{let _=tx.send(Ready {key:clipboard_key,result:Err(error.to_string()),clipboard:Some(Vec::new())});}} }); }); win.set_image_status("Preparing image…".into()); true }); let weak = window.as_weak(); window.on_cancel_image(move || { let cancelled = MEDIA.with(|m| { let mut m = m.borrow_mut(); if m.pending.as_ref().is_some_and(|p| p.request.is_some()) { return false; } m.pending = None; m.clipboard_key = None; true }); if !cancelled { return; } if let Some(w) = weak.upgrade() { w.set_has_pending_image(false); w.set_image_status("".into()); } }); let state = state.clone(); let weak = window.as_weak(); window.on_send_image(move || { let s = state.borrow(); let net = s.net.clone(); MEDIA.with(|m| { let mut m = m.borrow_mut(); let Some(p) = m.pending.as_mut() else { return }; if p.request.is_some() { return; } if p.generation != s.connection_generation || s.ui.active_dm_id.is_some() || s.ui.active_channel.as_deref() != Some(&p.channel) || !matches!(s.ui.conn, super::state::ConnState::Connected { .. }) { if let Some(w) = weak.upgrade() { w.set_image_status( "Select the original channel and connect before sending this image.".into(), ); } return; } let request_id = uuid::Uuid::new_v4().to_string(); p.request = Some(request_id.clone()); p.started = Instant::now(); let channel_id = p.channel.clone(); let data = p.bytes.clone(); tokio::spawn(async move { net.send(NetCommand::AttachmentUpload { request_id, channel_id, data, }) .await; }); if let Some(w) = weak.upgrade() { w.set_image_uploading(true); w.set_image_status("Sending image…".into()); } }); }); } pub fn receive(result: vnox_client::net::wire::AttachmentResult) { MEDIA.with(|m| { let mut m = m.borrow_mut(); if let Some(p) = m.pending.as_mut() && p.request.as_deref() == Some(&result.request_id) { if result.error.is_empty() { m.pending = None; } else { p.request = None; } // Display delivery state on the next UI tick. let _ = m.tx.send(Ready { key: "upload-status".into(), result: Err(if result.error.is_empty() { String::new() } else { result.error }), clipboard: None, }); return; } let key = result.request_id; let expected = format!( "attachment:{}:{}:{}", m.generation, result.channel_id, result.attachment_id ); if key != expected || !m .cache .get(&key) .is_some_and(|entry| entry.error == "Loading image…") { return; } if !result.error.is_empty() { if let Some(e) = m.cache.get_mut(&key) { e.error = result.error; } return; } if let Some(entry) = m.cache.get_mut(&key) { entry.error = "Decoding image…".into(); } let tx = m.tx.clone(); m.workers += 1; tokio::task::spawn_blocking(move || { let decoded = chat_images::decode(&result.data).map_err(|e| e.to_string()); let _ = tx.send(Ready { key, result: decoded, clipboard: None, }); }); }); } pub fn tick(window: &MainWindow, state: &crate::AppState) { MEDIA.with(|m| { let mut m = m.borrow_mut(); if m.generation != state.connection_generation { m.cache.clear(); m.pending = None; m.clipboard_key = None; m.generation = state.connection_generation; window.set_has_pending_image(false); window.set_image_uploading(false); window.set_image_status("".into()); } let visible_model = if window.get_show_forum() { window.get_forum_replies() } else { window.get_messages() }; let visible: Vec = visible_model .iter() .filter_map(|item| source_key(&item.media_source, state)) .collect(); m.cache.retain(|key, _| visible.contains(key)); while let Ok(ready) = m.rx.try_recv() { if ready.key == "upload-status" { window.set_image_uploading(false); window.set_image_status(ready.result.err().unwrap_or_default().into()); window.set_has_pending_image(m.pending.is_some()); continue; } m.workers = m.workers.saturating_sub(1); if let Some(bytes) = ready.clipboard { let expected = format!( "clipboard:{}:{}", state.connection_generation, state.ui.active_channel.as_deref().unwrap_or("") ); if m.clipboard_key.as_deref() != Some(&ready.key) || !ready.key.starts_with(&format!("{expected}:")) || state.ui.active_dm_id.is_some() { continue; } m.clipboard_key = None; match ready.result { Ok(image) => { let image = Animation::new(image); window.set_pending_image(image.frame()); window.set_has_pending_image(true); window.set_image_status( "Image ready. Send it to this channel or cancel.".into(), ); m.pending = Some(Pending { bytes, image, channel: state.ui.active_channel.clone().unwrap_or_default(), generation: state.connection_generation, request: None, started: Instant::now(), }); } Err(e) => window.set_image_status(format!("Cannot paste image: {e}").into()), } continue; } if !m.cache.contains_key(&ready.key) { continue; } match ready.result { Ok(image) => { let image = Animation::new(image); let total: usize = m .cache .values() .filter_map(|e| e.image.as_ref()) .map(|a| a.bytes) .sum(); if total + image.bytes > 64 * 1024 * 1024 { m.cache.insert( ready.key, Entry { image: None, error: "Preview cache is full. Open another channel to release images." .into(), started: Instant::now(), }, ); continue; } m.cache.insert( ready.key, Entry { image: Some(image), error: String::new(), started: Instant::now(), }, ); } Err(e) => { m.cache.insert( ready.key, Entry { image: None, error: format!("Image unavailable: {e}"), started: Instant::now(), }, ); } } } if let Some(p) = m.pending.as_mut() { if p.request.is_some() && p.started.elapsed().as_secs() > 35 { p.request = None; window.set_image_uploading(false); window.set_image_status( "Image delivery could not be confirmed. Check the channel before retrying." .into(), ); } window.set_pending_image(p.image.frame()); } for entry in m.cache.values_mut() { if entry.image.is_none() && entry.error == "Loading image…" && entry.started.elapsed().as_secs() > 35 { entry.error = "Image request timed out".into(); } } let pending_fetches = m .cache .iter() .filter(|(k, e)| { k.starts_with("attachment:") && e.image.is_none() && e.error == "Loading image…" }) .count(); let model = if window.get_show_forum() { window.get_forum_replies() } else { window.get_messages() }; let mut slots = 3usize.saturating_sub(m.workers + pending_fetches); for row in 0..model.row_count() { let Some(mut item) = model.row_data(row) else { continue; }; let previous = item.clone(); let body = item.media_source.to_string(); let source = body.as_str(); let attachment = if window.get_show_forum() { None } else { source .strip_prefix("vnox-attachment:") .filter(|id| uuid::Uuid::parse_str(id).is_ok()) }; let remote = chat_images::image_url(source); let key = if let Some(id) = attachment { format!( "attachment:{}:{}:{}", state.connection_generation, state.ui.active_channel.as_deref().unwrap_or(""), id ) } else if let Some(url) = &remote { format!("url:{url}") } else { continue; }; if !m.cache.contains_key(&key) && slots > 0 && m.cache.len() < 24 { slots -= 1; m.cache.insert( key.clone(), Entry { image: None, error: "Loading image…".into(), started: Instant::now(), }, ); if let Some(attachment_id) = attachment { let net = state.net.clone(); let channel_id = state.ui.active_channel.clone().unwrap_or_default(); let attachment_id = attachment_id.to_string(); let request_id = key.clone(); tokio::spawn(async move { net.send(NetCommand::AttachmentFetch { request_id, channel_id, attachment_id, }) .await; }); } else if let Some(url) = remote { m.workers += 1; let tx = m.tx.clone(); let request = key.clone(); tokio::spawn(async move { let result = async { let bytes = chat_images::fetch(&url).await?; tokio::task::spawn_blocking(move || chat_images::decode(&bytes)).await? } .await .map_err(|e: anyhow::Error| e.to_string()); let _ = tx.send(Ready { key: request, result, clipboard: None, }); }); } } item.media_ready = false; item.media_image = Default::default(); item.media_status = "Image queue is full".into(); if let Some(entry) = m.cache.get(&key) { if let Some(image) = &entry.image { item.media_image = image.frame(); item.media_ready = true; item.media_ratio = image.ratio; item.media_status = "".into(); } else { item.media_status = entry.error.as_str().into(); } } if attachment.is_some() { item.content = "".into(); } if item.media_ready != previous.media_ready || item.media_image != previous.media_image || item.media_status != previous.media_status || item.media_ratio != previous.media_ratio || item.content != previous.content { model.set_row_data(row, item); } } }); } fn source_key(source: &str, state: &crate::AppState) -> Option { if super::forum::visible(&state.ui) && source.starts_with("vnox-attachment:") { return None; } if let Some(id) = source .strip_prefix("vnox-attachment:") .filter(|id| uuid::Uuid::parse_str(id).is_ok()) { Some(format!( "attachment:{}:{}:{}", state.connection_generation, state.ui.active_channel.as_deref().unwrap_or(""), id )) } else { chat_images::image_url(source).map(|url| format!("url:{url}")) } }