[go: up one dir, main page]

gdk4/
cairo_interaction.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use cairo::{Context, Region};
4use gdk_pixbuf::Pixbuf;
5use glib::translate::*;
6
7use crate::{ffi, Rectangle, Surface, RGBA};
8
9// rustdoc-stripper-ignore-next
10/// Trait containing integration methods with [`cairo::Surface`].
11pub trait GdkCairoSurfaceExt {
12    #[doc(alias = "gdk_cairo_region_create_from_surface")]
13    fn create_region(&self) -> Region;
14}
15
16impl GdkCairoSurfaceExt for cairo::Surface {
17    fn create_region(&self) -> Region {
18        unsafe {
19            from_glib_full(ffi::gdk_cairo_region_create_from_surface(
20                self.to_glib_none().0,
21            ))
22        }
23    }
24}
25
26// rustdoc-stripper-ignore-next
27/// Trait containing integration methods with [`cairo::Context`].
28pub trait GdkCairoContextExt: sealed::Sealed {
29    // rustdoc-stripper-ignore-next
30    /// # Safety
31    ///
32    /// It's the responsibility of the caller to ensure that source
33    /// is a valid GL resource.
34    #[doc(alias = "gdk_cairo_draw_from_gl")]
35    #[allow(clippy::too_many_arguments)]
36    unsafe fn draw_from_gl(
37        &self,
38        surface: &Surface,
39        source: i32,
40        source_type: i32,
41        buffer_scale: i32,
42        x: i32,
43        y: i32,
44        width: i32,
45        height: i32,
46    ) {
47        skip_assert_initialized!();
48        ffi::gdk_cairo_draw_from_gl(
49            self.to_raw(),
50            surface.to_glib_none().0,
51            source,
52            source_type,
53            buffer_scale,
54            x,
55            y,
56            width,
57            height,
58        );
59    }
60
61    #[doc(alias = "gdk_cairo_set_source_rgba")]
62    #[doc(alias = "set_source_rgba")]
63    fn set_source_color(&self, rgba: &RGBA) {
64        unsafe {
65            ffi::gdk_cairo_set_source_rgba(self.to_raw(), rgba.to_glib_none().0);
66        }
67    }
68
69    #[doc(alias = "gdk_cairo_set_source_pixbuf")]
70    fn set_source_pixbuf(&self, pixbuf: &Pixbuf, x: f64, y: f64) {
71        unsafe {
72            ffi::gdk_cairo_set_source_pixbuf(self.to_raw(), pixbuf.to_glib_none().0, x, y);
73        }
74    }
75
76    #[doc(alias = "gdk_cairo_rectangle")]
77    fn add_rectangle(&self, rectangle: &Rectangle) {
78        unsafe {
79            ffi::gdk_cairo_rectangle(self.to_raw(), rectangle.to_glib_none().0);
80        }
81    }
82
83    #[doc(alias = "gdk_cairo_region")]
84    fn add_region(&self, region: &Region) {
85        unsafe {
86            ffi::gdk_cairo_region(self.to_raw(), region.to_glib_none().0);
87        }
88    }
89}
90
91impl GdkCairoContextExt for Context {}
92
93mod sealed {
94    use cairo::{ffi::cairo_t, Context};
95
96    pub trait Sealed {
97        fn to_raw(&self) -> *mut cairo_t;
98    }
99
100    impl Sealed for Context {
101        fn to_raw(&self) -> *mut cairo_t {
102            self.to_raw_none()
103        }
104    }
105}