gtk4/
closure_expression.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use glib::{translate::*, Value};
4
5use crate::{ffi, prelude::*, ClosureExpression, Expression};
6
7define_expression!(ClosureExpression, ffi::GtkClosureExpression);
8
9impl ClosureExpression {
10    #[doc(alias = "gtk_closure_expression_new")]
11    pub fn new<R>(
12        params: impl IntoIterator<Item = impl AsRef<Expression>>,
13        closure: glib::RustClosure,
14    ) -> Self
15    where
16        R: ValueType,
17    {
18        assert_initialized_main_thread!();
19
20        let params = params
21            .into_iter()
22            .map(|e| e.as_ref().clone())
23            .collect::<Vec<_>>();
24
25        unsafe {
26            from_glib_full(ffi::gtk_closure_expression_new(
27                R::Type::static_type().into_glib(),
28                closure.as_ref().to_glib_none().0,
29                params.len() as u32,
30                params.to_glib_full(),
31            ))
32        }
33    }
34
35    #[doc(alias = "gtk_closure_expression_new")]
36    pub fn with_callback<R, F>(
37        params: impl IntoIterator<Item = impl AsRef<Expression>>,
38        callback: F,
39    ) -> Self
40    where
41        F: Fn(&[Value]) -> R + 'static,
42        R: ValueType,
43    {
44        assert_initialized_main_thread!();
45        let closure = glib::Closure::new_local(move |values| {
46            let ret = callback(values);
47            Some(ret.to_value())
48        });
49
50        let params = params
51            .into_iter()
52            .map(|e| e.as_ref().clone())
53            .collect::<Vec<_>>();
54
55        unsafe {
56            from_glib_full(ffi::gtk_closure_expression_new(
57                R::Type::static_type().into_glib(),
58                closure.to_glib_none().0,
59                params.len() as u32,
60                params.to_glib_full(),
61            ))
62        }
63    }
64}