Skip to main content

gtk4/subclass/
symbolic_paintable.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3// rustdoc-stripper-ignore-next
4//! Traits intended for implementing the [`SymbolicPaintable`] interface.
5
6use glib::translate::*;
7
8use crate::{SymbolicPaintable, ffi, prelude::*, subclass::prelude::*};
9
10pub trait SymbolicPaintableImpl:
11    PaintableImpl + ObjectSubclass<Type: IsA<SymbolicPaintable>>
12{
13    fn snapshot_symbolic(
14        &self,
15        snapshot: &gdk::Snapshot,
16        width: f64,
17        height: f64,
18        colors: &[gdk::RGBA],
19    ) {
20        self.parent_snapshot_symbolic(snapshot, width, height, colors)
21    }
22
23    #[cfg(feature = "v4_22")]
24    #[cfg_attr(docsrs, doc(cfg(feature = "v4_22")))]
25    fn snapshot_with_weight(
26        &self,
27        snapshot: &gdk::Snapshot,
28        width: f64,
29        height: f64,
30        colors: &[gdk::RGBA],
31        weight: f64,
32    ) {
33        self.parent_snapshot_with_weight(snapshot, width, height, colors, weight)
34    }
35}
36
37pub trait SymbolicPaintableImplExt: SymbolicPaintableImpl {
38    fn parent_snapshot_symbolic(
39        &self,
40        snapshot: &gdk::Snapshot,
41        width: f64,
42        height: f64,
43        colors: &[gdk::RGBA],
44    ) {
45        unsafe {
46            let type_data = Self::type_data();
47            let parent_iface = type_data.as_ref().parent_interface::<SymbolicPaintable>()
48                as *const ffi::GtkSymbolicPaintableInterface;
49
50            let func = (*parent_iface).snapshot_symbolic.unwrap();
51            func(
52                self.obj()
53                    .unsafe_cast_ref::<SymbolicPaintable>()
54                    .to_glib_none()
55                    .0,
56                snapshot.to_glib_none().0,
57                width,
58                height,
59                colors.as_ptr() as *const gdk::ffi::GdkRGBA,
60                colors.len() as _,
61            )
62        }
63    }
64
65    #[cfg(feature = "v4_22")]
66    #[cfg_attr(docsrs, doc(cfg(feature = "v4_22")))]
67    fn parent_snapshot_with_weight(
68        &self,
69        snapshot: &gdk::Snapshot,
70        width: f64,
71        height: f64,
72        colors: &[gdk::RGBA],
73        weight: f64,
74    ) {
75        unsafe {
76            let type_data = Self::type_data();
77            let parent_iface = type_data.as_ref().parent_interface::<SymbolicPaintable>()
78                as *const ffi::GtkSymbolicPaintableInterface;
79
80            let func = (*parent_iface).snapshot_with_weight.unwrap();
81            func(
82                self.obj()
83                    .unsafe_cast_ref::<SymbolicPaintable>()
84                    .to_glib_none()
85                    .0,
86                snapshot.to_glib_none().0,
87                width,
88                height,
89                colors.as_ptr() as *const gdk::ffi::GdkRGBA,
90                colors.len() as _,
91                weight,
92            )
93        }
94    }
95}
96
97impl<T: SymbolicPaintableImpl> SymbolicPaintableImplExt for T {}
98
99unsafe impl<T: SymbolicPaintableImpl> IsImplementable<T> for SymbolicPaintable {
100    fn interface_init(iface: &mut glib::Interface<Self>) {
101        let iface = iface.as_mut();
102
103        assert_initialized_main_thread!();
104
105        iface.snapshot_symbolic = Some(symbolic_paintable_snapshot_symbolic::<T>);
106        #[cfg(feature = "v4_22")]
107        {
108            iface.snapshot_with_weight = Some(symbolic_paintable_snapshot_with_weight::<T>);
109        }
110    }
111}
112
113unsafe extern "C" fn symbolic_paintable_snapshot_symbolic<T: SymbolicPaintableImpl>(
114    paintable: *mut ffi::GtkSymbolicPaintable,
115    snapshotptr: *mut gdk::ffi::GdkSnapshot,
116    width: f64,
117    height: f64,
118    colors: *const gdk::ffi::GdkRGBA,
119    n_colors: usize,
120) {
121    unsafe {
122        let instance = &*(paintable as *mut T::Instance);
123        let imp = instance.imp();
124
125        let snapshot: Borrowed<gdk::Snapshot> = from_glib_borrow(snapshotptr);
126
127        imp.snapshot_symbolic(
128            &snapshot,
129            width,
130            height,
131            if n_colors == 0 {
132                &[]
133            } else {
134                std::slice::from_raw_parts(colors as *const gdk::RGBA, n_colors)
135            },
136        )
137    }
138}
139
140#[cfg(feature = "v4_22")]
141unsafe extern "C" fn symbolic_paintable_snapshot_with_weight<T: SymbolicPaintableImpl>(
142    paintable: *mut ffi::GtkSymbolicPaintable,
143    snapshotptr: *mut gdk::ffi::GdkSnapshot,
144    width: f64,
145    height: f64,
146    colors: *const gdk::ffi::GdkRGBA,
147    n_colors: usize,
148    weight: f64,
149) {
150    unsafe {
151        let instance = &*(paintable as *mut T::Instance);
152        let imp = instance.imp();
153
154        let snapshot: Borrowed<gdk::Snapshot> = from_glib_borrow(snapshotptr);
155
156        imp.snapshot_with_weight(
157            &snapshot,
158            width,
159            height,
160            if n_colors == 0 {
161                &[]
162            } else {
163                std::slice::from_raw_parts(colors as *const gdk::RGBA, n_colors)
164            },
165            weight,
166        )
167    }
168}