use std::cell::RefCell;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::time::Duration;
use block2::RcBlock;
use objc2::rc::Retained;
use objc2::runtime::AnyObject;
use objc2::{AllocAnyThread, MainThreadMarker};
use objc2_app_kit::{NSBitmapImageFileType, NSBitmapImageRep, NSImage, NSImageCacheMode};
use objc2_foundation::{NSData, NSDictionary, NSError};
use objc2_web_kit::{WKSnapshotConfiguration, WKWebView};
use super::eval::run_loop_until;
use crate::engine::{EngineError, EngineResult, PageHandle};
pub(super) fn capture_to_png(
web_view: &WKWebView,
page: PageHandle,
captures_dir: Option<&Path>,
mtm: MainThreadMarker,
snapshot_width: Option<f64>,
) -> EngineResult<PathBuf> {
let bytes = snapshot_to_png_bytes(web_view, mtm, snapshot_width)?;
let dir = captures_dir
.map(Path::to_path_buf)
.unwrap_or_else(|| std::env::temp_dir().join("vibesurfer-webkit-captures"));
std::fs::create_dir_all(&dir).map_err(|e| EngineError::Other(e.to_string()))?;
let path = dir.join(format!(
"wk-{}-{}.png",
page.0,
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_millis())
.unwrap_or(0)
));
std::fs::write(&path, &bytes).map_err(|e| EngineError::Other(e.to_string()))?;
Ok(path)
}
pub(super) fn snapshot_to_png_bytes(
web_view: &WKWebView,
mtm: MainThreadMarker,
snapshot_width: Option<f64>,
) -> EngineResult<Vec<u8>> {
objc2::rc::autoreleasepool(|_| snapshot_to_png_bytes_inner(web_view, mtm, snapshot_width))
}
fn snapshot_to_png_bytes_inner(
web_view: &WKWebView,
mtm: MainThreadMarker,
snapshot_width: Option<f64>,
) -> EngineResult<Vec<u8>> {
let slot: Rc<RefCell<Option<Result<Retained<NSImage>, String>>>> = Rc::new(RefCell::new(None));
let slot_for_block = slot.clone();
let block = RcBlock::new(move |image: *mut NSImage, error: *mut NSError| {
if !error.is_null() {
let err = unsafe { &*error };
*slot_for_block.borrow_mut() = Some(Err(err.localizedDescription().to_string()));
return;
}
if image.is_null() {
*slot_for_block.borrow_mut() = Some(Err("null NSImage".into()));
return;
}
let img: Retained<NSImage> = unsafe { Retained::retain(image).expect("non-null NSImage") };
*slot_for_block.borrow_mut() = Some(Ok(img));
});
let config = unsafe { WKSnapshotConfiguration::new(mtm) };
unsafe { config.setAfterScreenUpdates(false) };
if let Some(w) = snapshot_width {
unsafe { config.setSnapshotWidth(Some(&objc2_foundation::NSNumber::new_f64(w))) };
}
unsafe {
web_view.takeSnapshotWithConfiguration_completionHandler(Some(&config), &block);
}
let slot_check = slot.clone();
let ok = run_loop_until(
move || slot_check.borrow().is_some(),
Duration::from_secs(10),
);
if !ok {
return Err(EngineError::Timeout {
budget: Duration::from_secs(10),
primitive: "capture",
});
}
let result = slot.borrow_mut().take();
let image = match result {
Some(Ok(img)) => img,
Some(Err(msg)) => return Err(EngineError::Other(format!("snapshot failed: {msg}"))),
None => unreachable!(),
};
image.setCacheMode(NSImageCacheMode::Never);
let cg =
unsafe { image.CGImageForProposedRect_context_hints(std::ptr::null_mut(), None, None) }
.ok_or_else(|| EngineError::Other("snapshot has no CGImage".into()))?;
let bitmap = NSBitmapImageRep::initWithCGImage(NSBitmapImageRep::alloc(), &cg);
let empty: Retained<NSDictionary<objc2_foundation::NSString, AnyObject>> = NSDictionary::new();
let png_data: Retained<NSData> =
unsafe { bitmap.representationUsingType_properties(NSBitmapImageFileType::PNG, &empty) }
.ok_or_else(|| EngineError::Other("PNG encoding returned nil".into()))?;
let bytes: &[u8] = unsafe { png_data.as_bytes_unchecked() };
Ok(bytes.to_vec())
}