use hyprforge_clipboard::{Content, Entry};
use iced_runtime::core::image::Handle;
use std::collections::HashMap;
use std::io::Cursor;
const MAX_PIXELS: u64 = 16 * 1024 * 1024;
fn dimensions(bytes: &[u8]) -> Option<(u32, u32)> {
image::ImageReader::new(Cursor::new(bytes))
.with_guessed_format()
.ok()?
.into_dimensions()
.ok()
}
fn fits_within_cap(width: u32, height: u32) -> bool {
(width as u64) * (height as u64) <= MAX_PIXELS
}
fn decode(bytes: &[u8]) -> Option<Handle> {
let (width, height) = dimensions(bytes)?;
if !fits_within_cap(width, height) {
return None;
}
let decoded = image::load_from_memory(bytes).ok()?.to_rgba8();
let (width, height) = decoded.dimensions();
Some(Handle::from_rgba(width, height, decoded.into_raw()))
}
#[derive(Default)]
pub struct Cache {
handles: HashMap<hyprforge_clipboard::EntryId, Option<Handle>>,
sizes: HashMap<hyprforge_clipboard::EntryId, Option<(u32, u32)>>,
}
impl Cache {
pub fn new() -> Cache {
Cache::default()
}
pub fn get(&mut self, entry: &Entry) -> Option<Handle> {
let Content::Image { bytes, .. } = &entry.content else {
return None;
};
self.handles.entry(entry.id.clone()).or_insert_with(|| decode(bytes)).clone()
}
pub fn size(&mut self, entry: &Entry) -> Option<(u32, u32)> {
let Content::Image { bytes, .. } = &entry.content else {
return None;
};
*self.sizes.entry(entry.id.clone()).or_insert_with(|| dimensions(bytes))
}
pub fn over_cap(&mut self, entry: &Entry) -> bool {
self.size(entry).is_some_and(|(w, h)| !fits_within_cap(w, h))
}
}
#[cfg(test)]
mod tests {
use super::*;
use hyprforge_clipboard::{EntryId, Mime};
fn tiny_png() -> Vec<u8> {
let mut pixel = image::RgbaImage::new(1, 1);
pixel.put_pixel(0, 0, image::Rgba([10, 20, 30, 255]));
let mut bytes = Vec::new();
image::DynamicImage::ImageRgba8(pixel)
.write_to(&mut Cursor::new(&mut bytes), image::ImageFormat::Png)
.unwrap();
bytes
}
fn image_entry(bytes: Vec<u8>) -> Entry {
let content = Content::Image { bytes, mime: Mime::new("image/png") };
Entry { id: EntryId::of(&content), content, copied_at: 0, pinned: false }
}
#[test]
fn a_small_real_image_decodes_to_a_handle() {
assert!(decode(&tiny_png()).is_some());
}
#[test]
fn bytes_that_are_not_an_image_at_all_never_produce_a_handle() {
assert!(decode(b"not an image").is_none());
}
#[test]
fn an_image_over_the_pixel_cap_is_refused_before_decoding() {
assert!(!fits_within_cap(5000, 5000), "25 megapixels must exceed the cap");
assert!(fits_within_cap(4096, 4096), "16 megapixels must fit exactly at the cap");
}
#[test]
fn a_text_entry_has_no_thumbnail() {
let mut cache = Cache::new();
let entry = Entry {
id: EntryId::of(&Content::Text("hi".into())),
content: Content::Text("hi".into()),
copied_at: 0,
pinned: false,
};
assert!(cache.get(&entry).is_none());
}
#[test]
fn an_images_size_is_read_from_its_header_and_text_has_none() {
let mut cache = Cache::new();
assert_eq!(cache.size(&image_entry(tiny_png())), Some((1, 1)));
assert!(!cache.over_cap(&image_entry(tiny_png())));
let text = Entry { id: EntryId::of(&Content::Text("hi".into())), content: Content::Text("hi".into()), copied_at: 0, pinned: false };
assert_eq!(cache.size(&text), None);
}
#[test]
fn decoding_the_same_entry_twice_only_decodes_once() {
let mut cache = Cache::new();
let entry = image_entry(tiny_png());
let first = cache.get(&entry);
let second = cache.get(&entry);
assert!(first.is_some());
assert_eq!(first, second);
assert_eq!(cache.handles.len(), 1, "must not decode the same id twice");
}
}