From 15b0eee801be1a24465c30eb0d53ddba926837bb Mon Sep 17 00:00:00 2001 From: Jason Francis Date: Sun, 14 Nov 2021 01:48:38 -0600 Subject: [PATCH] Add method to create a "this" expression from GObject types --- examples/expressions/main.rs | 11 ++++++----- gtk4/src/expression.rs | 9 +++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/examples/expressions/main.rs b/examples/expressions/main.rs index 49410c48e38e..ba23560ecece 100644 --- a/examples/expressions/main.rs +++ b/examples/expressions/main.rs @@ -36,11 +36,9 @@ fn build_ui(app: >k::Application) { // Instead of binding properties and manually unbinding them, you can create expressions. // The value can be obtained even if it is several steps away. - let metadata_expression = list_item + list_item .property_expression("item") - .chain_property::("metadata"); - - metadata_expression + .chain_property::("metadata") .chain_property::("title") .chain_closure_with_callback(|args| { let title: String = args[1].get().unwrap(); @@ -48,7 +46,10 @@ fn build_ui(app: >k::Application) { }) .bind(&title_label, "label", gtk::Widget::NONE); - metadata_expression + // Property expressions can also start from the `this` value, which is set as the last + // argument to the `bind` function. + gtk::ListItem::this_expression("item") + .chain_property::("metadata") .chain_property::("last-modified") .chain_closure::(closure!( |_: gtk::ListItem, last_modified: glib::DateTime| { diff --git a/gtk4/src/expression.rs b/gtk4/src/expression.rs index 952a8f75d455..9d28f9018d4c 100644 --- a/gtk4/src/expression.rs +++ b/gtk4/src/expression.rs @@ -277,6 +277,10 @@ pub trait GObjectPropertyExpressionExt { // rustdoc-stripper-ignore-next /// Create an expression looking up an object's property with a weak reference. fn property_expression_weak(&self, property_name: &str) -> crate::PropertyExpression; + + // rustdoc-stripper-ignore-next + /// Create an expression looking up a property in the bound `this` object. + fn this_expression(property_name: &str) -> crate::PropertyExpression; } impl> GObjectPropertyExpressionExt for T { @@ -289,6 +293,11 @@ impl> GObjectPropertyExpressionExt for T { let obj_expr = crate::ObjectExpression::new(self); crate::PropertyExpression::new(T::static_type(), Some(&obj_expr), property_name) } + + fn this_expression(property_name: &str) -> crate::PropertyExpression { + skip_assert_initialized!(); + crate::PropertyExpression::new(T::static_type(), Expression::NONE, property_name) + } } macro_rules! define_expression {