gtk4/
expression_watch.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use glib::{translate::*, value::FromValue, Value};
4
5use crate::ffi;
6
7glib::wrapper! {
8    /// An opaque structure representing a watched [`Expression`][crate::Expression].
9    ///
10    /// The contents of [`ExpressionWatch`][crate::ExpressionWatch] should only be accessed through the
11    /// provided API.
12    #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
13    #[doc(alias = "GtkExpressionWatch")]
14    pub struct ExpressionWatch(Shared<ffi::GtkExpressionWatch>);
15
16    match fn {
17        ref => |ptr| ffi::gtk_expression_watch_ref(ptr),
18        unref => |ptr| ffi::gtk_expression_watch_unref(ptr),
19    }
20}
21
22impl ExpressionWatch {
23    /// Evaluates the watched expression and on success stores the result
24    /// in `value`.
25    ///
26    /// This is equivalent to calling [`Expression::evaluate()`][crate::Expression::evaluate()] with the
27    /// expression and this pointer originally used to create `watch`.
28    /// ## `value`
29    /// an empty `GValue` to be set
30    ///
31    /// # Returns
32    ///
33    /// `TRUE` if the expression could be evaluated and `value` was set
34    #[doc(alias = "gtk_expression_watch_evaluate")]
35    pub fn evaluate(&self) -> Option<Value> {
36        assert_initialized_main_thread!();
37        unsafe {
38            let mut value = Value::uninitialized();
39            let ret = ffi::gtk_expression_watch_evaluate(
40                self.to_glib_none().0,
41                value.to_glib_none_mut().0,
42            );
43            if from_glib(ret) {
44                Some(value)
45            } else {
46                None
47            }
48        }
49    }
50
51    // rustdoc-stripper-ignore-next
52    /// Similar to [`Self::evaluate`] but panics if the value is of a different
53    /// type.
54    #[doc(alias = "gtk_expression_evaluate")]
55    pub fn evaluate_as<V: for<'b> FromValue<'b> + 'static>(&self) -> Option<V> {
56        self.evaluate().map(|v| {
57            v.get_owned::<V>()
58                .expect("Failed to evaluate to this value type")
59        })
60    }
61
62    /// Stops watching an expression.
63    ///
64    /// See [`Expression::watch()`][crate::Expression::watch()] for how the watch
65    /// was established.
66    #[doc(alias = "gtk_expression_watch_unwatch")]
67    pub fn unwatch(&self) {
68        unsafe { ffi::gtk_expression_watch_unwatch(self.to_glib_none().0) }
69    }
70}
71
72#[cfg(feature = "v4_2")]
73impl glib::prelude::StaticType for ExpressionWatch {
74    #[doc(alias = "gtk_expression_watch_get_type")]
75    #[inline]
76    fn static_type() -> glib::Type {
77        unsafe { from_glib(ffi::gtk_expression_watch_get_type()) }
78    }
79}