screen_info/
lib.rs

1//! # example
2//! Get all display info
3//! ```
4//! use display_info::DisplayInfo;
5//! use std::time::Instant;
6//!
7//! fn main() {
8//!   let start = Instant::now();
9//!
10//!   let display_infos = DisplayInfo::all().unwrap();
11//!   for display_info in display_infos {
12//!     println!("display_info {display_info:?}");
13//!   }
14//!   let display_info = DisplayInfo::from_point(100, 100).unwrap();
15//!   println!("display_info {display_info:?}");
16//!   println!("运行耗时: {:?}", start.elapsed());
17//! }
18//! ```
19
20use anyhow::Result;
21
22#[cfg(target_os = "macos")]
23mod darwin;
24#[cfg(target_os = "macos")]
25use core_graphics::display::CGDisplay;
26#[cfg(target_os = "macos")]
27use darwin::*;
28
29#[cfg(target_os = "windows")]
30mod win32;
31#[cfg(target_os = "windows")]
32use win32::*;
33#[cfg(target_os = "windows")]
34use windows::Win32::Graphics::Gdi::HMONITOR;
35
36#[cfg(target_os = "linux")]
37mod linux;
38#[cfg(target_os = "linux")]
39use linux::*;
40#[cfg(target_os = "linux")]
41use xcb::randr::Output;
42
43#[derive(Debug, Clone, Copy)]
44pub struct DisplayInfo {
45    /// Unique identifier associated with the display.
46    pub id: u32,
47    #[cfg(target_os = "macos")]
48    pub raw_handle: CGDisplay,
49    #[cfg(target_os = "windows")]
50    pub raw_handle: HMONITOR,
51    #[cfg(target_os = "linux")]
52    pub raw_handle: Output,
53    /// The display x coordinate.
54    pub x: i32,
55    /// The display x coordinate.
56    pub y: i32,
57    /// The display pixel width.
58    pub width: u32,
59    /// The display pixel height.
60    pub height: u32,
61    /// Can be 0, 90, 180, 270, represents screen rotation in clock-wise degrees.
62    pub rotation: f32,
63    /// Output device's pixel scale factor.
64    pub scale_factor: f32,
65    /// The display refresh rate.
66    pub frequency: f32,
67    /// Whether the screen is the main screen
68    pub is_primary: bool,
69}
70
71impl DisplayInfo {
72    pub fn all() -> Result<Vec<DisplayInfo>> {
73        get_all()
74    }
75
76    pub fn from_point(x: i32, y: i32) -> Result<DisplayInfo> {
77        get_from_point(x, y)
78    }
79}