use crate::frame::Frame;
use crate::{Error, Result};
pub trait Capture {
fn grab(&mut self) -> Result<Frame>;
fn grab_into(&mut self, dst: &mut Frame) -> Result<bool> {
*dst = self.grab()?;
Ok(true)
}
}
pub struct ScreenshotsCapture {
screen: screenshots::Screen,
}
impl ScreenshotsCapture {
pub fn primary() -> Result<Self> {
let screens = screenshots::Screen::all().map_err(|e| Error::Capture(e.to_string()))?;
let screen = screens
.into_iter()
.next()
.ok_or_else(|| Error::Capture("no display found".into()))?;
Ok(ScreenshotsCapture { screen })
}
}
impl Capture for ScreenshotsCapture {
fn grab(&mut self) -> Result<Frame> {
let img = self
.screen
.capture()
.map_err(|e| Error::Capture(e.to_string()))?;
let (width, height) = (img.width() as usize, img.height() as usize);
Ok(Frame::rgba8(width, height, img.into_raw()))
}
}