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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
// Take a look at the license at the top of the repository in the LICENSE file.
use glib::translate::*;
use graphene::{Point, Rect, Size};
use std::mem;
/// 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).
#[derive(Clone, Debug)]
#[doc(alias = "GskRoundedRect")]
pub struct RoundedRect(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(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(rounded_rect.assume_init())
}
}
/// Initializes the given [`RoundedRect`][crate::RoundedRect] with the given values.
///
/// This function will implicitly normalize the [`RoundedRect`][crate::RoundedRect]
/// before returning.
/// ## `bounds`
/// a `graphene_rect_t` describing the bounds
/// ## `top_left`
/// the rounding radius of the top left corner
/// ## `top_right`
/// the rounding radius of the top right corner
/// ## `bottom_right`
/// the rounding radius of the bottom right corner
/// ## `bottom_left`
/// the rounding radius of the bottom left corner
///
/// # Returns
///
/// the initialized rectangle
#[doc(alias = "gsk_rounded_rect_init")]
pub fn init(
&mut self,
bounds: Rect,
top_left: Size,
top_right: Size,
bottom_right: Size,
bottom_left: Size,
) {
unsafe {
ffi::gsk_rounded_rect_init(
&mut self.0,
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,
);
}
}
/// Initializes `self` to the given `bounds` and sets the radius
/// of all four corners to `radius`.
/// ## `bounds`
/// a `graphene_rect_t`
/// ## `radius`
/// the border radius
///
/// # Returns
///
/// the initialized rectangle
#[doc(alias = "gsk_rounded_rect_init_from_rect")]
pub fn init_from_rect(&mut self, bounds: Rect, radius: f32) {
unsafe {
ffi::gsk_rounded_rect_init_from_rect(&mut self.0, bounds.to_glib_none().0, radius);
}
}
/// 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.0);
}
}
/// 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.0, 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.0, 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.0)) }
}
/// 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.0,
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.0,
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.0,
rect.to_glib_none().0,
))
}
}
}
#[doc(hidden)]
impl FromGlibPtrNone<*const ffi::GskRoundedRect> for RoundedRect {
unsafe fn from_glib_none(ptr: *const ffi::GskRoundedRect) -> Self {
Self(*ptr)
}
}
#[doc(hidden)]
impl<'a> ToGlibPtr<'a, *const ffi::GskRoundedRect> for RoundedRect {
type Storage = &'a Self;
fn to_glib_none(&'a self) -> Stash<*const ffi::GskRoundedRect, Self> {
Stash(&self.0, self)
}
}