Skip to content

Commit 1a031c4

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

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

core-graphics/src/display.rs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,86 @@ 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::max(u32::min(count, max_displays), 1);
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+
let count = u32::max(u32::min(count, max_displays), 1);
205+
206+
let mut matching_display_count: u32 = 0;
207+
let mut displays: Vec<CGDirectDisplayID> = vec![0; count as usize];
208+
let result = unsafe {
209+
CGGetDisplaysWithRect(
210+
rect,
211+
max_displays,
212+
displays.as_mut_ptr(),
213+
&mut matching_display_count,
214+
)
215+
};
216+
217+
if result == 0 {
218+
Ok((displays, matching_display_count))
219+
} else {
220+
Err(result)
221+
}
222+
}
223+
144224
/// Returns the bounds of a display in the global display coordinate space.
145225
#[inline]
146226
pub fn bounds(&self) -> CGRect {
@@ -673,6 +753,12 @@ extern "C" {
673753
active_displays: *mut CGDirectDisplayID,
674754
display_count: *mut u32,
675755
) -> CGError;
756+
pub fn CGGetDisplaysWithPoint(
757+
point: CGPoint,
758+
max_displays: u32,
759+
displays: *mut CGDirectDisplayID,
760+
matching_display_count: *mut u32,
761+
) -> CGError;
676762
pub fn CGGetDisplaysWithRect(
677763
rect: CGRect,
678764
max_displays: u32,

0 commit comments

Comments
 (0)