Skip to main content

gtk4/
expression_watch.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use glib::{Value, translate::*, value::FromValue};
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) { Some(value) } else { None }
44        }
45    }
46
47    // rustdoc-stripper-ignore-next
48    /// Similar to [`Self::evaluate`] but panics if the value is of a different
49    /// type.
50    #[doc(alias = "gtk_expression_evaluate")]
51    pub fn evaluate_as<V: for<'b> FromValue<'b> + 'static>(&self) -> Option<V> {
52        self.evaluate().map(|v| {
53            v.get_owned::<V>()
54                .expect("Failed to evaluate to this value type")
55        })
56    }
57
58    /// Stops watching an expression.
59    ///
60    /// See [`Expression::watch()`][crate::Expression::watch()] for how the watch
61    /// was established.
62    #[doc(alias = "gtk_expression_watch_unwatch")]
63    pub fn unwatch(&self) {
64        unsafe { ffi::gtk_expression_watch_unwatch(self.to_glib_none().0) }
65    }
66}
67
68#[cfg(feature = "v4_2")]
69impl glib::prelude::StaticType for ExpressionWatch {
70    #[doc(alias = "gtk_expression_watch_get_type")]
71    #[inline]
72    fn static_type() -> glib::Type {
73        unsafe { from_glib(ffi::gtk_expression_watch_get_type()) }
74    }
75}