Skip to main content

gsk4/auto/
render_node.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{RenderNodeType, ffi};
6use glib::{prelude::*, translate::*};
7
8glib::wrapper! {
9    /// The basic block in a scene graph to be rendered using [`Renderer`][crate::Renderer].
10    ///
11    /// Each node has a parent, except the top-level node; each node may have
12    /// children nodes.
13    ///
14    /// Each node has an associated drawing surface, which has the size of
15    /// the rectangle set when creating it.
16    ///
17    /// Render nodes are meant to be transient; once they have been associated
18    /// to a [`Renderer`][crate::Renderer] it's safe to release any reference you have on
19    /// them. All [`RenderNode`][crate::RenderNode]s are immutable, you can only specify their
20    /// properties during construction.
21    ///
22    /// This is an Abstract Base Class, you cannot instantiate it.
23    #[doc(alias = "GskRenderNode")]
24    pub struct RenderNode(Shared<ffi::GskRenderNode>);
25
26    match fn {
27        ref => |ptr| ffi::gsk_render_node_ref(ptr),
28        unref => |ptr| ffi::gsk_render_node_unref(ptr),
29    }
30}
31
32impl StaticType for RenderNode {
33    fn static_type() -> glib::Type {
34        unsafe { from_glib(ffi::gsk_render_node_get_type()) }
35    }
36}
37
38impl RenderNode {
39    pub const NONE: Option<&'static RenderNode> = None;
40
41    /// Draws the contents of a render node on a cairo context.
42    ///
43    /// Typically, you'll use this function to implement fallback rendering
44    /// of render nodes on an intermediate Cairo context, instead of using
45    /// the drawing context associated to a [`gdk::Surface`][crate::gdk::Surface]'s rendering buffer.
46    ///
47    /// For advanced nodes that cannot be supported using Cairo, in particular
48    /// for nodes doing 3D operations, this function may fail.
49    /// ## `cr`
50    /// cairo context to draw to
51    #[doc(alias = "gsk_render_node_draw")]
52    pub fn draw(&self, cr: &cairo::Context) {
53        unsafe {
54            ffi::gsk_render_node_draw(
55                self.as_ref().to_glib_none().0,
56                mut_override(cr.to_glib_none().0),
57            );
58        }
59    }
60
61    /// Retrieves the boundaries of the @self.
62    ///
63    /// The node will not draw outside of its boundaries.
64    ///
65    /// # Returns
66    ///
67    ///
68    /// ## `bounds`
69    /// return location for the boundaries
70    #[doc(alias = "gsk_render_node_get_bounds")]
71    #[doc(alias = "get_bounds")]
72    pub fn bounds(&self) -> graphene::Rect {
73        unsafe {
74            let mut bounds = graphene::Rect::uninitialized();
75            ffi::gsk_render_node_get_bounds(
76                self.as_ref().to_glib_none().0,
77                bounds.to_glib_none_mut().0,
78            );
79            bounds
80        }
81    }
82
83    /// Gets a list of all children nodes of the rendernode.
84    ///
85    /// Keep in mind that for various rendernodes, their children have different
86    /// semantics, like the mask vs the source of a mask node. If you care about
87    /// thse semantics, don't use this function, use the specific getters instead.
88    ///
89    /// # Returns
90    ///
91    /// The children
92    #[cfg(feature = "v4_22")]
93    #[cfg_attr(docsrs, doc(cfg(feature = "v4_22")))]
94    #[doc(alias = "gsk_render_node_get_children")]
95    #[doc(alias = "get_children")]
96    pub fn children(&self) -> Vec<RenderNode> {
97        unsafe {
98            let mut n_children = std::mem::MaybeUninit::uninit();
99            let ret = FromGlibContainer::from_glib_none_num(
100                ffi::gsk_render_node_get_children(
101                    self.as_ref().to_glib_none().0,
102                    n_children.as_mut_ptr(),
103                ),
104                n_children.assume_init() as _,
105            );
106            ret
107        }
108    }
109
110    /// Returns the type of the render node.
111    ///
112    /// # Returns
113    ///
114    /// the type of @self
115    #[doc(alias = "gsk_render_node_get_node_type")]
116    #[doc(alias = "get_node_type")]
117    pub fn node_type(&self) -> RenderNodeType {
118        unsafe {
119            from_glib(ffi::gsk_render_node_get_node_type(const_override(
120                self.as_ref().to_glib_none().0,
121            )))
122        }
123    }
124
125    /// Gets an opaque rectangle inside the node that GTK can determine to
126    /// be fully opaque.
127    ///
128    /// There is no guarantee that this is indeed the largest opaque rectangle or
129    /// that regions outside the rectangle are not opaque. This function is a best
130    /// effort with that goal.
131    ///
132    /// The rectangle will be fully contained in the bounds of the node.
133    ///
134    /// # Returns
135    ///
136    /// true if part or all of the rendernode is opaque, false if no
137    ///   opaque region could be found.
138    ///
139    /// ## `out_opaque`
140    /// return location for the opaque rect
141    #[cfg(feature = "v4_16")]
142    #[cfg_attr(docsrs, doc(cfg(feature = "v4_16")))]
143    #[doc(alias = "gsk_render_node_get_opaque_rect")]
144    #[doc(alias = "get_opaque_rect")]
145    pub fn opaque_rect(&self) -> Option<graphene::Rect> {
146        unsafe {
147            let mut out_opaque = graphene::Rect::uninitialized();
148            let ret = from_glib(ffi::gsk_render_node_get_opaque_rect(
149                self.as_ref().to_glib_none().0,
150                out_opaque.to_glib_none_mut().0,
151            ));
152            if ret { Some(out_opaque) } else { None }
153        }
154    }
155
156    /// Serializes the @self for later deserialization via
157    /// gsk_render_node_deserialize(). No guarantees are made about the format
158    /// used other than that the same version of GTK will be able to deserialize
159    /// the result of a call to gsk_render_node_serialize() and
160    /// gsk_render_node_deserialize() will correctly reject files it cannot open
161    /// that were created with previous versions of GTK.
162    ///
163    /// The intended use of this functions is testing, benchmarking and debugging.
164    /// The format is not meant as a permanent storage format.
165    ///
166    /// # Returns
167    ///
168    /// a `GBytes` representing the node.
169    #[doc(alias = "gsk_render_node_serialize")]
170    pub fn serialize(&self) -> glib::Bytes {
171        unsafe {
172            from_glib_full(ffi::gsk_render_node_serialize(
173                self.as_ref().to_glib_none().0,
174            ))
175        }
176    }
177
178    /// This function is equivalent to calling [`serialize()`][Self::serialize()]
179    /// followed by `file_set_contents()`.
180    ///
181    /// See those two functions for details on the arguments.
182    ///
183    /// It is mostly intended for use inside a debugger to quickly dump a render
184    /// node to a file for later inspection.
185    /// ## `filename`
186    /// the file to save it to
187    ///
188    /// # Returns
189    ///
190    /// true if saving was successful
191    #[doc(alias = "gsk_render_node_write_to_file")]
192    pub fn write_to_file(&self, filename: impl AsRef<std::path::Path>) -> Result<(), glib::Error> {
193        unsafe {
194            let mut error = std::ptr::null_mut();
195            let is_ok = ffi::gsk_render_node_write_to_file(
196                self.as_ref().to_glib_none().0,
197                filename.as_ref().to_glib_none().0,
198                &mut error,
199            );
200            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
201            if error.is_null() {
202                Ok(())
203            } else {
204                Err(from_glib_full(error))
205            }
206        }
207    }
208}