mod image_utils;
use anyhow::{anyhow, Result};
use display_info::DisplayInfo;
use image::RgbaImage;
pub use display_info;
pub use image;
#[cfg(target_os = "macos")]
mod darwin;
#[cfg(target_os = "macos")]
use darwin::*;
#[cfg(target_os = "windows")]
mod win32;
#[cfg(target_os = "windows")]
use win32::*;
#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "linux")]
use linux::*;
#[derive(Debug, Clone, Copy)]
pub struct Screen {
pub display_info: DisplayInfo,
}
impl Screen {
pub fn new(display_info: &DisplayInfo) -> Self {
Screen {
display_info: *display_info,
}
}
pub fn all() -> Result<Vec<Screen>> {
let screens = DisplayInfo::all()?.iter().map(Screen::new).collect();
Ok(screens)
}
pub fn from_point(x: i32, y: i32) -> Result<Screen> {
let display_info = DisplayInfo::from_point(x, y)?;
Ok(Screen::new(&display_info))
}
pub fn capture(&self) -> Result<RgbaImage> {
capture_screen(&self.display_info)
}
pub fn capture_area(&self, x: i32, y: i32, width: u32, height: u32) -> Result<RgbaImage> {
let display_info = self.display_info;
let screen_x2 = display_info.x + display_info.width as i32;
let screen_y2 = display_info.y + display_info.height as i32;
let x1 = (x + display_info.x).clamp(display_info.x, screen_x2);
let y1 = (y + display_info.y).clamp(display_info.y, screen_y2);
let x2 = std::cmp::min(x1 + width as i32, screen_x2);
let y2 = std::cmp::min(y1 + height as i32, screen_y2);
if x1 >= x2 || y1 >= y2 {
return Err(anyhow!("Area size is invalid"));
}
capture_screen_area(
&display_info,
x1 - display_info.x,
y1 - display_info.y,
(x2 - x1) as u32,
(y2 - y1) as u32,
)
}
#[cfg(target_os = "windows")]
pub fn capture_area_ignore_area_check(
&self,
x: i32,
y: i32,
width: u32,
height: u32,
) -> Result<RgbaImage> {
let display_info = self.display_info;
capture_screen_area_ignore_sf(&display_info, x, y, width, height)
}
}