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
5//! [`SymbolicPaintable`](crate::SymbolicPaintable) interface.
6
7use glib::translate::*;
8
9use crate::{ffi, prelude::*, subclass::prelude::*, SymbolicPaintable};
10
11pub trait SymbolicPaintableImpl: PaintableImpl {
12    fn snapshot_symbolic(
13        &self,
14        snapshot: &gdk::Snapshot,
15        width: f64,
16        height: f64,
17        colors: &[gdk::RGBA],
18    ) {
19        self.parent_snapshot_symbolic(snapshot, width, height, colors)
20    }
21}
22
23mod sealed {
24    pub trait Sealed {}
25    impl<T: super::SymbolicPaintableImplExt> Sealed for T {}
26}
27
28pub trait SymbolicPaintableImplExt: sealed::Sealed + ObjectSubclass {
29    fn parent_snapshot_symbolic(
30        &self,
31        snapshot: &gdk::Snapshot,
32        width: f64,
33        height: f64,
34        colors: &[gdk::RGBA],
35    ) {
36        unsafe {
37            let type_data = Self::type_data();
38            let parent_iface = type_data.as_ref().parent_interface::<SymbolicPaintable>()
39                as *const ffi::GtkSymbolicPaintableInterface;
40
41            let func = (*parent_iface).snapshot_symbolic.unwrap();
42            func(
43                self.obj()
44                    .unsafe_cast_ref::<SymbolicPaintable>()
45                    .to_glib_none()
46                    .0,
47                snapshot.to_glib_none().0,
48                width,
49                height,
50                colors.to_glib_none().0,
51                colors.len() as _,
52            )
53        }
54    }
55}
56
57impl<T: SymbolicPaintableImpl> SymbolicPaintableImplExt for T {}
58
59unsafe impl<T: SymbolicPaintableImpl> IsImplementable<T> for SymbolicPaintable {
60    fn interface_init(iface: &mut glib::Interface<Self>) {
61        let iface = iface.as_mut();
62
63        assert_initialized_main_thread!();
64
65        iface.snapshot_symbolic = Some(symbolic_paintable_snapshot_symbolic::<T>);
66    }
67}
68
69unsafe extern "C" fn symbolic_paintable_snapshot_symbolic<T: SymbolicPaintableImpl>(
70    paintable: *mut ffi::GtkSymbolicPaintable,
71    snapshotptr: *mut gdk::ffi::GdkSnapshot,
72    width: f64,
73    height: f64,
74    colors: *const gdk::ffi::GdkRGBA,
75    n_colors: usize,
76) {
77    let instance = &*(paintable as *mut T::Instance);
78    let imp = instance.imp();
79
80    let snapshot: Borrowed<gdk::Snapshot> = from_glib_borrow(snapshotptr);
81
82    imp.snapshot_symbolic(
83        &snapshot,
84        width,
85        height,
86        if n_colors == 0 {
87            &[]
88        } else {
89            std::slice::from_raw_parts(colors as *const gdk::RGBA, n_colors)
90        },
91    )
92}