Skip to main content

gsk4/
border_node.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use glib::translate::*;
4
5use crate::{BorderNode, RenderNodeType, RoundedRect, ffi};
6
7define_render_node!(BorderNode, ffi::GskBorderNode, RenderNodeType::BorderNode);
8
9impl BorderNode {
10    /// Creates a [`RenderNode`][crate::RenderNode] that will stroke a border rectangle inside the
11    /// given @outline.
12    ///
13    /// The 4 sides of the border can have different widths and colors.
14    /// ## `outline`
15    /// a [`RoundedRect`][crate::RoundedRect] describing the outline of the border
16    /// ## `border_width`
17    /// the stroke width of the border on
18    ///     the top, right, bottom and left side respectively.
19    /// ## `border_color`
20    /// the color used on the top, right,
21    ///     bottom and left side.
22    ///
23    /// # Returns
24    ///
25    /// A new [`RenderNode`][crate::RenderNode]
26    #[doc(alias = "gsk_border_node_new")]
27    pub fn new(
28        outline: &RoundedRect,
29        border_width: &[f32; 4],
30        border_color: &[gdk::RGBA; 4],
31    ) -> Self {
32        unsafe {
33            from_glib_full(ffi::gsk_border_node_new(
34                outline.to_glib_none().0,
35                border_width.as_ptr() as *const [f32; 4],
36                border_color.as_ptr() as *const [gdk::ffi::GdkRGBA; 4],
37            ))
38        }
39    }
40
41    /// Retrieves the colors of the border.
42    ///
43    /// # Returns
44    ///
45    /// an array of 4 [`gdk::RGBA`][crate::gdk::RGBA]
46    ///   structs for the top, right, bottom and left color of the border
47    #[doc(alias = "gsk_border_node_get_colors")]
48    #[doc(alias = "get_colors")]
49    pub fn colors(&self) -> &[gdk::RGBA; 4] {
50        unsafe {
51            &*(ffi::gsk_border_node_get_colors(self.to_glib_none().0) as *const [gdk::RGBA; 4])
52        }
53    }
54
55    /// Retrieves the stroke widths of the border.
56    ///
57    /// # Returns
58    ///
59    /// an array of 4 floats
60    ///   for the top, right, bottom and left stroke width of the border,
61    ///   respectively
62    #[doc(alias = "gsk_border_node_get_widths")]
63    #[doc(alias = "get_widths")]
64    pub fn widths(&self) -> &[f32; 4] {
65        unsafe { &*ffi::gsk_border_node_get_widths(self.to_glib_none().0) }
66    }
67
68    /// Retrieves the snap value for the border
69    ///
70    /// # Returns
71    ///
72    /// the snap value
73    #[cfg(feature = "v4_24")]
74    #[cfg_attr(docsrs, doc(cfg(feature = "v4_24")))]
75    #[doc(alias = "gsk_border_node_get_border_snap")]
76    #[doc(alias = "get_border_snap")]
77    pub fn border_snap(&self) -> crate::RectSnap {
78        unsafe { from_glib(ffi::gsk_border_node_get_border_snap(self.to_glib_none().0)) }
79    }
80
81    /// Retrieves the snap value for this node
82    ///
83    /// # Returns
84    ///
85    /// the snap value
86    #[cfg(feature = "v4_24")]
87    #[cfg_attr(docsrs, doc(cfg(feature = "v4_24")))]
88    #[doc(alias = "gsk_border_node_get_snap")]
89    #[doc(alias = "get_snap")]
90    pub fn snap(&self) -> crate::RectSnap {
91        unsafe { from_glib(ffi::gsk_border_node_get_snap(self.to_glib_none().0)) }
92    }
93}
94
95impl std::fmt::Debug for BorderNode {
96    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
97        f.debug_struct("BorderNode")
98            .field("colors", &self.colors())
99            .field("widths", &self.widths())
100            .field("outline", &self.outline())
101            .finish()
102    }
103}