1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Take a look at the license at the top of the repository in the LICENSE file.

use std::mem;

use glib::translate::*;
use graphene::{Point, Rect, Size};

glib::wrapper! {
    /// A rectangular region with rounded corners.
    ///
    /// Application code should normalize rectangles using
    /// [`normalize()`][Self::normalize()]; this function will ensure that
    /// the bounds of the rectangle are normalized and ensure that the corner
    /// values are positive and the corners do not overlap.
    ///
    /// All functions taking a [`RoundedRect`][crate::RoundedRect] as an argument will internally
    /// operate on a normalized copy; all functions returning a [`RoundedRect`][crate::RoundedRect]
    /// will always return a normalized one.
    ///
    /// The algorithm used for normalizing corner sizes is described in
    /// [the CSS specification](https://drafts.csswg.org/css-backgrounds-3/#border-radius).
    #[doc(alias = "GskRoundedRect")]
    pub struct RoundedRect(BoxedInline<ffi::GskRoundedRect>);
}

impl RoundedRect {
    #[doc(alias = "gsk_rounded_rect_init")]
    pub fn new(
        bounds: Rect,
        top_left: Size,
        top_right: Size,
        bottom_right: Size,
        bottom_left: Size,
    ) -> Self {
        assert_initialized_main_thread!();
        unsafe {
            let mut rounded_rect = mem::MaybeUninit::uninit();
            ffi::gsk_rounded_rect_init(
                rounded_rect.as_mut_ptr(),
                bounds.to_glib_none().0,
                top_left.to_glib_none().0,
                top_right.to_glib_none().0,
                bottom_right.to_glib_none().0,
                bottom_left.to_glib_none().0,
            );
            Self::unsafe_from(rounded_rect.assume_init())
        }
    }

    #[doc(alias = "gsk_rounded_rect_init_from_rect")]
    #[doc(alias = "init_from_rect")]
    pub fn from_rect(bounds: Rect, radius: f32) -> Self {
        assert_initialized_main_thread!();
        unsafe {
            let mut rounded_rect = mem::MaybeUninit::uninit();
            ffi::gsk_rounded_rect_init_from_rect(
                rounded_rect.as_mut_ptr(),
                bounds.to_glib_none().0,
                radius,
            );
            Self::unsafe_from(rounded_rect.assume_init())
        }
    }

    /// Normalizes the passed rectangle.
    ///
    /// This function will ensure that the bounds of the rectangle
    /// are normalized and ensure that the corner values are positive
    /// and the corners do not overlap.
    ///
    /// # Returns
    ///
    /// the normalized rectangle
    #[doc(alias = "gsk_rounded_rect_normalize")]
    pub fn normalize(&mut self) {
        unsafe {
            ffi::gsk_rounded_rect_normalize(&mut self.inner);
        }
    }

    /// Offsets the bound's origin by @dx and @dy.
    ///
    /// The size and corners of the rectangle are unchanged.
    /// ## `dx`
    /// the horizontal offset
    /// ## `dy`
    /// the vertical offset
    ///
    /// # Returns
    ///
    /// the offset rectangle
    #[doc(alias = "gsk_rounded_rect_offset")]
    pub fn offset(&mut self, dx: f32, dy: f32) {
        unsafe {
            ffi::gsk_rounded_rect_offset(&mut self.inner, dx, dy);
        }
    }

    /// Shrinks (or grows) the given rectangle by moving the 4 sides
    /// according to the offsets given.
    ///
    /// The corner radii will be changed in a way that tries to keep
    /// the center of the corner circle intact. This emulates CSS behavior.
    ///
    /// This function also works for growing rectangles if you pass
    /// negative values for the @top, @right, @bottom or @left.
    /// ## `top`
    /// How far to move the top side downwards
    /// ## `right`
    /// How far to move the right side to the left
    /// ## `bottom`
    /// How far to move the bottom side upwards
    /// ## `left`
    /// How far to move the left side to the right
    ///
    /// # Returns
    ///
    /// the resized [`RoundedRect`][crate::RoundedRect]
    #[doc(alias = "gsk_rounded_rect_shrink")]
    pub fn shrink(&mut self, top: f32, right: f32, bottom: f32, left: f32) {
        unsafe {
            ffi::gsk_rounded_rect_shrink(&mut self.inner, top, right, bottom, left);
        }
    }

    /// Checks if all corners of @self are right angles and the
    /// rectangle covers all of its bounds.
    ///
    /// This information can be used to decide if [`ClipNode::new()`][crate::ClipNode::new()]
    /// or [`RoundedClipNode::new()`][crate::RoundedClipNode::new()] should be called.
    ///
    /// # Returns
    ///
    /// [`true`] if the rectangle is rectilinear
    #[doc(alias = "gsk_rounded_rect_is_rectilinear")]
    pub fn is_rectilinear(&self) -> bool {
        unsafe { from_glib(ffi::gsk_rounded_rect_is_rectilinear(&self.inner)) }
    }

    /// Checks if the given @point is inside the rounded rectangle.
    /// ## `point`
    /// the point to check
    ///
    /// # Returns
    ///
    /// [`true`] if the @point is inside the rounded rectangle
    #[doc(alias = "gsk_rounded_rect_contains_point")]
    pub fn contains_point(&self, point: Point) -> bool {
        unsafe {
            from_glib(ffi::gsk_rounded_rect_contains_point(
                &self.inner,
                point.to_glib_none().0,
            ))
        }
    }

    /// Checks if the given @rect is contained inside the rounded rectangle.
    /// ## `rect`
    /// the rectangle to check
    ///
    /// # Returns
    ///
    /// [`true`] if the @rect is fully contained inside the rounded rectangle
    #[doc(alias = "gsk_rounded_rect_contains_rect")]
    pub fn contains_rect(&self, rect: Rect) -> bool {
        unsafe {
            from_glib(ffi::gsk_rounded_rect_contains_rect(
                &self.inner,
                rect.to_glib_none().0,
            ))
        }
    }

    /// Checks if part of the given @rect is contained inside the rounded rectangle.
    /// ## `rect`
    /// the rectangle to check
    ///
    /// # Returns
    ///
    /// [`true`] if the @rect intersects with the rounded rectangle
    #[doc(alias = "gsk_rounded_rect_intersects_rect")]
    pub fn intersects_rect(&self, rect: Rect) -> bool {
        unsafe {
            from_glib(ffi::gsk_rounded_rect_intersects_rect(
                &self.inner,
                rect.to_glib_none().0,
            ))
        }
    }

    #[inline]
    pub fn bounds(&self) -> &graphene::Rect {
        unsafe {
            &*(&self.inner.bounds as *const graphene::ffi::graphene_rect_t as *const graphene::Rect)
        }
    }

    #[inline]
    pub fn corner(&self) -> &[graphene::Size; 4] {
        unsafe {
            &*(&self.inner.corner as *const [graphene::ffi::graphene_size_t; 4]
                as *const [graphene::Size; 4])
        }
    }
}

impl std::fmt::Debug for RoundedRect {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.debug_struct("RoundedRect")
            .field("is_rectilinear", &self.is_rectilinear())
            .field("bounds", &self.bounds())
            .field("corner", &self.corner())
            .finish()
    }
}