Skip to content

Commit 76a5f7b

Browse files
committed
feat(CGDisplay): add displays_with_point and displays_with_rect
1 parent 0933af0 commit 76a5f7b

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

core-graphics/src/display.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,85 @@ impl CGDisplay {
141141
CGDisplay::new(kCGNullDirectDisplayID)
142142
}
143143

144+
/// Return the number of online displays with bounds that include the
145+
/// specified point.
146+
pub fn display_count_with_point(point: CGPoint) -> Result<u32, CGError> {
147+
let mut matching_display_count: u32 = 0;
148+
let result = unsafe {
149+
CGGetDisplaysWithPoint(point, 0, ptr::null_mut(), &mut matching_display_count)
150+
};
151+
if result == 0 {
152+
Ok(matching_display_count)
153+
} else {
154+
Err(result)
155+
}
156+
}
157+
158+
/// Return a list of online displays with bounds that include the specified
159+
/// point.
160+
pub fn displays_with_point(
161+
point: CGPoint,
162+
max_displays: u32,
163+
) -> Result<(Vec<CGDirectDisplayID>, u32), CGError> {
164+
let count = CGDisplay::display_count_with_point(point)?;
165+
let count = u32::min(count, max_displays);
166+
167+
let mut matching_display_count: u32 = 0;
168+
let mut displays: Vec<CGDirectDisplayID> = vec![0; count as usize];
169+
let result = unsafe {
170+
CGGetDisplaysWithPoint(
171+
point,
172+
max_displays,
173+
displays.as_mut_ptr(),
174+
&mut matching_display_count,
175+
)
176+
};
177+
178+
if result == 0 {
179+
Ok((displays, matching_display_count))
180+
} else {
181+
Err(result)
182+
}
183+
}
184+
185+
/// Return the number of online displays with bounds that intersect the
186+
/// specified rectangle.
187+
pub fn display_count_with_rect(rect: CGRect) -> Result<u32, CGError> {
188+
let mut matching_display_count: u32 = 0;
189+
let result =
190+
unsafe { CGGetDisplaysWithRect(rect, 0, ptr::null_mut(), &mut matching_display_count) };
191+
if result == 0 {
192+
Ok(matching_display_count)
193+
} else {
194+
Err(result)
195+
}
196+
}
197+
198+
/// Return a list of online displays with bounds that intersect the specified rectangle.
199+
pub fn displays_with_rect(
200+
rect: CGRect,
201+
max_displays: u32,
202+
) -> Result<(Vec<CGDirectDisplayID>, u32), CGError> {
203+
let count = CGDisplay::display_count_with_rect(rect)?;
204+
205+
let mut matching_display_count: u32 = 0;
206+
let mut displays: Vec<CGDirectDisplayID> = vec![0; count as usize];
207+
let result = unsafe {
208+
CGGetDisplaysWithRect(
209+
rect,
210+
max_displays,
211+
displays.as_mut_ptr(),
212+
&mut matching_display_count,
213+
)
214+
};
215+
216+
if result == 0 {
217+
Ok((displays, matching_display_count))
218+
} else {
219+
Err(result)
220+
}
221+
}
222+
144223
/// Returns the bounds of a display in the global display coordinate space.
145224
#[inline]
146225
pub fn bounds(&self) -> CGRect {
@@ -673,6 +752,12 @@ extern "C" {
673752
active_displays: *mut CGDirectDisplayID,
674753
display_count: *mut u32,
675754
) -> CGError;
755+
pub fn CGGetDisplaysWithPoint(
756+
point: CGPoint,
757+
max_displays: u32,
758+
displays: *mut CGDirectDisplayID,
759+
matching_display_count: *mut u32,
760+
) -> CGError;
676761
pub fn CGGetDisplaysWithRect(
677762
rect: CGRect,
678763
max_displays: u32,

0 commit comments

Comments
 (0)