gtk4/
widget.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Take a look at the license at the top of the repository in the LICENSE file.

use glib::{translate::*, ControlFlow, WeakRef};

use crate::{ffi, prelude::*, Widget};

mod sealed {
    pub trait Sealed {}
    impl<T: super::IsA<super::Widget>> Sealed for T {}
}

// rustdoc-stripper-ignore-next
/// Trait containing manually implemented methods of [`Widget`](crate::Widget).
pub trait WidgetExtManual: sealed::Sealed + IsA<Widget> + 'static {
    /// Queues an animation frame update and adds a callback to be called
    /// before each frame.
    ///
    /// Until the tick callback is removed, it will be called frequently
    /// (usually at the frame rate of the output device or as quickly as
    /// the application can be repainted, whichever is slower). For this
    /// reason, is most suitable for handling graphics that change every
    /// frame or every few frames.
    ///
    /// The tick callback does not automatically imply a relayout or repaint.
    /// If you want a repaint or relayout, and aren’t changing widget properties
    /// that would trigger that (for example, changing the text of a label),
    /// then you will have to call [`WidgetExt::queue_resize()`][crate::prelude::WidgetExt::queue_resize()] or
    /// [`WidgetExt::queue_draw()`][crate::prelude::WidgetExt::queue_draw()] yourself.
    ///
    /// [`FrameClock::frame_time()`][crate::gdk::FrameClock::frame_time()] should generally be used
    /// for timing continuous animations and
    /// `Gdk::FrameTimings::get_predicted_presentation_time()` should be
    /// used if you are trying to display isolated frames at particular times.
    ///
    /// This is a more convenient alternative to connecting directly to the
    /// [`update`][struct@crate::gdk::FrameClock#update] signal of the frame clock, since you
    /// don't have to worry about when a frame clock is assigned to a widget.
    ///
    /// To remove a tick callback, pass the ID that is returned by this function
    /// to [`WidgetExtManual::remove()`][crate::prelude::WidgetExtManual::remove()].
    /// ## `callback`
    /// function
    ///   to call for updating animations
    ///
    /// # Returns
    ///
    /// an ID for this callback
    #[doc(alias = "gtk_widget_add_tick_callback")]
    fn add_tick_callback<P: Fn(&Self, &gdk::FrameClock) -> ControlFlow + 'static>(
        &self,
        callback: P,
    ) -> TickCallbackId {
        let callback_data: Box<P> = Box::new(callback);

        unsafe extern "C" fn callback_func<
            O: IsA<Widget>,
            P: Fn(&O, &gdk::FrameClock) -> ControlFlow + 'static,
        >(
            widget: *mut ffi::GtkWidget,
            frame_clock: *mut gdk::ffi::GdkFrameClock,
            user_data: glib::ffi::gpointer,
        ) -> glib::ffi::gboolean {
            let widget: Borrowed<Widget> = from_glib_borrow(widget);
            let frame_clock = from_glib_borrow(frame_clock);
            let callback: &P = &*(user_data as *mut _);
            let res = (*callback)(widget.unsafe_cast_ref(), &frame_clock);
            res.into_glib()
        }
        let callback = Some(callback_func::<Self, P> as _);

        unsafe extern "C" fn notify_func<
            O: IsA<Widget>,
            P: Fn(&O, &gdk::FrameClock) -> ControlFlow + 'static,
        >(
            data: glib::ffi::gpointer,
        ) {
            let _callback: Box<P> = Box::from_raw(data as *mut _);
        }
        let destroy_call = Some(notify_func::<Self, P> as _);

        let id = unsafe {
            ffi::gtk_widget_add_tick_callback(
                self.as_ref().to_glib_none().0,
                callback,
                Box::into_raw(callback_data) as *mut _,
                destroy_call,
            )
        };
        TickCallbackId {
            id,
            widget: self.upcast_ref().downgrade(),
        }
    }
}

impl<O: IsA<Widget>> WidgetExtManual for O {}

#[derive(Debug)]
pub struct TickCallbackId {
    id: u32,
    widget: WeakRef<Widget>,
}

impl PartialEq for TickCallbackId {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl TickCallbackId {
    /// Removes a tick callback previously registered with
    /// [`WidgetExtManual::add_tick_callback()`][crate::prelude::WidgetExtManual::add_tick_callback()].
    /// ## `id`
    /// an ID returned by [`WidgetExtManual::add_tick_callback()`][crate::prelude::WidgetExtManual::add_tick_callback()]
    #[doc(alias = "gtk_widget_remove_tick_callback")]
    #[doc(alias = "remove_tick_callback")]
    pub fn remove(self) {
        if let Some(widget) = self.widget.upgrade() {
            unsafe {
                ffi::gtk_widget_remove_tick_callback(widget.to_glib_none().0, self.id);
            }
        }
    }
}