Skip to content

Allow chaining expressions with Closure objects #714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions examples/expressions/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
mod metadata;
mod note;

use glib::closure;

use gtk::gio;
use gtk::glib;
use gtk::prelude::*;
Expand Down Expand Up @@ -40,19 +42,20 @@ fn build_ui(app: &gtk::Application) {

metadata_expression
.chain_property::<Metadata>("title")
.chain_closure(|args| {
.chain_closure_with_callback(|args| {
let title: String = args[1].get().unwrap();
format!("Title: {}", title)
})
.bind(&title_label, "label", gtk::Widget::NONE);

metadata_expression
.chain_property::<Metadata>("last-modified")
.chain_closure(|args| {
let last_modified: glib::DateTime = args[1].get().unwrap();
format!("Last Modified: {}", last_modified.format_iso8601().unwrap())
})
.bind(&last_modified_label, "label", gtk::Widget::NONE);
.chain_closure::<String>(closure!(
|_: gtk::ListItem, last_modified: glib::DateTime| {
format!("Last Modified: {}", last_modified.format_iso8601().unwrap())
}
))
.bind(&last_modified_label, "label", Some(list_item));

list_item.set_child(Some(&hbox));
});
Expand Down
20 changes: 11 additions & 9 deletions gtk4/src/closure_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@ define_expression!(

impl ClosureExpression {
#[doc(alias = "gtk_closure_expression_new")]
pub fn new<F, R>(params: impl IntoIterator<Item = impl AsRef<Expression>>, callback: F) -> Self
pub fn new<R, I, E>(params: I, closure: glib::RustClosure) -> Self
where
F: Fn(&[Value]) -> R + 'static,
R: ValueType,
I: IntoIterator<Item = E>,
E: AsRef<Expression>,
{
assert_initialized_main_thread!();
let closure = glib::Closure::new_local(move |values| {
let ret = callback(values);
Some(ret.to_value())
});

let params = params
.into_iter()
Expand All @@ -42,22 +39,27 @@ impl ClosureExpression {
unsafe {
from_glib_full(ffi::gtk_closure_expression_new(
R::Type::static_type().into_glib(),
closure.to_glib_none().0,
closure.as_ref().to_glib_none().0,
params.len() as u32,
params.to_glib_full(),
))
}
}

#[doc(alias = "gtk_closure_expression_new")]
pub fn with_closure<R>(
pub fn with_callback<F, R>(
params: impl IntoIterator<Item = impl AsRef<Expression>>,
closure: glib::Closure,
callback: F,
) -> Self
where
F: Fn(&[Value]) -> R + 'static,
R: ValueType,
{
assert_initialized_main_thread!();
let closure = glib::Closure::new_local(move |values| {
let ret = callback(values);
Some(ret.to_value())
});

let params = params
.into_iter()
Expand Down
51 changes: 43 additions & 8 deletions gtk4/src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,9 @@ impl Expression {
}

// rustdoc-stripper-ignore-next
/// Create a [`gtk::PropertyExpression`] that looks up for `property_name`
/// with self as parameter. This is useful in long chains of [`gtk::Expression`]s.
/// Create a [`PropertyExpression`](crate::PropertyExpression) that looks up for
/// `property_name` with self as parameter. This is useful in long chains of
/// [`Expression`](crate::Expression)s.
pub fn chain_property<T: IsA<glib::Object>>(
&self,
property_name: &str,
Expand All @@ -171,14 +172,47 @@ impl Expression {
}

// rustdoc-stripper-ignore-next
/// Create a [`gtk::ClosureExpression`] with self as a parameter. This is useful in long
/// chains of [`gtk::Expression`]s.
pub fn chain_closure<F, R>(&self, f: F) -> crate::ClosureExpression
/// Create a [`ClosureExpression`](crate::ClosureExpression) from a [`glib::Closure`] with self
/// as the second parameter and `R` as the return type. The return type is checked at run-time
/// and must always be specified. This is useful in long chains of
/// [`Expression`](crate::Expression)s when using the [`glib::closure!`] macro.
///
/// Note that the first parameter will always be the `this` object bound to the expression. If
/// `None` is passed as `this` then the type of the first parameter must be
/// `Option<glib::Object>` otherwise type checking will panic.
///
/// ```no_run
/// # use gtk4 as gtk;
/// use gtk::prelude::*;
/// use gtk::glib;
/// use glib::{closure, Object};
///
/// let button = gtk::Button::new();
/// button.set_label("Hello");
/// let label = button
/// .property_expression("label")
/// .chain_closure::<String>(closure!(|_: Option<Object>, label: &str| {
/// format!("{} World", label)
/// }))
/// .evaluate_as::<String, _>(gtk::Widget::NONE);
/// assert_eq!(label.unwrap(), "Hello World");
/// ```
pub fn chain_closure<R>(&self, closure: glib::RustClosure) -> crate::ClosureExpression
where
R: glib::value::ValueType,
{
crate::ClosureExpression::new::<R, _, _>(&[self], closure)
}

// rustdoc-stripper-ignore-next
/// Create a [`ClosureExpression`](crate::ClosureExpression) with self as the second parameter.
/// This is useful in long chains of [`Expression`](crate::Expression)s.
pub fn chain_closure_with_callback<F, R>(&self, f: F) -> crate::ClosureExpression
where
F: Fn(&[glib::Value]) -> R + 'static,
R: glib::value::ValueType,
{
crate::ClosureExpression::new(&[self], f)
crate::ClosureExpression::with_callback(&[self], f)
}
}

Expand Down Expand Up @@ -217,12 +251,13 @@ impl glib::value::ToValueOptional for Expression {
}

// rustdoc-stripper-ignore-next
/// Trait containing convenience methods in creating [`gtk::PropertyExpression`] that
/// Trait containing convenience methods in creating
/// [`PropertyExpression`](crate::PropertyExpression) that
/// looks up a property of a [`glib::Object`].
///
/// # Example
///
/// `label_expression` is a [`gtk::Expression`] that looks up at Button's `label`
/// `label_expression` is an [`Expression`](crate::Expression) that looks up at Button's `label`
/// property.
///
/// ```no_run
Expand Down