[go: up one dir, main page]

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    #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
9    #[doc(alias = "GtkExpressionWatch")]
10    pub struct ExpressionWatch(Shared<ffi::GtkExpressionWatch>);
11
12    match fn {
13        ref => |ptr| ffi::gtk_expression_watch_ref(ptr),
14        unref => |ptr| ffi::gtk_expression_watch_unref(ptr),
15    }
16}
17
18impl ExpressionWatch {
19    #[doc(alias = "gtk_expression_watch_evaluate")]
20    pub fn evaluate(&self) -> Option<Value> {
21        assert_initialized_main_thread!();
22        unsafe {
23            let mut value = Value::uninitialized();
24            let ret = ffi::gtk_expression_watch_evaluate(
25                self.to_glib_none().0,
26                value.to_glib_none_mut().0,
27            );
28            if from_glib(ret) {
29                Some(value)
30            } else {
31                None
32            }
33        }
34    }
35
36    // rustdoc-stripper-ignore-next
37    /// Similar to [`Self::evaluate`] but panics if the value is of a different
38    /// type.
39    #[doc(alias = "gtk_expression_evaluate")]
40    pub fn evaluate_as<V: for<'b> FromValue<'b> + 'static>(&self) -> Option<V> {
41        self.evaluate().map(|v| {
42            v.get_owned::<V>()
43                .expect("Failed to evaluate to this value type")
44        })
45    }
46
47    #[doc(alias = "gtk_expression_watch_unwatch")]
48    pub fn unwatch(&self) {
49        unsafe { ffi::gtk_expression_watch_unwatch(self.to_glib_none().0) }
50    }
51}
52
53#[cfg(feature = "v4_2")]
54impl glib::prelude::StaticType for ExpressionWatch {
55    #[doc(alias = "gtk_expression_watch_get_type")]
56    #[inline]
57    fn static_type() -> glib::Type {
58        unsafe { from_glib(ffi::gtk_expression_watch_get_type()) }
59    }
60}