use std::thread;
use std::time::Duration;
#[cfg(target_os = "windows")]
use arboard::Clipboard;
#[cfg(target_os = "windows")]
use image::{DynamicImage, ImageBuffer, Rgba};
pub fn start_snapshot_cropper() -> Option<DynamicImage> {
#[cfg(target_os = "windows")]
{
use std::process::Command;
let _ = Command::new("powershell")
.args(&["-NoProfile", "-Command", "Start-Process -WindowStyle Hidden 'ms-screenclip:'"])
.status();
thread::sleep(Duration::from_millis(1500));
let mut clipboard = Clipboard::new().ok()?;
if let Ok(data) = clipboard.get_image() {
eprintln!("desktop_cropper: got image {}x{} ({} bytes)", data.width, data.height, data.bytes.len());
if data.width > 0 && data.height > 0 {
let debug_path = std::env::temp_dir().join("qr_clipped_raw.png");
let mut rgba_data: Vec<u8> = Vec::with_capacity(data.bytes.len());
let bytes = data.bytes.as_ref();
for chunk in bytes.chunks(4) {
if chunk.len() >= 4 {
rgba_data.push(chunk[2]); rgba_data.push(chunk[1]); rgba_data.push(chunk[0]); rgba_data.push(chunk[3]); }
}
let buf = ImageBuffer::<Rgba<u8>, Vec<u8>>::from_raw(
data.width as u32,
data.height as u32,
rgba_data,
)?;
let dyn_img = DynamicImage::ImageRgba8(buf);
if dyn_img.save(&debug_path).is_ok() {
eprintln!("desktop_cropper: saved debug to {:?}", debug_path);
}
return Some(dyn_img);
}
} else {
eprintln!("desktop_cropper: get_image failed");
}
None
}
#[cfg(not(target_os = "windows"))]
{
None
}
}