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