1mod image_utils;
2
3use anyhow::{anyhow, Result};
4use display_info::DisplayInfo;
5use image::RgbaImage;
6
7pub use display_info;
8pub use image;
9
10#[cfg(target_os = "macos")]
11mod darwin;
12#[cfg(target_os = "macos")]
13use darwin::*;
14
15#[cfg(target_os = "windows")]
16mod win32;
17#[cfg(target_os = "windows")]
18use win32::*;
19
20#[cfg(target_os = "linux")]
21mod linux;
22#[cfg(target_os = "linux")]
23use linux::*;
24
25#[derive(Debug, Clone, Copy)]
27pub struct Screen {
28    pub display_info: DisplayInfo,
29}
30
31impl Screen {
32    pub fn new(display_info: &DisplayInfo) -> Self {
36        Screen {
37            display_info: *display_info,
38        }
39    }
40
41    pub fn all() -> Result<Vec<Screen>> {
43        let screens = DisplayInfo::all()?.iter().map(Screen::new).collect();
44        Ok(screens)
45    }
46
47    pub fn from_point(x: i32, y: i32) -> Result<Screen> {
49        let display_info = DisplayInfo::from_point(x, y)?;
50        Ok(Screen::new(&display_info))
51    }
52
53    pub fn capture(&self) -> Result<RgbaImage> {
55        capture_screen(&self.display_info)
56    }
57
58    pub fn capture_area(&self, x: i32, y: i32, width: u32, height: u32) -> Result<RgbaImage> {
60        let display_info = self.display_info;
61        let screen_x2 = display_info.x + display_info.width as i32;
62        let screen_y2 = display_info.y + display_info.height as i32;
63
64        let x1 = (x + display_info.x).clamp(display_info.x, screen_x2);
66        let y1 = (y + display_info.y).clamp(display_info.y, screen_y2);
67
68        let x2 = std::cmp::min(x1 + width as i32, screen_x2);
70        let y2 = std::cmp::min(y1 + height as i32, screen_y2);
71
72        if x1 >= x2 || y1 >= y2 {
74            return Err(anyhow!("Area size is invalid"));
75        }
76
77        capture_screen_area(
79            &display_info,
80            x1 - display_info.x,
81            y1 - display_info.y,
82            (x2 - x1) as u32,
83            (y2 - y1) as u32,
84        )
85    }
86
87    #[cfg(target_os = "windows")]
88    pub fn capture_area_ignore_area_check(
103        &self,
104        x: i32,
105        y: i32,
106        width: u32,
107        height: u32,
108    ) -> Result<RgbaImage> {
109        let display_info = self.display_info;
110        capture_screen_area_ignore_sf(&display_info, x, y, width, height)
111    }
112}