use mitsuami_core::NodeId;
use mitsuami_core::backend::{CaptureError, Image};
use objc2::Message;
use objc2_app_kit::NSBitmapFormat;
use super::AppKitBackend;
impl AppKitBackend {
pub(super) fn capture_now(&self, id: NodeId) -> Result<Image, CaptureError> {
let view = {
let state = self.state.borrow();
state.nodes.get(&id).ok_or(CaptureError::UnknownNode)?.widget.view().retain()
};
view.layoutSubtreeIfNeeded();
let bounds = view.bounds();
let rep = view
.bitmapImageRepForCachingDisplayInRect(bounds)
.ok_or_else(|| CaptureError::Failed("no bitmap for view".into()))?;
view.cacheDisplayInRect_toBitmapImageRep(bounds, &rep);
let (width, height) = (rep.pixelsWide() as usize, rep.pixelsHigh() as usize);
let (samples, bits, row) = (rep.samplesPerPixel() as usize, rep.bitsPerSample(), rep.bytesPerRow() as usize);
if bits != 8 || !(samples == 3 || samples == 4) {
return Err(CaptureError::Failed(format!("unsupported bitmap: {samples} samples × {bits} bits")));
}
let alpha_first = rep.bitmapFormat().contains(NSBitmapFormat::AlphaFirst);
let data = rep.bitmapData();
if data.is_null() {
return Err(CaptureError::Failed("bitmap has no data".into()));
}
let bytes = unsafe { std::slice::from_raw_parts(data, row * height) };
let mut rgba = Vec::with_capacity(width * height * 4);
for y in 0..height {
for x in 0..width {
let p = &bytes[y * row + x * samples..][..samples];
let [r, g, b, a] = match (samples, alpha_first) {
(4, true) => [p[1], p[2], p[3], p[0]],
(4, false) => [p[0], p[1], p[2], p[3]],
_ => [p[0], p[1], p[2], 255],
};
rgba.extend_from_slice(&[r, g, b, a]);
}
}
let scale = if bounds.size.width > 0.0 { width as f32 / bounds.size.width as f32 } else { 1.0 };
Ok(Image { width: width as u32, height: height as u32, scale_factor: scale, rgba })
}
}