Skip to main content

gtk4/
snapshot.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::borrow::Borrow;
4
5use glib::translate::*;
6
7use crate::{Snapshot, ffi, prelude::*};
8
9pub trait SnapshotExtManual: IsA<Snapshot> + 'static {
10    /// Appends a stroked border rectangle inside the given @outline.
11    ///
12    /// The four sides of the border can have different widths and colors.
13    /// ## `outline`
14    /// the outline of the border
15    /// ## `border_width`
16    /// the stroke width of the border on
17    ///   the top, right, bottom and left side respectively.
18    /// ## `border_color`
19    /// the color used on the top, right,
20    ///   bottom and left side.
21    #[doc(alias = "gtk_snapshot_append_border")]
22    fn append_border(
23        &self,
24        outline: &gsk::RoundedRect,
25        border_width: &[f32; 4],
26        border_color: &[gdk::RGBA; 4],
27    ) {
28        unsafe {
29            let border_color_ptr: Vec<gdk::ffi::GdkRGBA> =
30                border_color.iter().map(|c| *c.to_glib_none().0).collect();
31            ffi::gtk_snapshot_append_border(
32                self.as_ref().to_glib_none().0,
33                outline.to_glib_none().0,
34                border_width,
35                border_color_ptr.as_ptr() as *const _,
36            )
37        }
38    }
39
40    /// Sets the snapping mode to use when appending snappable content
41    /// to the snapshot.
42    ///
43    /// The snap mode is part of the current state, so [`SnapshotExt::save()`][crate::prelude::SnapshotExt::save()]
44    /// and [`SnapshotExt::restore()`][crate::prelude::SnapshotExt::restore()] can be used to remember a snap mode.
45    /// ## `snap`
46    /// the snapping mode to use
47    #[cfg(feature = "v4_24")]
48    #[cfg_attr(docsrs, doc(cfg(feature = "v4_24")))]
49    #[doc(alias = "gtk_snapshot_set_snap")]
50    fn set_snap(&self, snap: gsk::RectSnap) {
51        unsafe {
52            ffi::gtk_snapshot_set_snap(self.as_ref().to_glib_none().0, snap.into_glib());
53        }
54    }
55
56    /// Inserts a debug node with a message.
57    ///
58    /// Debug nodes don't affect the rendering at all, but can be
59    /// helpful in identifying parts of a render node tree dump,
60    /// for example in the GTK inspector.
61    /// ## `message`
62    /// a printf-style format string
63    #[doc(alias = "gtk_snapshot_push_debug")]
64    fn push_debug(&self, message: impl IntoGStr) {
65        unsafe {
66            message.run_with_gstr(|message| {
67                ffi::gtk_snapshot_push_debug(self.as_ref().to_glib_none().0, message.as_ptr())
68            })
69        }
70    }
71}
72
73impl<O: IsA<Snapshot>> SnapshotExtManual for O {}
74
75impl AsRef<Snapshot> for gdk::Snapshot {
76    #[inline]
77    fn as_ref(&self) -> &Snapshot {
78        self.downcast_ref().unwrap()
79    }
80}
81
82impl From<gdk::Snapshot> for Snapshot {
83    #[inline]
84    fn from(e: gdk::Snapshot) -> Snapshot {
85        assert_initialized_main_thread!();
86        e.downcast().unwrap()
87    }
88}
89
90impl Borrow<Snapshot> for gdk::Snapshot {
91    #[inline]
92    fn borrow(&self) -> &Snapshot {
93        self.downcast_ref().unwrap()
94    }
95}
96
97unsafe impl IsA<Snapshot> for gdk::Snapshot {}