use crate::ViewId;
use crate::host::with_host;
#[derive(Clone)]
pub struct Snapshot {
pub width: u32,
pub height: u32,
pub rgba: Vec<u8>,
}
impl std::fmt::Debug for Snapshot {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Snapshot({}x{})", self.width, self.height)
}
}
pub fn snapshot(id: ViewId) -> Option<Snapshot> {
with_host(|host| {
let view = &host.views.get(&id)?.view;
let bounds = view.bounds();
let rep = view.bitmapImageRepForCachingDisplayInRect(bounds)?;
view.cacheDisplayInRect_toBitmapImageRep(bounds, &rep);
let (w, h) = (rep.pixelsWide() as usize, rep.pixelsHigh() as usize);
let (bpr, spp) = (rep.bytesPerRow() as usize, rep.samplesPerPixel() as usize);
let data = rep.bitmapData();
if data.is_null() || rep.bitsPerSample() != 8 || !(3..=4).contains(&spp) {
return None;
}
let alpha_first = rep.bitmapFormat().contains(objc2_app_kit::NSBitmapFormat::AlphaFirst);
let src = unsafe { std::slice::from_raw_parts(data, bpr * h) };
let mut rgba = Vec::with_capacity(w * h * 4);
for row in src.chunks(bpr).take(h) {
for px in row[..w * spp].chunks(spp) {
let (r, g, b, a) = match (spp, alpha_first) {
(4, true) => (px[1], px[2], px[3], px[0]),
(4, false) => (px[0], px[1], px[2], px[3]),
_ => (px[0], px[1], px[2], 255),
};
rgba.extend_from_slice(&[r, g, b, a]);
}
}
Some(Snapshot { width: w as u32, height: h as u32, rgba })
})
.flatten()
}