1use std::mem;
4
5use crate::ffi;
6use glib::translate::*;
7use graphene::{Point, Rect, Size};
8
9glib::wrapper! {
10 #[doc(alias = "GskRoundedRect")]
11 pub struct RoundedRect(BoxedInline<ffi::GskRoundedRect>);
12}
13
14impl RoundedRect {
15 #[doc(alias = "gsk_rounded_rect_init")]
16 pub fn new(
17 bounds: Rect,
18 top_left: Size,
19 top_right: Size,
20 bottom_right: Size,
21 bottom_left: Size,
22 ) -> Self {
23 assert_initialized_main_thread!();
24 unsafe {
25 let mut rounded_rect = mem::MaybeUninit::uninit();
26 ffi::gsk_rounded_rect_init(
27 rounded_rect.as_mut_ptr(),
28 bounds.to_glib_none().0,
29 top_left.to_glib_none().0,
30 top_right.to_glib_none().0,
31 bottom_right.to_glib_none().0,
32 bottom_left.to_glib_none().0,
33 );
34 Self::unsafe_from(rounded_rect.assume_init())
35 }
36 }
37
38 #[doc(alias = "gsk_rounded_rect_init_from_rect")]
39 #[doc(alias = "init_from_rect")]
40 pub fn from_rect(bounds: Rect, radius: f32) -> Self {
41 assert_initialized_main_thread!();
42 unsafe {
43 let mut rounded_rect = mem::MaybeUninit::uninit();
44 ffi::gsk_rounded_rect_init_from_rect(
45 rounded_rect.as_mut_ptr(),
46 bounds.to_glib_none().0,
47 radius,
48 );
49 Self::unsafe_from(rounded_rect.assume_init())
50 }
51 }
52
53 #[doc(alias = "gsk_rounded_rect_normalize")]
54 pub fn normalize(&mut self) {
55 unsafe {
56 ffi::gsk_rounded_rect_normalize(&mut self.inner);
57 }
58 }
59
60 #[doc(alias = "gsk_rounded_rect_offset")]
61 pub fn offset(&mut self, dx: f32, dy: f32) {
62 unsafe {
63 ffi::gsk_rounded_rect_offset(&mut self.inner, dx, dy);
64 }
65 }
66
67 #[doc(alias = "gsk_rounded_rect_shrink")]
68 pub fn shrink(&mut self, top: f32, right: f32, bottom: f32, left: f32) {
69 unsafe {
70 ffi::gsk_rounded_rect_shrink(&mut self.inner, top, right, bottom, left);
71 }
72 }
73
74 #[doc(alias = "gsk_rounded_rect_is_rectilinear")]
75 pub fn is_rectilinear(&self) -> bool {
76 unsafe { from_glib(ffi::gsk_rounded_rect_is_rectilinear(&self.inner)) }
77 }
78
79 #[doc(alias = "gsk_rounded_rect_contains_point")]
80 pub fn contains_point(&self, point: Point) -> bool {
81 unsafe {
82 from_glib(ffi::gsk_rounded_rect_contains_point(
83 &self.inner,
84 point.to_glib_none().0,
85 ))
86 }
87 }
88
89 #[doc(alias = "gsk_rounded_rect_contains_rect")]
90 pub fn contains_rect(&self, rect: Rect) -> bool {
91 unsafe {
92 from_glib(ffi::gsk_rounded_rect_contains_rect(
93 &self.inner,
94 rect.to_glib_none().0,
95 ))
96 }
97 }
98
99 #[doc(alias = "gsk_rounded_rect_intersects_rect")]
100 pub fn intersects_rect(&self, rect: Rect) -> bool {
101 unsafe {
102 from_glib(ffi::gsk_rounded_rect_intersects_rect(
103 &self.inner,
104 rect.to_glib_none().0,
105 ))
106 }
107 }
108
109 #[inline]
110 pub fn bounds(&self) -> &graphene::Rect {
111 unsafe {
112 &*(&self.inner.bounds as *const graphene::ffi::graphene_rect_t as *const graphene::Rect)
113 }
114 }
115
116 #[inline]
117 pub fn corner(&self) -> &[graphene::Size; 4] {
118 unsafe {
119 &*(&self.inner.corner as *const [graphene::ffi::graphene_size_t; 4]
120 as *const [graphene::Size; 4])
121 }
122 }
123}
124
125impl std::fmt::Debug for RoundedRect {
126 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
127 f.debug_struct("RoundedRect")
128 .field("is_rectilinear", &self.is_rectilinear())
129 .field("bounds", &self.bounds())
130 .field("corner", &self.corner())
131 .finish()
132 }
133}