1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//! # example
//! Get all display info
//! ```
//! use display_info::DisplayInfo;
//! use std::time::Instant;
//!
//! fn main() {
//!   let start = Instant::now();
//!
//!   let display_infos = DisplayInfo::all().unwrap();
//!   for display_info in display_infos {
//!     println!("display_info {display_info:?}");
//!   }
//!   let display_info = DisplayInfo::from_point(100, 100).unwrap();
//!   println!("display_info {display_info:?}");
//!   println!("运行耗时: {:?}", start.elapsed());
//! }
//! ```

#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "linux")]
use linux::{get_all, get_from_point, ScreenRawHandle};

#[cfg(target_os = "macos")]
mod macos;
#[cfg(target_os = "macos")]
use macos::{get_all, get_from_point, ScreenRawHandle};

#[cfg(target_os = "windows")]
mod windows;
#[cfg(target_os = "windows")]
use windows::{get_all, get_from_point, ScreenRawHandle};

use anyhow::Result;

#[derive(Debug, Clone)]
pub struct DisplayInfo {
    /// The Display Name
    pub name: String,
    /// Unique identifier associated with the display.
    pub id: u32,
    /// Native screen raw handle
    pub raw_handle: ScreenRawHandle,
    /// The display x coordinate.
    pub x: i32,
    /// The display x coordinate.
    pub y: i32,
    /// The display pixel width.
    pub width: u32,
    /// The display pixel height.
    pub height: u32,
    /// Can be 0, 90, 180, 270, represents screen rotation in clock-wise degrees.
    pub rotation: f32,
    /// Output device's pixel scale factor.
    pub scale_factor: f32,
    /// The display refresh rate.
    pub frequency: f32,
    /// Whether the screen is the main screen
    pub is_primary: bool,
}

impl DisplayInfo {
    pub fn all() -> Result<Vec<DisplayInfo>> {
        get_all()
    }

    pub fn from_point(x: i32, y: i32) -> Result<DisplayInfo> {
        get_from_point(x, y)
    }

    pub fn from_name(name: impl ToString) -> Result<DisplayInfo> {
        let name = name.to_string();
        let display_infos = get_all()?;

        display_infos
            .iter()
            .find(|&d| d.name == name)
            .cloned()
            .ok_or_else(|| anyhow::anyhow!("Get display info failed"))
    }
}