feat: preview video thumbnails in a floating window

This commit is contained in:
2026-10-09 22:08:34 +02:00
parent 656474df5b
commit 689f14a634
5 changed files with 1152 additions and 36 deletions
+301
View File
@@ -0,0 +1,301 @@
use std::{
io::Cursor,
sync::mpsc::{self, Receiver, TryRecvError},
thread,
};
use image::{ImageFormat, ImageReader, Limits};
use ratatui::{
Frame,
layout::{Alignment, Rect, Size},
widgets::{Paragraph, Wrap},
};
use ratatui_image::{
FilterType, Resize, StatefulImage,
picker::Picker,
thread::{ResizeRequest, ResizeResponse, ThreadProtocol},
};
use crate::youtube::ThumbnailJob;
struct ThumbnailImage {
id: String,
protocol: ThreadProtocol,
replies: Receiver<Result<ResizeResponse, String>>,
complete: bool,
}
pub struct Thumbnail {
picker: Picker,
selected: Option<String>,
fetch: Option<ThumbnailJob>,
decoder: Option<Receiver<Result<ThumbnailImage, String>>>,
image: Option<ThumbnailImage>,
// ponytail: one completed thumbnail plus an in-flight replacement, not a video cache.
cached: Option<ThumbnailImage>,
error: Option<String>,
}
impl Thumbnail {
pub fn new() -> Self {
Self {
picker: Picker::halfblocks(),
selected: None,
fetch: None,
decoder: None,
image: None,
cached: None,
error: None,
}
}
/// Call once in raw mode/alternate screen, before the event loop reads stdin.
pub fn query_terminal(&mut self) {
self.picker = Picker::from_query_stdio().unwrap_or_else(|_| Picker::halfblocks());
let font = self.picker.font_size();
if font.width == 0 || font.height == 0 {
self.picker = Picker::halfblocks();
}
}
pub fn select(&mut self, id: &str) {
if self.selected.as_deref() == Some(id) {
return;
}
self.cancel();
self.selected = Some(id.to_owned());
if self.cached.as_ref().is_some_and(|image| image.id == id) {
self.image = self.cached.take();
return;
}
match ThumbnailJob::start(id) {
Ok(job) => self.fetch = Some(job),
Err(error) => self.error = Some(format!("Could not load thumbnail: {error}")),
}
}
pub fn cancel(&mut self) {
self.fetch = None;
// Each decode has its own receiver: dropping it invalidates even a queued completion.
self.decoder = None;
self.selected = None;
self.error = None;
if let Some(image) = self.image.take()
&& image.complete
{
self.cached = Some(image);
}
}
pub fn draw(&mut self, frame: &mut Frame, area: Rect) {
self.poll();
if area.width == 0 || area.height == 0 || self.selected.is_none() {
return;
}
let mut message = self.error.as_deref().unwrap_or("Loading thumbnail...");
if let Some(image) = self.image.as_mut() {
let resize = Resize::Fit(Some(FilterType::Triangle));
if let Some(size) = image.protocol.size_for(resize.clone(), area.into()) {
frame.render_stateful_widget(
StatefulImage::new().resize(resize),
centered(area, size),
&mut image.protocol,
);
if image.protocol.protocol_type().is_some() {
return;
}
}
message = "Preparing thumbnail...";
} else if self.decoder.is_some() {
message = "Decoding thumbnail...";
}
frame.render_widget(
Paragraph::new(message)
.alignment(Alignment::Center)
.wrap(Wrap { trim: true }),
Rect::new(
area.x,
area.y + area.height / 2,
area.width,
area.height - area.height / 2,
),
);
}
fn poll(&mut self) {
if let Some(result) = self.fetch.as_mut().and_then(ThumbnailJob::poll) {
self.fetch = None;
match result {
Ok(bytes) => {
let picker = self.picker.clone();
let id = self.selected.clone().expect("A fetch has a selected video");
let (tx, rx) = mpsc::channel();
match thread::Builder::new()
.name("thumbnail-decode".into())
.spawn(move || {
let _ = tx.send(prepare(id, bytes, picker));
}) {
Ok(_) => self.decoder = Some(rx),
Err(error) => {
self.error = Some(format!("Could not decode thumbnail: {error}"));
}
}
}
Err(error) => self.error = Some(format!("Could not load thumbnail: {error}")),
}
}
if let Some(result) = self.decoder.as_ref().map(Receiver::try_recv) {
match result {
Ok(result) => {
self.decoder = None;
match result {
Ok(image) => self.image = Some(image),
Err(error) => self.error = Some(error),
}
}
Err(TryRecvError::Disconnected) => {
self.decoder = None;
self.error = Some("Thumbnail decoder stopped before completing.".into());
}
Err(TryRecvError::Empty) => {}
}
}
if let Some(image) = self.image.as_mut() {
match image.replies.try_recv() {
Ok(Ok(response)) => {
// ThreadProtocol rejects obsolete requests; the widget checks the current
// area again before rendering, so rapid resizes cannot draw an old size.
if image.protocol.update_resized_protocol(response) {
image.complete = true;
self.cached = None;
}
}
Ok(Err(error)) => {
self.image = None;
self.error = Some(format!("Could not render thumbnail: {error}"));
}
Err(TryRecvError::Disconnected) => {
self.image = None;
self.error = Some("Thumbnail renderer stopped before completing.".into());
}
Err(TryRecvError::Empty) => {}
}
}
}
}
fn prepare(id: String, bytes: Vec<u8>, picker: Picker) -> Result<ThumbnailImage, String> {
let image = decode(bytes)?;
let protocol = picker.new_resize_protocol(image);
let (requests_tx, requests_rx) = mpsc::channel::<ResizeRequest>();
let (replies_tx, replies_rx) = mpsc::channel();
thread::Builder::new()
.name("thumbnail-resize".into())
.spawn(move || {
for request in requests_rx {
if replies_tx
.send(request.resize_encode().map_err(|error| error.to_string()))
.is_err()
{
break;
}
}
})
.map_err(|error| format!("Could not prepare thumbnail renderer: {error}"))?;
Ok(ThumbnailImage {
id,
protocol: ThreadProtocol::new(requests_tx, Some(protocol)),
replies: replies_rx,
complete: false,
})
}
fn decode(bytes: Vec<u8>) -> Result<image::DynamicImage, String> {
if bytes.len() > 2 * 1024 * 1024 {
return Err("Thumbnail exceeds the 2 MiB download limit.".into());
}
let mut reader = ImageReader::with_format(Cursor::new(bytes), ImageFormat::Jpeg);
let mut limits = Limits::default();
limits.max_image_width = Some(4096);
limits.max_image_height = Some(4096);
limits.max_alloc = Some(16 * 1024 * 1024);
reader.limits(limits);
reader
.decode()
.map_err(|error| format!("Could not decode thumbnail JPEG: {error}"))
}
fn centered(area: Rect, size: Size) -> Rect {
let width = size.width.min(area.width);
let height = size.height.min(area.height);
Rect::new(
area.x + (area.width - width) / 2,
area.y + (area.height - height) / 2,
width,
height,
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rejects_corrupt_and_oversized_jpeg_input() {
assert!(decode(b"not a JPEG".to_vec()).is_err());
assert!(decode(vec![0; 2 * 1024 * 1024 + 1]).is_err());
}
#[test]
fn cancellation_discards_queued_decode_completion() {
let mut thumbnail = Thumbnail::new();
let (tx, rx) = mpsc::channel();
let (requests_tx, _requests_rx) = mpsc::channel();
let (_replies_tx, replies_rx) = mpsc::channel();
let protocol = thumbnail
.picker
.new_resize_protocol(image::DynamicImage::new_rgb8(1, 1));
thumbnail.selected = Some("old-video".into());
thumbnail.decoder = Some(rx);
assert!(
tx.send(Ok(ThumbnailImage {
id: "old-video".into(),
protocol: ThreadProtocol::new(requests_tx, Some(protocol)),
replies: replies_rx,
complete: false,
}))
.is_ok()
);
thumbnail.cancel();
thumbnail.selected = Some("new-video".into());
thumbnail.poll();
assert!(thumbnail.image.is_none());
assert!(thumbnail.error.is_none());
}
#[test]
fn resized_image_preserves_aspect_and_stays_inside_popup() {
let picker = Picker::halfblocks();
let font = picker.font_size();
let protocol = picker.new_resize_protocol(image::DynamicImage::new_rgb8(480, 360));
let area = Rect::new(7, 3, 80, 10);
let size = protocol.size_for(Resize::Fit(None), area.into());
let image_area = centered(area, size);
assert!(image_area.x >= area.x && image_area.right() <= area.right());
assert!(image_area.y >= area.y && image_area.bottom() <= area.bottom());
let left = image_area.x - area.x;
let right = area.right() - image_area.right();
let top = image_area.y - area.y;
let bottom = area.bottom() - image_area.bottom();
assert!(left.abs_diff(right) <= 1 && top.abs_diff(bottom) <= 1);
let pixel_width = u32::from(image_area.width) * u32::from(font.width);
let pixel_height = u32::from(image_area.height) * u32::from(font.height);
// Cell rounding may add at most one cell to either fitted pixel dimension.
assert!(
pixel_width.abs_diff(pixel_height * 4 / 3)
<= u32::from(font.width) + u32::from(font.height) * 4 / 3
);
assert_eq!(centered(Rect::new(7, 3, 0, 0), size), Rect::new(7, 3, 0, 0));
}
}