Skip to main content

gtk4/auto/
expression.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{ExpressionWatch, ffi};
6use glib::{prelude::*, translate::*};
7use std::boxed::Box as Box_;
8
9glib::wrapper! {
10    ///
11    /// ```text
12    ///
13    ///
14    /// This is an Abstract Base Class, you cannot instantiate it.
15    #[doc(alias = "GtkExpression")]
16    pub struct Expression(Shared<ffi::GtkExpression>);
17
18    match fn {
19        ref => |ptr| ffi::gtk_expression_ref(ptr),
20        unref => |ptr| ffi::gtk_expression_unref(ptr),
21    }
22}
23
24impl StaticType for Expression {
25    fn static_type() -> glib::Type {
26        unsafe { from_glib(ffi::gtk_expression_get_type()) }
27    }
28}
29
30impl Expression {
31    pub const NONE: Option<&'static Expression> = None;
32
33    /// Bind `target`'s property named `property` to `self`.
34    ///
35    /// The value that `self` evaluates to is set via `g_object_set()` on
36    /// `target`. This is repeated whenever `self` changes to ensure that
37    /// the object's property stays synchronized with `self`.
38    ///
39    /// If `self`'s evaluation fails, `target`'s `property` is not updated.
40    /// Use a [`TryExpression`][crate::TryExpression] to provide a fallback for this case.
41    ///
42    /// Note that this function takes ownership of `self`. If you want
43    /// to keep it around, you should `Gtk::Expression::ref()` it beforehand.
44    /// ## `target`
45    /// the target object to bind to
46    /// ## `property`
47    /// name of the property on `target` to bind to
48    /// ## `this_`
49    /// the this argument for
50    ///   the evaluation of `self`
51    ///
52    /// # Returns
53    ///
54    /// a [`ExpressionWatch`][crate::ExpressionWatch]
55    #[doc(alias = "gtk_expression_bind")]
56    pub fn bind(
57        &self,
58        target: &impl IsA<glib::Object>,
59        property: &str,
60        this_: Option<&impl IsA<glib::Object>>,
61    ) -> ExpressionWatch {
62        unsafe {
63            from_glib_none(ffi::gtk_expression_bind(
64                self.as_ref().to_glib_full(),
65                target.as_ref().to_glib_none().0,
66                property.to_glib_none().0,
67                this_.map(|p| p.as_ref()).to_glib_none().0,
68            ))
69        }
70    }
71
72    /// Gets the `GType` that this expression evaluates to.
73    ///
74    /// This type is constant and will not change over the lifetime
75    /// of this expression.
76    ///
77    /// # Returns
78    ///
79    /// The type returned from [`evaluate()`][Self::evaluate()]
80    #[doc(alias = "gtk_expression_get_value_type")]
81    #[doc(alias = "get_value_type")]
82    pub fn value_type(&self) -> glib::types::Type {
83        unsafe {
84            from_glib(ffi::gtk_expression_get_value_type(
85                self.as_ref().to_glib_none().0,
86            ))
87        }
88    }
89
90    /// Checks if the expression is static.
91    ///
92    /// A static expression will never change its result when
93    /// [`evaluate()`][Self::evaluate()] is called on it with the same arguments.
94    ///
95    /// That means a call to [`watch()`][Self::watch()] is not necessary because
96    /// it will never trigger a notify.
97    ///
98    /// # Returns
99    ///
100    /// `TRUE` if the expression is static
101    #[doc(alias = "gtk_expression_is_static")]
102    pub fn is_static(&self) -> bool {
103        unsafe {
104            from_glib(ffi::gtk_expression_is_static(
105                self.as_ref().to_glib_none().0,
106            ))
107        }
108    }
109
110    /// Watch the given `expression` for changes.
111    ///
112    /// The @notify function will be called whenever the evaluation of `self`
113    /// may have changed.
114    ///
115    /// GTK cannot guarantee that the evaluation did indeed change when the @notify
116    /// gets invoked, but it guarantees the opposite: When it did in fact change,
117    /// the @notify will be invoked.
118    /// ## `this_`
119    /// the `this` argument to
120    ///   watch
121    /// ## `notify`
122    /// callback to invoke when the expression changes
123    ///
124    /// # Returns
125    ///
126    /// The newly installed watch. Note that the only
127    ///   reference held to the watch will be released when the watch is unwatched
128    ///   which can happen automatically, and not just via
129    ///   [`ExpressionWatch::unwatch()`][crate::ExpressionWatch::unwatch()]. You should call `Gtk::ExpressionWatch::ref()`
130    ///   if you want to keep the watch around.
131    #[doc(alias = "gtk_expression_watch")]
132    pub fn watch<P: Fn() + 'static>(
133        &self,
134        this_: Option<&impl IsA<glib::Object>>,
135        notify: P,
136    ) -> ExpressionWatch {
137        let notify_data: Box_<P> = Box_::new(notify);
138        unsafe extern "C" fn notify_func<P: Fn() + 'static>(user_data: glib::ffi::gpointer) {
139            unsafe {
140                let callback = &*(user_data as *mut P);
141                (*callback)()
142            }
143        }
144        let notify = Some(notify_func::<P> as _);
145        unsafe extern "C" fn user_destroy_func<P: Fn() + 'static>(data: glib::ffi::gpointer) {
146            unsafe {
147                let _callback = Box_::from_raw(data as *mut P);
148            }
149        }
150        let destroy_call4 = Some(user_destroy_func::<P> as _);
151        let super_callback0: Box_<P> = notify_data;
152        unsafe {
153            from_glib_none(ffi::gtk_expression_watch(
154                self.as_ref().to_glib_none().0,
155                this_.map(|p| p.as_ref()).to_glib_none().0,
156                notify,
157                Box_::into_raw(super_callback0) as *mut _,
158                destroy_call4,
159            ))
160        }
161    }
162}