display_info/
lib.rs

1//! # example
2//! Get all display info
3//! ```
4//! use display_info::DisplayInfo;
5//! use std::time::Instant;
6//!
7//! let start = Instant::now();
8//!
9//! let display_infos = DisplayInfo::all().unwrap();
10//! for display_info in display_infos {
11//!   println!("display_info {display_info:?}");
12//! }
13//! let display_info = DisplayInfo::from_point(100, 100).unwrap();
14//! println!("display_info {display_info:?}");
15//! println!("运行耗时: {:?}", start.elapsed());
16//! ```
17
18pub mod error;
19use error::{DIError, DIResult};
20
21#[cfg(all(target_family = "unix", not(target_os = "macos")))]
22mod linux;
23#[cfg(all(target_family = "unix", not(target_os = "macos")))]
24use linux::ScreenRawHandle;
25
26#[cfg(target_os = "macos")]
27mod macos;
28#[cfg(target_os = "macos")]
29use macos::ScreenRawHandle;
30
31#[cfg(target_os = "windows")]
32mod windows;
33#[cfg(target_os = "windows")]
34use windows::ScreenRawHandle;
35
36#[derive(Debug, Clone)]
37pub struct DisplayInfo {
38    /// Unique identifier associated with the display.
39    pub id: u32,
40    /// The display name
41    pub name: String,
42    /// The display friendly name
43    pub friendly_name: String,
44    /// Native screen raw handle
45    pub raw_handle: ScreenRawHandle,
46    /// The display x coordinate.
47    pub x: i32,
48    /// The display x coordinate.
49    pub y: i32,
50    /// The display pixel width.
51    pub width: u32,
52    /// The display pixel height.
53    pub height: u32,
54    /// The width of a display in millimeters. This value may be 0.
55    pub width_mm: i32,
56    /// The height of a display in millimeters. This value may be 0.
57    pub height_mm: i32,
58    /// Can be 0, 90, 180, 270, represents screen rotation in clock-wise degrees.
59    pub rotation: f32,
60    /// Output device's pixel scale factor.
61    pub scale_factor: f32,
62    /// The display refresh rate.
63    pub frequency: f32,
64    /// Whether the screen is the main screen
65    pub is_primary: bool,
66}
67
68impl DisplayInfo {
69    pub fn from_name(name: impl ToString) -> DIResult<DisplayInfo> {
70        let name = name.to_string();
71        let display_infos = DisplayInfo::all()?;
72
73        display_infos
74            .iter()
75            .find(|&d| d.name == name)
76            .cloned()
77            .ok_or_else(|| DIError::new("Get display info failed"))
78    }
79}