1// Take a look at the license at the top of the repository in the LICENSE file.
23use glib::{translate::*, value::FromValue, Value};
45use crate::ffi;
67glib::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")]
14pub struct ExpressionWatch(Shared<ffi::GtkExpressionWatch>);
1516match fn {
17ref => |ptr| ffi::gtk_expression_watch_ref(ptr),
18 unref => |ptr| ffi::gtk_expression_watch_unref(ptr),
19 }
20}
2122impl 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")]
35pub fn evaluate(&self) -> Option<Value> {
36assert_initialized_main_thread!();
37unsafe {
38let mut value = Value::uninitialized();
39let ret = ffi::gtk_expression_watch_evaluate(
40self.to_glib_none().0,
41value.to_glib_none_mut().0,
42 );
43if from_glib(ret) {
44Some(value)
45 } else {
46None47 }
48 }
49 }
5051// 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")]
55pub fn evaluate_as<V: for<'b> FromValue<'b> + 'static>(&self) -> Option<V> {
56self.evaluate().map(|v| {
57v.get_owned::<V>()
58 .expect("Failed to evaluate to this value type")
59 })
60 }
6162/// 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")]
67pub fn unwatch(&self) {
68unsafe { ffi::gtk_expression_watch_unwatch(self.to_glib_none().0) }
69 }
70}
7172#[cfg(feature = "v4_2")]
73impl glib::prelude::StaticTypefor ExpressionWatch {
74#[doc(alias = "gtk_expression_watch_get_type")]
75 #[inline]
76fn static_type() -> glib::Type {
77unsafe { from_glib(ffi::gtk_expression_watch_get_type()) }
78 }
79}