1use std::fmt;
4
5use glib::translate::*;
6
7use crate::{ffi, Rect, Vec2};
8
9impl Rect {
10 #[doc(alias = "graphene_rect_get_vertices")]
19 #[doc(alias = "get_vertices")]
20 pub fn vertices(&self) -> &[Vec2; 4] {
21 unsafe {
22 let mut out: [ffi::graphene_vec2_t; 4] = std::mem::zeroed();
23 ffi::graphene_rect_get_vertices(self.to_glib_none().0, &mut out as *mut _);
24 &*(&out as *const [ffi::graphene_vec2_t; 4] as *const [Vec2; 4])
25 }
26 }
27
28 #[doc(alias = "graphene_rect_init")]
45 pub fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
46 assert_initialized_main_thread!();
47 unsafe {
48 let mut rect = Self::uninitialized();
49 ffi::graphene_rect_init(rect.to_glib_none_mut().0, x, y, width, height);
50 rect
51 }
52 }
53}
54
55impl fmt::Debug for Rect {
56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57 f.debug_struct("Rect")
58 .field("x", &self.x())
59 .field("y", &self.y())
60 .field("width", &self.width())
61 .field("height", &self.height())
62 .finish()
63 }
64}
65
66impl Default for Rect {
67 fn default() -> Self {
68 Self::zero()
69 }
70}
71
72#[cfg(test)]
73mod tests {
74 use super::*;
75 use crate::Point;
76
77 #[test]
78 fn contains_point() {
79 let rect = Rect::new(100., 100., 100., 100.);
80
81 let right = Point::new(250., 150.);
82 let below = Point::new(150., 50.);
83 let left = Point::new(50., 150.);
84 let above = Point::new(150., 250.);
85
86 assert!(!rect.contains_point(&right));
87 assert!(!rect.contains_point(&below));
88 assert!(!rect.contains_point(&left));
89 assert!(!rect.contains_point(&above));
90 }
91}